Vector reserve() in C++ STL Last Updated : 20 Nov, 2024 Comments Improve Suggest changes Like Article Like Report 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 quick look at an example that illustrates vector reserve() method: C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v; // Increase the capacity of vector v.reserve(9); cout << v.capacity(); return 0; } OutputCapacity: 9This article covers the syntax, usage, and common examples of vector reserve() method in C++:Table of ContentSyntax of vector reserve()Example of vector reserve()Increase Capacity of VectorTry to Decrease Capacity of VectorHow the vector reserve() works?vector reserve() in C++ – FAQsSyntax of Vector reserve()The vector reserve() is the member method of std::vector class defined inside <vector> header file.v.reserve(n);Parametersn: Number of elements for which the memory is to be reserved.Return ValueThis function does not return any value.Examples of vector reserve()The following examples demonstrates the use of vector reserve() method for different scenarios and purposes:Increase the Capacity of Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Capacity initially cout << v.capacity() << endl; // Increase the capacity of vector v.reserve(9); cout << v.capacity(); return 0; } Output5 9Explanation: Initially the capacity of vector is 5, with vector reserve() we increase the capacity of vector to 9.Decrease the Capacity of Vector C++ #include <bits/stdc++.h> using namespace std; int main() { vector<int> v(5); // Increase the capacity of vector v.reserve(9); // Decrease the capacity v.reserve(7); cout << v.capacity(); return 0; } OutputFinal Capacity: 9Explanation: The vector reserve() method increase the capacity of vector, but cannot not decrease it. Comment More infoAdvertise with us Next Article Vector reserve() in C++ STL A anmolpanxq Follow Improve Article Tags : C++ STL CPP-Functions cpp-vector cpp-containers-library +1 More Practice Tags : CPPSTL Similar Reads 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 Vector insert() in C++ STL In C++, the vector insert() is a built-in function used to insert new elements at the given position in a vector. In this article, we will learn about the vector insert() function in C++. Letâs take a look at an example that shows the how to use this function:C++#include <bits/stdc++.h> using 4 min read C++ STL - Vector in Reverse Order Prerequisite: Vectors in C++ A vector can be printed in reverse order with the following methods: By traversing in the backward direction using indexingBy traversing in the backward direction using begin() and end() functions in C++ STLBy traversing in the backward direction using rbegin() and rend( 3 min read Vector in C++ STL C++ vector is a dynamic array that stores collection of elements same type in contiguous memory. It has the ability to resize itself automatically when an element is inserted or deleted.Create a VectorBefore creating a vector, we must know that a vector is defined as the std::vector class template i 7 min read Vector Operator = in C++ STL In C++, the vector operator = is used to assign the contents of one vector to another. It allows you to copy elements from one vector to another or initialize a vector with another vector's contents.Letâs take a look at a simple code example:C++#include <bits/stdc++.h> using namespace std; int 3 min read Like