xxxxxxxxxx
// C++ program to show that priority_queue is by
// default a Max Heap
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main ()
{
// Creates a max heap
priority_queue <int> pq;
pq.push(5);
pq.push(1);
pq.push(10);
pq.push(30);
pq.push(20);
// One by one extract items from max heap
while (pq.empty() == false)
{
cout << pq.top() << " ";
pq.pop();
}
return 0;
}
xxxxxxxxxx
// C++ Program to implement min heap
// using default priority_queue(max-heap)
#include <iostream>
#include <queue>
using namespace std;
int main()
{
// data
int arr[] = { 25, 7, 9, 15, 20, 36, 50 };
// default priority_queue using max-heap
priority_queue<int> pq;
// size of the array
int n = sizeof(arr) / sizeof(arr[0]);
// multiply -1 with all elements while
// inserting
for (int i = 0; i < n; i++) {
pq.push((-1) * arr[i]);
}
// multiply all elements with -1 while
// retrieve the elements
while (!pq.empty()) {
cout << (pq.top()) * (-1) << " ";
pq.pop();
}
return 0;
}
xxxxxxxxxx
// C++ program to use priority_queue to implement min heap
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main ()
{
// Creates a min heap
priority_queue <int, vector<int>, greater<int> > pq;
pq.push(5);
pq.push(1);
pq.push(10);
pq.push(30);
pq.push(20);
// One by one extract items from min heap
while (pq.empty() == false)
{
cout << pq.top() << " ";
pq.pop();
}
return 0;
}