Create a Random-Access Iterator

Learn to create a random-access iterator.

This recipe is an example of a full-featured contiguous iterator, also called a random-access iterator. This is the most complete type of iterator for a container. A random-access iterator includes all the features of all the other types of container iterators, along with its random-access capabilities.

How to do it

We need a container for our iterator. We’ll use a simple array for this, and we’ll call it Container. The iterator class is nested within the Container class.

All of this is designed to be consistent with the STL container interfaces.

  • Container is defined as a template class. Its private section has only two elements:

Get hands-on with 1200+ tech skills courses.