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
Post a Comment