3.2. Making Sense of Iterators
The obvious question is, how do we implement this layer of abstraction? We need a collection of objects that support the same set of operators as the built-in pointer (++, *, ==, !=) but allow us to provide a unique implementation of those operators. We can do exactly this with the C++ class mechanism. We’ll design a set of iterator classes that are programmed using the same syntax as that of a pointer. For example, if first and last are list class iterators, we can write
// first and last are iterator class objects while ( first != last ) { cout << *first << ' '; ++first; }
the same as if first and last are actual pointers. The difference is that the dereference operator (*), the inequality operator (!=), and ...
Get Essential C++ now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.