Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/named req/RandomAccessIterator"

From cppreference.com
< cpp‎ | named req
(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===
  
* {{concept|BidirectionalIterator}}
+
Satisfies {{concept|BidirectionalIterator}} and, additionally, the following requirements:
* {{concept|LessThanComparable}}
+
 
+
  
 
{|table class="wikitable"
 
{|table class="wikitable"
Line 16: Line 14:
 
!Expression||Return||Equivalent expression||Notes
 
!Expression||Return||Equivalent expression||Notes
 
|-
 
|-
|{{c|i +{{=}} n}}
+
|{{c|r +{{=}} n}}
 
|{{c|It&}}
 
|{{c|It&}}
|{{c|while ( n > 0 )
+
|{{c|1=if(n>=0)
   ++i;}}
+
   while(n--) ++r;
 +
else
 +
  while(n++) --r;
 +
return r;}}
 
|
 
|
*{{ttb|n}} can b both positive or negative
+
*{{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 {{=}} i;
+
return i += n;}}  
return i +{{=}} n;}}  
+
||
 
|-
 
|-
 
|{{c|n + i}}||{{c|It}}||{{c|i + n}}||
 
|{{c|n + i}}||{{c|It}}||{{c|i + n}}||
 
|-
 
|-
|{{c|i -{{=}} n}}||{{c|It&}}||{{c|i +{{=}} -n}}||
+
|{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}}||
 
|-
 
|-
|{{c|i - n}}||{{c|It}}||{{c|i + -n}}||
+
|{{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[n]}}||convertible to {{c|T}}||*(a + n)||
+
|{{c|b - a}}||{{tt|difference}}||{{tt|n}}||returns {{tt|n}} such that {{c|1=a+b==b}}
 
|-
 
|-
|{{c|a < b}}||{{c|bool}}||{{c|(a-b) > 0}}
+
|{{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 expression is true)
+
* {{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|n}} is an integer of type {{ttb|difference_type}}
+
*{{ttb|reference}} is the type {{c|std::iterator_traits<It>::reference}}
*{{ttb|T}} is the iterator's value type
+
*{{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;
else
   while(n++) --r;
return r;

  • n can be both positive or negative
  • Constant complexity (that is, the equivalent expression cannot be used as implementation)
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 < a)
  • if a < b then !(b < a)
  • if a < b and b < c then a < c
  • a < b or b < a or a == b
    (exactly one of the expressions is true)
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 concept
  • T is the type std::iterator_traits<It>::value_type
  • reference is the type std::iterator_traits<It>::reference
  • difference is the type std::iterator_traits<It>::difference_type
  • i, a, b are objects of type It or const It
  • r is a value of type It&
  • n is an integer of type difference

The above rules imply that RandomAccessIterator also implements Template:concept.