How to Use Custom Comparator with Set in C++? Last Updated : 27 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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, 5, 3, 4, 2} Output: mySet = {5, 4, 3, 2, 1}Declare a Set with a Custom Comparator in C++To declare a set with a custom comparator in C++, we have to pass the comparator function to the set at the time of its declaration. This function will then be used to determine the order of elements in the set. Syntaxstd::set<T, Compare_Type> mySet;Here, T is the type of elements in the set, and Compare is the type of the comparator. C++ Program to Declare a Set with a Custom Comparator C++ // C++ Program to illustrate how to declare a set with a // custom comparator #include <cmath> #include <iostream> #include <set> using namespace std; // Custom comparator for sorting integers based on absolute // values struct AbsoluteValueComparator { bool operator()(int a, int b) const { return abs(a) < abs(b); } }; int main() { // Declare a set of integers with the custom comparator set<int, AbsoluteValueComparator> mySet; // Insert elements into the set mySet.insert(-5); mySet.insert(3); mySet.insert(-8); mySet.insert(2); // Print the elements in the set (sorted based on // absolute values) cout << "Elements in the set: "; for (int element : mySet) { cout << element << " "; } return 0; } OutputElements in the set: 2 3 -5 -8 Comment More infoAdvertise with us Next Article How to Use Custom Comparator with Set in C++? P prajval22110945 Follow Improve Article Tags : C++ Programs C++ STL cpp-set CPP Examples +1 More Practice Tags : CPPSTL Similar Reads 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 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 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 using Member Function as Comparator in C++? In C++, sorting is a common operation that can be customized by providing a comparator function. When working with classes, we might need to sort objects based on their member variables. Using a member function as a comparator is a good technique to achieve this.In this article, we will learn how to 3 min read How to Sort a Vector of Custom Objects in C++? In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the s 2 min read Like