Prefer algorithms to hand-written loops?
- by FredOverflow
Which of the following to you find more readable? The hand-written loop:
for (std::vector<Foo>::const_iterator it = vec.begin(); it != vec.end(); ++it)
{
bar.process(*it);
}
Or the algorithm invocation:
#include <algorithm>
#include <functional>
std::for_each(vec.begin(), vec.end(),
std::bind1st(std::mem_fun_ref(&Bar::process), bar));
I wonder if std::for_each is really worth it, given such a simple example already requires so much code.
What are your thoughts on this matter?