Skip to content
Related Articles

Related Articles

Improve Article
multimap value_comp() function in C++ STL
  • Last Updated : 22 Oct, 2018

The multimap::value_comp() method 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. Here the 1st object compares the object of type std::multimap::type. The arguments taken by this function object are of member type type. It is defined in multimap as an alias of pair.

Syntax:

multimap::compared_value value_comp() const;

Here compared_value is a nested class type

Parameters: It does not accepts any parameter.

Return Value: This method returns the comparison object which is an object of the member type multimap::compared_value, which is a nested class that uses the internal comparison object to generate the appropriate comparison functional class.

Below program illustrate the multimap value_comp() function:




// C++ program to show
// the use of multimap::value_comp
  
#include <iostream>
#include <map>
using namespace std;
  
int main()
{
    multimap<char, int> m;
  
    // making of pair
    m.insert(make_pair('a', 10));
    m.insert(make_pair('b', 20));
    m.insert(make_pair('c', 30));
    m.insert(make_pair('d', 40));
  
    pair<char, int> p = *m.rbegin();
    // last element
  
    multimap<char, int>::iterator i = m.begin();
  
    do {
  
        cout << (*i).first
             << " = " << (*i).second
             << '\n';
  
    } while (m.value_comp()(*i++, p));
  
    return 0;
}
Output:
a = 10
b = 20
c = 30
d = 40
Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.
My Personal Notes arrow_drop_up
Recommended Articles
Page :