std::distance in C++ - GeeksforGeeks

30 Nov, 2021
If we have two iterators and we want to find the total no. of elements between the two iterators, then that is facilitated by std::distance(), defined inside the header file .It has an important feature that just like we have vectors in science, which have both magnitude and direction, std::distance also has direction associated with it. This means that calculating the distance between first and last and then calculating distance between last and first will not be same, as in the second case, it will have a negative sign associated with it, since we are moving backwards. Syntax:  
std::distance(InputIterator first, InputIterator last)
Here, first and last are input iterators between which we have to calculate distance.

Returns: The number of elements between first and last.
Example:  
Input: v = 10 20 30 40 50
first pointing to v.begin() and last pointing to v.end()
Output: No. of elements: 5
 
CPP
// C++ program to demonstrate std::distance()#include <iostream>#include <vector>#include <iterator>using namespace std;int main(){    vector<int> v;    int i;     for (i = 0; i < 10; ++i)    {        v.push_back(i);    }     /*v contains 0 1 2 3 4 5 6 7 8 9*/     vector<int>::iterator first;    vector<int>::iterator last;     // first pointing to 0    first = v.begin();     // last pointing to 5    last = v.begin() + 5;     // Calculating no. of elements between first and last    int num = std::distance(first, last);     // Displaying num    cout << num << "\n";    return 0;}
Output:  
5
Here, total number of elements between first (pointing to 0) and last (pointing to 5) is 5, i.e., 0 1 2 3 4. So element pointed to by last has not been counted by distance().  
What happens when we reverse the order while calculating distance?
Since, it counts the no. of elements between the two input iterators, one thing to be kept in mind is that it does not count the element being pointed to by last (if any), while calculating the distance, whereas element pointed to by first is counted i,e range is [first,last). 
CPP
// C++ program to demonstrate the use of std::distance// with reverse order#include <iostream>#include <vector>#include <iterator>using namespace std;int main(){    vector<int> v;    int i;     for (i = 0; i < 10; ++i)    {        v.push_back(i);    }     // Calculating no. of elements in vector v    int num = std::distance(v.begin(), v.end());     // Displaying num    cout << num << "\n";     // Calculating in reverse order    num = std::distance(v.end(), v.begin());     // Displaying num    cout << num << "\n";    return 0;}
Output:  
10
-10
Time Complexity: Constant for random-access iterators and O(n) for all other iterators.This article is contributed by Mrigendra Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
vishalsharma14
cpp-iterator
STL
C++
STL
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Operator Overloading in C++
Polymorphism in C++
Sorting a vector in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Queue in C++ Standard Template Library (STL)
Inline Functions in C++
Array of Strings in C++ (5 Different Ways to Create)
Convert string to char array in C++