C++ std::vector reserve() Function

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

Definition

In C++, std::vector::reserve(n) asks the vector to make sure its capacity() is at least n. The number of elements (size()) does not change.

Call it before a loop of push_back() when you already know the final count. That avoids several reallocations and keeps old pointers valid longer. 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

Pass the number of elements you expect to store.

C++
v.reserve(1000);
for (int i = 0; i < 1000; ++i) {
    v.push_back(i);
}
CallWhat it does
v.reserve(1000)Ensures capacity is at least 1000. Size is unchanged. If capacity was already larger, nothing happens.

reserve() does not create elements. Use resize(n) when you want n real elements (default-constructed or filled).

Library declarations
C++
void reserve(size_type new_cap);

Parameters

ParameterDescription
new_capRequired. The minimum capacity you want. If it is greater than max_size(), the call throws std::length_error.

Return Value

Return ValueDescription
Nothingreserve() returns void. Check capacity() afterwards if you want to see the new room.

C++ Examples

Example 1 — Basic reserve()

Reserve room, then push a few values and print size and capacity.

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

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

    v.push_back(10);
    v.push_back(20);

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

How It Works

  1. After reserve(8), capacity is at least 8 while size is still 0.
  2. Two push_back() calls raise size to 2. Capacity stays at least 8.
  3. No reallocation was needed for those two inserts.

Example 2 — Reserve Before a Loop

Compare the idea: reserve once, then fill with a known count.

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

int main() {
    const int N = 5;
    std::vector<int> v;
    v.reserve(N);

    for (int i = 0; i < N; ++i) {
        v.push_back(i * 10);
    }

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << "| cap=" << v.capacity() << '\n';
    return 0;
}

How It Works

  1. N is the exact number of values you will add.
  2. reserve(N) allocates once. The loop then fills the vector without growing storage again.
  3. The printed capacity is at least 5. Exact values can be larger on some libraries, but one reserve is still the goal.

Example 3 — reserve() vs resize()

Show that reserve leaves size at 0, while resize creates elements.

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

int main() {
    std::vector<int> a;
    a.reserve(3);
    std::cout << "after reserve: size=" << a.size()
              << " cap=" << a.capacity() << '\n';

    std::vector<int> b;
    b.resize(3);
    std::cout << "after resize: size=" << b.size()
              << " cap=" << b.capacity() << '\n';
    return 0;
}

How It Works

  1. reserve(3) only prepares memory. Size stays 0, so a range-based for would print nothing.
  2. resize(3) creates three elements (zeros for int). Size becomes 3.
  3. Pick reserve when you will push_back yourself. Pick resize when you want the elements to exist now.

Frequently Asked Questions

It makes sure the vector can hold at least n elements without reallocating. It does not change size or create elements.
Call it when you know (or can estimate) how many push_back or emplace_back calls you will make. It reduces reallocations and can keep pointers valid.
reserve() only changes capacity. resize() changes size and creates or destroys elements.
No. Passing a smaller number than the current capacity does nothing. Use shrink_to_fit() to ask for a smaller block.
No. std::deque has no reserve() or capacity(). Those are vector features tied to one contiguous block.

Next: resize()

Change the number of elements, adding or removing at the back with std::vector.

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

7 people found this page helpful