public member function
<map>
std::map::value_compvalue_compare value_comp() const;
Return value comparison object
Returns a comparison object that can be used to compare two elements to get whether the key of the first one goes before the second.The arguments taken by this function object are of member type value_type (defined in map as an alias of pair<const key_type,mapped_type>), but the mapped_type part of the value is not taken into consideration in this comparison.
The comparison object returned is an object of the member type map::value_compare, which is a nested class that uses the internal comparison object to generate the appropriate comparison functional class. It is defined with the same behavior as:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class Key, class T, class Compare, class Alloc>
class map<Key,T,Compare,Alloc>::value_compare
{ // in C++98, it is required to inherit binary_function<value_type,value_type,bool>
friend class map;
protected:
Compare comp;
value_compare (Compare c) : comp(c) {} // constructed with map's comparison object
public:
typedef bool result_type;
typedef value_type first_argument_type;
typedef value_type second_argument_type;
bool operator() (const value_type& x, const value_type& y) const
{
return comp(x.first, y.first);
}
}
Notice that value_compare has no public constructor, therefore no objects can be directly created from this nested class outside map members.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// map::value_comp
#include <iostream>
#include <map>
int main ()
{
std::map<char,int> mymap;
mymap['x']=1001;
mymap['y']=2002;
mymap['z']=3003;
std::cout << "mymap contains:\n";
std::pair<char,int> highest = *mymap.rbegin(); // last element
std::map<char,int>::iterator it = mymap.begin();
do {
std::cout << it->first << " => " << it->second << '\n';
} while ( mymap.value_comp()(*it++, highest) );
return 0;
}
mymap contains: x => 1001 y => 2002 z => 3003
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4