The inner loop can be rewritten using the following algorithms:
std::for_each( (*polygon)->points.begin(), (*polygon)->points.end(), &Point::DoSomething );
Mixing this with an outer loop is a bit more complicated:
std::for_each( polygons.begin(), polygons.end(), []( Polygon* polygon ) { std::for_each( polygon->points.begin(), polygon->points.end(), &Point::DoSomething ); } );
If we had some kind of complex iterator, we could really express your intention, which is to do something for every point in every polygon. And a range library, such as Boost.Range, will allow you to avoid naming each container twice, given that you want to work with your entire range.
My ideal version of your code would look like this:
for_each( flatten( polygons ), &Point::DoSomething );
were flatten return the representation of each Point in each Polygon , as if it were the only continuous range. Please note that this is something that can be done in simple C ++ 03, and all that we lack in Boost.Range to achieve this is the flatenning range, which is not difficult to implement in terms of the join range.
Otherwise, the for-loop along with auto will help you reduce the iteration pattern by throwing away the range and forgetting about complex types.
K-ballo
source share