C++ STL: Can arrays be used transparently with STL functions? C++ STL: Can arrays be used transparently with STL functions? arrays arrays

C++ STL: Can arrays be used transparently with STL functions?


Well, you ask about an array. You can just easily get a pointer to its elements, so it basically boils down to the question whether pointers can be used transparently with STL functions. A pointer actually is the most powerful kind of an iterator. There are different kinds

  • Input iterator: Only forward and one-pass, and only read
  • Output iterator: Only forward and one-pass, and only write

  • Forward iterator: Only forward, and read/write
  • Bidirectional iterator: Forward and backward, and read/write
  • Random access iterator: Arbitrary steps forward and backward in one breath, and read/write

Now each iterator in the second group supports all the things of all iterators mentioned before it. A pointer models the last kind of iterators - a random access iterator. You may add/subtract an arbitrary integer and you may read and write. And all except the output iterator has a operator-> that can be used to access a member of the element type we iterate over.

Normally, iterators have several typedefs as members

  • value_type - what the iterator iterates over (int, bool, string, ...)
  • reference - reference to the value_type
  • pointer - pointer to the value_type
  • difference_type - what type the distance between two iterators has (returned by std::distance).
  • iterator_category - this is a tag-type: it is typedefed to a type that represents the kind of the iterator. either std::input_iterator_tag, ..., std::random_access_iterator_tag. Algorithms can use it to overload on different kinds of iterators (like std::distance is faster for random access iterators, because it can just return a - b)

Now, a pointer of course does not have those members. C++ has an iterator_traits template and specializes it for pointers. So if you want to get the value type of any iterator, you do

iterator_traits<T>::value_type

And whether it is a pointer or some other iterator, it will give you the value_type of that iterator.

So - yes, a pointer can very well be used with STL algorithms. As someone else mentioned, even std::vector<T>::iterator can be a T*. A pointer is a very good example of an iterator even. Because it is so exceedingly simple but at the same time so powerful that it can iterate over a range.


The standard has designed iterators to feel and behave as much like pointers as possible. Also, since iterators are based on templates, the only relevant thing is that the iterator type has the proper operators defined. The result is that pointers will out-of-the-box behave just like random access iterators.

In fact, a possible implementation of std::vector<T>::iterator is to just make it a T*.

Of course, for an array you won't have the useful begin() and end() methods to find the valid iterator range, but that's the problem you always have with C style arrays.

Edit: Actually, as has been mentioned in the comments and other answers, you can implement those functions for arrays if the array is not dynamic and has not decayed into a pointer. But my basic point was that you have to be more careful than when using the standard containers.


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.