Have you ever wanted to be able to iterate through your own class as if it was a cool STL container?
Do something like this:
Then it there is a way to integrate your class into STL and get powerful support. In order to be able to provide interface to for each loop you need to support in your custom classes set of operations such as:
After giving it a good time to fix all the bugs and understand them all as single piece you will notice what really for each loop needs to be able to iterate through a custom object.
Here a code example. Simple one.
If you have a big class on your hands that stores all the data and you want to implement a more secure and encapsulated class you can implement a set of Wrappers and Iterators to iterate all of those edges of your class.
This code sample is a bit crazy. But it shows how you can separate iteration routine and the rest of class logic. Controlling complexity is a goal of software architecture. Patterns, SOLID and other instruments are there only cause of some bright minds wanted to show their jobs on that topic for other people to use it.
Iterators are neat concept separating STL algorithms and containers. Watching on Iterator pattern from another angle reveals different way to look at it's place in STL. Since Iterators usually implemented inside of containers they as glue connects two islands of the STL archipelago.
It's better to think about this relations as if there is a strictly established policy of data translation. Such high level of abstraction model let us see what the idea behind design of STL, as well as start to see other libraries on such level.
Iterate and prosper.