C++ std::vector resize() Function

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

Definition

In C++, std::vector::resize() changes how many elements a std::vector holds. If the new size is bigger, new elements are added at the back. If it is smaller, extra elements are removed from the back.

New elements are filled with a default value (0 for numbers) or with a value that you choose. 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 new size. You can pass a second argument to choose the value of any new elements.

C++
v.resize(5);        // new elements are 0
v.resize(5, 9);     // new elements are 9
v.resize(2);        // keep only the first 2
CallWhat it does
v.resize(5)Makes the size 5. Missing elements are added at the back and set to T(), which is 0 for int.
v.resize(5, 9)Makes the size 5. Missing elements are copies of 9.
v.resize(2)Makes the size 2 by removing elements from the back.

Unlike std::vector, a vector has no reserve() or capacity(), so resize() is the way to set an exact element count.

Library declarations
C++
void resize(size_type count);
void resize(size_type count, const value_type& value);

Parameters

ParameterDescription
countRequired. The new number of elements.
valueOptional. The value used for new elements when the vector grows. If it is missing, new elements are value-initialized (0 for numbers).

Return Value

Return ValueDescription
Nothingresize() returns void. Check the result with size().

C++ Examples

Example 1 — Grow with resize()

Make a vector of three elements five elements long.

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

int main() {
    std::vector<int> v = {1, 2, 3};

    v.resize(5);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << v.size() << ")\n";
    return 0;
}

How It Works

  1. The vector starts with the three values 1 2 3.
  2. resize(5) needs two more elements, so it adds them at the back.
  3. No value was given, so the new elements are set to 0. The old elements are kept.

Example 2 — Shrink with resize()

Make a vector shorter. The extra elements are dropped from the back.

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

int main() {
    std::vector<int> v = {10, 20, 30, 40, 50};

    v.resize(2);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << "(size " << v.size() << ")\n";
    return 0;
}

How It Works

  1. The vector starts with five elements.
  2. resize(2) keeps the first two elements and destroys the other three at the back.
  3. The result is 10 20 with size 2. Elements from the front are never the ones removed.

Example 3 — Grow with a Chosen Value

Pass a second argument to fill the new elements with something other than zero.

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

int main() {
    std::vector<int> v = {1, 2};

    v.resize(5, 9);

    for (int n : v) {
        std::cout << n << ' ';
    }
    std::cout << '\n';
    return 0;
}

How It Works

  1. The vector begins as 1 2, so three new elements are needed.
  2. resize(5, 9) adds three copies of 9 at the back.
  3. The old values 1 2 are not touched. The extra argument only affects the new elements.

Frequently Asked Questions

It changes the number of elements in the vector. A bigger size adds elements at the back, and a smaller size removes elements from the back.
If you pass no value, they are value-initialized, which is 0 for numbers and an empty object for classes such as std::string. You can pass a second argument to choose the value.
No. It only adds or removes at the back. The first elements always stay the same, unless the vector shrinks to a size of 0.
resize() changes the element count. reserve() only prepares memory and exists for vector, not for vector.
Nothing changes. The vector already has that many elements.

Next: shrink_to_fit()

Ask the vector to release unused memory (C++11, non-binding) with std::vector.

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

8 people found this page helpful