
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Priority Queue in C++ using STL
Priority Queue is a special type of queue in which elements can be accessed based on their priority. In this article, we will learn how to use the priority_queue container from the Standard Template Library (STL) in C++.
What is Priority Queue?
A Priority Queue is a container that stores data in such a way that the highest (or the lowest) priority element will always at the front of queue. Meaning, a priority queue does not follow FIFO rule of a regular queue, it processes elements based on ceratin priority. This priority can be defined as highest elements at front (max-heap) or lowest elements at front (min-heap). The STL library of C++ provides a pre-defined priority_queue container adapter that is implemented on top of a heap data structure.
For example, in the code we have shown how elements are arranged based on their priority in a priority queue:
// Declare a priority queue priority_queue<int> pq; // Add Data pq.push(30); pq.push(10); pq.push(20); pq.pop(); // This operation will remove 30 (the highest element)
Using priority_queue Container in STL
The priority_queue container is defined in the <queue> header of STL. It provides a max-heap by default, where the largest element has the highest priority. To define a min-heap, you can use a custom comparator such as greater<> with the help of <vector> and <functional> headers. Below are some points about this container:
- Header: <queue>, <vector>, <functional>
-
Syntax (for max-heap):
priority_queue<datatype> queue_name;
-
Syntax (for min-heap):
priority_queue<datatype, vector<datatype>, greater<datatype>> queue_name;
- Common functions:
- push() : Function used to insert an element into the priority queue.
- pop() : Function used to remove the element with the highest priority.
- top() : Function used to access the element with the highest priority.
- empty() : Function used to check if the priority queue is empty.
- size() : Function used to return the number of elements in the priority queue.
Steps to Implement Priority Queue in C++ STL
Following are steps/algorithm to use priority queue using C++ STL:
- Include the <queue> header file.
- Declare a priority_queue with desired data type.
- Use push() to insert elements.
- Use pop() to remove the highest priority element.
- Access top element using top().
- Use empty() and size() to check status.
C++ Program to Implement Priority Queue using STL
The below code is the implementation of the above algorithm in C++ language.
#include <iostream> #include <queue> using namespace std; int main() { priority_queue<int> pq; // Insert elements pq.push(30); pq.push(10); pq.push(20); cout << "Top Element: " << pq.top() << endl; // Remove one element pq.pop(); cout << "After one pop operation:" << endl; cout << "Top Element: " << pq.top() << endl; cout << "Priority Queue Size: " << pq.size() << endl; // Check if empty if (pq.empty()) { cout << "Priority Queue is empty" << endl; } else { cout << "Priority Queue is not empty" << endl; } return 0; }
The output of above code will be
Top Element: 30 After one pop operation: Top Element: 20 Priority Queue Size: 2 Priority Queue is not empty
Time and Space Complexity
- push(): O(log n)
- pop(): O(log n)
- top(): O(1)
Space Complexity: O(n), where n is the number of elements in the priority queue.