std::distance (3) - Linux Manuals
std::distance: std::distance
NAME
Synopsis
Defined in header <iterator>
template< class InputIt >
typename std::iterator_traits<InputIt>::difference_type (until C++17)
distance( InputIt first, InputIt last );
template< class InputIt >
constexpr typename std::iterator_traits<InputIt>::difference_type (since C++17)
distance( InputIt first, InputIt last );
Returns the number of hops from first to last.
The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first. (until C++11)
If InputIt is not LegacyRandomAccessIterator, the behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first. If InputIt is LegacyRandomAccessIterator, the behavior is undefined if last is not reachable from first and first is not reachable from last. (since C++11)
first - iterator pointing to the first element
last - iterator pointing to the end of the range
Type requirements
-
InputIt must meet the requirements of LegacyInputIterator. The operation is more efficient if InputIt additionally meets the requirements of LegacyRandomAccessIterator
Return value
The number of increments needed to go from first to last.
The value may be negative if random-access iterators are used and first is reachable from last
(since C++11)
Complexity
Linear.
However, if InputIt additionally meets the requirements of LegacyRandomAccessIterator, complexity is constant.
Example
// Run this code
Output:
See also
advance (function template)
count (function template)
count_if