1

Find the distance between two Iterators in a Set in C++

 1 year ago
source link: https://thispointer.com/find-the-distance-between-two-iterators-in-a-set-in-c/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Find the distance between two Iterators in a Set in C++

August 25, 2023 / C++, std::set / By Ritika

This tutorial will discuss how to find the distance between two iterators in a set in C++.

In C++, if you have a set containing various numbers and you want to determine the distance between two specific iterators, you can use the std::distance() function from the Standard Template Library (STL).

Advertisements

vid5e62792b95ec8618094391.jpg?cbuster=1600267117
find-the-distance-between-two-iterators-in-a-set-in-c
liveView.php?hash=ozcmPTEznXRiPTEzqzyxX2V2ZW50PTUjJaNypaZypyRcoWU9MTY5MmMmNwIlMCZ2nWRspGkurWVlVzVlPTMhMS4jJaM9MTAkMwx3JaN0YT0jJat9NDUmJax9MmI1JaZcZF9jYXNmRG9gYWyhPXRbnXNjo2yhqGVlLzNioSZmqWJJZD10nGympG9coaRypv5wo20zZGVvqWqJozZipz1uqGyiow0znXNBpHA9MCZmZGg2PSZlnT02QmY5NmY2NTUmNmQ2MTp0NmM3QmpmNxImMTqCNTQmMDqEN0I2NDMlMmAmMwMmMxQmMDM4MxQmMwM5NUYmMwMlN0Q3QwpmMmEmMwMmMmQmOTM2MmQmOTqEN0I0MmMkMmYmNwqEN0I1MmY0NDp2ODpjNwMmMmQlNmY2MTU3MmUmMDVBNTt0OTp1NTxmMwM5NmQ3RDqCNwI2MmY4NmI2RwZENwU3RDqCNmE2NDY1NmM2Qwp0NxY3MDqEN0I2RwZDNwx2RTp1Nmt3RDqCNTtmNDM1MmM3RDqCNTxmMmMlMmU3RDqCNwYmMTqEN0I0QmMkMmImNTMlMmE3REZFRxUzZGyunWQ9JaVmZXJJpEFxZHI9NDUhNmphMTp4LwImNSZ1p2VlVUE9TW96nWkfYSUlRwUhMCUlMCUlOFtkMSUmQvUlMEkcoaV4JTIjrDt2XmY0JTI5JTIjQXBjoGVXZWJLnXQyMx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mwx5MmBuY2YzY29hqGVhqEZcoGVJZD0jJz1yZGyuUGkurUkcp3RJZD0jJz1yZGyuTGymqEyxPTAzZHVlPTMlJzqxpHI9MCZaZHBlQ29hp2VhqD0znXNXZVBup3NHZHBlPTEzY2NjYT0jJzNwpGFDo25mZW50PSZwYaVmqGVlPTE2OTMmMmYlMwQjNmpzqWyxPVNyn2yhZG9TUGkurWVlNwRyZTQlOWMlOTEmZCZjqWJVpzj9nHR0pHMyM0EyMxYyMxZ0nGympG9coaRypv5wo20yMxZznW5xLXRbZS1xnXN0YW5wZS1vZXR3ZWVhLXR3ol1cqGVlYXRipaMgnW4gYS1mZXQgnW4gYlUlRvZzoG9uqFN0YXR1pm1zYWkmZSZynWRmpD1cnXEzpHucZD0lMDp2NmpjNwA3

For instance, suppose you have a set, and you retrieve an iterator pointing to the value 2. Then you retrieve another iterator pointing to the value 7. To find the distance (or the number of elements) between these two iterators (or values), you can use std::distance().

Copy to clipboard
std::set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Get the iterator to the elements with value 2
auto it1 = numbers.find(2);
// Get the iterator to the elements with values 7
auto it2 = numbers.find(7);
if (it1 != numbers.end() &&
it2 != numbers.end())
// Find the distance between the two iterators:
int dist = std::distance(it1, it2);
std::cout <<"Distance between two iterators: " << dist << std::endl;
std::cout << "One of the elements not found in the set n";
std::set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

// Get the iterator to the elements with value 2
auto it1 = numbers.find(2);
// Get the iterator to the elements with values 7
auto it2 = numbers.find(7);


if (it1 != numbers.end() &&
    it2 != numbers.end())
{
    // Find the distance between the two iterators:
    int dist = std::distance(it1, it2);

    std::cout <<"Distance between two iterators: " << dist << std::endl;
}
else
{
    std::cout << "One of the elements not found in the set n";
}

It’s important to note that you cannot directly subtract these two iterators to obtain the distance between them. This is because, in a set, arithmetic operations other than incrementing or decrementing an iterator by one are not supported. Thankfully, std::distance() handles this for you and returns the exact number of elements between the two iterators.

So, by utilizing std::distance, you can easily find the distance between two iterators in a set in C++.

Let’s see the complete example,

Copy to clipboard
#include <iostream>
#include <set>
#include <iterator>
int main()
std::set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// Get the iterator to the elements with value 2
auto it1 = numbers.find(2);
// Get the iterator to the elements with values 7
auto it2 = numbers.find(7);
if (it1 != numbers.end() &&
it2 != numbers.end())
// Find the distance between the two iterators:
int dist = std::distance(it1, it2);
std::cout <<"Distance between two iterators: " << dist << std::endl;
std::cout << "One of the elements not found in the set n";
return 0;
#include <iostream>
#include <set>
#include <iterator>

int main()
{
    std::set<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    // Get the iterator to the elements with value 2
    auto it1 = numbers.find(2);

    // Get the iterator to the elements with values 7
    auto it2 = numbers.find(7);


    if (it1 != numbers.end() &&
        it2 != numbers.end())
    {
        // Find the distance between the two iterators:
        int dist = std::distance(it1, it2);

        std::cout <<"Distance between two iterators: " << dist << std::endl;
    }
    else
    {
        std::cout << "One of the elements not found in the set n";
    }

    return 0;
}

Output

Copy to clipboard
Distance between two iterators: 5
Distance between two iterators: 5

Summary

Today, we learned how to find the distance between two iterators in a set in C++.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK