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`)
Line 18: Line 18:
 
|{{c|r +{{=}} n}}
 
|{{c|r +{{=}} n}}
 
|{{c|It&}}
 
|{{c|It&}}
|{{c|1=if(n>=0)
+
|{{c|1=if ( n >= 0 )
 
   while(n--) ++r;
 
   while(n--) ++r;
 
else
 
else
Line 34: Line 34:
 
|-
 
|-
 
|{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}}
 
|{{c|r -{{=}} n}}||{{c|It&}}||{{c|return r +{{=}} -n;}}
 +
||
 
|-
 
|-
 
|{{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|n - i}}||{{c|It}}||{{c|i - n}}
 
 
|-
 
|-
|{{c|b - a}}||{{tt|difference}}||{{tt|n}}||returns {{tt|n}} such that {{c|1=a+n==b}}
+
|{{c|b - a}}||{{c|difference}}||{{c|n}}||returns {{ttb|n}} such that {{ttb|a + n {{==}} b}}, where {{ttb|b {{==}} a + (b - a)}}.
 
|-
 
|-
|{{c|i[n]}}||convertible to {{tt|reference}}||*(i + n)
+
|{{c|i[n]}}||convertible to {{c|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}}

Revision as of 20:43, 14 September 2013

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 standard pointer is an example of a type that satisfies this concept.

Requirements

In addition to the above requirement, for a type It to be an RandomAccessIterator, instances a, b, i, and r of It must:

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 temp += n;

n + i It i + n
r -= n It& return r += -n;
i - n It It temp = i;

return temp -= n;

b - a difference n returns n such that a + n == b, where 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)

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.

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

See also