How seriously do we take Scott Meyer's recommendation "Prefer algorithm calls 
to hand-written loops"? (Item 43 of Effective STL)

For example, these two functions have identical functionality. Which one is 
prefereable? (and yes, I chose a deliberately simple example...)

Angus

void func1(Buffer const & buffer, PreviewLoader & ploader)
{
        Buffer::inset_iterator it  = buffer.inset_const_iterator_begin();
        Buffer::inset_iterator end = buffer.inset_const_iterator_end();

        for (; it != end; ++it) {
                if ((*it)->lyxCode() == Inset::MATH_CODE) {
                        (*it)->generatePreview(ploader);
                }
        }
}

namespace {

struct FindMathInset {
        bool operator()(Inset const * inset)
        {
                return inset && inset->lyxCode() == Inset::MATH_CODE;
        }
};
 
} // namespace anon

void func2(Buffer const & buffer, PreviewLoader & ploader)
{
        Buffer::inset_iterator it  = buffer.inset_const_iterator_begin();
        Buffer::inset_iterator end = buffer.inset_const_iterator_end();

        while (it = std::find_if(it, end, FindMathInset()) != end) {
                (*it++)->generatePreview(ploader);
        }
}

Reply via email to