In C++, top() is the only way to look at an element of a std::priority_queue. It returns the element that would come out first, which is the largest one by default, because a priority queue is a max-heap.
top() only reads. It does not remove the element, so calling it twice gives the same answer. A priority queue has no [] operator, no at(), and no iterators, so top() is the one element you can see at any moment.
This function is a member function of std::priority_queue<T>, declared in the <queue> header. It has been available since the first C++ standard, so every compiler supports it. You call it with a dot, for example pq.top(...).
Syntax
Call top() on the queue. It takes no arguments.
C++
std::priority_queue<int> pq;
pq.push(30);
pq.push(50);
pq.push(10);
int biggest = pq.top(); // 50, the largest element
Call
What it does
pq.top()
Returns a const reference to the highest-priority element. The element stays in the queue.
Because the result is a const reference, you can read it or copy it, but you cannot change it. Changing it would break the heap order. To change a priority, pop() the element and push() a new one.
Library declarations
C++
const_reference top() const;
// priority_queue<T, Container, Compare> from <queue>
// top() returns c.front(), the element at the front
// of the underlying container (the heap root).
Parameters
Parameter
Description
None
top() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
const T&
A constant reference to the element with the highest priority. By default that is the largest value.
Undefined behavior
If the queue is empty there is no element to return. Check empty() first.
C++ Examples
Example 1 — Read the Largest Element
Push four numbers in any order. The biggest one is always on top, and reading it does not remove it.
The three template arguments are the element type int, the container std::vector<int>, and the comparison std::greater<int>. The last one flips the order.
With greater, a smaller number counts as higher priority, so the heap root is now the minimum.
top() works the same way as before. It returns the root, which is 10 here. Include <functional> to use std::greater.
Example 3 — Check empty() Before top() (Pairs)
Pairs are compared by the first value, then by the second. The first value works well as a priority number.
C++
#include <iostream>
#include <queue>
#include <string>
#include <utility>
int main() {
std::priority_queue<std::pair<int, std::string>> tasks;
if (tasks.empty()) {
std::cout << "No tasks yet, so top() is not called.\n";
}
tasks.push({2, "write report"});
tasks.push({5, "fix bug"});
tasks.push({1, "drink tea"});
if (!tasks.empty()) {
std::cout << "Do first: " << tasks.top().second
<< " (priority " << tasks.top().first << ")\n";
}
return 0;
}
Output
No tasks yet, so top() is not called.
Do first: fix bug (priority 5)
How It Works
The queue starts empty, so empty() is true and the program skips top(). This avoids undefined behavior.
Three tasks are pushed. Pairs compare the number first, so {5, "fix bug"} beats {2, ...} and {1, ...}.
The second check !tasks.empty() is true. Now top() is safe, and .first and .second read the pair parts.
Frequently Asked Questions
It returns a const reference to the highest-priority element without removing it. With the default settings that is the largest element in the queue.
No. top() only reads the element. Call pop() afterwards to remove it. The usual pattern is to read with top() and then call pop().
It is undefined behavior. The program may crash or return garbage. Always check that !pq.empty() before calling top().
Use a min-heap: std::priority_queue<int, std::vector<int>, std::greater<int>> pq. The greater comparison puts the smallest value on top. Include <functional> for std::greater.
No. top() returns a const reference, so the value is read-only. To change it, pop() the element and push() the new value.