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 ‘<'.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 multiset_name.key_comp();
Parameter: This function does not accept any parameter.
Return Values: The function returns a copy of the comparison object used by the container.
Below examples illustrate the multiset::key_comp() method:
Example 1 :
// C++ program to illustrate the // multiset::key_comp() function #include <bits/stdc++.h> using namespace std; int main() { // Creating a multiset named m; multiset<int> m; multiset<int>::key_compare comp = m.key_comp(); // Inserting elements into multiset m.insert(10); m.insert(20); m.insert(30); m.insert(40); cout << "Multiset has the elements\n"; // Store key value of last element int highest = *m.rbegin(); // initializing the iterator multiset<int>::iterator it = m.begin(); // printing elements of all multiset do { cout << " " << *it; } while (comp(*it++, highest)); return 0; } |
Multiset has the elements 10 20 30 40
Example 2 :
// C++ program to illustrate the // multiset::key_comp() function #include <bits/stdc++.h> using namespace std; int main() { // Creating a multiset named m; multiset<int> m; multiset<int>::key_compare comp = m.key_comp(); // Inserting elements into multiset m.insert(100); m.insert(200); m.insert(300); m.insert(400); cout << "Multiset has the elements\n"; // Store key value of last element int highest = *m.rbegin(); // initializing the iterator multiset<int>::iterator it = m.begin(); // printing elements of all multiset do { cout << " " << *it; } while (comp(*it++, highest)); return 0; } |
Multiset has the elements 100 200 300 400
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- multiset insert() function in C++ STL
- multiset find() function in C++ STL
- multiset equal_range() function in C++ STL
- multiset clear() function in C++ STL
- multiset empty() function in C++ STL
- multiset count() function in C++ STL
- multiset emplace_hint() function in C++ STL
- multiset rbegin() and rend() function in C++ STL
- multiset crbegin() and crend() function in C++ STL
- multiset cbegin() and cend() function in C++ STL
- multiset begin() and end() function in C++ STL
- multiset get_allocator() function in C++ STL
- Multiset in C++ Standard Template Library (STL)
- multiset::operator= in C++ STL
- multiset::swap() in C++ STL
- multiset::emplace() in C++ STL
- multiset lower_bound() in C++ STL with Examples
- multiset upper_bound() in C++ STL with Examples
- multiset max_size() in C++ STL with Examples
- multiset size() in C++ STL with Examples
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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.

