Iterator Conventions

The STL facilities make widespread use of iterators, to mediate between the various algorithms and the sequences upon which they act. For brevity in the remainder of this document, the name of an iterator type (or its prefix) indicates the category of iterators required for that type. In order of increasing power, the categories are summarized here as:

Note that an object pointer can take the place of a random-access iterator, or any other iterator for that matter.

The hierarchy of iterator categories can be summarized by showing three sequences. For write-only access to a sequence, you can use any of:

output iterator ->
    forward iterator ->
    bidirectional iterator ->
    random-access iterator

The right arrow means "can be replaced by." So any algorithm that calls for an output iterator should work well with a forward iterator, for example, but not the other way around.

For read-only access to a sequence, you can use any of:

input iterator ->
    forward iterator ->
    bidirectional iterator ->
    random-access iterator

An input iterator is the weakest of all categories, in this case.

Finally, for read/write access to a sequence, you can use any of:

forward iterator ->
    bidirectional iterator ->
    random-access iterator

Remember that an object pointer can always serve as a random-access iterator. Therefore, it can serve as any category of iterator, as long as it supports the proper read/write access to the sequence it designates.

This "algebra" of iterators is fundamental to practically everything else in the Standard Template Library. It is important to understand the capabilities, and limitations, of each iterator category to see how iterators are used by containers and algorithms in STL.