Priority Queue
Priority Queue in C++
C++ Priority Queue Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() {
// Max heap (default - largest element at top)
priority_queue<int> pq;
pq.push(30);
pq.push(10);
pq.push(50);
pq.push(20);
pq.push(40);
cout << "Priority Queue (Max Heap):" << endl;
cout << "Top element (maximum): " << pq.top() << endl;
cout << "\nElements in descending order:" << endl;
while (!pq.empty()) {
cout << pq.top() << " ";
pq.pop();
}
cout << endl;
// Min heap
priority_queue<int, vector<int>, greater<int>> minHeap;
minHeap.push(30);
minHeap.push(10);
minHeap.push(50);
minHeap.push(20);
minHeap.push(40);
cout << "\nPriority Queue (Min Heap):" << endl;
cout << "Top element (minimum): " << minHeap.top() << endl;
cout << "\nElements in ascending order:" << endl;
while (!minHeap.empty()) {
cout << minHeap.top() << " ";
minHeap.pop();
}
cout << endl;
// Custom comparator
auto cmp = [](int a, int b) { return a > b; };
priority_queue<int, vector<int>, decltype(cmp)> customPQ(cmp);
customPQ.push(5);
customPQ.push(2);
customPQ.push(8);
cout << "\nCustom Priority Queue:" << endl;
while (!customPQ.empty()) {
cout << customPQ.top() << " ";
customPQ.pop();
}
cout << endl;
return 0;
}Priority Queue (Max Heap): Top element (maximum): 50 Elements in descending order: 50 40 30 20 10 Priority Queue (Min Heap): Top element (minimum): 10 Elements in ascending order: 10 20 30 40 50 Custom Priority Queue: 2 5 8
Understanding Priority Queue
This program teaches you how to use Priority Queue in C++. Priority queue is a container that provides constant-time access to the largest (or smallest) element. By default, it's a max heap, but can be configured as a min heap. It's essential for algorithms requiring priority-based processing.
---
1. What This Program Does
The program demonstrates priority queue operations:
Priority queues provide efficient access to highest/lowest priority elements.
---
2. Header Files Used
---
3. Understanding Priority Queue
Heap Concept
:
Key Features
:
---
4. Max Heap (Default)
Declaration
:
priority_queue<int> pq;
Operations
:
pq.push(30); // Insert
pq.top(); // Access maximum
pq.pop(); // Remove maximum
How it works
:
---
5. Min Heap
Declaration
:
priority_queue<int, vector<int>, greater<int>> minHeap;
How it works
:
---
6. Custom Comparator
Lambda Comparator
:
auto cmp = [](int a, int b) { return a > b; };
priority_queue<int, vector<int>, decltype(cmp)> customPQ(cmp);
How it works
:
---
7. When to Use Priority Queue
Best For
:
Example Scenarios
:
---
8. Important Considerations
Heap Property
:
Performance
:
No Random Access
:
---
9. return 0;
This ends the program successfully.
---
Summary
This program is fundamental for learning heap data structures, understanding priority-based algorithms, and preparing for advanced graph algorithms and scheduling problems in C++ programs.
Let us now understand every line and the components of the above program.
Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.
Practical Learning Notes for Priority Queue
This C++ program is part of the "STL Containers Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.
A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.
For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.