Tutorialspoint.dev

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

Priority queues are a type of container adapters, specifically designed such that the first element of the queue is the greatest of all elements in the queue and elements are in non decreasing order(hence we can see that each element of the queue has a priority{fixed order}).
 
Methods of priority queue are:

Below is the C++ program to show the functionality of Priority Queue:

#include <iostream>
#include <queue>
  
using namespace std;
  
void showpq(priority_queue <int> gq)
{
    priority_queue <int> g = gq;
    while (!g.empty())
    {
        cout << ' ' << g.top();
        g.pop();
    }
    cout << ' ';
}
  
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 << " gquiz.size() : " << gquiz.size();
    cout << " gquiz.top() : " << gquiz.top();
  
  
    cout << " gquiz.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

 
Recent articles on priority queue in STL

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



This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter