Priority queue of pairs in C++ with ordering by first and second element
Last Updated :
01 Nov, 2023
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 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(Max)
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.
C++
// C++ implementation of the
// priority queue of pairs
// ordered by the first element
#include <iostream>
#include <queue>
using namespace std;
// Function to print the data of
// the priority queue ordered by first
void showpq(
priority_queue<pair<int, int> > g)
{
// Loop to print the elements
// until the priority queue
// is not empty
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
// Driver Code
int main()
{
priority_queue<pair<int, int> > p1;
// Insertion of elements
p1.push(make_pair(4, 5));
p1.push(make_pair(5, 4));
p1.push(make_pair(1, 6));
p1.push(make_pair(7, 3));
p1.push(make_pair(9, 4));
showpq(p1);
return 0;
}
Output9 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 first element (Min)
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.By default priority queues are max heaps. Therefore, we just have to use the priority queue of pairs with greater<> function object.
C++
// C++ implementation of the
// priority queue of pairs
// ordered by the first element (min)
#include <iostream>
#include <queue>
using namespace std;
// Function to print the data of
// the priority queue ordered by first
void showpq(priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>> > g)
{
// Loop to print the elements
// until the priority queue
// is not empty
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
// Driver Code
int main()
{
// Min heap
priority_queue<pair<int,int> , vector<pair<int,int>>, greater<pair<int,int>> > p1;
// Insertion of elements
p1.push(make_pair(4, 5));
p1.push(make_pair(5, 4));
p1.push(make_pair(1, 6));
p1.push(make_pair(7, 3));
p1.push(make_pair(9, 4));
showpq(p1);
return 0;
}
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 -
C++
// C++ implementation of the
// priority queue in which elements
// are sorted by the second element
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pd;
// Structure of the condition
// for sorting the pair by its
// second elements
struct myComp {
constexpr bool operator()(
pair<int, int> const& a,
pair<int, int> const& b)
const noexcept
{
return a.second < b.second;
}
};
// Function to show the elements
// of the priority queue
void showpq(
priority_queue<pd,
vector<pd>, myComp>
g)
{
// Loop to print the elements
// until the priority queue
// is not empty
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
// Driver Code
int main()
{
priority_queue<pd, vector<pd>, myComp> p1;
p1.push(make_pair(4, 5));
p1.push(make_pair(5, 4));
p1.push(make_pair(1, 6));
p1.push(make_pair(7, 3));
p1.push(make_pair(9, 4));
// Function Call
showpq(p1);
return 0;
}
Output1 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:
C++
// C++ implementation of the priority
// queue sorted by the second element
// in the decreasing order
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> pd;
// Structure of the operator
// overloading for comparison
struct myComp {
constexpr bool operator()(
pair<int, int> const& a,
pair<int, int> const& b)
const noexcept
{
return a.second > b.second;
}
};
// Function to print the elements
// of the priority queue
void showpq(
priority_queue<pd, vector<pd>, myComp> g)
{
// Loop to print the elements
// of the priority queue
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
// Driver Code
int main()
{
priority_queue<pd, vector<pd>, myComp> p1;
// Insertion of the elements
p1.push(make_pair(4, 5));
p1.push(make_pair(5, 4));
p1.push(make_pair(1, 6));
p1.push(make_pair(7, 3));
p1.push(make_pair(9, 4));
showpq(p1);
return 0;
}
Output7 3
5 4
9 4
4 5
1 6
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