Even if we don’t use reserve() it will expand till 1024 and filling the 1025 element it will inc. its size.
And if we don’t want to shift the location of v again and again we can simply do vector v(1000,0);
and input values to v[I].
This was my doubt, correct me if I am wrong anywhere
What was the use of reserve?
hello @namanmittal0007
void reserve (size_type n);
Request a change in capacity
Requests that the vector capacity be at least enough to contain n elements.
If n is greater than the current vector capacity, the function causes the container to reallocate its storage increasing its capacity to n (or greater).
In all other cases, the function call does not cause a reallocation and the vector capacity is not affected.
This function has no effect on the vector size and cannot alter its elements.
reallocation will occur whenever size exceedes capacity. (doesnt matter whether we have used reserve or not)
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.