/* A priority queue maintains a set of elements. The supported operations are
insertion and, depending on the type of the queue, retrieval and removal
of either the minimum or maximum element. Insertion and removal take
O(logn) time, and retrieval takes O(1) time. */
priority_queue<int> q;
q.push(3); // 3
q.push(5); // 3 5
q.push(7); // 3 5 7
q.push(2); // 2 3 5 7
cout << q.top() << "
"; // 7
q.pop();
cout << q.top() << "
"; // 5
q.pop();
q.push(6);
cout << q.top() << "
"; // 6
q.pop();
//Shubh'grepper
// Implementation of priority_queue in c++
//queue with elements in decreasing order
priority_queue<int> pq;
// queue with elements in increasing order using compare function inside declaration
priority_queue <int, vector<int>, greater<int> > pq;
//priority_queue of type pair<int, int>
#define pp pair<int, int>
priority_queue <pp, vector<pp>, greater<pp> > pq;
priority_queue<int, vector<int>, greater<int> > gq
priority_queue<int> pq;//this works like rank (e.g: rank=1 > rank=2)
//the lower the rank the higher is the priority
pq.push(3);
cout<<pq.top()<<endl;
cout<<pq.size()<<endl;
pq.pop();
if(pq.empty()){
cout<<"priority queue is empty"<<endl;
}
priority_queue<int, vector<int>, greater<int>> pqr;
//this is the reverse priority queue
//this works like score the higher the score the more is it's priority
pqr.push(1);
pqr.push(4);
cout<<pqr.top()<<endl;
pqr.pop();
cout<<pqr.size()<<endl;
if(!pqr.empty()){
cout<<"the priority queue is not empty"<<endl;
}
// priority_queue::swap
#include <iostream> // std::cout
#include <queue> // std::priority_queue
int main ()
{
std::priority_queue<int> foo,bar;
foo.push (15); foo.push(30); foo.push(10);
bar.push (101); bar.push(202);
foo.swap(bar);
std::cout << "size of foo: " << foo.size() << '
';
std::cout << "size of bar: " << bar.size() << '
';
return 0;
}