Iterators
An iterator is an abstraction of a pointer used for pointing
into containers and other sequences. An ordinary pointer can point to
different elements in 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 that the same operators have the same behavior
for any container, even trees and lists. See the <iterator> section of Chapter 13 for more details.
Iterator Categories
There are five categories of iterators:
- Input
Permits you to read a sequence in one pass. The increment (
++) operator advances to the next element, but there is no decrement operator. The dereference (*) operator returns an rvalue, not an lvalue, so you can read elements but not modify them.- Output
Permits you to write a sequence in one pass. The increment (
++) operator advances to the next element, but there is no decrement operator. You can dereference an element only to assign a value to it. You cannot compare output iterators.- Forward
Permits unidirectional access to a sequence. You can refer to and assign to an item as many times as you want. You can use a forward iterator wherever an input iterator is required or wherever an output iterator is required.
- Bidirectional
Similar to a forward iterator but also supports the
--(decrement) operator to move the iterator back one position.- Random access
Similar to a bidirectional iterator but also supports the
[](subscript) operator to access ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access