Share via


distance

distance

template<class Init, class Dist>
    ptrdiff_t distance(InIt first, InIt last);

The template function sets a count n to zero. It then effectively advances first and increments n until first == last. If InIt is a random-access iterator type, the function evaluates the expression n += last - first. Otherwise, it performs each iterator increment by evaluating ++first.

In this implementation, if a translator does not support partial specialization of templates, the return type is ptrdiff_t instead of iterator_traits<InIt>::distance_type. If you are not certain this type is adequate, use the template function:

template<class InIt, class Dist>
    void _Distance(InIt first, InIt last, Dist& n0);

which adds n to the value stored in n0.

See the related sample program.