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.
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
Parameter
Description
None
size() takes no parameters. Call it with empty brackets.
Return Value
Return Value
Description
size_type
The 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.
limit is declared as std::size_t, the same unsigned type that size() returns, so the comparison is clean.
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.
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;
}
Output
Items: 1 2 3
Size after: 3
Front after: 1
How It Works
The loop saves q.size() into nonce, before it starts. This matters, because the size must not be read again while the loop changes the queue.
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.
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.