C++ std::priority_queue::top() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
C++98

Definition

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
CallWhat 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

ParameterDescription
Nonetop() takes no parameters. Call it with empty brackets.

Return Value

Return ValueDescription
const T&A constant reference to the element with the highest priority. By default that is the largest value.
Undefined behaviorIf 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.

C++
#include <iostream>
#include <queue>

int main() {
    std::priority_queue<int> pq;
    pq.push(30);
    pq.push(10);
    pq.push(50);
    pq.push(20);

    std::cout << "Top: " << pq.top() << '\n';
    std::cout << "Top again: " << pq.top() << '\n';
    std::cout << "Size: " << pq.size() << '\n';
    return 0;
}

How It Works

  1. The queue is a max-heap by default. After four push() calls the value 50 sits at the root, even though it was pushed third.
  2. top() only reads the root. The second call gives 50 again, because nothing was removed.
  3. size() still returns 4. This proves top() never changes the queue. Only pop() can remove an element.

Example 2 — Smallest on Top with greater<int>

To see the smallest value, build a min-heap. You give the container and a comparison type after the element type.

C++
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

int main() {
    std::priority_queue<int, std::vector<int>, std::greater<int>> pq;
    pq.push(30);
    pq.push(10);
    pq.push(50);
    pq.push(20);

    std::cout << "Smallest on top: " << pq.top() << '\n';
    return 0;
}

How It Works

  1. 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.
  2. With greater, a smaller number counts as higher priority, so the heap root is now the minimum.
  3. 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;
}

How It Works

  1. The queue starts empty, so empty() is true and the program skips top(). This avoids undefined behavior.
  2. Three tasks are pushed. Pairs compare the number first, so {5, "fix bug"} beats {2, ...} and {1, ...}.
  3. 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.

Back to the function index

See all 8 std::priority_queue functions grouped by what they do.

Function index →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful