What is this for a loop?

What does a for loop do? I just do not understand.

list<pair<int, double> > nabors;
list<pair<int, double> >::iterator i;

for (i = nabors.begin(); i != nabors.end() && dist >= i->second; i++);
+3
source share
3 answers

Finding the first element of naborssatisfying the condition

dist < i->second

If no element satisfies this condition, the iterator ipoints to nabors.end().

+25
source

Maybe the code is clearer with an std::find_ifexplicit predicate?

class further_away_than
{
    double dist;
public:
    further_away_than(double dist) : dist(dist) {}

    bool operator()(const pair<int, double>& p)
    {
        return p.second > dist;
    }
};

#include <algorithm>

// ...

    i = find_if(nabors.begin(), nabors.end(), further_away_than(dist));

I don’t know, I'm just a fan of STL :)

+3
source

You can check out some STL tutorials and iterators.

here is one http://www.cprogramming.com/tutorial/stl/iterators.html

+2
source

Source: https://habr.com/ru/post/1724625/


All Articles