O'Reilly logo

Essential C++ by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required