C++ priority queues
In C++, a priority queue is a type of container adaptor in the C++ Standard Library that allows elements to be processed in a specific order based on their priority. This data structure is a heap-based container, which is implemented in the <queue> header. A priority queue orders its elements in such a way that the highest (or lowest) priority element is always accessible at the top. By default, C++ uses a max-heap implementation, where the largest element has the highest priority.
std::priority_queue
std::priority_queue
are ordered so that the largest (or highest priority) element is at the top of the container.priority_queue
is implemented using a binary heap structure, which provides an efficient way to manage the order.To declare a priority queue:
std::priority_queue<T> pq;
Where:
T
is the type of elements in the priority queue (e.g., int
, std::string
).You can also define the container with a custom comparison function to change the priority order:
std::priority_queue<T, Container, Compare> pq;
Where:
Container
is the underlying container type (like std::vector
by default).Compare
is a binary function or a comparison object that defines the ordering.
#include <iostream>
#include <queue>
int main() {
// Declare a priority queue of integers (max-heap by default)
std::priority_queue<int> pq;
// Insert elements into the priority queue
pq.push(10);
pq.push(20);
pq.push(15);
// Displaying the top element
std::cout << "Top element: " << pq.top() << std::endl; // Output: 20
return 0;
}
Explanation:
push()
adds elements to the priority queue.top()
accesses the element with the highest priority (the largest in a max-heap).By default, the std::priority_queue
implements a max-heap, where the largest element has the highest priority. However, you can change this behavior to implement a min-heap by providing a custom comparator.
#include <iostream>
#include <queue>
#include <vector>
#include <functional> // For std::greater
int main() {
// Declare a priority queue with min-heap (using std::greater)
std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
// Insert elements into the priority queue
pq.push(10);
pq.push(20);
pq.push(15);
// Displaying the top element (smallest element in min-heap)
std::cout << "Top element: " << pq.top() << std::endl; // Output: 10
return 0;
}
Explanation:
std::greater<int>
, the priority queue behaves like a min-heap, where the smallest element has the highest priority.std::priority_queue
push()
pq.push(30); // Adds 30 to the priority queue
pop()
pq.pop(); // Removes the element with the highest priority (top element)
top()
std::cout << "Top element: " << pq.top() << std::endl;
size()
std::cout << "Size of the priority queue: " << pq.size() << std::endl;
empty()
if (pq.empty()) {
std::cout << "The priority queue is empty!" << std::endl;
} else {
std::cout << "The priority queue is not empty!" << std::endl;
}
Unlike other containers such as vectors or lists, you cannot directly iterate through a std::priority_queue
since it is a container adaptor designed to give access only to the top element. However, you can pop elements from the queue to process them one by one.
#include <iostream>
#include <queue>
int main() {
std::priority_queue<int> pq;
pq.push(10);
pq.push(20);
pq.push(15);
// Process elements by popping from the queue
while (!pq.empty()) {
std::cout << pq.top() << " "; // Display the top element
pq.pop(); // Remove the top element
}
return 0;
}
Output:
20 15 10
Explanation:
std::priority_queue
top()
function allows constant-time access to the highest priority element, making priority queues ideal for scheduling and decision-making tasks.std::priority_queue
grows dynamically as elements are added.std::priority_queue
Use a priority queue when: