Signup/Sign In

C++ STL Priority Queue Program

Hello Everyone!

In this tutorial, we will learn about the working of Priority Queue, in the C++ programming language.

To understand the basic functionality of the Priority Queue in CPP, we will recommend you to visit C++ STL Priority Queue, where we have explained this concept in detail from scratch.

For a better understanding of its implementation, refer to the well-commented C++ code given below.


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

//Function to print the elements of the Priority Queue
void show(priority_queue<int> q)
    //Copying the Priority Queue into another to maintain the original Priority Queue
    priority_queue<int> pq = q;

    while (!pq.empty())
        cout << "\t" <<; //printing the top most element
        pq.pop();                 //deleting the top most element to move to the next

    cout << endl;

int main()
    cout << "\n\nWelcome to Studytonight :-)\n\n\n";
    cout << " =====  Program to demonstrate the Implementation of Priority Queue, in CPP  ===== \n\n";

    int i;

    //Declaring a Priority Queue of integers
    //Note: by default the priority queue is Max heap in c++
    priority_queue<int> q;

    //Filling the elements
    cout << "Inserting elements into the Priority Queue\n\n";
    for (i = 1; i < 6; i++)
        q.push(i * 10);

    cout << "The Priority Queue is: ";

    cout << "\n\nThe number of elements in the Priority Queue are : " << q.size();

    cout << "\n\nThe first element or the element with the highest priority is: " <<;

    cout << "\n\nAfter Deleting the top most element or the highest priority element from the Priority Queue, it becomes: ";

    cout << "\n\n\n";

    return 0;


c++ Priority Queue

We hope that this post helped you develop a better understanding of the concept of Priority Queue and its implementation in CPP. For any query, feel free to reach out to us via the comments section down below.

Keep Learning : )

About the author:
Nikita Pandey is a talented author and expert in programming languages such as C, C++, and Java. Her writing is informative, engaging, and offers practical insights and tips for programmers at all levels.