C++ Map Library - crend() Function


Advertisements

Description

The C++ function std::map::equal_range() returns range of elements that matches specific key.

The range is defined by two iterators, one points to the first element that is not less than key k and another points to the first element greater than key k.

Declaration

Following is the declaration for std::map::equal_range() function form std::map header.

C++98

pair<const_iterator,const_iterator> equal_range (const key_type& k) const;
pair<iterator,iterator> equal_range (const key_type& k);

Parameters

k − Key to be searched.

Return value

If object is constant qualified then method returns a pair of constant iterator otherwise pair of non-constant iterator.

Exceptions

This member function doesn't throw exception.

Time complexity

Logarithmic i.e. O(log n)

Example

The following example shows the usage of std::map::equal_range() function.

#include <iostream>
#include <map>

using namespace std;

int main(void) {
   map<char, int> m = {
            {'a', 1},
            {'b', 2},
            {'c', 3},
            {'d', 4},
            {'e', 5},
            };

   auto ret = m.equal_range('b');

   cout << "Lower bound is " << ret.first->first <<
      " = " << ret.first->second << endl;

   cout << "Upper bound is " << ret.second->first <<
      " = " << ret.second->second << endl;

   return 0;
}

Let us compile and run the above program, this will produce the following result −

Lower bound is b = 2
Upper bound is c = 3
map.htm
Advertisements