Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | named req
(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 standard pointer is an example of a type that satisfies this concept.
+
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}}
  
In addition to the above requirement, for a type {{tt|It}} to be an {{tt|RandomAccessIterator}}, instances {{tt|a}}, {{tt|b}}, {{tt|i}}, and {{tt|r}} of {{tt|It}} must:
+
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||Equivalent expression||Notes
+
!Expression||Return type||Operational semantics||Notes
 
|-
 
|-
 
|{{c|r +{{=}} n}}
 
|{{c|r +{{=}} n}}
 
|{{c|It&}}
 
|{{c|It&}}
|{{c|1=if ( n >= 0 )
+
|{{c|1=difference_type m = n;
  while(n--) ++r;
+
if (m >= 0) while (m--) ++r;
else
+
else while (m++) --r;
  while(n++) --r;
+
 
return r;}}
 
return r;}}
 
|
 
|
 
*{{ttb|n}} can be both positive or negative
 
*{{ttb|n}} can be both positive or negative
*Constant complexity (that is, the equivalent expression cannot be used as implementation)
+
*The complexity is constant (that is, the implementation cannot actually execute the while loop shown in operational semantics)
 
|-
 
|-
|{{c|i + n}}||{{c|It}}
+
|{{c|a + n}}
|{{c|1=It temp = i;
+
{{c|n + a}}
 +
|{{c|It}}
 +
|{{c|1=It temp = a;
 
return temp += n;}}  
 
return temp += n;}}  
|-
+
|
|{{c|n + i}}||{{c|It}}||{{c|i + 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|difference}}||{{c|n}}||returns {{ttb|n}} such that {{ttb|a + n {{==}} b}}, where {{ttb|b {{==}} a + (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|reference}}||{{c|*(i + n)}}
+
|{{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)}}||
 
|}
 
|}
====Table Notes====
 
*{{ttb|It}} is the type implementing this concept
 
*{{ttb|T}} is the type {{c|std::iterator_traits<It>::value_type}}
 
*{{ttb|reference}} is the type {{c|std::iterator_traits<It>::reference}}
 
*{{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}}.
+
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

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;
else while (m++) --r;
return r;

  • 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)
a + n

n + a

It It temp = a;

return temp += n;

  • n can be both positive or negative
  • a + n == n + a
r -= n It& return r += -n;
i - n It It temp = i;
return temp -= n;
b - a difference_type return n;

Precondition:

  • there exists a value n of type difference_type such that a+n==b

Postcondition:

  • b == a + (b - a).
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)

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

A mutable RandomAccessIterator is a RandomAccessIterator that additionally satisfies the Template:concept requirements.

See also