One of the best solutions for this is something like STLSoft's array_proxy<> template. Unfortunately, the doc page generated from the source code by doxygen isn't a whole lot of help understanding the template. The source code might actually be a bit better:
The array_proxy<>
template is described nicely in Matthew Wilson's book, Imperfect C++. The version I've used is a cut-down version of what's on the STLSoft site so I didn't have to pull in the whole library. My version's not as portable, but that makes it much simpler than what's on STLSoft (which jumps through a whole lot of portability hoops).
If you set up a variable like so:
int myArray[100];
array_proxy<int> myArrayProx( myArray);
The variable myArrayProx
has many of the STL interfaces - begin()
, end()
, size()
, iterators, etc.
So in many ways, the array_proxy<>
object behaves just like a vector (though push_back()
isn't there since the array_proxy<>
can't grow - it doesn't manage the array's memory, it just wraps it in something a little closer to a vector).
One really nice thing with array_proxy<>
is that if you use them as function parameter types, the function can determine the size of the array passed in, which isn't true of native arrays. And the size of the wrapped array isn't part of the template's type, so it's quite flexible to use.