# Random Access Iterator

### Description

A Random Access Iterator is a Bidirectional Iterator that provides constant-time methods for moving forward and backward on a sequence in arbitrary-sized steps.

### Refinement of

Bidirectional Iterator

### Valid expressions

Random Access Iterator defines two new expressions and refines the ones described in Bidirectional Iterator.

Expression | Expression type
--- | ---
`typename i::next` | A model of Random Access Iterator
`typename i::prior` | A model of Random Access Iterator
`typename i::template advance<n>::type` | A model of Random Access Iterator
`typename i::template distance<j>::type` | A model of Integral Constant

### Expression semantics

Semantics of an expression is defined only where it is not defined in Bidirectional Iterator.

Expression | Complexity | Precondition | Semantics | Postcondition
--- | --- | --- | --- | ---
`typename i::template advance<n>::type` | Amortized constant time | Including `i` itself, there must be `n::value` dereferenceable or past-the-end iterators following or preceding `i`, depending on whether `n` is positive or negative. | If `n::value > 0`, equivalent to executing `i::next` `n::value` times; if `n::value < 0`, equivalent to executing `i::prior` `n::value` times; if `n::value == 0`, this is a null operation [1]. | The resulting iterator is dereferenceable or past-the-end.
`typename i::template distance<j>::type` | Amortized constant time | Either `i` is reachable from `j` or `j` is reachable from `i`, or both. | Returns an integral constant `n` such that `i::template advance<n>::type` is identical to `j`.

### Invariants

For any random access iterators `i` and `j` the following invariants always hold:

- if `i::advance<n>::type` is well-defined, then `i::advance<n>::type::advance< negate<n>::type >::type` is a null operation.

### See also

Random Access Sequence, Bidirectional Iterator

Table of Content
Last edited July 15, 2002 11:35 am