Custom Comparator for Multimap in C++ Last Updated : 05 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++ multimap is a container to store key-value pairs allowing duplicate keys which is not allowed in a map container. By default, the multimap container uses the less than '<' operator to compare the keys for ordering the entries but also allows the use of the custom comparator. In this article, we will learn how to use a multimap with a custom comparator in C++. Custom Comparator for Multimap in C++To use a std::multimap with a custom comparator, we can define a comparison function or a functor (a class that overloads the operator()) and then use it as a template parameter when declaring the multimap so that the elements of the multimap will be ordered automatically based on the conditions mentioned in the custom comparator. C++ Program to Use a Multimap with a Custom ComparatorThe below example demonstrates how we can use a multimap with a custom comparator in C++. C++ // C++ Program To Use a Multimap With a Custom Comparator #include <iostream> #include <map> #include <string> using namespace std; // Custom comparator functor for multimap struct CustomComparator { bool operator()(const string& a, const string& b) const { // Compare based on string length return a.length() < b.length(); } }; int main() { // Creating a multimap using custom comparator multimap<string, int, CustomComparator> mp2 = { { "apple", 5 }, { "apple", 10 }, { "banana", 3 }, { "orange", 4 }, { "kiwi", 2 } }; // Printing elements of the multimap cout << "Multimap using custom comparator" << endl; for (auto pair : mp2) { cout << pair.first << ": " << pair.second << endl; } return 0; } OutputMultimap using custom comparator kiwi: 2 apple: 5 apple: 10 banana: 3 orange: 4 Time Complexity: O(N log N), here N is the number of elements in a multimap.Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article Custom Comparator for Multimap in C++ R rohan_paul Follow Improve Article Tags : C++ Programs C++ STL cpp-multimap CPP Examples +1 More Practice Tags : CPPSTL Similar Reads How to Use Custom Comparator with Set in C++? In C++, sets are associative containers that store unique elements in some sorted order. By default, set store data in increasing order but we can change this using a custom comparator. In this article, we will learn, how to declare a set with a custom comparator in C++ STL. Example Input: Data = {1 2 min read How to Initialize Multiset with Custom Comparator in C++? In C++, a multiset container stores the data in a sorted order. By default, this order is increasing order (using < operator as comparator) but we can change this order by providing a custom comparator. In this article, we will learn how to initialize a multiset with a custom comparator function 2 min read How to Sort a Vector Using a Custom Comparator in C++? In C++, the std::sort() function sorts the given vector in increasing order by default. The custom comparator is a function that defines the order in which the elements of a std::vector should be sorted. It is passed as the parameter to the std::sort() function.In this article, we will learn how to 4 min read How to Create Deque of Multimap in C++? In C++, a deque (double-ended queue) is a data structure that allows insertion and deletion at both ends, while a multimap is an associative container that contains key-value pairs, where multiple keys can have the same value. In this article, we will learn how to create a deque of multimaps in C++ 2 min read How to Create a Multimap of Arrays in C++? In C++, a multimap is similar to a map with the addition that multiple elements can have the same keys. Also, it is NOT required that the key-value and mapped value pair have to be unique in this case. In this article, we will learn how to create a multimap of arrays in C++ STL. Example Input: myArr 2 min read Overloading Relational Operators in C++ In C++, operator overloading is used to redefine the behavior of already existing operators. Similarly, overloading the relational operators is commonly used to compare the instances of user-defined classes. By overloading these operators we can easily define the behavior of comparisons for the obje 4 min read How to Create a Stack of Multimap in C++? In C++, Stacks are a type of container adaptor with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. A multimap is a container that stores key-value pairs in an ordered manner. In this article, we will learn how to c 2 min read How to Delete a Pair from a Multimap in C++? In C++, multimap stores key-value pairs and for the same key there can be multiple values in a multimap. In this article, we will learn how to delete a pair from a multimap in C++. Example Input:mpp={{"apple", 1},{"banana", 2},{"apple", 3}, {"orange", 4}}keyToRemove=applevalueToRemove=3Output:apple: 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 â<'.It is a function pointer or a function object which takes two arguments of the same t 2 min read Comparator Class in C++ with Examples Comparator Classes are used to compare the objects of user-defined classes. In order to develop a generic function use template, and in order to make the function more generic use containers, so that comparisons between data can be made. Syntax cpp class comparator_class { public: // Comparator func 5 min read Like