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 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++
#include <iostream>
#include <queue>
using namespace std;
void showpq(
priority_queue<pair< int , int > > g)
{
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
int main()
{
priority_queue<pair< int , int > > 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));
showpq(p1);
return 0;
}
|
Output
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 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++
#include <iostream>
#include <queue>
using namespace std;
void showpq(priority_queue<pair< int , int > , vector<pair< int , int >>, greater<pair< int , int >> > g)
{
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
int main()
{
priority_queue<pair< int , int > , vector<pair< int , int >>, greater<pair< int , int >> > 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));
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++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair< int , int > pd;
struct myComp {
constexpr bool operator()(
pair< int , int > const & a,
pair< int , int > const & b)
const noexcept
{
return a.second < b.second;
}
};
void showpq(
priority_queue<pd,
vector<pd>, myComp>
g)
{
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
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));
showpq(p1);
return 0;
}
|
Output
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:
C++
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair< int , int > pd;
struct myComp {
constexpr bool operator()(
pair< int , int > const & a,
pair< int , int > const & b)
const noexcept
{
return a.second > b.second;
}
};
void showpq(
priority_queue<pd, vector<pd>, myComp> g)
{
while (!g.empty()) {
cout << g.top().first
<< " " << g.top().second
<< endl;
g.pop();
}
cout << endl;
}
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));
showpq(p1);
return 0;
}
|
Output
7 3
5 4
9 4
4 5
1 6
Last Updated :
01 Nov, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...