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 reallocationsEffect 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 capacityExample 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. Comment More infoAdvertise with us Next Article Vector resize() vs Vector reserve() C cipher_encoder Follow Improve Article Tags : C++ STL cpp-vector Practice Tags : CPPSTL Similar Reads Vector reserve() in C++ STL In C++, vector reserve() is a built-in function that reserves the memory for at least a specified number of elements in the vector. It changes the capacity of the vector such that you can add at least the specified number of elements without triggering the internal memory reallocation.Letâs take a q 2 min read When to use Vector reserve() in C++? In a vector, when the number of elements to be inserted are greater than the current capacity, it is reallocated to a larger memory block and all the items are copied to this new block making this reallocation an expensive operation with time complexity of O(n).This mechanism works well if you don't 2 min read Vector emplace() vs insert() in C++ In C++, STL vector provides two methods for element insertion at the given position: vector emplace() and vector insert(). Although both methods provide similar functionality, there are a difference regarding how they work.The following table lists the primary differences between the vector emplace 3 min read How to create a new vector from a given vector in R In this article, we will discuss How to create a new vector from a given vector in R Programming Language. Create a new vector from a given vectorYou can use various functions and techniques depending on your needs to create a new vector from a given vector in R. Here are some common methods. 1. Sub 2 min read Vector size() in C++ STL In C++, the vector size() is a built-in method used to find the size of a vector. The size of a vector tells us the number of elements currently present in the vector. In this article, we will learn about the vector size() method.Let's take a look at the simple code example:C++#include <bits/stdc 3 min read Like