tags:

views:

278

answers:

3

What is the for loop doing? I just can't understand it.

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

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

Thanks in advance

+25  A: 

It's finding the first element in nabors that satisfies the condition

dist < i->second

If no element satisfies that condition, the iterator i points to nabors.end().

James McNellis
@Dominic: Thank you for the correction; I just thought of that, came back to fix it, and found it was already corrected. :-)
James McNellis
@James - no problem!
Dominic Rodger
+2  A: 

you may want to check some STL and iterators tutorials.

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

Yin Zhu
+3  A: 

Maybe the code is clearer with std::find_if and an explicit 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));

Dunno, I'm just an STL fanboy :)

Fred