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

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

Definition

In C++, size() tells you how many elements a std::queue holds right now. Every push() makes the number one bigger, and every pop() makes it one smaller.

A new queue has size 0. Because a queue hides everything except its two ends, size() is often the only way to know how long the line is without emptying it.

This function is a member function of std::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 q.size(...).

Syntax

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

C++
std::queue<int> q;
q.push(10);
q.push(20);
q.push(30);

std::size_t n = q.size();   // 3
CallWhat it does
q.size()Returns the number of elements as a size_type (usually std::size_t). It never changes the queue.

size_type is unsigned, so avoid expressions like q.size() - 1 when the queue may be empty, because 0 - 1 wraps around to a huge number. Use empty() to test for emptiness. size() runs in constant time.

Library declarations
C++
size_type size() const;

// queue<T, Container> from <queue>
// size() returns c.size()
// size_type is the container's size type (std::size_t for deque).

Parameters

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

Return Value

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

C++ Examples

Example 1 — Watch the Size Change

Print the size at three moments: when empty, after pushes, and after a pop.

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

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

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

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

How It Works

  1. A new queue holds nothing, so size() prints 0.
  2. Each push() adds one element at the back. After three calls the counter shows 3.
  3. pop() removes the front element 10, so the size drops by one to 2. The queue now holds 20, 30.

Example 2 — Keep a Queue From Growing Past a Limit

A queue never fills up by itself. You can use size() to stop new items when the line is already long enough.

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

int main() {
    const std::size_t limit = 3;
    std::queue<int> q;

    for (int item = 1; item <= 5; ++item) {
        if (q.size() < limit) {
            q.push(item);
            std::cout << "Added " << item << '\n';
        } else {
            std::cout << "Full, skipped " << item << '\n';
        }
    }

    std::cout << "Size: " << q.size() << '\n';
    std::cout << "Front: " << q.front() << ", Back: " << q.back() << '\n';
    return 0;
}

How It Works

  1. limit is declared as std::size_t, the same unsigned type that size() returns, so the comparison is clean.
  2. For items 1 to 3 the check q.size() < limit is true, so each one is pushed. When the size reaches 3 the check fails, and items 4 and 5 are skipped.
  3. The queue keeps only the first three items in arrival order. front() is 1 and back() is 3.

Example 3 — Print Every Element and Keep the Queue

Popping empties the queue. To print all elements and still keep them, rotate each element to the back exactly size() times.

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

int main() {
    std::queue<int> q;
    q.push(1);
    q.push(2);
    q.push(3);

    std::cout << "Items:";
    for (std::size_t i = 0, n = q.size(); i < n; ++i) {
        int x = q.front();
        q.pop();
        std::cout << ' ' << x;
        q.push(x);
    }
    std::cout << '\n';

    std::cout << "Size after: " << q.size() << '\n';
    std::cout << "Front after: " << q.front() << '\n';
    return 0;
}

How It Works

  1. The loop saves q.size() into n once, before it starts. This matters, because the size must not be read again while the loop changes the queue.
  2. Each round copies front() into x, removes it with pop(), prints it, and pushes it to the back. The element moves from the front of the line to the end.
  3. After exactly n = 3 rounds every element has moved around the line once. The order is back to 1, 2, 3, so the size is still 3 and front() is 1.

Frequently Asked Questions

It returns the number of elements currently in the queue, as an unsigned size_type. An empty queue returns 0.
No. size() runs in constant time, so it is just as fast for a queue with a million elements as for a queue with one.
Not by itself. The queue grows as needed until memory runs out. If you want a limit, check size() before push(), as in the bounded queue example above.
size() returns an unsigned type. When the queue is empty, 0 - 1 wraps around to a very large value. Check !q.empty() first, or compare with addition instead.
Both give the same answer. Prefer empty() because it states your intent clearly and works the same way on every standard container.

Next: swap()

Exchange the contents of two queues of the same type with std::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.

14 people found this page helpful