Difference between revisions of "cpp/named req/RandomAccessIterator"
From cppreference.com
(Added operator[] to the table, as per ISO/IEC 14882:2003 24.1.5 (Table 76)) |
(brought the whole thing in line with std) |
||
Line 8: | Line 8: | ||
===Requirements=== | ===Requirements=== | ||
− | + | Satisfies {{concept|BidirectionalIterator}} and, additionally, the following requirements: | |
− | + | ||
− | + | ||
{|table class="wikitable" | {|table class="wikitable" | ||
Line 16: | Line 14: | ||
!Expression||Return||Equivalent expression||Notes | !Expression||Return||Equivalent expression||Notes | ||
|- | |- | ||
− | |{{c| | + | |{{c|r +{{=}} n}} |
|{{c|It&}} | |{{c|It&}} | ||
− | |{{c| | + | |{{c|1=if(n>=0) |
− | ++ | + | while(n--) ++r; |
+ | else | ||
+ | while(n++) --r; | ||
+ | return r;}} | ||
| | | | ||
− | *{{ttb|n}} can | + | *{{ttb|n}} can be both positive or negative |
− | *Constant complexity | + | *Constant complexity (that is, the equivalent expression cannot be used as implementation) |
|- | |- | ||
− | |{{c|i + n}} | + | |{{c|i + n}}||{{c|It}} |
− | |{{c|It}} | + | |{{c|1=It temp = i; |
− | |{{c|It temp | + | return i += n;}} |
− | return i + | + | || |
|- | |- | ||
|{{c|n + i}}||{{c|It}}||{{c|i + n}}|| | |{{c|n + i}}||{{c|It}}||{{c|i + n}}|| | ||
|- | |- | ||
− | |{{c| | + | |{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}}|| |
|- | |- | ||
− | |{{c|i - n}}||{{c|It}}||{{c|i | + | |{{c|i - n}}||{{c|It}}||{{c|1=It temp = i; |
+ | return i -= n;}} | ||
+ | || | ||
|- | |- | ||
|{{c|n - i}}||{{c|It}}||{{c|i - n}}|| | |{{c|n - i}}||{{c|It}}||{{c|i - n}}|| | ||
|- | |- | ||
− | |{{c|a | + | |{{c|b - a}}||{{tt|difference}}||{{tt|n}}||returns {{tt|n}} such that {{c|1=a+b==b}} |
|- | |- | ||
− | |{{c|a < b}}||{{c|bool}}||{{c| | + | |{{c|i[n]}}||convertible to {{tt|reference}}||*(i + n)|| |
+ | |- | ||
+ | |{{c|a < b}}||contextually convertible to {{c|bool}}||{{c|b - a > 0}} | ||
|Strict total ordering relation: | |Strict total ordering relation: | ||
* {{ttb|!(a < a)}} | * {{ttb|!(a < a)}} | ||
* if {{ttb|a < b}} then {{ttb|!(b < a)}} | * if {{ttb|a < b}} then {{ttb|!(b < a)}} | ||
* if {{ttb|a < b}} and {{ttb|b < c}} then {{ttb|a < c}} | * if {{ttb|a < b}} and {{ttb|b < c}} then {{ttb|a < c}} | ||
− | * {{ttb|a < b}} or {{ttb|b < a}} or {{ttb|a {{==}} b}} <br> (exactly one of the | + | * {{ttb|a < b}} or {{ttb|b < a}} or {{ttb|a {{==}} b}} <br> (exactly one of the expressions is true) |
|- | |- | ||
− | |{{c|a > b}}||{{c|bool}}||{{c|b < a}}|| | + | |{{c|a > b}}||contextually convertible to {{c|bool}}||{{c|b < a}}||Total ordering relation opposite to {{c|a < b}} |
|- | |- | ||
− | |{{c|a >{{=}} b}}||{{c|bool}}||{{c|!(a < b)}} || | + | |{{c|a >{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a < b)}} || |
|- | |- | ||
− | |{{c|a <{{=}} b}}||{{c|bool}}||{{c|!(a > b)}}|| | + | |{{c|a <{{=}} b}}||contextually convertible to {{c|bool}}||{{c|!(a > b)}}|| |
|} | |} | ||
====Table Notes==== | ====Table Notes==== | ||
*{{ttb|It}} is the type implementing this concept | *{{ttb|It}} is the type implementing this concept | ||
− | *{{ttb|i}}, {{ttb|a}}, {{ttb|b}} are objects of type {{ttb|It}} | + | *{{ttb|T}} is the type {{c|std::iterator_traits<It>::value_type}} |
− | *{{ttb| | + | *{{ttb|reference}} is the type {{c|std::iterator_traits<It>::reference}} |
− | *{{ttb| | + | *{{ttb|difference}} is the type {{c|std::iterator_traits<It>::difference_type}} |
+ | *{{ttb|i}}, {{ttb|a}}, {{ttb|b}} are objects of type {{ttb|It}} or {{ttb|const It}} | ||
+ | *{{ttb|r}} is a value of type {{ttb|It&}} | ||
+ | *{{ttb|n}} is an integer of type {{ttb|difference}} | ||
+ | |||
+ | The above rules imply that RandomAccessIterator also implements {{concept|LessThanComparable}}. |
Revision as of 02:42, 18 May 2012
Template:cpp/concept/title Template:cpp/concept/sidebar
An Iterator that can be moved to point to any element in constant time.
A standard pointer satisfies this concept.
Requirements
Satisfies Template:concept and, additionally, the following requirements:
Expression | Return | Equivalent expression | Notes |
---|---|---|---|
r += n | It& | if(n>=0) while(n--) ++r; |
|
i + n | It | It temp = i; return i += n; |
|
n + i | It | i + n | |
r -= n | It& | return r += -n; | |
i - n | It | It temp = i; return i -= n; |
|
n - i | It | i - n | |
b - a | difference |
n |
returns n such that a+b==b
|
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) |
Table Notes
It
is the type implementing this conceptT
is the type std::iterator_traits<It>::value_typereference
is the type std::iterator_traits<It>::referencedifference
is the type std::iterator_traits<It>::difference_typei
,a
,b
are objects of typeIt
orconst It
r
is a value of typeIt&
n
is an integer of typedifference
The above rules imply that RandomAccessIterator also implements Template:concept.