I would like to create a vector of some complex type by reading individual elements from a stream. I know the size of the vector in advance. Is it better to specify the number of elements in the vector constructor or use the fallback method? Which of these two is better?
int myElementCount = stream.ReadInt(); vector<MyElement> myVector(myElementCount); for (int i = 0; i < myElementCount; i++) { myVector[i] = stream.ReadMyElement(); }
or
int myElementCount = stream.ReadInt(); vector<MyElement> myVector; myVector.reserve(myElementCount); for (int i = 0; i < myElementCount; i++) { myVector.push_back(stream.ReadMyElement()); }
How about when I just create an ints vector or some other simple type.
c ++ vector std
mk33
source share