Jump to content
  • Advertisement
Sign in to follow this  
DarkCybo1

Is it unneccesary to replace most container-loops with std::for_each?

This topic is 4603 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Such as, leaving a function object class to update an std::list of objects, instead of writing an iterator loop and have the objects update themselves or have the app state doing it?

Share this post


Link to post
Share on other sites
Advertisement
It's not necessary, of course, but some might say it makes the code more readable/self-evident.

It's also a lot quicker to write the code, if you use lambda functors such as those Boost.Lambda provides.

Share this post


Link to post
Share on other sites
I wouldn't move the body of a for-loop into a function or functor solely for the purpose of replacing the for-loop with for_each(), but it seems like a good idea to replace
    for ( iterator i = c.begin(); i != c.end(); ++i )
{
foo( *i );
}
with
    for_each( c.begin(), c.end(), foo ); 
since that is exactly what for_each() does.

Share this post


Link to post
Share on other sites
Right. If you already have a function for it, then use that, but otherwise it's not a hugely useful refactoring most of the time.

On the other hand, you should probably use the erase-remove idiom (with std::remove or std::remove_if) if you need that functionality, rather than trying to mess around with individual element erasures - since apparently everyone gets it wrong most of the time, and it's therefore such a popular topic around here ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!