O'Reilly logo

STL Pocket Reference by Ray Lischner

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required