|
value_compare value_comp ( ) const;
Return value comparison object
Returns a comparison object that can be used to compare two element values (pairs) to get whether the key of the first goes before the second.
The mapped value, although part of the pair, is not taken into consideration in this comparison - only the key value.
The comparison object returned is an object of the member type multimap::value_compare, which is a nested class that uses the Compare class from the multimap template class (the third template parameter) to generate the appropriate comparison class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
template <class Key, class T, class Compare, class Allocator>
class multimap<Key,T,Compare,Allocator>::value_compare
: public binary_function<value_type,value_type,bool>
{
friend class multimap;
protected:
Compare comp;
value_compare (Compare c) : comp(c) {}
public:
bool operator() (const value_type& x, const value_type& y) const
{
return comp(x.first, y.first);
}
}
|
The public member of this comparison class returns true if the key of the first argument is considered to go before that of the second, according to the strict weak ordering specified by the comparison object on multimap construction, and false otherwise.
Notice that value_compare has no public constructor, therefore no objects can be directly created from this nested class outside multimap members.
Parameters
none
Return value
The value comparison object.
multimap::value_compare is a member type defined as described above.
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
|
// multimap::value_comp
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<char,int> mymultimap;
multimap<char,int>::iterator it;
pair<char,int> highest;
mymultimap.insert(pair<char,int>('x',101));
mymultimap.insert(pair<char,int>('y',202));
mymultimap.insert(pair<char,int>('y',252));
mymultimap.insert(pair<char,int>('z',303));
cout << "mymultimap contains:\n";
highest=*mymultimap.rbegin(); // last element
it=mymultimap.begin();
do {
cout << (*it).first << " => " << (*it).second << endl;
} while ( mymultimap.value_comp()(*it++, highest) );
return 0;
}
|
Output:
mymultimap contains:
x => 101
y => 202
y => 252
z => 303
|
Complexity
Constant.
See also
multimap::count | Count elements with a specific key (public member function) |
|