9 Iterators library [iterators]

9.3 Iterator requirements [iterator.requirements]

9.3.15 Concept RandomAccessIterator [iterators.random.access]

The RandomAccessIterator concept refines BidirectionalIterator ([iterators.bidirectional]) and adds support for constant-time advancement with +=, +, -=, and -, and the computation of distance in constant time with -. Random access iterators also support array notation via subscripting.

  template <class I>
  concept bool RandomAccessIterator =
    BidirectionalIterator<I> &&
    DerivedFrom<iterator_category_t<I>, random_access_iterator_tag> &&
    StrictTotallyOrdered<I> &&
    SizedSentinel<I, I> &&
    requires(I i, const I j, const difference_type_t<I> n) {
      { i += n } -> Same<I>&;
      { j + n }  -> Same<I>&&;
      { n + j }  -> Same<I>&&;
      { i -= n } -> Same<I>&;
      { j - n }  -> Same<I>&&;
      j[n];
      requires Same<decltype(j[n]), reference_t<I>>;
    };

Let a and b be valid iterators of type I such that b is reachable from a. Let n be the smallest value of type difference_type_t<I> such that after n applications of ++a, then bool(a == b). RandomAccessIterator<I> is satisfied only if:

  • (a += n) is equal to b.

  • &(a += n) is equal to &a.

  • (a + n) is equal to (a += n).

  • For any two positive integers x and y, if a + (x + y) is valid, then a + (x + y) is equal to (a + x) + y.

  • a + 0 is equal to a.

  • If (a + (n - 1)) is valid, then a + n is equal to ++(a + (n - 1)).

  • (b += -n) is equal to a.

  • (b -= n) is equal to a.

  • &(b -= n) is equal to &b.

  • (b - n) is equal to (b -= n).

  • If b is dereferenceable, then a[n] is valid and is equal to *b.