How to Initialize Multiset with Custom Comparator in C++? Last Updated : 07 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 in C++. For Example, Input: 1 8 6 4 9 3 2 5 7 Output: myMultiset = {9, 8, 7, 6, 5, 4, 3, 2, 1}Initialize a Multiset with a Custom Comparator in C++The comparator is a binary predicate function that compares the two values and returns true if the order needs to be changed and false if the order is already correct. We can declare this comparator as a function, functor, or lambda expression and pass it as an argument to the std::multiset constructor. C++ Program to Initialize a Multiset with a Custom Comparator in C++ C++ // C++ Program to demonstrate the initialization of custom // comparator with a multiset #include <iostream> #include <set> using namespace std; int main() { // Define a custom comparator lambda function auto customComparator = [](const int& a, const int& b) { return a > b; }; // Create a multiset with the custom comparator multiset<int, decltype(customComparator)> customSet( customComparator); // Insert elements into the multiset customSet.insert(5); customSet.insert(2); customSet.insert(8); // Print elements of the multiset for (const auto& element : customSet) { cout << element << " "; } return 0; } Output8 5 2 Time Complexity: O(N logN), where N is the number of elements to be inserted.Space Complexity: O(N) Comment More infoAdvertise with us Next Article How to Initialize Multiset with Custom Comparator in C++? K kumardhanshiv Follow Improve Article Tags : C++ Programs C++ STL cpp-multiset 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 Multimap with Default Values in C++? In C++, a multimap is a container provided by the STL library that stores key-value pairs in an ordered manner. Unlike a map, a multimap allows multiple values to be associated with a single key. In this article, we will learn how to initialize a multimap with default values in C++. Initialize Multi 2 min read Custom Comparator for Multimap in C++ 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 articl 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 Insert Multiple Elements to a Multiset in C++? In C++, a multiset is a container that stores elements in a specific order. Multiple elements can have the same values. In this article, we will learn how to insert multiple elements into a multiset in C++. Example: Input: myMultiset = {1, 4, 5, 9}; ElementsToBeAdded = {2, 3, 4} Output: myMultiset = 2 min read Like