Namespaces
Variants
Views
Actions

std::distance

From cppreference.com
Defined in header <iterator>
template< class InputIterator >

typename std::iterator_traits<InputIterator>::difference_type

    distance( InputIterator first, InputIterator last );

Returns the number of elements between first and last.

If InputIterator is a random access iterator last - first is returned.

If InputIterator is a input, forward or bidirectional iterator the number of increment operations to get from first to last is returned.

Contents

[edit] Parameters

first - Iterator pointing to the first element.

If InputIterator is a random access iterator last shall be reachable from first or first shall be reachable from last.

last - Iterator pointing to the last element.

If InputIterator is a random access iterator last shall be reachable from first or first shall be reachable from last.

If is a input, forward or bidirectional iterator last shall be reachable from first.

[edit] Return value

The number of elements between first and last.

[edit] Example

#include <iostream>
#include <iterator>
#include <vector>
 
int main() 
{
    std::vector<int> v{ 3, 1, 4 };
 
    auto distance = std::distance(v.begin(), v.end());
 
    std::cout << distance << '\n';
}

Output:

3

[edit] See also

advances an iterator by given distance
(function) [edit]