© Dmitri Nesteruk 2022
D. NesterukDesign Patterns in Modern C++20https://doi.org/10.1007/978-1-4842-7295-4_16

16. Iterator

Dmitri Nesteruk1  
(1)
St. Petersburg, Russia
 

Whenever you start working with complicated data structures, you encounter the problem of traversal. This can be handled in different ways, but the most common way of traversing, say, a vector is using something called an iterator.

An iterator is, quite simply, an object that can point to an element of a collection and also knows how to move to the next element in the collection. As such, it is only required to implement the ++ operator and the != operator (so you can compare two iterators and check if they point to the same thing). That’s it.

The C++ Standard Library makes heavy use ...

Get Design Patterns in Modern C++20: Reusable Approaches for Object-Oriented Software Design 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.