map::key_comp
public member function
key_compare key_comp ( ) const;
Return key comparison object
Returns the comparison object associated with the container, which can be used to compare the keys of two elements in the container.
This comparison object is set on object construction, and may either be a pointer to a function or an object of a class with a function call operator. In both cases it takes two arguments of the same type as the element keys, and returns true if the first argument is considered to go before the second in the strict weak ordering for the map object, and false otherwise.
Notice that the returned comparison object takes as parameters the element keys themselves, not entire element values (pairs) as referenced by a map iterator. To compare the keys of elements using dereferenced iterators, the member function map::value_comp may better suit your needs.
Parameters
none
Return value
The key comparison object.
map::key_compare is a member type defined to Compare, which is the third template parameter in the map class template.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
|
// map::key_comp
#include <iostream>
#include <map>
using namespace std;
int main ()
{
map<char,int> mymap;
map<char,int>::key_compare mycomp;
map<char,int>::iterator it;
char highest;
mycomp = mymap.key_comp();
mymap['a']=100;
mymap['b']=200;
mymap['c']=300;
cout << "mymap contains:\n";
highest=mymap.rbegin()->first; // key value of last element
it=mymap.begin();
do {
cout << (*it).first << " => " << (*it).second << endl;
} while ( mycomp((*it++).first, highest) );
cout << endl;
return 0;
}
|
Output:
mymap contains:
a => 100
b => 200
c => 300
|
Complexity
Constant.
See also
map::find | Get iterator to element (public member function) |
map::count | Count elements with a specific key (public member function) |
|