Does resizing () reduce the reservation made by an earlier reservation () to a smaller size?

So, if I reserve (100) first, add some elements and then resize (0) (or any other number smaller than the current size), will the vector reallocate memory to less space than 100 elements?

+6
source share
2 answers

vector<T>::resize(0) should not cause a reallocation or deletion of allocated memory, and for this reason, in most cases, vector<T>::clear() preferable.

See the answers to this question for more details: std :: vector resize down

+6
source

Running vector::resize(0) or a smaller count, not the current count, should not free memory. However, it can destroy these elements.

For reference on std::vector::resize , see std :: vector :: resize

+2
source

Source: https://habr.com/ru/post/913092/


All Articles