Priority queue can contain elements with various data types such as integer, pair of integers, custom data type. But one thing is common that there is one element that defines the priority of the element.
Therefore, the Priority queue of pairs can have two types of ordering –
- Ordered by the first element of pair
- Ordered by the second element of pair
Priority Queue ordered by first element
In C++ if the element is in the form of pairs, then by default the priority of the elements is dependent upon the first element. Therefore, we just have to use the priority queue of pairs only.
9 4 7 3 5 4 4 5 1 6
Note: If the first element of some pairs will be same, then the comparison will be made on the basis of the second element.
Priority Queue ordered by second element (Max)
The idea is to use structure with the concept of the operator overloading in the priority queue for ordering the pairs by its second element.
Below is the implementation of the priority queue ordered by second element –
1 6 4 5 9 4 5 4 7 3
Priority queue ordered by second element (Min)
The idea is to use the operator overloading to implement the priority queue ordered by its second element with the minimum element at the top.
Below is the implementation of the priority queue:
7 3 5 4 9 4 4 5 1 6
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.
- Priority queue of pairs in C++ (Ordered by first)
- Applications of Priority Queue
- Priority Queue | Set 1 (Introduction)
- Priority Queue in Python
- Priority Queue using Linked List
- STL Priority Queue for Structure or Class
- Priority Queue of Vectors in C++ STL with Examples
- Does STL priority queue allow duplicate values?
- Priority Queue using doubly linked list
- Priority Queue in C++ Standard Template Library (STL)
- Find the K closest points to origin using Priority Queue
- CPU Scheduling in Operating Systems using priority queue with gantt chart
- Queue of Pairs in C++ STL with Examples
- Count of distinct possible pairs such that the element from A is greater than the element from B
- Lexicographical ordering using Heap Sort
- Count equal element pairs in the given array
- Print all the pairs that contains the positive and negative values of an element
- Check if a queue can be sorted into another queue using a stack
- Stack and Queue in Python using queue Module
- queue::empty() and queue::size() in C++ STL
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.
Improved By : Akanksha_Rai