C++ std::vector capacity() Function

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

Definition

In C++, std::vector::capacity() tells you how many elements the vector can store in its current memory block before it must allocate a larger one.

size() is how many elements you have now. capacity() is how many would fit without a reallocation. Include <vector> to use it.

This is a member of std::vector<T>, a dynamic contiguous array. Elements sit in one block, so v[i] and data() are fast. Growing past capacity() reallocates and can invalidate iterators, references, and pointers. Prefer push_back / pop_back at the end; middle inserts shift elements.

Syntax

Call capacity with empty brackets. It takes no arguments.

C++
std::size_t cap = v.capacity();
std::cout << v.size() << " / " << v.capacity();
CallWhat it does
v.capacity()Returns the current storage size in elements. Always >= v.size().

Use reserve(n) to raise capacity in advance. Use shrink_to_fit() to ask the library to free spare room (it is a non-binding request).

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

Parameters

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

Return Value

Return ValueDescription
size_typeThe number of elements that fit without reallocating. Often larger than size().

C++ Examples

Example 1 — Print size() and capacity()

See that capacity can be larger than size after several push_back calls.

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

int main() {
    std::vector<int> v;
    for (int i = 0; i < 5; ++i) {
        v.push_back(i);
        std::cout << "size=" << v.size()
                  << " capacity=" << v.capacity() << '\n';
    }
    return 0;
}

How It Works

  1. The vector starts empty. Each push_back() grows size() by one.
  2. When size would exceed capacity, the vector allocates a bigger block (often about double). Capacity then jumps ahead of size.
  3. Exact capacity values depend on the library, but the idea is the same: spare room avoids reallocating on every insert.

Example 2 — reserve() Raises Capacity

Ask for room for 100 elements, then add a few and show capacity stays high.

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

int main() {
    std::vector<int> v;
    v.reserve(100);

    v.push_back(1);
    v.push_back(2);
    v.push_back(3);

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

How It Works

  1. reserve(100) asks the vector to prepare storage for at least 100 elements.
  2. Three push_back() calls only change the size. Capacity stays at least 100, so no reallocation happened.
  3. This pattern is common when you know how many elements you will add.

Example 3 — Same Size, Different Capacity

Two vectors can hold the same count of elements but report different capacities.

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

int main() {
    std::vector<int> a = {1, 2, 3};
    std::vector<int> b;
    b.reserve(50);
    b = {1, 2, 3};

    std::cout << "a size=" << a.size() << " cap=" << a.capacity() << '\n';
    std::cout << "b size=" << b.size() << " cap=" << b.capacity() << '\n';
    return 0;
}

How It Works

  1. Vector a was built from a short list, so its capacity is often equal to its size.
  2. Vector b reserved 50 slots first, then got three values. Size is 3, but capacity stays at least 50.
  3. Both store the same elements. Capacity is about reserved memory, not about the logical length.

Frequently Asked Questions

It returns how many elements fit in the current storage without a reallocation. It is always at least size().
size() is the number of elements you use. capacity() is the number of elements the current block can hold. Capacity can be larger than size.
clear() sets size to 0, but capacity usually stays the same. The memory block is kept for later push_back calls.
Call reserve(n). It makes capacity at least n without changing size.
std::array has a fixed size and no capacity(). std::deque has no capacity() or reserve() either. Those members are special to vector (and string).

Next: cbegin()

Get a read-only iterator to the first element with std::vector.

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

13 people found this page helpful