std::priority_queue
std::priority_queue<T> is a max-heap adapter. top() is the largest element according to the comparator. push and pop are O(log n). There is no iterator and no decrease-key.
Detailed Explanation
A FIFO queue serves the oldest item. A priority queue serves the most important item. Dijkstra's algorithm, a job scheduler, and "keep the 10 largest scores" are the usual uses. std::priority_queue is a heap over an underlying vector by default.
The comparator defaults to std::less<T>, which makes a max heap: top() is the greatest. That surprises people who wanted the smallest next. For a min heap, write std::priority_queue<int, std::vector<int>, std::greater<int>>. For a custom order, write a functor whose operator() returns true when the first argument should sink toward the bottom (the heap's "less").
push / emplace insert and sift. top() peeks. pop() removes the top and returns void. empty and size forward. You cannot walk the heap in sorted order without popping. You cannot edit an element in the middle and re-heapify; there is no handle. If you need decrease-key, use std::set, a policy-based data structure, or a hand-rolled binary heap of indices.
The underlying container must support random-access iterators (vector or deque). vector is the default and the right choice.
Equality of priorities is allowed. The heap is not stable: two equal elements may pop in either order. If you need FIFO among equals, store a pair of (priority, insertion_index).
Building a heap from an existing vector can use the constructor that takes a comparator and a container, which is linear via std::make_heap. Repeated push is O(n log n).
std::make_heap, push_heap, pop_heap, and sort_heap in <algorithm> are the same heap on a sequence you own. Use those when you need to peek at the unsorted tail or sort in place. Use priority_queue when you want the restricted API.
Not thread-safe. Not a replacement for set when you need to erase an arbitrary value: priority_queue cannot find an arbitrary value in better than linear time, and it does not even offer that operation.
Code Examples
#include <iostream>
#include <queue>
#include <vector>
int main() {
std::priority_queue<int> maxh;
maxh.push(2);
maxh.push(9);
maxh.push(5);
std::cout << maxh.top() << '\n'; // prints: 9
maxh.pop();
std::cout << maxh.top() << '\n'; // prints: 5
std::priority_queue<int, std::vector<int>, std::greater<int>> minh;
minh.push(2);
minh.push(9);
std::cout << minh.top() << '\n'; // prints: 2
}#include <queue>
#include <string>
#include <utility>
#include <vector>
int main() {
using Job = std::pair<int, std::string>; // priority, name
std::priority_queue<Job> jobs;
jobs.push({10, "render"});
jobs.push({1, "log"});
// jobs.top() is {10, "render"}
}Common Gotchas
Default is a max heap
`top()` is the largest, not the smallest. Use `greater<T>` for a min heap (Dijkstra).
`pop()` is `void` and `top()` on empty is undefined
same as `stack` and `queue`.
No decrease-key, no iterators
you cannot update a node's priority in place. Push a new node and ignore stale ones, or pick a different structure.
Custom comparators must be consistent
changing the order of two in-heap elements via a mutating comparator is undefined.