Difference between revisions of "cpp/named req/RandomAccessIterator"
(Undo revision 143724 by 2600:1700:4900:1340:A9E3:6B24:FE2B:7D1C (talk)) |
m (unifying fmt to, hopefully, reduce a confusion) |
||
Line 4: | Line 4: | ||
A {{named req|RandomAccessIterator}} is a {{named req|BidirectionalIterator}} that can be moved to point to any element in constant time. | A {{named req|RandomAccessIterator}} is a {{named req|BidirectionalIterator}} that can be moved to point to any element in constant time. | ||
− | If a {{named req|RandomAccessIterator}} {{ | + | If a {{named req|RandomAccessIterator}} {{c|it}} originates from a {{named req|Container}}, then {{c|it}}'s {{tt|value_type}} is the same as the container's, so dereferencing ({{c|*it}}) obtains the container's {{tt|value_type}}. |
A pointer to an element of an array satisfies all requirements of {{named req/core|RandomAccessIterator}} | A pointer to an element of an array satisfies all requirements of {{named req/core|RandomAccessIterator}} | ||
===Requirements=== | ===Requirements=== | ||
− | The type {{ | + | The type {{c|It}} satisfies {{named req/core|RandomAccessIterator}} if |
− | + | * The type {{c|It}} satisfies {{named req|BidirectionalIterator}} | |
− | * The type {{ | + | |
And, given | And, given | ||
Line 17: | Line 16: | ||
* {{tt|difference_type}}, the type denoted by {{c|std::iterator_traits<It>::difference_type}} | * {{tt|difference_type}}, the type denoted by {{c|std::iterator_traits<It>::difference_type}} | ||
* {{tt|reference}}, the type denoted by {{c|std::iterator_traits<It>::reference}} | * {{tt|reference}}, the type denoted by {{c|std::iterator_traits<It>::reference}} | ||
− | * {{ | + | * {{c|i}}, {{c|a}}, {{c|b}}, objects of type {{c|It}} or {{c|const It}} |
− | * {{ | + | * {{c|r}}, a value of type {{c|It&}} |
− | * {{ | + | * {{c|n}}, an integer of type {{tt|difference_type}} |
The following expressions must be valid and have their specified effects | The following expressions must be valid and have their specified effects | ||
Line 46: | Line 45: | ||
*{{c|1=a + n == n + a}} | *{{c|1=a + n == n + a}} | ||
|- | |- | ||
− | |{{c|r - | + | |{{c|1=r -= n}}||{{c|It&}}||{{c|1=return r += -n;}} |
− | |The absolute value of {{ | + | |The absolute value of {{c|n}} must be within the range of representable values of {{tt|difference_type}}. {{mark unreviewed dr|LWG|2519}} |
|- | |- | ||
|{{c|i - n}}||{{c|It}}||{{c|1=It temp = i; | |{{c|i - n}}||{{c|It}}||{{c|1=It temp = i; | ||
Line 54: | Line 53: | ||
|{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}|| | |{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}|| | ||
Precondition: | Precondition: | ||
− | * there exists a value {{ | + | * there exists a value {{c|n}} of type {{tt|difference_type}} such that {{c|1=a+n==b}} |
Postcondition: | Postcondition: | ||
* {{c|1=b == a + (b - a)}}. | * {{c|1=b == a + (b - a)}}. | ||
Line 64: | Line 63: | ||
* same as of {{c|b - a}}<!-- LWG3236 --> | * same as of {{c|b - a}}<!-- LWG3236 --> | ||
Strict total ordering relation: | Strict total ordering relation: | ||
− | * {{ | + | * {{c|!(a < a)}} |
− | * if {{ | + | * if {{c|a < b}} then {{c|!(b < a)}} |
− | * if {{ | + | * if {{c|a < b}} and {{c|b < c}} then {{c|a < c}} |
− | * {{ | + | * {{c|a < b}} or {{c|b < a}} or {{c|1=a == b}}<br> (exactly one of the expressions is true) |
|- | |- | ||
|{{c|a > b}}||contextually convertible to {{c|bool}}||{{c|b < a}}||Total ordering relation opposite to {{c|a < b}} | |{{c|a > b}}||contextually convertible to {{c|bool}}||{{c|b < a}}||Total ordering relation opposite to {{c|a < b}} | ||
|- | |- | ||
− | |{{c|a > | + | |{{c|1=a >= b}}||contextually convertible to {{c|bool}}||{{c|!(a < b)}}|| |
|- | |- | ||
− | |{{c|a < | + | |{{c|1=a <= b}}||contextually convertible to {{c|bool}}||{{c|!(a > b)}}|| |
|} | |} | ||
Revision as of 10:27, 25 September 2022
A LegacyRandomAccessIterator is a LegacyBidirectionalIterator that can be moved to point to any element in constant time.
If a LegacyRandomAccessIterator it originates from a Container, then it's value_type
is the same as the container's, so dereferencing (*it) obtains the container's value_type
.
A pointer to an element of an array satisfies all requirements of LegacyRandomAccessIterator
Requirements
The type It satisfies LegacyRandomAccessIterator if
- The type It satisfies LegacyBidirectionalIterator
And, given
-
value_type
, the type denoted by std::iterator_traits<It>::value_type -
difference_type
, the type denoted by std::iterator_traits<It>::difference_type -
reference
, the type denoted by std::iterator_traits<It>::reference - i, a, b, objects of type It or const It
- r, a value of type It&
- n, an integer of type
difference_type
The following expressions must be valid and have their specified effects
Expression | Return type | Operational semantics | Notes |
---|---|---|---|
r += n | It& | difference_type m = n; if (m >= 0) while (m--) ++r; |
|
a + n
n + a |
It | It temp = a; return temp += n; |
|
r -= n | It& | return r += -n; | The absolute value of n must be within the range of representable values of difference_type .
|
i - n | It | It temp = i; return temp -= n; |
|
b - a | difference_type |
return n; |
Precondition:
Postcondition:
|
i[n] | convertible to reference |
*(i + n) | |
a < b | contextually convertible to bool | Equivalent to return b - a > 0; | Precondition:
Strict total ordering relation:
|
a > b | contextually convertible to bool | b < a | Total ordering relation opposite to a < b |
a >= b | contextually convertible to bool | !(a < b) | |
a <= b | contextually convertible to bool | !(a > b) |
The above rules imply that LegacyRandomAccessIterator also implements LessThanComparable.
A mutable LegacyRandomAccessIterator is a LegacyRandomAccessIterator that additionally satisfies the LegacyOutputIterator requirements.
ConceptFor the definition of std::iterator_traits, the following exposition-only concept is defined.
where the exposition-only concept |
(since C++20) |
See also
(C++20) |
specifies that a bidirectional_iterator is a random-access iterator, supporting advancement in constant time and subscripting (concept) |
Iterator library | provides definitions for iterators, iterator traits, adaptors, and utility functions |