Open In App

Vector resize() vs Vector reserve()

Last Updated : 29 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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():

Parametervector resize()vector reserve()
PurposeChanges the number of elements in the vector.Changes the capacity of the vector.
Impact on SizeModifies the size.Does not modify the size.
Impact on CapacityMay modify the capacity.Can only increase the capacity.
Element InitializationAdds default-initialized or specific elements.Does not initialize any elements.
UsageAdjust the vector to a specific size.Optimize memory allocation for future growth.
Syntaxresize(n);
resize(n, val);
reserve(n);
When to UseWhen 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 ElementsRemoves 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;
}

Output
Capacity: 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.

Article Tags :
Practice Tags :

Similar Reads