Iterator

Back to Generic programming

An iterator is an object which allows traversal and protected node access in a graph data structure. Iterators are important in the design of data structures because they allow to separate the data structure from the algorithms that use that data structure. Iterators are an essential component of the C++ Standard Library, where their meaning is strictly defined. While the C++ iterators are always restricted to sequences, we shall adopt the more general definition.

Iterator ranges

While the iterator concept is appropriate for traversing trees and graphs, it is not well suited for sequential traversal. The problems associated with sequential iterators are as follows:

A partial solution to these problems is to combine the iterators into a single object called an iterator range. This instantly solves the problem of composition and significantly aids in the problem of incorrect pairing. However, because the individual iterators must still be accessed at some point, the problems are merely pushed deeper, rather than fundamentally solved.

A complete solution to the presented problems would be to take a sequence of elements as the primary abstraction. However, this solution is impractical, since all the algorithms in the C++ Standard Library are built around the iterator concept.

Pastel uses the iterator range solution, which has been elegantly implemented in the Boost.Range library. This library lifts the abstraction level of the C++ Standard Library algorithms to iterator ranges, and offers ways to compose and filter ranges.

Learn more

Files

ConstantIterator class

A constant value iterator

CountingIterator class

An iterator adapter for dereferencing the adapted iterator

Iterator macros

Iterator ranges

NullIterator class

An iterator for sending data into oblivion.

Range algorithm concept

Rectangle iterator

SparseIterator class

An iterator adapter for a larger step size.

Testing for iterators