Kernel SDK Developer's Guide > Dictionaries of Objects > Concept of Iterators
Concept of Iterators

The C++ platform provides a mechanism for traversing and accessing various data structures, such as arrays, lists, dictionaries, and containers using a special-purposed data type: an iterator.

An iterator is an object that allows a programmer to traverse through a container and access the container's elements; the behavior is similar to a cursor. The container interface usually provides various types of iterators. Iterators are often implemented according to the structures underlying the container implementation. The implementation of iterators are often bound to the container implementation to enable operational semantics. Usually the container provides methods for creating iterators, which provides a way to traverse through the container and access its data elements. An iterator has the semantics and behavior of a pointer, but provides additional functionality.

An iterator can be thought of as a pointer type that has two primary operations: referencing one particular element in a container (called element accessing), and modifying itself so it points to the next element (called element traversing). There must also be a way to create an iterator that points to a first element as well as a way to determine when the iterator has exhausted all of the elements in the container. The primary purpose of an iterator is to allow a programmer to process every element of a container while isolating the user from the internal structure of the container. The container can store elements in any manner it wishes while the user can treat the container as if it were a simple sequence or list. Iterators can provide additional operations or can have various behaviors.

Using iterators has the following advantages:

  • Requires no counting of loops for all container data, in particular for data structures with no or slow random access, for example, lists or trees.
  • Provides a consistent interface for iterating through data structures of all kinds, and therefore makes code more readable, reusable, and less sensitive to change in a data structure.
  • Gives additional restrictions on data access, such as ensuring that elements can not be skipped or that a previously visited element can not be accessed a second time.
  • Allows modification of a container without invalidating the iterator. For example, once an iterator has advanced beyond the first element it may be possible to insert additional elements into the beginning of the container with predictable results. This would be problematic with indexing since the index numbers must change.
  • Allows a programmer to delimit the implementation of a container object and implement methods that must process the elements of a container.

Implementation of iterators usually includes an abstract class that declares the iterating interface and various specific classes derived from this abstract class that adapt the iterating interface to specific containers.

See Also

Overview of Classes that Implements an Iterator

Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.