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
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
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().
you may want to check some STL and iterators tutorials.
here is one http://www.cprogramming.com/tutorial/stl/iterators.html
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 :)