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:
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.
Overview of Classes that Implements an Iterator
Copyright © 2002 – 2020. Open Design Alliance. All rights reserved.
|