Vector resize() vs Vector reserve()
Last Updated :
23 Jul, 2025
In C++, vector resize() and vector reserve() are two member methods of vector to manage memory of the container. In this article, we will learn the differences between vector resize() and vector reserve() method in C++.
- Vector size() modifies the size of the vector, i.e., the number of elements currently present in the vector.
- Vector reserve() is used to modify the allocated memory (capacity) of the vector i.e. number of elements a vector can store before reallocation.
The following table lists the primary differences between the vector resize() and vector reserve():
Parameter | vector resize() | vector reserve() |
---|
Purpose | Changes the number of elements in the vector. | Changes the capacity of the vector. |
---|
Impact on Size | Modifies the size. | Does not modify the size. |
---|
Impact on Capacity | May modify the capacity. | Can only increase the capacity. |
---|
Element Initialization | Adds default-initialized or specific elements. | Does not initialize any elements. |
---|
Usage | Adjust the vector to a specific size. | Optimize memory allocation for future growth. |
---|
Syntax | resize(n); resize(n, val); | reserve(n); |
---|
When to Use | When need a vector of a specific size with initialized elements. | When we want to pre-allocate memory for a known or estimated size to avoid reallocations |
---|
Effect on Existing Elements | Removes or add elements to the vector, potentially adding new elements. | No effect on existing elements. |
---|
As we can infer from the above table, resize() works on size of the vector while reserve works on the capacity of the vector. To know more about size and capacity of the vector, refer to the article - Vector size vs capacity
Example of resize() and reserve()
The change in capacity and size of vector through vector reserve() and vector resize() respectively can be observed in the below program:
C++
#include <bits/stdc++.h>
using namespace std;
int main() {
vector<int> v;
// reserve() change only capacity of vector
v.reserve(5);
cout << "Capacity: " << v.capacity();
cout << "\tSize: " << v.size() << endl;
// Try to decrease capacity of vector v1
v.reserve(2);
cout << "Capacity: " << v.capacity();
cout << "\tSize: " << v.size() << endl;
// Increase size
v.resize(9);
cout << "Capacity: " << v.capacity();
cout << "\tSize: " << v.size() << endl;
// Decrease size
v.resize(5);
cout << "Capacity: " << v.capacity();
cout << "\tSize: " << v.size() << endl;
return 0;
}
OutputCapacity: 5 Size: 0
Capacity: 5 Size: 0
Capacity: 9 Size: 9
Capacity: 9 Size: 5
Explanation: In the above example,
- v.reserve(5) will allocate memory for at least 5 elements, but the vector's size remains the same until elements are added. But v.reserve(2) will not be able to decrease the size of the vector.
- v.resize(9) increases the size to 9 along with capacity, while v.resize(5) reduces the size to 5. The capacity might change automatically when the size increases, but you can still have a larger capacity than the current size.
Explore
C++ Basics
Core Concepts
OOP in C++
Standard Template Library(STL)
Practice & Problems