Create a random-access iterator
This recipe is an example of a full-featured contiguous/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.
While I felt it important to include a complete iterator in this chapter, with over 700 lines of code this example is somewhat larger than the other examples in this book. I'll cover the essential components of the code here. Please see the full source at https://github.com/PacktPublishing/CPP-20-STL-Cookbook/blob/main/chap04/container-iterator.cpp.
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 atemplate
class...