T
The value type of X
x, y
Object of type X
t
Object of type T
Definitions
A type that is a model of Trivial Iterator may be mutable , meaning that the values referred to by objects of that type may be modified, or constant , meaning that they may not. For example, int* is a mutable iterator type and const int* is a constant iterator type. If an iterator type is mutable, this implies that its value type is a model of Assignable; the converse, though, is not necessarily true.
A Trivial Iterator may have a singular value, meaning that the results of most operations, including comparison for equality, are undefined. The only operation that a is guaranteed to be supported is assigning a nonsingular iterator to a singular iterator.
A Trivial Iterator may have a dereferenceable value, meaning that dereferencing it yields a well-defined value. Dereferenceable iterators are always nonsingular, but the converse is not true. For example, a null pointer is nonsingular (there are well defined operations involving null pointers) even thought it is not dereferenceable.
Invalidating a dereferenceable iterator means performing an operation after which the iterator might be nondereferenceable or singular. For example, if p is a pointer, then delete p invalidates p .
Valid expressions
In addition to the expressions defined in Assignable, Equality Comparable, and Default Constructible, the following expressions must be valid.
Name |
Expression |
Type requirements |
Return type |
Default constructor |
X x |
|
|
Dereference |
*x |
|
Convertible to T [1] |
Dereference assignment |
*x = t |
X is mutable |
|
Member access |
x->m [2] |
T is a type for which x.m is defined |
|
Expression semantics
Name |
Expression |
Precondition |
Semantics |
Postcondition |
Default constructor |
X x |
|
|
x is singular |
Dereference |
*x |
x is dereferenceable |
|
|
Dereference assignment |
*x = t |
x is dereferenceable |
|
*x is a copy of t |
Member access |
x->m |
x is dereferenceable |
Equivalent to (*x).m |
|
Complexity guarantees
The complexity of operations on trivial iterators is guaranteed to be amortized constant time.
Invariants
Identity |
x == y if and only if &*x == &*y |
Models
• A pointer to an object that is not part of an array.
Notes
[1] The requirement for the return type of *x is specified as "convertible to T ", rather than simply T , because it sometimes makes sense for an iterator to return some sort of proxy object instead of the object that the iterator conceptually points to. Proxy objects are implementation details rather than part of an interface (one use of them, for example, is to allow an iterator to behave differently depending on whether its value is being read or written), so the value type of an iterator that returns a proxy is still T .
[2] Defining operator-> for iterators depends on a feature that is part of the C++ language but that is not yet implemented by all C++ compilers. If your compiler does not yet support this feature, the workaround is to use (*it).m instead of it->m .
See also
Input Iterator, Output Iterator, Forward Iterator, Bidirectional Iterator, Random Access Iterator, Iterator Overview
Category: iterators
Component type: concept
Description
An Input Iterator is an iterator that may be dereferenced to refer to some object, and that may be incremented to obtain the next iterator in a sequence. Input Iterators are not required to be mutable.
Refinement of
Trivial iterator.
Associated types
Value type |
The type of the value obtained by dereferencing an Input Iterator |
Distance type |
A signed integral type used to represent the distance from one iterator to another, or the number of elements in a range. |
Notation
X
A type that is a model of Input Iterator
T
The value type of X
i, j
Object of type X
t
Object of type T
Definitions
An iterator is past-the-end if it points beyond the last element of a container. Past-the-end values are nonsingular and nondereferenceable.
An iterator is valid if it is dereferenceable or past-the-end.
An iterator i is incrementable if there is a "next" iterator, that is, if ++i is well-defined. Past-the-end iterators are not incrementable.
An Input Iterator j is reachable from an Input Iterator i if, after applying operator++ to i a finite number of times, i == j . [1]
The notation [i,j) refers to a range of iterators beginning with i and up to but not including j .
The range [i,j) is a valid range if both i and j are valid iterators, and j is reachable from i [2].
Valid expressions
In addition to the expressions defined in Trivial Iterator, the following expressions must be valid.
Name |
Expression |
Return type |
Preincrement |
++i |
X& |
Postincrement |
(void)i++ |
|
Postincrement and dereference |
*i++ |
T |
Expression semantics
Name |
Expression |
Precondition |
Semantics |
Postcondition |
Dereference |
*t |
i is incrementable |
|
|
Preincrement |
++i |
i is dereferenceable |
|
i is dereferenceable or past-the-end [3] [4] |
Postincrement |
(void)i++ |
i is dereferenceable |
Equivalent to (void)++i |
i is dereferenceable or past-the-end [3] [4] |
Postincrement and dereference |
*i++ |
i is dereferenceable |
Equivalent to {T t = *i; ++i; return t;} |
i is dereferenceable or past-the-end [3] [4] |
Complexity guarantees
All operations are amortized constant time.
Models
• istream_iterator
Notes
[1] i == j does not imply ++i == ++j .
[2] Every iterator in a valid range [i, j) is dereferenceable, and j is either dereferenceable or past-the-end. The fact that every iterator in the range is dereferenceable follows from the fact that incrementable iterators must be deferenceable.
Читать дальше