- before push back, i shrink capacity to zero, then i push back item into vector, will it reallocate?
- is reallocate means slow down in performance?
-
if so, why need to shrink capacity to zero or fit the size?
vector<int> mandy2(5); vector<int>().swap(mandy2); printf("mandy2 size: %d\n", mandy2.size()); printf("mandy2 capacity: %d\n", mandy2.capacity()); mandy2.push_back(1); mandy2.push_back(2); mandy2.push_back(6);
before push back, i shrink capacity to zero, then i push back item into
Share
Yes it will.
Reference the doccumentation:
void push_back ( const T& x );Theorotically,Yes!
How much it hurts you can only be conclusively said after profiling your code for the same.
You do that only, if you know that you do not want to add any further elements to the vector, that ensures your vector does not consume more memory than you would actually need ever.