Many people use containers with begin and end methods.
I do not love for loops for iterating over containers, it is very ugly and not maintanable.
The past solution was to use std::for_each + functors.
It can be good, but thus you need to define a global helper class for a small thing. So many used to create highly extendable class that do anything.
And of course there is std::bind1st, std::bind2nd, std::mem_fun, std::ptr_fun ugliness. Do not use it !!
And there is boost::bind the great thing, and boost::lambda the thing. I love the both. They help you to write a short code which does what it meant to be and you have a local function.
But still you cannot do all using boost::lambda , perhaps it compiles slow and produces not optimizable code.
The only solution for optimizing is to use loop.. Not now !
Boost has Boost.Foreach library which helps you to iterate in a very neat way over the container/built in array type/string literal. All what you need. But it is Boost and it is macro, and it does not provide an optimizable code even if it is better than Boost.Lambda.
The story begins with discovery of for each in VC8. VC8 has C++/CLI which must have for each to iterate over System::Array or any type derived from System::IEnumerator. But in fact for each works on Native programs too.
Even more it produces the same code like for. And it can be used with Standard Library !
Wow !
The happiness is good but it has disadvantage, it cannot get reference in the item type.
After seeking for a solution for this, and for full replacement to BOOST_FOREACH which can use Boost.Range for extandability. I have found a solution.
In Boost – Ticket #1295 there is my solution for foreach which uses advantage of VC 8 and later.
Update:
foreach for poor, it is implementation of for each which passes almost all boost.foreach tests except noncopyable, rvalue_const, rvalue_nonconst.
Enjoy
Update2:
If you lucky user of VC 9.0 or even VC 9.0 SP1you may have paid attention that BOOST_FOREACH produces exactly the same code like ugly hand-writed for loop, and i mean the for loop with end caching which many forget to do !
So there is no need for MS specific for each at all. Just use VC 9.0 SP1