Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | named req
m
(Explicitly state what dereferencing the iterator does (accesses a container's value type)
Line 3: Line 3:
  
 
A {{named req|RandomAccessIterator}} is a {{named req|BidirectionalIterator}} that can be moved to point to any element in constant time.
 
A {{named req|RandomAccessIterator}} is a {{named req|BidirectionalIterator}} that can be moved to point to any element in constant time.
 +
 +
If a {{named req|RandomAccessIterator}} {{tt|it}} originates from a {{named req|Container}}, then {{tt|it}}'s {{c|value_type}} is the same as the container's, so dereferencing ({{c|*it}}) obtains the container's {{c|value_type}}.
  
 
A pointer to an element of an array satisfies all requirements of {{named req/core|RandomAccessIterator}}
 
A pointer to an element of an array satisfies all requirements of {{named req/core|RandomAccessIterator}}

Revision as of 22:04, 14 February 2021

 
 
C++ named requirements
 

A LegacyRandomAccessIterator is a LegacyBidirectionalIterator that can be moved to point to any element in constant time.

If a LegacyRandomAccessIterator it originates from a Container, then it's value_type is the same as the container's, so dereferencing (*it) obtains the container's value_type.

A pointer to an element of an array satisfies all requirements of LegacyRandomAccessIterator

Requirements

The type It satisfies LegacyRandomAccessIterator 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; The absolute value of n must be within the range of representable values of difference_type.
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 Equivalent to return b - a > 0; Precondition:
  • same as of b - a

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 LegacyRandomAccessIterator also implements LessThanComparable.

A mutable LegacyRandomAccessIterator is a LegacyRandomAccessIterator that additionally satisfies the LegacyOutputIterator requirements.

Concept

For the definition of std::iterator_traits, the following exposition-only concept is defined.

template<class I>

concept __LegacyRandomAccessIterator =
  __LegacyBidirectionalIterator<I> && std::totally_ordered<I> &&
  requires(I i, typename std::incrementable_traits<I>::difference_type n) {
    { i += n } -> std::same_as<I&>;
    { i -= n } -> std::same_as<I&>;
    { i +  n } -> std::same_as<I>;
    { n +  i } -> std::same_as<I>;
    { i -  n } -> std::same_as<I>;
    { i -  i } -> std::same_as<decltype(n)>;
    {  i[n]  } -> std::convertible_to<std::iter_reference_t<I>>;

  };

where the exposition-only concept __LegacyBidirectionalIterator is described in LegacyBidirectionalIterator#Concept.

(since C++20)

See also

specifies that a bidirectional_iterator is a random-access iterator, supporting advancement in constant time and subscripting
(concept) [edit]