Skip to content
Related Articles

Related Articles

multimap key_comp() in C++ STL
  • Difficulty Level : Basic
  • Last Updated : 24 Oct, 2018

The std::multimap::key_comp() is an inbuilt function in C++ STL which returns a copy of the comparison object used by the container.By default, this is a less object, which returns the same as operator ‘<'.It is a function pointer or a function object which takes two arguments of the same type as the container elements and returns true if the first argument is considered to go before the second in the strict weak ordering it defines or false otherwise.Two keys are considered equivalent if key_comp returns false reflexively (i.e., no matter the order in which the keys are passed as arguments).

Syntax:

key_compare multimap_name.key_comp();

Parameters: This function does not accept any parameter.

Return value: The function returns a copy of the comparison object used by the container.

Below examples illustrate the multimap::key_comp() method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// multimap::key_comp() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // Creating a multimap named m;
    multimap<char, int> m;
  
    multimap<char, int>::key_compare
        comp
        = m.key_comp();
  
    // Inserting elements into multimap
    m.insert(make_pair('a', 10));
    m.insert(make_pair('b', 20));
    m.insert(make_pair('c', 30));
    m.insert(make_pair('d', 40));
  
    cout << "Multimap has the elements\n";
  
    // Store key value of last element
    char l = m.rbegin()->first;
  
    // initializing the iterator
    map<char, int>::iterator it = m.begin();
  
    // printing elements of all multimap
    do {
  
        cout << it->first
             << " => "
             << it->second
             << '\n';
  
    } while (comp((*it++).first, l));
  
    return 0;
}

chevron_right


Output:

Multimap has the elements
a => 10
b => 20
c => 30
d => 40

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate the
// multimap::key_comp() function
  
#include <bits/stdc++.h>
using namespace std;
  
int main()
{
  
    // Creating a multimap named m;
    multimap<char, int> m;
  
    multimap<char, int>::key_compare
        comp
        = m.key_comp();
  
    // Inserting elements into multimap
    m.insert(make_pair('a', 100));
    m.insert(make_pair('b', 200));
    m.insert(make_pair('c', 300));
    m.insert(make_pair('d', 400));
  
    cout << "Multimap has the elements\n";
  
    // Store key value of last element
    char l = m.rbegin()->first;
  
    // initializing the iterator
    map<char, int>::iterator it = m.begin();
  
    // printing elements of all multimap
    do {
  
        cout << it->first
             << " => "
             << it->second
             << '\n';
  
    } while (comp((*it++).first, l));
  
    return 0;
}

chevron_right


Output:

Multimap has the elements
a => 100
b => 200
c => 300
d => 400

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :