Skip to content
Related Articles

Related Articles

Priority Queue in C++ Standard Template Library (STL)

Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 08 Jul, 2022
Improve Article
Save Article

Priority queues are a type of container adapters, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue and elements are in nonincreasing order (hence we can see that each element of the queue has a priority {fixed order}). However in C++ STL, by default, the top element is always the greatest element. We can also change it to the smallest element at the top. Priority queues are built on the top to the max heap and uses array or vector as an internal structure.

Following is an example to demonstrate the priority queue and its various methods.
 

CPP




// CPP Program to demonstrate Priority Queue
#include <iostream>
#include <queue>
using namespace std;
  
void showpq(priority_queue<int> gq)
{
    priority_queue<int> g = gq;
    while (!g.empty()) {
        cout << '\t' << g.top();
        g.pop();
    }
    cout << '\n';
}
  
// Driver Code
int main()
{
    priority_queue<int> gquiz;
    gquiz.push(10);
    gquiz.push(30);
    gquiz.push(20);
    gquiz.push(5);
    gquiz.push(1);
  
    cout << "The priority queue gquiz is : ";
    showpq(gquiz);
  
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.top() : " << gquiz.top();
  
    cout << "\ngquiz.pop() : ";
    gquiz.pop();
    showpq(gquiz);
  
    return 0;
}

Output

The priority queue gquiz is :     30    20    10    5    1

gquiz.size() : 5
gquiz.top() : 30
gquiz.pop() :     20    10    5    1

Note: By default, C++ creates a max-heap for priority queue.

GeeksforGeeks-CPP-Foundation-Course

How to create a min-heap for the priority queue? 
We can pass another parameter while creating the priority queue to make it a min heap. C++ provides the below syntax for the same.  

Syntax:

priority_queue <int, vector<int>, greater<int>> g = gq;  

CPP




// C++ program to demonstrate min heap for priority queue
#include <iostream>
#include <queue>
using namespace std;
  
void showpq(
    priority_queue<int, vector<int>, greater<int> > gq)
{
    priority_queue<int, vector<int>, greater<int> > g = gq;
    while (!g.empty()) {
        cout << '\t' << g.top();
        g.pop();
    }
    cout << '\n';
}
  
// Driver Code
int main()
{
    priority_queue<int, vector<int>, greater<int> > gquiz;
    gquiz.push(10);
    gquiz.push(30);
    gquiz.push(20);
    gquiz.push(5);
    gquiz.push(1);
  
    cout << "The priority queue gquiz is : ";
    showpq(gquiz);
  
    cout << "\ngquiz.size() : " << gquiz.size();
    cout << "\ngquiz.top() : " << gquiz.top();
  
    cout << "\ngquiz.pop() : ";
    gquiz.pop();
    showpq(gquiz);
  
    return 0;
}

Output

The priority queue gquiz is :     1    5    10    20    30

gquiz.size() : 5
gquiz.top() : 1
gquiz.pop() :     5    10    20    30

Note: The above syntax may be difficult to remember, so in case of numeric values, we can multiply the values with -1 and use max heap to get the effect of min heap.

C++




// CPP Program to demonstrate Priority Queue having min element at top
#include <iostream>
#include <queue>
using namespace std;
  
int main()
{
      int arr[]={1,2,3};
    
    priority_queue<int> pq1(arr, arr+3);     // creating priority queue using array
    
      cout<<"Max priority queue: ";
      while(pq1.empty()==false){
      cout<<pq1.top()<<" ";
      pq1.pop();
    }
      cout<<endl;
    
    
      for(int i=0; i<3; i++){   
      arr[i]=-arr[i];             // multiplying each array element by -1
    }
    
    priority_queue<int> pq2(arr, arr+3);
    
      cout<<"Min priority queue: ";
      while(pq2.empty()==false){
      cout<<-pq2.top()<<" ";
      pq2.pop();
    }
      
}

Output

Max priority queue: 3 2 1 
Min priority queue: 1 2 3 

Methods of Priority Queue are: 

Method

Definition

priority_queue::empty()Returns whether the queue is empty.
priority_queue::size() Returns the size of the queue.
priority_queue::top()Returns a reference to the topmost element of the queue.
priority_queue::push() Adds the element ‘g’ at the end of the queue.
priority_queue::pop()Deletes the first element of the queue.
priority_queue::swap()Used to swap the contents of two queues provided the queues must be of the same type, although sizes may differ.
priority_queue::emplace()Used to insert a new element into the priority queue container.
priority_queue value_type Represents the type of object stored as an element in a priority_queue. It acts as a synonym for the template parameter.

Must Read: Recent articles on priority queue in STL 

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.  


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!