I was under the assumption that STL functions could be used only with STL data containers (like vector
) until I saw this piece of code:
#include
Yes and this is on purpose. Iterators can be implemented as pointers and therefore you can use pointers as iterators.
Short answer: STL algorithms are generally defined to work with iterators of various sorts. An iterator is defined by its behavior: it must be dereferenceable with *, it must be incrementable with ++, and various other things that also define what sort of iterator it is (the most general is random access). Remember that STL algorithms are templates, so the question is one of syntax. Similarly, a class instance with operator() defined works syntactically just like a function, so they can be used interchangeably.
A pointer does everything needed to be a random-access iterator. Therefore, it is a random-access iterator, and can be used as such in STL algorithms. You could look at vector implementations; you're very likely to find that vector<whatever>::iterator
is a whatever *
.
This doesn't make an array a valid STL container, but it does make pointers valid STL iterators.
Is such usage of arrays with STL functions allowed by the standard?
yes
If yes, how do archaic structures like arrays fit into the grand STL plan of templated iterators, containers and functions?
Iterators was designed with similar semantic as pointers.
Also, are there any caveats or details in such usage that the programmer should be careful about?
I prefer next usage:
int a[] = {9, 8, 7};
const size_t a_size = lengthof( a );
cerr << "Sum: " << accumulate( a, a + a_size , 0, plus<int>()) << endl;
Or it much better and safer to use boost::array:
boost::array< int, 3 > a = { 9, 8, 7 };
cerr << "Sum: " << accumulate( a.begin(), a.end(), 0, plus<int>()) << endl;
Pointers model Trivial Iterator, and pointer from arrays model Random Access Iterator. So yes, it's perfectly legal.
If you are interested on the usage constraints of each S(T)L algorithm, familiarize yourself the iterator models.
As int a[] can be treated as a pointer. And in C++ pointers can be incremented and point after that to the next element. And as pointers can be compared then pointers can be used as iterators.
There are requirements for iterators pointed in the standard 24.1 section. And pointers meet them. Here is some of them
All iterators i support the expression*i
Just as a regular pointer to an array guarantees that there is a pointer value pointing past the last element of the array, so for any iterator type there is an iterator value that points past the last element of a corresponding container.
The introduction to boost::array (a simple templated wrapper for conventional arrays, which also defines STL-compatible iterator types and begin()
/end()
etc) contains some interesting discussion of their degree of compatability with STL.