distance

namespace opentl {
    template <typename It, typename S>
        requires opentl::sentinel_for<S, It>
    constexpr opentl::ptrdiff_t distance(It first, S last) noexcept;
}

Returns the number of positions between the first and last iterators.

Declared in <iterator/distance.hpp>.

Template Parameters

Name Description
It The iterator type.
S The sentinel type.

Parameters

Name Description
first The first iterator.
last The last sentinel.

Return Value

The number of positions between the first and last iterators, negative when the last iterator precedes the first for random access iterators.

Remarks

The distance of random access iterators of the same type is computed with the subtraction operator. Otherwise the first iterator is incremented until it reaches the last sentinel, counting the positions travelled.

Example

#include <opentl/iterator.hpp>

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

    auto n = opentl::distance(first, last);  // 4
}