A priority queue in C++ is std::priority_queue, a container adapter from the <queue> header that always gives you the largest element first. It stores elements in a binary heap built on top of another container (a std::vector by default), so push() and pop() take O(log n) time and top() takes O(1).
To get the smallest element first, pass std::greater<T> as the comparator. To order your own types, supply a comparison function object or a lambda.
std::priority_queueA priority queue is an abstract data type with three core operations: insert an element, read the element with the highest priority, and remove it. Unlike a normal queue, the order of arrival does not matter. The element that compares as "largest" under the ordering leaves first.
std::priority_queue implements this with a binary heap, a complete binary tree stored in an array where every parent is not smaller than its children. The heap is maintained with the standard algorithms std::push_heap and std::pop_heap, which is why the underlying container must offer random access and front(), push_back() and pop_back().
The template signature is:
std::vector and std::deque both satisfy the container requirements. std::less produces a max-heap, which is the reason the default behaviour is "largest first".
std::priority_queueThe interface is deliberately small:
| Member | What it does | Complexity |
|---|---|---|
push(x) | Inserts a copy or move of x | O(log n) |
emplace(args...) | Constructs an element in place | O(log n) |
top() | Returns a const reference to the highest-priority element | O(1) |
pop() | Removes the top element, returns nothing | O(log n) |
empty() | Returns true when no elements remain | O(1) |
size() | Returns the number of elements | O(1) |
swap(other) | Exchanges contents with another queue of the same type | O(1) |
Always check empty() before calling top() or pop(). Calling them on an empty queue is undefined behaviour. Also note that pop() does not return the removed value, so read it with top() first.
std::greaterSwap the comparator to make the smallest element come out first. You must also name the underlying container, because the comparator is the third template argument.
The comparator answers the question "does a have lower priority than b?". The element for which no other element compares higher ends up on top. Since C++20 you can pass a lambda type directly because stateless lambdas are default constructible; in earlier standards, pass the lambda to the constructor.
A comparator must be a strict weak ordering. Using <= instead of < breaks that rule and can corrupt the heap. If two elements have equal priority, their relative order is unspecified, because a heap is not stable. Add a sequence number to the comparison when you need first-in-first-out order among equals.
You can construct a queue from an iterator range or an existing vector. This uses std::make_heap and runs in O(n), which is faster than pushing n elements one by one at O(n log n).
If you need to understand or customise the mechanism, the standard library heap algorithms operate on any random-access range. This is exactly what the adapter does internally.
std::pop_heap moves the largest element to the end of the range, and pop_back() then removes it. Writing the heap by hand like this is useful when you need access to the underlying storage, for example to iterate over all elements or to remove an arbitrary item, which std::priority_queue does not allow.
Priority queues appear whenever you repeatedly need the "best" pending item:
A* search, using a min-heap keyed by distance.A short Dijkstra step shows the usual pattern. Because there is no decrease-key operation, push a new entry and skip stale ones when they are popped:
The main limitations are that you cannot iterate over the elements, cannot change the priority of an element already in the queue, and cannot remove an arbitrary element. If you need those, use a std::set, a std::map, or your own heap with an index.
std::priority_queue a max-heap or a min-heap?It is a max-heap by default, because the default comparator is std::less. Use std::greater as the third template argument to get a min-heap.
top() is O(1), push() and pop() are O(log n), and building from n existing elements is O(n). Space usage is O(n).
No. The adapter exposes only top(), so the only way to see every element is to pop them one by one, which also destroys the queue. Copy it first or use a vector with the heap algorithms if you need to inspect all elements.
std::priority_queue has no decrease-key or update operation. Push the new value and ignore outdated entries when they surface, or use a custom heap that tracks element positions.
std::priority_queue is a heap-backed container adapter that returns the highest-priority element first, with O(log n) insertion and removal. Use std::greater for a min-heap, a lambda or functor for custom types, and the range constructor to build from existing data in linear time. Remember that it is not stable, cannot be iterated, and does not support changing priorities in place.