C++ std::deque size() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
std::deque

Definition

In C++, std::deque::size() returns the number of elements that a std::deque holds right now. A deque with three elements has the size 3.

The size changes whenever you add or remove elements, for example with push_back() or pop_front(). Include <deque> to use it.

This is a member of std::deque<T>, a double-ended queue. A deque is fast at adding and removing at both ends, and it allows access by index like std::vector. Unlike a vector, its elements are stored in several blocks, not in one contiguous block.

Syntax

Call size with empty brackets. It takes no arguments.

C++
std::size_t n = d.size();
for (std::size_t i = 0; i < d.size(); ++i) { }
CallWhat it does
d.size()Returns the number of elements.
d.size() - 1The index of the last element. Careful: it is wrong when the deque is empty.

To only check for no elements, empty() is clearer than size() == 0.

Library declarations
C++
size_type size() const noexcept;

Parameters

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

Return Value

Return ValueDescription
size_typeAn unsigned number: how many elements the deque holds. It is 0 for an empty deque.

C++ Examples

Example 1 — Basic size()

Count the elements of a small deque.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {10, 20, 30, 40};

    std::cout << "size: " << d.size() << '\n';
    return 0;
}

How It Works

  1. The deque holds four numbers.
  2. size() counts them and returns 4.
  3. The last valid index is size() - 1, which is 3.

Example 2 — Loop with size()

Use size() as the limit of an index loop.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d = {3, 6, 9};

    for (std::size_t i = 0; i < d.size(); ++i) {
        std::cout << "d[" << i << "] = " << d[i] << '\n';
    }
    return 0;
}

How It Works

  1. The index i starts at 0 and goes up while it is smaller than d.size().
  2. The type std::size_t matches the return type of size(), so the compiler has nothing to warn about.
  3. The loop never reaches index 3, so d[i] always stays inside the deque.

Example 3 — Watch the Size Change

The size grows and shrinks as you add and remove elements at both ends.

C++
#include <deque>
#include <iostream>

int main() {
    std::deque<int> d;
    std::cout << "start: " << d.size() << '\n';

    d.push_back(1);
    d.push_front(2);
    d.push_back(3);
    std::cout << "after 3 pushes: " << d.size() << '\n';

    d.pop_front();
    std::cout << "after 1 pop: " << d.size() << '\n';
    return 0;
}

How It Works

  1. A new deque is empty, so size() is 0.
  2. Each of the three pushes adds one element. It does not matter whether the push is at the front or the back, so the size is 3.
  3. pop_front() removes one element, and the size goes down to 2.

Frequently Asked Questions

It returns the number of elements that the deque holds right now, as an unsigned number.
size() is how many elements the deque has now. max_size() is the largest number it could ever have in theory.
size() is unsigned. If the deque is empty, d.size() - 1 wraps around to a huge number instead of -1. Check empty() first.
No. It takes constant time. The deque keeps the count, so it does not count the elements one by one.
A deque has size() only. capacity() exists for std::vector and std::string, but not for std::deque.

Next: swap()

Exchange the content of two deques with std::deque.

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.

9 people found this page helpful