I’m using a vector. For my purposes, I generally need it to have (n) elements which will only be accessed & modified using the index [] operator.
At a certain point, my algorithm will decide it needs more space and that I need to enlarge this vector to say… 2 times it’s size.
Is there a way I can double the size and have all of the new space in the vector default initialized with the vector element type?
Basically, instead of manually inserting elements to fill up the capacity after it doubles, I would like its entire capacity to be fully populated and default initialized after the increase. As far as I know, reserve just reserves space (and doesn’t initialize elements in it, so while I could use that and a loop-insert, I was hoping there’s a… more fun? way).
So, I want a similar effect to that of creating a new vector with this constructor:
std::vector<MyType> vec(2*oldvec.size(), MyType());
except I would like to maintain the elements that were already in the vector before it doubled in size as well.
You want std::vector::resize().