Boost logo

Boost Users :

Subject: Re: [Boost-users] Iterating over the output of a member function in std::for_each
From: Mathias Gaunard (mathias.gaunard_at_[hidden])
Date: 2012-03-01 14:44:47


On 03/01/2012 05:52 PM, Andrew Hundt wrote:
> I have a class with an accessor member function that I want to call and
> apply the result to a functor using std::for_each. I have a working
> version below that uses a for loop and for_each, but the for_each
> version is cryptic and cumbersome. Is there a way I can make the
> for_each version more concise?
>
> #if 0
> // for loop version:
> for(value_vector_type::iterator it = values.begin();
> it!=values.end(); it++){
> avg(it->getValue()); // I want to put this in a for_each loop
> }
> #else
> // bind version:
> std::for_each(values.begin(), values.end(), // iterate over all
> values
> boost::bind(
> boost::mem_fn(&average_type::operator()), // attach the
> averaging functor to the output of the getvalue call
> &avg,
> boost::bind(
> boost::mem_fn(&value_wrapper_type::getValue), // bind the
> getValue call to each element in values
> _1
> )
> )
> );
> #endif

for_each(values.begin(), values.end(),
   bind(avg, bind(&value_wrapper_type::getValue, _1))
);


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net