AnonMail2005 said:
Vector is the way to go.
I have read in Meyers that &v[0] is not advisable unless v.size () > 0.
To the intermediates:
Bow before /C++ Coding Standards : 101 Rules, Guidelines, and Best
Practices/ (C++ in Depth Series) by Herb Sutter & Andrei Alexandrescu. It
covers such scratchy situations as interfacing to a C API that likes raw
pointers.
They advise the &v[0] trick, and I can't recall if they warn about the
size() == 0 situation.
Now I thought that all containers always have a valid .begin() and .end(),
and if they equal then the container is empty, so this might suggest that
&v[0] is always valid (whereas v[0] is not, because it dereferences an
unconstructed object).
So where am I wrong?
To the newbies:
The original poster appeared to be writing their own function that takes a
pointer. If so, they should avoid pointers (as should coders at all levels),
and should pass a reference to a vector:
typedef std::vector<int> ints_type;
void foo(ints_type & ints);
References have fewer features, so they are always better than pointers, and
vectors have more features than arrays, so they are always better than
arrays.
Always pick the technique with fewer features. Or more features. I'll try
again.
Always pick the technique with the safest features. Raw pointers have many
unsafe features, and vectors have many safe ones.