min heap stl code example

Example 1: min heap in c++

priority_queue , greater> minHeap;

Example 2: min heap priority queue c++

#include
std::priority_queue , std::greater > minHeap;

Example 3: max heap c++ stl;

// C++ program to show that priority_queue is by 
// default a Max Heap 
#include  
using namespace std; 

// Driver code 
int main () 
{ 
	// Creates a max heap 
	priority_queue  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; 
}

Example 4: Priority Queue using Min Heap in c++

#include 
using namespace std;
 

int main ()
{
   
    priority_queue  pq;
    pq.push(5);
    pq.push(1);
    pq.push(10);
    pq.push(30);
    pq.push(20);
 
   
    while (pq.empty() == false)
    {
        cout << pq.top() << " ";
        pq.pop();
    }
 
    return 0;
}

Tags:

Misc Example