advance

namespace opentl {
    template <typename It>
        requires opentl::random_access_iterator<It>
    constexpr void advance(It& it, opentl::ptrdiff_t n) noexcept;

    template <typename It>
        requires opentl::bidirectional_iterator<It>
    constexpr void advance(It& it, opentl::ptrdiff_t n) noexcept;

    template <typename It>
        requires opentl::iterator<It>
    constexpr void advance(It& it, opentl::ptrdiff_t n) noexcept;
}

Advances the iterator by n positions.

Declared in <iterator/advance.hpp>.

Template Parameters

Name Description
It The iterator type.

Parameters

Name Description
it The iterator to advance.
n The number of positions to advance by, negative values backtrack.

Remarks

The most constrained overload that models the iterator is selected, each one stepping the iterator the least it has to.

Iterator concept Behaviour
random_access_iterator Applies the offset to the iterator in a single step.
bidirectional_iterator Increments or decrements the iterator one position at a time.
iterator Increments the iterator one position at a time. Backtracking is not supported.

Example

#include <opentl/iterator.hpp>

int main() {
    int values[] = { 1, 2, 3, 4 };
    int* it = values;

    opentl::advance(it, 2);   // points to 3
    opentl::advance(it, -1);  // points to 2
}