class
<iterator>

std::random_access_iterator_tag

struct random_access_iterator_tag {};
Random-access iterator category
Empty class to identify the category of an iterator as a random-access iterator:

Random-access iterators


Random-access iterators are iterators that can be used to access elements at an arbitrary offset position relative to element they point to, offering the same functionality as pointers.

Random-access iterators are the most complete iterators in terms of functionality. All pointer types are also valid random-access iterators.

There is not a single type of random-access iterator: Each container may define its own specific iterator type able to iterate through it and access its elements. But all random access iterators support -at least- the following operations:

propertyvalid expressions
Is default-constructible, copy-constructible, copy-assignable and destructibleX a;
X b(a);
b = a;
Can be compared for equivalence using the equality/inequality operators
(meaningful when both iterator values iterate over the same underlying sequence).
a == b
a != b
Can be dereferenced as an rvalue (if in a dereferenceable state).*a
a->m
For mutable iterators (non-constant iterators):
Can be dereferenced as an lvalue (if in a dereferenceable state).
*a = t
Can be incremented (if in a dereferenceable state).
The result is either also dereferenceable or a past-the-end iterator.
Two iterators that compare equal, keep comparing equal after being both increased.
++a
a++
*a++
Can be decremented (if a dereferenceable iterator value precedes it).
--a
a--
*a--
Supports the arithmetic operators + and - between an iterator and an integer value, or subtracting an iterator from another.a + n
n + a
a - n
a - b
Can be compared with inequality relational operators (<, >, <= and >=).a < b
a > b
a <= b
a >= b
Supports compound assignment operations += and -=a += n
a -= n
Supports the offset dereference operator ([])a[n]
propertyvalid expressions
Is default-constructible, copy-constructible, copy-assignable and destructibleX a;
X b(a);
b = a;
Can be compared for equivalence using the equality/inequality operators
(meaningful when both iterator values iterate over the same underlying sequence).
a == b
a != b
Can be dereferenced as an rvalue (if in a dereferenceable state).*a
a->m
For mutable iterators (non-constant iterators):
Can be dereferenced as an lvalue (if in a dereferenceable state).
*a = t
Can be incremented (if in a dereferenceable state).
The result is either also dereferenceable or a past-the-end iterator.
Two iterators that compare equal, keep comparing equal after being both increased.
++a
a++
*a++
Can be decremented (if a dereferenceable iterator value precedes it).
--a
a--
*a--
Supports the arithmetic operators + and - between an iterator and an integer value, or subtracting an iterator from another.a + n
n + a
a - n
a - b
Can be compared with inequality relational operators (<, >, <= and >=).a < b
a > b
a <= b
a >= b
Supports compound assignment operations += and -=a += n
a -= n
Supports the offset dereference operator ([])a[n]
Lvalues are swappable.swap(a,b)

Where X is a random-access iterator type, a and b are objects of this iterator type, n is a value of its difference type, and t is an object of the type pointed by the iterator type (or some other type that can be assigned to the lvalue returned by dereferencing an object of type X).

Constant iterators are iterators that do not fulfill the requirements of an output iterator; Dereferencing them yields a reference to a constant element (such as const T&).

See also