c++ - Why set the size of a vector -


is there performance reason why vector initialized size?

for example vector<myclass> v(10); opposed vector<myclass> v , push_back needed?

vector<myclass> v(10) pre-allocates internal array once time , auto-fills 10 default-constructed myclass objects.

vector<myclass> v not pre-allocate array, can use reserve() , resize() that.

push_back() re-allocate , copy internal array each time new size() exceed current capacity().


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -