Difference between revisions of "cpp/named req/RandomAccessIterator"
From cppreference.com
(There is no such requirement `n - it`) |
(update wording) |
||
Line 4: | Line 4: | ||
A {{tt|RandomAccessIterator}} is a {{concept|BidirectionalIterator}} that can be moved to point to any element in constant time. | A {{tt|RandomAccessIterator}} is a {{concept|BidirectionalIterator}} that can be moved to point to any element in constant time. | ||
− | A | + | A pointer to an element of an array satisfies all requirements of {{tt|RandomAccessIterator}} |
===Requirements=== | ===Requirements=== | ||
+ | The type {{tt|It}} satisfies {{tt|RandomAccessIterator}} if | ||
− | * {{concept|BidirectionalIterator}} | + | * The type {{tt|It}} satisfies {{concept|BidirectionalIterator}} |
− | + | And, given | |
+ | * {{tt|value_type}}, the type denoted by {{c|std::iterator_traits<It>::value_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|i}}, {{tt|a}}, {{tt|b}}, objects of type {{tt|It}} or {{tt|const It}} | ||
+ | * {{tt|r}}, a value of type {{tt|It&}} | ||
+ | * {{tt|n}}, an integer of type {{tt|difference_type}} | ||
+ | |||
+ | The following expressions must be valid and have their specified effects | ||
{|table class="wikitable" | {|table class="wikitable" | ||
|- | |- | ||
− | !Expression||Return|| | + | !Expression||Return type||Operational semantics||Notes |
|- | |- | ||
|{{c|r +{{=}} n}} | |{{c|r +{{=}} n}} | ||
|{{c|It&}} | |{{c|It&}} | ||
− | |{{c|1=if ( | + | |{{c|1=difference_type m = n; |
− | + | if (m >= 0) while (m--) ++r; | |
− | else | + | else while (m++) --r; |
− | + | ||
return r;}} | return r;}} | ||
| | | | ||
*{{ttb|n}} can be both positive or negative | *{{ttb|n}} can be both positive or negative | ||
− | * | + | *The complexity is constant (that is, the implementation cannot actually execute the while loop shown in operational semantics) |
|- | |- | ||
− | |{{c| | + | |{{c|a + n}} |
− | |{{c|1=It temp = | + | {{c|n + a}} |
+ | |{{c|It}} | ||
+ | |{{c|1=It temp = a; | ||
return temp += n;}} | return temp += n;}} | ||
− | | | + | | |
− | + | *{{ttb|n}} can be both positive or negative | |
+ | *{{c|1=a + n == n + a}} | ||
|- | |- | ||
|{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}} | |{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}} | ||
Line 37: | Line 48: | ||
|- | |- | ||
|{{c|i - n}}||{{c|It}}||{{c|1=It temp = i; | |{{c|i - n}}||{{c|It}}||{{c|1=It temp = i; | ||
− | return temp -= n;}} | + | return temp -= n;}}|| |
|- | |- | ||
− | |{{c|b - a}}||{{ | + | |{{c|b - a}}||{{tt|difference_type}}||{{c|return n;}}|| |
+ | Precondition: | ||
+ | * there exists a value {{tt|n}} of type {{tt|difference_type}} such that {{c|1=a+n==b}} | ||
+ | Postcondition: | ||
+ | * {{c|1=b == a + (b - a)}}. | ||
|- | |- | ||
− | |{{c|i[n]}}||convertible to {{ | + | |{{c|i[n]}}||convertible to {{tt|reference}}||{{c|*(i + n)}}|| |
|- | |- | ||
|{{c|a < b}}||contextually convertible to {{c|bool}}||{{c|b - a > 0}} | |{{c|a < b}}||contextually convertible to {{c|bool}}||{{c|b - a > 0}} | ||
Line 52: | Line 67: | ||
|{{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 >{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a < b)}} | + | |{{c|a >{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a < b)}}|| |
|- | |- | ||
− | |{{c|a <{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a > b)}} | + | |{{c|a <{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a > b)}}|| |
|} | |} | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | The above rules imply that RandomAccessIterator also implements {{concept|LessThanComparable}}. | + | The above rules imply that {{tt|RandomAccessIterator}} also implements {{concept|LessThanComparable}}. |
A {{tt|mutable RandomAccessIterator}} is a {{tt|RandomAccessIterator}} that additionally satisfies the {{concept|OutputIterator}} requirements. | A {{tt|mutable RandomAccessIterator}} is a {{tt|RandomAccessIterator}} that additionally satisfies the {{concept|OutputIterator}} requirements. |
Revision as of 10:33, 26 June 2015
Template:cpp/concept/title Template:cpp/concept/navbar
A RandomAccessIterator
is a Template:concept that can be moved to point to any element in constant time.
A pointer to an element of an array satisfies all requirements of RandomAccessIterator
Requirements
The type It
satisfies RandomAccessIterator
if
- The type
It
satisfies Template:concept
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 typeIt
orconst It
-
r
, a value of typeIt&
-
n
, an integer of typedifference_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; | |
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 | b - a > 0 | 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 RandomAccessIterator
also implements Template:concept.
A mutable RandomAccessIterator
is a RandomAccessIterator
that additionally satisfies the Template:concept requirements.