std::distance() in C++

In this tute, we will discuss about std::distance() in C++. The std::distance() is an in-built function provided in the Standard Library (STL).

The std::distance() method is used to find the distance between two iterators. It takes two iterators as arguments and returns an integer. The returned integer can be both positive and negative based on the order of iterators passed.

 

std::distance() in C++ with syntax and example

Syntax:

int distance(iterator first, iterator last)

For example, let’s consider the number line and see what would the distance() method returns.

distance(5, 10) => 5 (Considers 5, 6, 7, 8, 9)

distance(5, 5) => 0

From the two examples, you would’ve probably recognized that the distance() method calculates the number of elements in the range [first, last). Let’s see the implementation of using the std::distance() method.

 

Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<ll> ar = {10, 20, 30, 40, 50};

    cout<<"Distance between ar.begin() and ar.end(): "<<distance(ar.begin(), ar.end())<<endl;
    cout<<"Distance between ar.begin()+2 and ar.end()-1: "<<distance(ar.begin()+2, ar.end()-1);

    return 0;
}

Output:

Distance between ar.begin() and ar.end(): 5
Distance between ar.begin()+2 and ar.end()-1: 2

 

We can clearly see that it returns the distance between the two iterators. But what happens if the iterators are placed in the reverse order? Will it still return a positive value? Let’s see that as well.

 

std::distance() in C++ for reverse order:

The distance() method does not return a positive value all the time. If the iterators are in the reverse order, it returns a negative value. The value returned is the exact negative value of the one returned while the iterators are passed in the forward direction.

For example, let’s consider the number line again and see what would the distance() method returns.

distance(10, 5) => -5 (Considers 10, 9, 8, 7, 6)

distance(5, 5) => 0

As discussed earlier, we know that the distance() method calculates the number of elements in the range [first, last). Let’s see the implementation of using the std::distance() method for iterators in the reverse order.

 

Code:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    vector<ll> ar = {10, 20, 30, 40, 50};

    cout<<"Distance between ar.begin() and ar.end(): "<<distance(ar.begin(), ar.end())<<endl;
    cout<<"Distance between ar.end() and ar.begin(): "<<distance(ar.end(), ar.begin())<<endl;

    cout<<"Distance between ar.begin()+2 and ar.end()-1: "<<distance(ar.begin()+2, ar.end()-1)<<endl;
    cout<<"Distance between ar.end()-1 and ar.begin()+2: "<<distance(ar.end()-1, ar.begin()+2);

    return 0;
}

Output:

Distance between ar.begin() and ar.end(): 5
Distance between ar.end() and ar.begin(): -5
Distance between ar.begin()+2 and ar.end()-1: 2
Distance between ar.end()-1 and ar.begin()+2: -2

 

Time Complexity:

The time complexity is O(1) for random access iterators and O(n) for other iterators.

 

Finally, If you have any queries or doubts related to std::distance() in C++, simply comment in the comment section provided below.

Also, read:

Leave a Reply