Iterators

An iterator is an abstraction of a pointer used for pointing into containers and other sequences. An ordinary pointer can point to various elements in an array or to one position past the last element of an array. The ++ operator advances the pointer to the next element, and the * operator dereferences the pointer to return a value from the array. Iterators generalize the concept so the same operators have the same behavior for any container, even trees and lists.

You can look at iterators from two different perspectives: the implementor’s or the user’s. This book does not cover the implementation of iterators. To use an iterator, you must know the iterator’s category and traits. These topics are covered in this section, along with a number of specialized iterators. All function and class templates discussed in this section are declared in the <iterator> header.

Iterator Categories

There are five categories of iterators:

Input

Lets you read a sequence in one pass. The increment operator (++) advances to the next element, but there is no decrement operator. Use the dereference operator (*) to read elements. You cannot read a single element more than once, and you cannot modify elements.

Output

Lets you write a sequence in one pass. The increment operator (++) advances to the next element, but there is no decrement operator. Use the dereference operator (*) only to assign a value to an element. You cannot assign a value more than once to a single element. Unlike other iterator ...

Get STL Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.