Priority Queue in C++

Priority Queue in C++.

IntermediateSTL Containers ProgramsExample 9 of 10
priority-queue.cpp
1#include <iostream>
2#include <queue>
3#include <vector>
4using namespace std;
5
6int main() {
7 // Max heap (default - largest element at top)
8 priority_queue<int> pq;
9
10 pq.push(30);
11 pq.push(10);
12 pq.push(50);
13 pq.push(20);
14 pq.push(40);
15
16 cout << "Priority Queue (Max Heap):" << endl;
17 cout << "Top element (maximum): " << pq.top() << endl;
18
19 cout << "\nElements in descending order:" << endl;
20 while (!pq.empty()) {
21 cout << pq.top() << " ";
22 pq.pop();
23 }
24 cout << endl;
25
26 // Min heap
27 priority_queue<int, vector<int>, greater<int>> minHeap;
28
29 minHeap.push(30);
30 minHeap.push(10);
31 minHeap.push(50);
32 minHeap.push(20);
33 minHeap.push(40);
34
35 cout << "\nPriority Queue (Min Heap):" << endl;
36 cout << "Top element (minimum): " << minHeap.top() << endl;
37
38 cout << "\nElements in ascending order:" << endl;
39 while (!minHeap.empty()) {
40 cout << minHeap.top() << " ";
41 minHeap.pop();
42 }
43 cout << endl;
44
45 // Custom comparator
46 auto cmp = [](int a, int b) { return a > b; };
47 priority_queue<int, vector<int>, decltype(cmp)> customPQ(cmp);
48
49 customPQ.push(5);
50 customPQ.push(2);
51 customPQ.push(8);
52
53 cout << "\nCustom Priority Queue:" << endl;
54 while (!customPQ.empty()) {
55 cout << customPQ.top() << " ";
56 customPQ.pop();
57 }
58 cout << endl;
59
60 return 0;
61}

Output

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

What's going on

The program demonstrates priority queue operations:

Max heap (default - largest at top)
Min heap (smallest at top)
Custom comparators
Priority-based element access

Priority queues provide efficient access to highest/lowest priority elements.

Header Files Used

1.#include <iostream>
Provides cout and cin for input/output operations.
2.#include <queue>
Provides priority_queue container class.
3.#include <vector>
Provides vector for priority_queue implementation.

Understanding Priority Queue

Heap Concept

:

Binary heap data structure
Root contains max (or min) element
Maintains heap property
Efficient priority access

Key Features

:

O(1) access to top element
O(log n) insert/delete
Max heap (default) or min heap
Priority-based ordering

Max Heap (Default)

Declaration

:

priority_queue<int> pq;

Operations

:

pq.push(30); // Insert

pq.top(); // Access maximum

pq.pop(); // Remove maximum

How it works

:

Largest element always at top
Maintains max heap property
O(log n) insert/delete
O(1) top access

Min Heap

Declaration

:

priority_queue<int, vector<int>, greater<int>> minHeap;

How it works

:

Template: priority_queue<Type, Container, Comparator>
greater<int>: makes it min heap
Smallest element at top
Useful for finding minimum

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

:

Custom comparison function
Defines priority order
Flexible ordering
Can use any comparison logic

When to Use Priority Queue

Best For

:

Dijkstra's algorithm
Heap sort
Task scheduling by priority
Finding k largest/smallest
Event simulation
Merge k sorted lists

Example Scenarios

:

Shortest path algorithms
Top K elements
Priority-based scheduling
Median finding
Huffman coding

Important Considerations

Heap Property

:

Max heap: parent >= children
Min heap: parent <= children
Maintained automatically
Top element always max/min

Performance

:

Top access: O(1)
Insert: O(log n)
Delete: O(log n)
Efficient for priority operations

No Random Access

:

Only top element accessible
Must pop to access other elements
Use other containers if random access needed

return 0;

This ends the program successfully.