Elegant way to iterate in C++ <p>Let's say I have a vector of Polygons, where each polygon contains a vector of Points. I have to iterate over all the points of all the polygons many times in my code, I end up having to write the same code over and over again:</p> <pre><code>for(std::vector&lt;Polygon*&gt;::const_iterator polygon = polygons.begin(); polygon != polygons.end(); polygon++) { for(std::vector&lt;Point&gt;::const_iterator point = (*polygon)-&gt;points.begin(); point != (*polygon)-&gt;points.end(); point++) { (*point).DoSomething(); } } </code></pre> <p>I really feel that is a lot of code for two simple iterations, and feel like it's clogging the code and interfering with the readability.</p> <p>Some options I thought are:</p> <ul> <li>using #defines - but it would make unportable (to use in other parts of the code). Furthermore, #defines are considered evil nowadays;</li> <li>iterate over vector->size() - it doesn't seem the most elegant way;</li> <li>calling a method with a function pointer - but in this case, the code that should be inside of the loop would be far from the loop.</li> </ul> <p>So, what would be the most clean and elegant way of doing this?</p>