Re: Simple vector efficiency question
On May 6, 5:53 pm, "crea" <n...@invalid.com> wrote:
Simple question: Is STL vector always as efficient way as using normal
c-dynamical arrays?
So is
vector<int> ve1;
ve1.reserve(100);
always as fast/efficient than
int* ve2 = new int[100];
?
It's always close enough.
and other operations as well?
Do you always use vector instead of normal c-arrays? in what situations not?
Two cases, at least, where C style arrays are preferred:
1. Static constant arrays of POD types, initialized using
agglomerate initialization. First, because they avoid order
of initialization problems (since static initialization can
be used), and second, because the compiler can work out the
size of the array itself from the initializers---you don't
have to count.
2. For small, fixed length arrays which are used in great
number, e.g. something like Point3D (where double[3] is
probably preferrable over std::vector). In such cases,
there's very little advantage for std::vector, and the
dynamic allocations (e.g. if you write something like
std::vector<Point3D> array(100000)) can kill you.
Otherwise... We do a lot of time critical numbers crunching on
more or less large vectors (from about 30 elements up), we use
std::vector exclusively, and it's never caused a performance
problem.
--
James Kanze