Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 60045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:06:02+00:00 2026-05-10T18:06:02+00:00

I have a collection of elements that I need to operate over, calling member

  • 0

I have a collection of elements that I need to operate over, calling member functions on the collection:

std::vector<MyType> v; ... // vector is populated 

For calling functions with no arguments it’s pretty straight-forward:

std::for_each(v.begin(), v.end(), std::mem_fun(&MyType::myfunc)); 

A similar thing can be done if there’s one argument to the function I wish to call.

My problem is that I want to call a function on elements in the vector if it meets some condition. std::find_if returns an iterator to the first element meeting the conditions of the predicate.

std::vector<MyType>::iterator it  =        std::find_if(v.begin(), v.end(), MyPred()); 

I wish to find all elements meeting the predicate and operate over them.

I’ve been looking at the STL algorithms for a ‘find_all‘ or ‘do_if‘ equivalent, or a way I can do this with the existing STL (such that I only need to iterate once), rather than rolling my own or simply do a standard iteration using a for loop and comparisons.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T18:06:02+00:00Added an answer on May 10, 2026 at 6:06 pm

    Boost Lambda makes this easy.

    #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> #include <boost/lambda/if.hpp>  std::for_each( v.begin(), v.end(),                 if_( MyPred() )[ std::mem_fun(&MyType::myfunc) ]               ); 

    You could even do away with defining MyPred(), if it is simple. This is where lambda really shines. E.g., if MyPred meant ‘is divisible by 2’:

    std::for_each( v.begin(), v.end(),                 if_( _1 % 2 == 0 )[ std::mem_fun( &MyType::myfunc ) ]              ); 

    Update: Doing this with the C++0x lambda syntax is also very nice (continuing with the predicate as modulo 2):

    std::for_each( v.begin(), v.end(),                [](MyType& mt ) mutable                {                  if( mt % 2 == 0)                  {                     mt.myfunc();                   }                } ); 

    At first glance this looks like a step backwards from boost::lambda syntax, however, it is better because more complex functor logic is trivial to implement with c++0x syntax… where anything very complicated in boost::lambda gets tricky quickly. Microsoft Visual Studio 2010 beta 2 currently implements this functionality.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a collection of elements in a std::vector that are sorted in a
I have a collection of HTML elements that is structured and hierarchical. When an
I have a Collection of type string that can contain any number of elements.
I have roughly 420,000 elements that I need to store easily in a Set
Writing Scala code, I regularly encounter cases where I have processor functions that operate
On my jsp page I have a loop that loops elements in a collection,
I use Java 6 Collecetions API. I need a collection that should have only
I have an an object (BlogPost) that contains an M:N collection of elements (Tags).
i have a collection of elements sorted by the elements' Name property. i need
I have List collection that is populated in specific order (the requirement is that,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.