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

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

Definition

In C++, size() returns the number of elements currently stored in a std::priority_queue. A new queue has size 0. Every push() adds one, and every pop() takes one away.

Duplicates count too. If you push the same number three times, the size is 3. The size says nothing about priority. It only counts the elements, so you still use top() to see the largest or most important one.

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

Syntax

Call size() on the queue. It takes no arguments and returns an unsigned number.

C++
std::priority_queue<int> pq;
pq.push(5);
pq.push(9);

std::size_t n = pq.size();   // 2
CallWhat it does
pq.size()Returns how many elements the queue holds. The queue is not changed.

The return type is size_type, an unsigned integer. Be careful when you subtract from it. If the queue is empty, pq.size() - 1 does not give -1. It wraps around to a very large number. Check empty() first.

Library declarations
C++
size_type size() const;

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

Parameters

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

Return Value

Return ValueDescription
size_typeThe number of elements in the queue, as an unsigned integer. It is 0 for an empty queue.

C++ Examples

Example 1 — Watch the Size Change

Print the size after every change to see how push() and pop() move it up and down.

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

int main() {
    std::priority_queue<int> pq;
    std::cout << "Start: " << pq.size() << '\n';

    pq.push(10);
    pq.push(40);
    pq.push(20);
    std::cout << "After 3 pushes: " << pq.size() << '\n';

    pq.pop();
    std::cout << "After 1 pop: " << pq.size() << '\n';
    return 0;
}

How It Works

  1. A new queue has no elements, so size() prints 0.
  2. Each push() adds one element. After three pushes the size is 3.
  3. pop() removes the top element (40), so the size falls to 2. The value that was removed is not printed.

Example 2 — Duplicates Count, top() Does Not Change the Size

The queue keeps every pushed element, even equal ones. Reading with top() never removes anything.

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

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

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

How It Works

  1. The same value 5 is pushed three times. A priority queue is not a set, so all three copies stay inside and size() is 3.
  2. top() returns one 5. It only reads the root element.
  3. The second size() is still 3, which shows that reading with top() does not shrink the queue.

Example 3 — Keep Only the 3 Largest Values

A fixed-size min-heap is a classic trick. Push every number, and when the size goes above K, pop the smallest one.

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

int main() {
    std::vector<int> data = {5, 1, 9, 3, 7, 8};
    const std::size_t k = 3;

    std::priority_queue<int, std::vector<int>, std::greater<int>> pq;

    for (int n : data) {
        pq.push(n);
        if (pq.size() > k) {
            pq.pop();
        }
    }

    std::cout << "Size: " << pq.size() << '\n';
    std::cout << "Kept:";
    while (!pq.empty()) {
        std::cout << ' ' << pq.top();
        pq.pop();
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The queue is a min-heap, so its top() is always the smallest value. That is the one we want to throw away when there are too many.
  2. After each push() the code checks pq.size() > k. When the size reaches 4, pop() drops the smallest value, so the size never stays above 3.
  3. When the loop ends, the three biggest numbers 7, 8, and 9 remain. Draining a min-heap prints them from smallest to largest.

Frequently Asked Questions

It returns the number of elements currently in the priority queue, as an unsigned size_type. An empty queue has size 0.
Yes. A priority_queue keeps every element you push, including equal values. Pushing 5 three times gives a size of 3.
No. Both only read. Only push(), emplace(), pop(), and swap() change the contents of the queue.
Both work, but empty() is clearer and just as fast. Use size() when you need the actual count, for example to keep only the top K values.
size() returns an unsigned type. On an empty queue, 0 - 1 wraps around to a very large number instead of -1. Check empty() first, or compare without subtracting.

Next: swap()

Swap the contents of two priority queues in one step with std::priority_queue.

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

12 people found this page helpful