C++ Standard Library C++ STL Library

C++ multimap - value_comp() Function

The C++ multimap::value_comp function is used to compare two elements to get whether the key of the first one goes before the second.


value_compare value_comp() const;
value_compare value_comp() const;


No parameter is required.

Return Value

The comparison object associated to the container.

Time Complexity

Constant i.e, Θ(1).


In the example below, the multimap::value_comp function is used print all elements of the given multimap.

#include <iostream>
#include <map>
using namespace std;
int main (){
  multimap<string, string> MMap;
  multimap<string, string>::iterator it;

  MMap.insert(pair<string, string>("USA", "New York"));
  MMap.insert(pair<string, string>("CAN", "Toronto"));
  MMap.insert(pair<string, string>("CAN", "Montreal"));
  MMap.insert(pair<string, string>("IND", "Delhi"));

  //creating a value_comp object
  multimap<string, string>::value_compare MyComp = MMap.value_comp();

  //printing the content of the multimap using value_comp object
  it = MMap.begin();
  cout<<"MMap contains:\n ";
  do {
    cout<<it->first<<"  "<<it->second<<"\n ";
  } while(MyComp(*it++, *MMap.rbegin()));

  return 0;

The output of the above code will be:

MMap contains:
 CAN  Toronto
 CAN  Montreal
 IND  Delhi
 USA  New York

❮ C++ <map> Library