Iterator Concepts
From Cppreference
C++ Standard Library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Iterator library | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contents |
[edit] Notation
In the following, as in the standard, the following notation is used:
- X is the iterator type being discussed
- a and b are instances of the iterator, with type X
- reference refers to iterator_traits<X>::reference
- difference_type refers to iterator_traits<X>::difference_type
[edit] Iterator Requirements
Iterator is a general concept that does not guarantee much beyond the ability to advance and dereference elements. Formally, an iterator has the following requirements:
- CopyConstructable
- CopyAssignable
- Destructible
- Swappable
In addition, the following expressions are valid:
- *a returns type reference as long as the itearator is dereferencable,
- ++a returns X&.
Examples:
- All iterators in the standard library model the Iterator concept.
[edit] InputIterator Requirements
A class X satisfies the requirements of an InputIterator for the value type T if X satisfies the Iterator and EqualityComparable requirements and the following expressions are valid:
- a!=b is convertible to bool as long as (a,b) is in the domain of == operator,
- *a returns type reference as long as the itearator is dereferencable,
- a->m() is equivalent to (*a).m(),
- ++a returns X&,
- a++ is equivalent to ++a,
- *(a++) is equivalent to
{ T tmp = *a; ++a; return a; }
[edit] OutputIterator Requirements
A class X satisfies the requirements of an OutputIterator if X satisfies the Iterator requirements.