liveBook · Manning

Iterator

As explained in Object-Oriented Software Design in C++

An iterator is an abstraction that provides sequential access to elements of a data structure without exposing its representation.

An iterator provides sequential access to elements without exposing how a collection stores them. It lets one algorithm process arrays, vectors, linked lists, or maps through a common interface. section 11.1 section 11.1.3

The design problem appears when one report must process teams whose Player objects are stored differently. Without an iterator, the report becomes coupled to each collection representation. Refactoring moves traversal into collection-specific iterator classes. section 11.1 section 11.1.3

A typical iterator design separates responsibilities:

  • Define a common Iterator interface with next() and has_next(). section 11.1.3
  • Store a private cursor in each concrete iterator; represent it as an array index, vector iterator, or map iterator. section 11.1.3
  • Let each collection create the iterator appropriate for its representation. section 11.1.3
  • Have the client repeatedly test has_next(), retrieve the current element with next(), and let the iterator advance. section 11.1.3

Reporting baseball teams

Team 1 stores players in an array, Team 2 in a vector, and Team 3 in a map. TeamReport requests an iterator from each Team and prints every player's student ID and name without accessing the underlying collection. Adding or changing a sequential collection can therefore avoid changes to the reporting algorithm. section 11.1 section 11.4

Figure 11.2 Each Team subclass creates an iterator that is appropriate for its sequential collection of Player objects. Class TeamReport delegates iterating over the various forms of sequential collections to the Iterator subclasses. Each Iterator subclass knows how to iterate over its form of sequential collection and has a private cursor member variable to keep track of the current Player object. Class TeamReport does not know how each Team subclass stores its sequence of Player objects, and therefore, it is loosely coupled from the Team subclasses. The grayed-out portions of the diagram haven’t changed logically from figure 11.1.

Shows how collection subclasses create representation-specific iterators while the client delegates traversal and remains loosely coupled. section 11.1.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Explains interfaces, cursors, delegation, and traversal code, while stopping short of a broader implementation treatment.
Examples
Uses baseball team reports over array-, vector-, and map-based Player collections.
What it assumes
Assumes familiarity with sequential collections and introduces the need to hide their representations behind a common interface.
What you can do after
Build it from scratch by defining iterator operations, storing traversal state, and delegating collection-specific iteration.
How it connects
Connects sequential collections, polymorphism, encapsulation, loose coupling, and native C++ iterators.

Also explained in

Compare how 15 books explain iterator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.