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
Call
What 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.
Optional. 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 Value
Description
Nothing
resize() 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;
}
Output
1 2 3 0 0 (size 5)
How It Works
The vector starts with the three values 1 2 3.
resize(5) needs two more elements, so it adds them at the back.
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;
}
Output
10 20 (size 2)
How It Works
The vector starts with five elements.
resize(2) keeps the first two elements and destroys the other three at the back.
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;
}
Output
1 2 9 9 9
How It Works
The vector begins as 1 2, so three new elements are needed.
resize(5, 9) adds three copies of 9 at the back.
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.