In C++, priority_queue implements heap. Below are some examples of creating priority queue of pair type.
Max Priority queue (Or Max heap) ordered by first element
#include <bits/stdc++.h>
using namespace std;
int main()
{
priority_queue<pair< int , int > > pq;
pq.push(make_pair(10, 200));
pq.push(make_pair(20, 100));
pq.push(make_pair(15, 400));
pair< int , int > top = pq.top();
cout << top.first << " " << top.second;
return 0;
}
|
Output :
20 100
Min Priority queue (Or Min heap) ordered by first element
#include <bits/stdc++.h>
using namespace std;
typedef pair< int , int > pi;
int main()
{
priority_queue<pi, vector<pi>, greater<pi> > pq;
pq.push(make_pair(10, 200));
pq.push(make_pair(20, 100));
pq.push(make_pair(15, 400));
pair< int , int > top = pq.top();
cout << top.first << " " << top.second;
return 0;
}
|
Output :
10 200