Doubt about fixing the size of the vector

Instead of using
vector v;
v.reserve(100),
if I do
vector v(100),

Is there any difference ? Internally are both the same thing ?

Hi Sagnik,

Both of these will do the same thing only difference is when you write vector v(100); it means that you construct a container with 100 elements using the constructor and
v.reserve(100), reserve() is the function which requests that the vector capacity to be at least enough to contain 100 elements. And If 100 is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to 100 (or greater).

1 Like