In C++, std::priority_queue<T> is a container from the <queue> header that always keeps the most important element on top. You add elements in any order, and you always take out the one with the highest priority first.
By default the largest value has the highest priority, so the queue is a max-heap. The daily pattern is simple: push() to add, top() to read the best element, and pop() to remove it. Use empty() before you read or remove.
This page teaches what a priority queue is. The priority_queue Functions index lists every function guide in this series.
You can only see the top.
A priority queue has no [], no at(), and no begin() or end(). You cannot loop over all elements with iterators. Reading or removing from an empty queue with top() or pop() is undefined behavior, so check empty() first.
Syntax
C++ Syntax
Common ways to create and use a std::priority_queue:
C++
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
std::priority_queue<int> maxHeap; // largest on top
std::priority_queue<int, std::vector<int>, std::greater<int>> minHeap; // smallest on top
maxHeap.push(10);
maxHeap.push(40);
maxHeap.emplace(20); // C++11
int biggest = maxHeap.top(); // 40
maxHeap.pop(); // removes 40, returns nothing
std::size_t n = maxHeap.size(); // 2
bool none = maxHeap.empty(); // false
std::vector<int> v = {3, 1, 2};
std::priority_queue<int> fromVector(v.begin(), v.end()); // build from a range
Reference
Max-Heap vs Min-Heap
The only thing that changes is the comparison. Here is a quick comparison for beginners:
Feature
Max-heap (default)
Min-heap
Type
priority_queue<int>
priority_queue<int, vector<int>, greater<int>>
Comparison used
std::less
std::greater
What top() gives
The largest element
The smallest element
Pop order for 4, 1, 7
7, 4, 1
1, 4, 7
Extra headers
<queue>
<queue>, <vector>, <functional>
Typical use
Biggest score, most urgent task
Shortest distance, smallest ticket, top K largest
Guide
priority_queue vs queue
Both are container adaptors from <queue>. They share push(), pop(), size(), and empty(). The difference is the order in which elements come out:
Both queues get the same three numbers. The only difference is the type: the second queue adds the container std::vector<int> and the comparison std::greater<int>.
The default std::less makes a max-heap, so the largest number 7 counts as the highest priority and leaves first.
std::greater flips the rule, so the smallest number 1 counts as the highest priority. The min-heap prints 1 4 7.
Example 3 — queue vs priority_queue
A normal queue serves items in the order they arrived. A priority queue serves the most important item first.
std::priority_queue is a container adaptor from the <queue> header. It keeps its elements in a heap, so the element with the highest priority is always on top. By default the largest value is on top, which is called a max-heap.
Add std::greater as the third template argument: std::priority_queue<int, std::vector<int>, std::greater<int>> pq. Then the smallest value is on top. Include <functional> and <vector>.
std::queue serves elements in the order they arrived (first in, first out) and reads with front(). std::priority_queue serves the highest-priority element first and reads with top(), no matter when it was added.
No. A priority_queue has no iterators and no operator[]. You can only see the top element. To visit every element, loop with while (!pq.empty()) and call top() then pop().
Open the C++ std::priority_queue Functions index. It lists all 8 member functions with a short description, the C++ version needed, and a full tutorial for each.