tmp/tmpvevux_ok/{from.md → to.md}
RENAMED
|
@@ -1,48 +1,60 @@
|
|
| 1 |
### In general <a id="iterator.requirements.general">[[iterator.requirements.general]]</a>
|
| 2 |
|
| 3 |
Iterators are a generalization of pointers that allow a C++ program to
|
| 4 |
-
work with different data structures (
|
| 5 |
-
be able to construct template algorithms that
|
| 6 |
-
efficiently on different types of data structures,
|
| 7 |
-
formalizes not just the interfaces but also the semantics
|
| 8 |
-
assumptions of iterators. An input iterator `i` supports
|
| 9 |
-
`*i`, resulting in a value of some object type `T`,
|
| 10 |
-
type* of the iterator. An output iterator `i` has a
|
| 11 |
-
types that are
|
| 12 |
-
expression `*i = o` is valid where `o` is a
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
corresponding signed integer type called the *difference type* of the
|
| 17 |
-
iterator.
|
| 18 |
|
| 19 |
-
Since iterators are an abstraction of pointers, their semantics
|
| 20 |
generalization of most of the semantics of pointers in C++. This ensures
|
| 21 |
that every function template that takes iterators works as well with
|
| 22 |
-
regular pointers. This
|
| 23 |
-
|
| 24 |
-
iterators*, *
|
| 25 |
-
iterators* and *
|
| 26 |
-
[[
|
| 27 |
|
| 28 |
-
**Table: Relations among iterator categories** <a id="
|
| 29 |
|
| 30 |
-
|
|
| 31 |
-
| ----------------- | ------------------------------- | ------------------------- | ------------------------ |
|
| 32 |
-
| **Random Access** | $\rightarrow$ **Bidirectional** | $\rightarrow$ **Forward** | $\rightarrow$ **Input** |
|
| 33 |
-
|
|
| 34 |
|
| 35 |
|
| 36 |
-
|
| 37 |
-
can be used whenever an input iterator is specified; Bidirectional
|
| 38 |
-
iterators also satisfy all the requirements of forward iterators and can
|
| 39 |
-
be used whenever a forward iterator is specified; Random access
|
| 40 |
-
iterators also satisfy all the requirements of bidirectional iterators
|
| 41 |
-
and can be used whenever a bidirectional iterator is specified.
|
| 42 |
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
called *mutable iterators*. Nonmutable iterators are referred to as
|
| 45 |
*constant iterators*.
|
| 46 |
|
| 47 |
In addition to the requirements in this subclause, the nested
|
| 48 |
*typedef-name*s specified in [[iterator.traits]] shall be provided for
|
|
@@ -50,24 +62,14 @@ the iterator type.
|
|
| 50 |
|
| 51 |
[*Note 1*: Either the iterator type must provide the *typedef-name*s
|
| 52 |
directly (in which case `iterator_traits` pick them up automatically),
|
| 53 |
or an `iterator_traits` specialization must provide them. — *end note*]
|
| 54 |
|
| 55 |
-
Iterators that further satisfy the requirement that, for integral values
|
| 56 |
-
`n` and dereferenceable iterator values `a` and `(a + n)`, `*(a + n)` is
|
| 57 |
-
equivalent to `*(addressof(*a) + n)`, are called *contiguous iterators*.
|
| 58 |
-
|
| 59 |
-
[*Note 2*: For example, the type “pointer to `int`” is a contiguous
|
| 60 |
-
iterator, but `reverse_iterator<int *>` is not. For a valid iterator
|
| 61 |
-
range [`a`,`b`) with dereferenceable `a`, the corresponding range
|
| 62 |
-
denoted by pointers is [`addressof(*a)`,`addressof(*a) + (b - a)`); `b`
|
| 63 |
-
might not be dereferenceable. — *end note*]
|
| 64 |
-
|
| 65 |
Just as a regular pointer to an array guarantees that there is a pointer
|
| 66 |
value pointing past the last element of the array, so for any iterator
|
| 67 |
type there is an iterator value that points past the last element of a
|
| 68 |
-
corresponding sequence.
|
| 69 |
Values of an iterator `i` for which the expression `*i` is defined are
|
| 70 |
called *dereferenceable*. The library never assumes that past-the-end
|
| 71 |
values are dereferenceable. Iterators can also have singular values that
|
| 72 |
are not associated with any sequence.
|
| 73 |
|
|
@@ -76,52 +78,60 @@ with `int* x;`), `x` must always be assumed to have a singular value of
|
|
| 76 |
a pointer. — *end example*]
|
| 77 |
|
| 78 |
Results of most expressions are undefined for singular values; the only
|
| 79 |
exceptions are destroying an iterator that holds a singular value, the
|
| 80 |
assignment of a non-singular value to an iterator that holds a singular
|
| 81 |
-
value, and, for iterators that
|
| 82 |
requirements, using a value-initialized iterator as the source of a copy
|
| 83 |
or move operation.
|
| 84 |
|
| 85 |
-
[*Note
|
| 86 |
although the distinction only matters for types with trivial default
|
| 87 |
constructors such as pointers or aggregates holding
|
| 88 |
pointers. — *end note*]
|
| 89 |
|
| 90 |
In these cases the singular value is overwritten the same way as any
|
| 91 |
other value. Dereferenceable values are always non-singular.
|
| 92 |
|
| 93 |
-
An iterator `j` is called *reachable* from an iterator `i` if and only
|
| 94 |
-
if there is a finite sequence of applications of the expression `++i`
|
| 95 |
-
that makes `i == j`. If `j` is reachable from `i`, they refer to
|
| 96 |
-
elements of the same sequence.
|
| 97 |
-
|
| 98 |
Most of the library’s algorithmic templates that operate on data
|
| 99 |
-
structures have interfaces that use ranges. A *range* is
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
`i`
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
All the categories of iterators require only those functions that are
|
| 109 |
realizable for a given category in constant time (amortized). Therefore,
|
| 110 |
-
requirement tables for the iterators do not
|
|
|
|
| 111 |
|
| 112 |
-
Destruction of
|
| 113 |
-
previously obtained from that iterator.
|
| 114 |
|
| 115 |
-
An *invalid
|
| 116 |
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
`iterator_traits<X>::difference_type` and
|
| 120 |
-
`iterator_traits<X>::reference`, respectively, `n` denotes a value of
|
| 121 |
-
`difference_type`, `u`, `tmp`, and `m` denote identifiers, `r` denotes a
|
| 122 |
-
value of `X&`, `t` denotes a value of value type `T`, `o` denotes a
|
| 123 |
-
value of some type that is writable to the output iterator.
|
| 124 |
|
| 125 |
-
[*Note
|
| 126 |
-
`
|
| 127 |
|
|
|
|
| 1 |
### In general <a id="iterator.requirements.general">[[iterator.requirements.general]]</a>
|
| 2 |
|
| 3 |
Iterators are a generalization of pointers that allow a C++ program to
|
| 4 |
+
work with different data structures (for example, containers and ranges)
|
| 5 |
+
in a uniform manner. To be able to construct template algorithms that
|
| 6 |
+
work correctly and efficiently on different types of data structures,
|
| 7 |
+
the library formalizes not just the interfaces but also the semantics
|
| 8 |
+
and complexity assumptions of iterators. An input iterator `i` supports
|
| 9 |
+
the expression `*i`, resulting in a value of some object type `T`,
|
| 10 |
+
called the *value type* of the iterator. An output iterator `i` has a
|
| 11 |
+
non-empty set of types that are `indirectly_writable` to the iterator;
|
| 12 |
+
for each such type `T`, the expression `*i = o` is valid where `o` is a
|
| 13 |
+
value of type `T`. For every iterator type `X`, there is a corresponding
|
| 14 |
+
signed integer-like type [[iterator.concept.winc]] called the
|
| 15 |
+
*difference type* of the iterator.
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
Since iterators are an abstraction of pointers, their semantics are a
|
| 18 |
generalization of most of the semantics of pointers in C++. This ensures
|
| 19 |
that every function template that takes iterators works as well with
|
| 20 |
+
regular pointers. This document defines six categories of iterators,
|
| 21 |
+
according to the operations defined on them: *input iterators*, *output
|
| 22 |
+
iterators*, *forward iterators*, *bidirectional iterators*, *random
|
| 23 |
+
access iterators*, and *contiguous iterators*, as shown in
|
| 24 |
+
[[iterators.relations]].
|
| 25 |
|
| 26 |
+
**Table: Relations among iterator categories** <a id="iterators.relations">[iterators.relations]</a>
|
| 27 |
|
| 28 |
+
| | | | | |
|
| 29 |
+
| -------------- | ------------------------------- | ------------------------------- | ------------------------- | ------------------------ |
|
| 30 |
+
| **Contiguous** | $\rightarrow$ **Random Access** | $\rightarrow$ **Bidirectional** | $\rightarrow$ **Forward** | $\rightarrow$ **Input** |
|
| 31 |
+
| | | | | $\rightarrow$ **Output** |
|
| 32 |
|
| 33 |
|
| 34 |
+
The six categories of iterators correspond to the iterator concepts
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
- `input_iterator` [[iterator.concept.input]],
|
| 37 |
+
- `output_iterator` [[iterator.concept.output]],
|
| 38 |
+
- `forward_iterator` [[iterator.concept.forward]],
|
| 39 |
+
- `bidirectional_iterator` [[iterator.concept.bidir]]
|
| 40 |
+
- `random_access_iterator` [[iterator.concept.random.access]], and
|
| 41 |
+
- `contiguous_iterator` [[iterator.concept.contiguous]],
|
| 42 |
+
|
| 43 |
+
respectively. The generic term *iterator* refers to any type that models
|
| 44 |
+
the `input_or_output_iterator` concept [[iterator.concept.iterator]].
|
| 45 |
+
|
| 46 |
+
Forward iterators meet all the requirements of input iterators and can
|
| 47 |
+
be used whenever an input iterator is specified; Bidirectional iterators
|
| 48 |
+
also meet all the requirements of forward iterators and can be used
|
| 49 |
+
whenever a forward iterator is specified; Random access iterators also
|
| 50 |
+
meet all the requirements of bidirectional iterators and can be used
|
| 51 |
+
whenever a bidirectional iterator is specified; Contiguous iterators
|
| 52 |
+
also meet all the requirements of random access iterators and can be
|
| 53 |
+
used whenever a random access iterator is specified.
|
| 54 |
+
|
| 55 |
+
Iterators that further meet the requirements of output iterators are
|
| 56 |
called *mutable iterators*. Nonmutable iterators are referred to as
|
| 57 |
*constant iterators*.
|
| 58 |
|
| 59 |
In addition to the requirements in this subclause, the nested
|
| 60 |
*typedef-name*s specified in [[iterator.traits]] shall be provided for
|
|
|
|
| 62 |
|
| 63 |
[*Note 1*: Either the iterator type must provide the *typedef-name*s
|
| 64 |
directly (in which case `iterator_traits` pick them up automatically),
|
| 65 |
or an `iterator_traits` specialization must provide them. — *end note*]
|
| 66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
Just as a regular pointer to an array guarantees that there is a pointer
|
| 68 |
value pointing past the last element of the array, so for any iterator
|
| 69 |
type there is an iterator value that points past the last element of a
|
| 70 |
+
corresponding sequence. Such a value is called a *past-the-end value*.
|
| 71 |
Values of an iterator `i` for which the expression `*i` is defined are
|
| 72 |
called *dereferenceable*. The library never assumes that past-the-end
|
| 73 |
values are dereferenceable. Iterators can also have singular values that
|
| 74 |
are not associated with any sequence.
|
| 75 |
|
|
|
|
| 78 |
a pointer. — *end example*]
|
| 79 |
|
| 80 |
Results of most expressions are undefined for singular values; the only
|
| 81 |
exceptions are destroying an iterator that holds a singular value, the
|
| 82 |
assignment of a non-singular value to an iterator that holds a singular
|
| 83 |
+
value, and, for iterators that meet the *Cpp17DefaultConstructible*
|
| 84 |
requirements, using a value-initialized iterator as the source of a copy
|
| 85 |
or move operation.
|
| 86 |
|
| 87 |
+
[*Note 2*: This guarantee is not offered for default-initialization,
|
| 88 |
although the distinction only matters for types with trivial default
|
| 89 |
constructors such as pointers or aggregates holding
|
| 90 |
pointers. — *end note*]
|
| 91 |
|
| 92 |
In these cases the singular value is overwritten the same way as any
|
| 93 |
other value. Dereferenceable values are always non-singular.
|
| 94 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
Most of the library’s algorithmic templates that operate on data
|
| 96 |
+
structures have interfaces that use ranges. A *range* is an iterator and
|
| 97 |
+
a *sentinel* that designate the beginning and end of the computation, or
|
| 98 |
+
an iterator and a count that designate the beginning and the number of
|
| 99 |
+
elements to which the computation is to be applied.[^1]
|
| 100 |
+
|
| 101 |
+
An iterator and a sentinel denoting a range are comparable. A range
|
| 102 |
+
\[`i`, `s`) is empty if `i == s`; otherwise, \[`i`, `s`) refers to the
|
| 103 |
+
elements in the data structure starting with the element pointed to by
|
| 104 |
+
`i` and up to but not including the element, if any, pointed to by the
|
| 105 |
+
first iterator `j` such that `j == s`.
|
| 106 |
+
|
| 107 |
+
A sentinel `s` is called *reachable from* an iterator `i` if and only if
|
| 108 |
+
there is a finite sequence of applications of the expression `++i` that
|
| 109 |
+
makes `i == s`. If `s` is reachable from `i`, \[`i`, `s`) denotes a
|
| 110 |
+
valid range.
|
| 111 |
+
|
| 112 |
+
A *counted range* `i`+\[0, `n`) is empty if `n == 0`; otherwise,
|
| 113 |
+
`i`+\[0, `n`) refers to the `n` elements in the data structure starting
|
| 114 |
+
with the element pointed to by `i` and up to but not including the
|
| 115 |
+
element, if any, pointed to by the result of `n` applications of `++i`.
|
| 116 |
+
A counted range `i`+\[0, `n`) is valid if and only if `n == 0`; or `n`
|
| 117 |
+
is positive, `i` is dereferenceable, and `++i`+\[0, `-``-``n`) is valid.
|
| 118 |
+
|
| 119 |
+
The result of the application of library functions to invalid ranges is
|
| 120 |
+
undefined.
|
| 121 |
|
| 122 |
All the categories of iterators require only those functions that are
|
| 123 |
realizable for a given category in constant time (amortized). Therefore,
|
| 124 |
+
requirement tables and concept definitions for the iterators do not
|
| 125 |
+
specify complexity.
|
| 126 |
|
| 127 |
+
Destruction of a non-forward iterator may invalidate pointers and
|
| 128 |
+
references previously obtained from that iterator.
|
| 129 |
|
| 130 |
+
An *invalid iterator* is an iterator that may be singular.[^2]
|
| 131 |
|
| 132 |
+
Iterators are called *constexpr iterators* if all operations provided to
|
| 133 |
+
meet iterator category requirements are constexpr functions.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
+
[*Note 3*: For example, the types “pointer to `int`” and
|
| 136 |
+
`reverse_iterator<int*>` are constexpr iterators. — *end note*]
|
| 137 |
|