Open In App

multimap key_comp in C++ STL

Last Updated : 12 Nov, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Syntax:

key_compare key_comp();

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

Parameters:

It does not accept any parameter.

Returns:

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;
}


Output:

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

Similar Reads

multimap key_comp() in C++ STL
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 ‘&lt;&#039;.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 t
2 min read
multimap::crbegin() and multimap::crend() in C++ STL
multimap::crbegin() is a built-in function in C++ STL which returns a constant reverse iterator referring to the last element in the multimap container. Since multimap container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container's sorting criterion. Syntax: multimap_name.crbeg
3 min read
multimap::cbegin() and multimap::cend() in C++ STL
multimap::cbegin() is a built-in function in C++ STL which returns a constant iterator referring to the first element in the multimap container. Since multimap container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.cbegin()Param
3 min read
multimap::begin() and multimap::end() in C++ STL
multimap::begin() is a built-in function in C++ STL that returns an iterator referring to the first element in the multimap container. Since the multimap container contains the element in an ordered way, begin() will point to that element that will come first according to the container's sorting criterion. Syntax: multimap_name.begin() Parameters:
3 min read
set::key_comp() in C++ STL
set::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 '. This object determines the order of the elements in the container. It is a function pointer or a function object which takes two arguments of the same type as
2 min read
map key_comp() in C++ STL
The std::map::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 ‘&lt;&#039;.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 fi
2 min read
multiset key_comp() function in C++ STL
The std::multiset::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 ‘&lt;&#039;.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
2 min read
map key_comp() function in C++ STL
The map::key_comp() is a function in STL in C++ that returns a copy of comparison object used by container that compare keys. Syntax: map.key_comp() Return value: This method returns the comparison object used by container that compare keys. Below examples illustrate the working of key_comp() method: Example: C/C++ Code // C++ program to demonstrat
2 min read
multimap::operator= in C++ STL
multimap::operator= is used to assign new contents to the container by replacing the existing contents. It also modifies the size according to the new contents. Syntax:- multimap1 = (multimap2) Parameters : Another container of the same type. Result : Assign the contents of the container passed as parameter to the container written on left side of
2 min read
multimap::swap() in C++ STL
multimap::swap() is used to swap the contents of one multimap with another multimap of same type and size. Syntax:- multimap1.swap(multimap2) Parameters : The name of the multimap with which the contents have to be swapped. Result : All the elements of the 2 multimap are swapped. Examples: Input: multimap1 = { ('a',1), ('b',2), ('c',3) multimap2 =
2 min read
Article Tags :
Practice Tags :