Custom Comparator in Priority_queue in C++ STL Last Updated : 26 May, 2025 Comments Improve Suggest changes Like Article Like Report In C++, a priority queue is an STL container that utilizes the Heap Data Structure. It is useful when we need to retrieve the min or max element in constant time O(1), while insertion and deletion operations are performed in O(log n) time.Custom ComparatorA custom comparator is a function or function-like object (functor) used with algorithms or data structures, such as sorting or priority_queue. It modifies their default behavior.Syntax of Custom Comparator in Priority_queueThe following is the syntax for using a priority queue with a custom comparator: C++ priority_queue<data_type, container, comparator> ds; where,data_type: Type of the priority queue.container: Container is passed as an underlying container to store the elements.comparator: Comparator decides the ordering of elements.Similarly to passing the greater<datatype> inbuilt comparator, we can also pass a user-defined comparator to priority_queue by declaring a Compare class and overloading the operator() function. C++ class Compare { public: bool operator()(data_type a, data_type b) { if(cond) { return true; } return false; } }; In the above code,When true is returned, it means the order is NOT correct and swapping of elements takes place.When false is returned, it means the order is correct and NO swapping of elements takes place.Different ways to implement custom comparator, refer this article - Comparator in C++ExampleWe have pair of integers inside the priority_queue:First element of the pair is sorted based on Ascending order. (Smaller first)Second element of the pair is sorted based on Descending order. (Larger first)Input:{100,11} {100,41} {100,21} {300,1} {300,2} {1,1} {1,2} {1,20}Output:Top to Bottom:1 201 21 1100 41100 21100 11300 2300 1 C++ #include <iostream> #include <queue> #define PII pair<int, int> using namespace std; // Based on first part in ascending and // second part in descending first basis class Compare { public: bool operator()(PII a, PII b) { if (a.first > b.first) { return true; } else if (a.first == b.first && a.second < b.second) { return true; } return false; } }; int main() { priority_queue<PII, vector<PII>, Compare> ds; ds.push({ 100, 11 }); ds.push({ 100, 41 }); ds.push({ 100, 21 }); ds.push({ 300, 1 }); ds.push({ 300, 2 }); ds.push({ 1, 1 }); ds.push({ 1, 2 }); ds.push({ 1, 20 }); cout << "The priority queue is : \n"; while (!ds.empty()) { cout << ds.top().first << " " << ds.top().second << "\n"; // heapify happens ds.pop(); } return 0; } OutputThe priority queue is : 1 20 1 2 1 1 100 41 100 21 100 11 300 2 300 1 Why do we need a Custom Comparator?In Complex objects or pairs of data, we may need a different set of orders that are not entirely Ascending or Descending.Comparator in the case of priority_queue decides the ordering of elements, which is basically in the competition among the elements, it decides who needs to be at the top of the priority queue.In the case of Min-Heap, the greater<int> was used.Custom Comparator in priority_queueIn the above code:Time Complexity: O(Log K), where K is the size of the heap. If Total insertions are N, then it is O(N*LogK). In the above case, the K = N, therefore it boils down to O(N*LogN).Space Complexity: O(K), where K is the size of the heap. Comment More infoAdvertise with us Next Article Custom Comparator in Priority_queue in C++ STL rbkraj000 Follow Improve Article Tags : Technical Scripter C++ Technical Scripter 2022 STL cpp-priority-queue +1 More Practice Tags : CPPSTL Similar Reads Priority Queue in C++ STL In C++, priority queue is a type of queue in which there is some priority assigned to the elements. According to this priority, elements are removed from the queue. By default, the value of the element being inserted is considered as priority. Higher its value, higher its priority. But this can be c 6 min read priority_queue::push() and priority_queue::pop() in C++ STL In C++, priority_queue::push() and priority_queue::pop() methods are used to insert and delete the element from the priority_queue container. They both are the member functions of std::priority_queue class defined inside <queue> header file. In this article, we will learn about priority_queue: 2 min read priority_queue::top() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. In general, elements are arranged according to some priority. However in C++ STL, the top element is the greatest elem 3 min read priority_queue::empty() and priority_queue::size() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma 4 min read priority_queue emplace() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma 4 min read priority_queue::swap() in C++ STL Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma 3 min read priority_queue value_type in C++ STL The priority_queue :: value_type method is a built-in function in C++ STL which represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter. Time complexity: O(1)Syntax: priority_queue::value_type variable_name It has no parameters and no r 2 min read Priority Queue of Sets in C++ with Examples Priority Queues Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). Functi 4 min read Priority queue of pairs in C++ with ordering by first and second element Priority Queue: Priority queue is the extension of the queue in which elements associated with priority and elements having higher priority is popped first. Priority queue can contain elements with various data types such as integer, pair of integers, custom data type. But one thing is common that t 5 min read Multiple comparisons in a C++ priority queue? What is a Priority Queue? A Priority Queue is an abstract data type that is similar to a queue, and every element has some priority value associated with it. The priority of the elements in a priority queue determines the order in which elements are served (i.e., the order in which they are removed) 5 min read Like