Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

C++ STL: priority_queue (Complete Guide)

Divya Jain
Divya Jain

A priority queue is a type of dynamic container in STL. It stores the elements in a sorted order where the first element of the queue is the greatest of all and the other elements are sorted in non-increasing order. 

They are implemented as a heap in memory. The greatest element will be the one that will be deleted first. They are present in #include<queue> header file.

Heaps are of two types:

  • Max-heap: When the elements are stored in non-increasing order and the greatest element will be the one that will be deleted first, i.e largest element has the highest priority. By default, C++ creates a max-heap.
    Syntax:
    priority_queue < data_type > name;

Example:

priority_queue < int > pq; //initializes a priority queue (max-heap) of size 0 which stores integer values 
  • Min-heap: When the elements are stored in non-decreasing order and the smallest element will be the one that will be deleted first, i.e smallest element has the highest priority.
    Syntax:
    priority_queue<data_type,vector<int>,greater<int>> name;

Example:

priority_queue < int, vector < int > , greater < int >> pq; //initializes a priority queue (min-heap) of size 0 which stores integer values 

Functions on priority_queues

  • push(element): It pushes the value at the end of the queue. After that, the elements are reordered according to the priority.
    Time Complexity: O(logN) where is the size of the queue
    Parameters: the element to be inserted
    Return value: void
     
  • pop(): It deletes the highest priority element from the queue. If it's a min-heap, the smallest element gets deleted otherwise maximum element gets deleted. 
    Parameters: None
    Return value: void
     
  • size(): It tells us the size of the queue. 
    Parameters: None
    Return value: integer, the size of the queue
     
  • top(): It returns the highest priority element of the queue. 
    Parameters: None 
    Return value: the highest priority element of the queue
     
  • swap(queue_name): It swaps the elements of the two queues. 
    Parameters: queue name to be swapped with
    Return value: void
     
  • empty(): It tells us whether the queue is empty or not. 
    Parameters: None
    Return value: boolean, true if the queue is empty else false

Example of max-heap

#include<iostream>
#include<queue>
using namespace std;

int main() {
  priority_queue < int > pq; //max-heap
  for (int i = 0; i < 5; i++) {
    pq.push(i + 1);
  }
  cout << "The size of queue is : " << pq.size() << '\n';
  cout << "The highest priority element of queue is " << pq.top() << '\n';
  pq.pop();
  pq.pop();
  pq.push(10);
  pq.push(20);
  pq.push(30);
  if (!pq.empty()) {
    cout << "The new size of queue is " << pq.size() << '\n';
  }
  cout << "After inserting and deleting elements, the queue is " << '\n';
  while (!pq.empty()) {
    cout << pq.top() << " ";
    pq.pop();
  }
}

Output

The size of queue is : 5
The highest priority element of queue is 5
The new size of queue is 6
After inserting and deleting elements, the queue is 
30 20 10 3 2 1 

Example of min-heap

#include<iostream>
#include<queue>
using namespace std;

int main() {
  priority_queue < int, vector < int > , greater < int >> pq; //min-heap
  for (int i = 0; i < 5; i++) {
    pq.push(i + 1);
  }
  cout << "The size of queue is : " << pq.size() << '\n';
  cout << "The highest priority element of queue is " << pq.top() << '\n';
  pq.pop();
  pq.pop();
  pq.push(10);
  pq.push(20);
  pq.push(30);
  if (!pq.empty()) {
    cout << "The new size of queue is " << pq.size() << '\n';
  }
  cout << "After inserting and deleting elements, the queue is " << '\n';
  while (!pq.empty()) {
    cout << pq.top() << " ";
    pq.pop();
  }
}

Output

The size of queue is : 5
The highest priority element of queue is 1
The new size of queue is 6
After inserting and deleting elements, the queue is 
3 4 5 10 20 30 
Divya Jain
Divya Jain
Divya is an incoming SDE at Atlassian. She loves solving problems and web development. She loves to explore new things and is up for interesting conversations about tech.
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet