public member function
<map>

std::multimap::value_comp

value_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 multimap 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 multimap::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 multimap<Key,T,Compare,Alloc>::value_compare
{   // in C++98, it is required to inherit binary_function<value_type,value_type,bool>
  friend class multimap;
protected:
  Compare comp;
  value_compare (Compare c) : comp(c) {}  // constructed with multimap'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);
  }
}


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 container's comparison object, key_comp), 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 comparison object for element values.
Member type value_compare is a nested class type (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
// multimap::value_comp
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymultimap;

  mymultimap.insert(std::make_pair('x',101));
  mymultimap.insert(std::make_pair('y',202));
  mymultimap.insert(std::make_pair('y',252));
  mymultimap.insert(std::make_pair('z',303));

  std::cout << "mymultimap contains:\n";

  std::pair<char,int> highest = *mymultimap.rbegin();          // last element

  std::multimap<char,int>::iterator it = mymultimap.begin();
  do {
    std::cout << (*it).first << " => " << (*it).second << '\n';
  } while ( mymultimap.value_comp()(*it++, highest) );

  return 0;
}


Output:
mymultimap contains:
x => 101
y => 202
y => 252
z => 303

Complexity

Constant.

Iterator validity

No changes.

Data races

The container is accessed.
No contained elements are accessed: concurrently accessing or modifying them is safe.

Exception safety

Strong guarantee: if an exception is thrown, there are no changes in the container.

See also