std::distance
From cppreference.com
                    
                                        
                    
                    
                                                            
                    | Defined in header  <iterator> | ||
| template< class InputIt > typename std::iterator_traits<InputIt>::difference_type  | ||
Returns the number of elements between first and last.
The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first.
| Contents | 
[edit] Parameters
| first | - | iterator pointing to the first element | 
| last | - | iterator pointing to the end of the range | 
| Type requirements | ||
| - InputItmust meet the requirements ofInputIterator. The operation is more efficient ifInputItadditionally meets the requirements ofRandomAccessIterator | ||
[edit] Return value
The number of elements between first and last.
[edit] Complexity
Linear.
However, if InputIt additionally meets the requirements of RandomAccessIterator, complexity is constant.
[edit] Example
Run this code
#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) | 


