Open In App

std::distance in C++

Last Updated : 28 Oct, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The std::distance() in C++ STL is a built-in function used to calculate the number of elements between two iterators. It is defined inside <iterator> header file. In this article, we will learn about the std::distance function in C++ with examples.

Example:

C++
// C++ Program to illustrate the use of
// std::distance() function
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {11, 9, 12, 15, 67};

    cout << distance(v.begin(), v.end());

    return 0;
}

Output
5

std::distance() Syntax

std::distance(first, last);

Parameters

  • first: Iterator/pointer to the first element of the given range.
  • last: Iterator/pointer to the element just after the last element of the given range.

Return Value

  • Returns the number of elements between the two iterators. If first comes before last, the result is positive; otherwise, it is negative.

Working of std::distance()

The std::distance() function calculates the distance between two iterators by iterating from the first iterator to the last. For random-access iterators, such as those used by vectors and deques, the calculation is done in constant time (O(1)). For other types of iterators, such as those for lists or sets, it has a linear time complexity of O(n).

More Examples of std::distance()

The following examples demonstrates the use of std::distance() function in different scenarios:

Example 1: Finding Number of Elements in a Part of Vector

C++
// C++ Program to find the number of elements
// in a part of vector using iterators
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {11, 9, 12, 15, 67};

    // First iterator pointing to 11
    auto first = v.begin();

    // Last iterator pointing to 67
    auto last = v.begin() + 4;

    // Find the number of elements between first
    // and last [first,last)
    cout << distance(first, last);
    return 0;
}

Output
4

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 2: Finding the Number of Elements in an Array

C++
// C++ Program to find the number of elements
// in an array using iterators
#include <bits/stdc++.h>
using namespace std;

int main() {
    int arr[5] = {11, 9, 12, 15, 67};
  	int n = sizeof(arr)/sizeof(arr[0]);

    auto first = arr;
    auto last = arr + n;

    // Find the number of elements between first
    // and last [first,last)
    cout << distance(first, last);
    return 0;
}

Output
5

Time Complexity: O(1)
Auxiliary Space: O(1)

Example 3: Finding the Number of Elements in a Part of List

C++
// C++ Program to find the number of elements
// in a part of set using iterators
#include <bits/stdc++.h>
using namespace std;

int main() {
    set<int> s = {11, 9, 12, 15, 67};
  
  	// Iterator to element 9
    auto first = s.find(9);
  
  	// Iterator to element 67
    auto last = s.find(67);
  
  	// Finding the number of elements between
  	// the first and last [first, last)
    cout << distance(first, last);
    return 0;
}

Output
4

Time Complexity: O(n), where n is the number of elements between the two iterators of std::set.
Auxiliary Space: O(1)

Example 4: Negative Distance

C++
// C++ Program to show when the negative distance
// is returned by std::distance function
#include <bits/stdc++.h>
using namespace std;

int main() {
    vector<int> v = {11, 9, 12, 15, 67};
  	
  	// First iterator after the last iterator
    auto first = v.end();
    auto last = v.begin();
  
  	// Finding the distance between [first, last)
    cout << distance(first, last);
  
    return 0;
}

Output
-5

Example 5: Passing Iterators of Different Containers to std::distance()

C++
// C++ Program to illustrate std::distance()
// in reverse order
#include <bits/stdc++.h>
using namespace std;
int main() {
    vector<int> v1 = {11, 9, 12, 15, 67};
  	vector<int> v2 = {7, 4, 1, 5, 2};

    // Iterator to 12 in vector v1
    auto first = v1.begin() + 2;

    // Iterator to 2 in vector v2
    auto last = v2.begin() + 4;

    // Find the number of elements between first
    // and last [first,last)
    cout << distance(first, last) << endl;
    return 0;
}

Output
10

Time Complexity: O(1)
Auxiliary Space: O(1)

Explanation: In this example, we have taken the iterators to two different vectors, and got the output as 10. But this information is pretty useless for a regular person as there is no guarantee that the vectors declared subsequently will be stored continuously after the previous one.


Practice Tags :

Similar Reads