Iterators (STL)

Iterators are used to point to first-class container elements. They hold state information sensitive to the particular containers on which they operate; thus, iterators are implemented appropriately for each type of container. Certain iterator operations are uniform across containers. For example, the dereferencing operator (*) dereferences an iterator so that you can use the element to which it points. The ++ operation on an iterator moves it to the container's next element.

Function begin returns an iterator pointing to the first element of the container. Function end returns an iterator pointing to the first element past the end of the container (an element that doesn't exist). This iterator is typically used in an equality or inequality comparison to determine whether the "moving iterator" has reached the end of the container.

An object of type iterator refers to a container element that can be modified. An object of type const_iterator refers to a container element that cannot be modified. Operations performed on a const_iterator return const references to prevent modification to elements of the container being manipulated. Using const_iterators where appropriate is another example of the principle of least privilege.

istream_iterator, ostream_iterator
We use iterators with sequences (also called ranges). These sequences can be in containers, or they can be input sequences or output sequences by using istream_iterator or ostream_iterator. These can be used with Standard Library algorithm to create powerful statements: