Prefer algorithms to hand-written loops?

Posted by FredOverflow on Programmers See other posts from Programmers or by FredOverflow
Published on 2011-01-15T10:32:34Z Indexed on 2011/01/15 10:59 UTC
Read the original article Hit count: 337

Filed under:
|

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?

© Programmers or respective owner

Related posts about c++

Related posts about algorithms