Damir Damir - 1 month ago 7
C++ Question

How to set initial size of stl:: vector?

I have

vector<CustomClass*>
and I put lot off items in vector and I need fast access, so I don't use list. How to set initial size of vector (for example to be 20 000 places, so to avoid copy when I insert new ) ?

Answer
std::vector<CustomClass *> whatever(20000);

or:

std::vector<CustomClass *> whatever;
whatever.reserve(20000);

The former sets the actual size of the array -- i.e., makes it a vector of 20000 pointers. The latter leaves the vector empty, but reserves space for 20000 pointers, so you can insert (up to) that many without it having to reallocate.

You should probably be aware that chances of this doing any real good are minimal though.