multimap key_comp in C++ STL

This is the part of Standard Template Library(STL) of C++. To use this STL, use Namespace: std and include “map” header file in the program.
It returns the function object or comparison object or ordering delegate that compares the keys, which is a copy of this container’s constructor argument.
It is a function pointer or an object that takes two arguments of the same type as the element keys and determines the order of the elements in the container.


key_compare key_comp();

Here key_compare is the type of the comparison object, which is associated to the container.


It does not accept any parameter.


It returns the key comparison function object or ordering delegate, which is defined in multimap as an alias of its third template parameter.

Below is an example of multimap::key_comp:





// c++ program to show
// the use of multimap::key_comp
#include <iostream>
#include <map>
using namespace std;
// Driver code
int main()
    multimap<char, int> m1;
    //'comp' works as a variable
    multimap<char, int>::key_compare comp = m1.key_comp();
    // set the values of the pairs
    m1.insert(make_pair('a', 10));
    m1.insert(make_pair('b', 20));
    m1.insert(make_pair('b', 30));
    m1.insert(make_pair('c', 40));
    // key value of last element
    char h = m1.rbegin()->first;
    multimap<char, int>::iterator i = m1.begin();
    do {
        cout << (*i).first << " = " << (*i).second << '\n';
    } while (comp((*i++).first, h));
    return 0;



a = 10
b = 20
b = 30
c = 40

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.