Iterators

The capabilities of iterators varies with the type of object they belong to. In particular, iterators for arrays and vectors can be used for arithmetic, which apply to the underlying subscript. For instance,

std::array<int> a = { 8, 18, 12, 4, 2, 17, 9 }; auto p = a.begin() + 3;
will set the iterator to point to position 3, and *p will have the value 4. Then, p -= 2 will move p to position 1.

Array and vector iterators can also be subscripted, with subscript zero being where the iterator is pointing, and the subscript relative to that. So after the two statements above, p[2] has the value 17, and p[-2] is valid and has the value 18.

Iterators for lists can only be increment and decremented, using ++ and --. Attempts at general arithmetic will not compile.

Iterators are classified in the following categories.

Random AccessThe array and vector iterators are random access, having all iterator capabilities.
BidirectionalLists have bidirectional iterators. They can move forwards and backwards, but no general arithmetic or subscripting.
ForwardForward are similar to bidirectional, but can only move forward (increment), but not backward (decrement). The forward_list container has these.
InputInput iterators can only be moved forward, and can only be referenced (use * on the right of assignment), not stored (cannot use * on the left of an assignemnt). After being referenced, they must be incremented before they can be referenced again. These are similar to restrictions to Java iterators.
OutputSimilar to input, but can only be stored.