Check out example codes for "max heap in c++". It will help you in understanding the concepts better.

Code Example 1

``````// 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;
}``````

Code Example 2

``priority_queue <int> maxHeap;``

Code Example 3

``````// 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;
}``````

Code Example 4

``````// C++ code to demonstrate the working of
// push_heap() and pop_heap()
#include<bits/stdc++.h>
using namespace std;
int main()
{

// Initializing a vector
vector<int> v1 = {20, 30, 40, 25, 15};

// Converting vector into a heap
// using make_heap()
make_heap(v1.begin(), v1.end());

// Displaying the maximum element of heap
// using front()
cout << "The maximum element of heap is : ";
cout << v1.front() << endl;

// using push_back() to enter element
// in vector
v1.push_back(50);

// using push_heap() to reorder elements
push_heap(v1.begin(), v1.end());

// Displaying the maximum element of heap
// using front()
cout << "The maximum element of heap after push is : ";
cout << v1.front() << endl;

// using pop_heap() to delete maximum element
pop_heap(v1.begin(), v1.end());
v1.pop_back();

// Displaying the maximum element of heap
// using front()
cout << "The maximum element of heap after pop is : ";
cout << v1.front() << endl;

return 0;
}``````

Code Example 5

``````// 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;
}``````

Learn ReactJs, React Native from akashmittal.com