![]() |
VOOZH | about |
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:
Capacity: 9
This article covers the syntax, usage, and common examples of vector reserve() method in C++:
Table of Content
The vector reserve() is the member method of std::vector class defined inside <vector> header file.
v.reserve(n);
The following examples demonstrates the use of vector reserve() method for different scenarios and purposes:
5 9
Explanation: Initially the capacity of vector is 5, with vector reserve() we increase the capacity of vector to 9.
Final Capacity: 9
Explanation: The vector reserve() method increase the capacity of vector, but cannot not decrease it.