Damir Damir - 3 years ago 169
C++ Question

How to set initial size of stl:: vector?

I have

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 Source
std::vector<CustomClass *> whatever(20000);


std::vector<CustomClass *> whatever;

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download