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

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

Definition

In C++, empty() tells you whether a std::priority_queue has no elements. It returns true for an empty queue and false when at least one element is waiting.

It is the safety check for the whole container. top() and pop() are undefined behavior on an empty queue, so you ask empty() first. A priority queue has no iterators, so while (!pq.empty()) is also the normal way to walk through every element.

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.empty(...).

Syntax

Call empty() on the queue. It takes no arguments and returns a bool.

C++
std::priority_queue<int> pq;

bool a = pq.empty();   // true, nothing inside

pq.push(7);
bool b = pq.empty();   // false, one element inside
CallWhat it does
pq.empty()Returns true if the queue has no elements, otherwise false. It never changes the queue.

empty() is fast and runs in constant time. It is the same as asking pq.size() == 0, but it reads more clearly. Since C++20 the function is marked [[nodiscard]], so a compiler may warn if you call it and ignore the answer.

Library declarations
C++
[[nodiscard]] bool empty() const;   // [[nodiscard]] since C++20

// priority_queue<T, Container, Compare> from <queue>
// empty() returns c.empty()

Parameters

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

Return Value

Return ValueDescription
trueThe queue has no elements.
falseThe queue has at least one element.

C++ Examples

Example 1 — Check an Empty Queue

A new queue is empty. After one push() it is not. Use std::boolalpha to print true and false.

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

int main() {
    std::priority_queue<int> pq;

    std::cout << std::boolalpha;
    std::cout << "Before push: " << pq.empty() << '\n';

    pq.push(42);
    std::cout << "After push:  " << pq.empty() << '\n';
    return 0;
}

How It Works

  1. A freshly created priority_queue holds nothing, so empty() returns true.
  2. std::boolalpha makes cout print the words true and false instead of 1 and 0.
  3. After push(42) the queue has one element, so empty() returns false.

Example 2 — Walk Through Every Element with while (!pq.empty())

You cannot loop over a priority queue with iterators, so use top(), pop(), and empty() together.

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

int main() {
    std::priority_queue<int> pq;
    pq.push(4);
    pq.push(9);
    pq.push(2);

    while (!pq.empty()) {
        std::cout << pq.top() << ' ';
        pq.pop();
    }
    std::cout << '\n';

    std::cout << std::boolalpha << "Now empty: " << pq.empty() << '\n';
    return 0;
}

How It Works

  1. The loop runs while empty() is false. Each round prints the current top(), then pop() removes it.
  2. The largest value always comes out first, so the numbers appear in falling order: 9, 4, 2.
  3. After the third pop() the queue has no elements, empty() becomes true, and the loop stops. The last line confirms this.

Example 3 — A Safe Helper Function

Wrap the check in a small function that returns a default value when the queue is empty.

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

int largestOr(const std::priority_queue<int>& pq, int fallback) {
    if (pq.empty()) {
        return fallback;
    }
    return pq.top();
}

int main() {
    std::priority_queue<int> none;
    std::priority_queue<int> some;
    some.push(3);
    some.push(8);
    some.push(5);

    std::cout << "Empty queue: " << largestOr(none, -1) << '\n';
    std::cout << "Filled queue: " << largestOr(some, -1) << '\n';
    return 0;
}

How It Works

  1. largestOr() receives the queue by const reference. empty() and top() are both const functions, so this is allowed.
  2. For the empty queue, empty() is true, so the function returns the fallback -1 and never touches top().
  3. For the filled queue, empty() is false, so top() is safe to call and returns the largest value 8.

Frequently Asked Questions

It returns true if the priority_queue has no elements, and false if it has at least one element.
Calling top() or pop() on an empty queue is undefined behavior. A quick if (!pq.empty()) check keeps your program safe.
A priority_queue has no iterators. Use while (!pq.empty()) { use pq.top(); pq.pop(); }. This also removes the elements one by one from the queue.
Yes. Both mean the queue holds no elements. empty() is the clearer way to write it and is the style most C++ programmers prefer.
There is no clear() function. Assign a new empty queue, such as pq = std::priority_queue<int>(); or swap with an empty one. Calling empty() afterwards returns true.

Next: pop()

Remove the highest-priority element. It does not return the value with std::priority_queue.

pop() tutorial →

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.

8 people found this page helpful