|
Boost Users : |
Subject: Re: [Boost-users] Iterating over the output of a member function in std::for_each
From: Andrew Hundt (athundt_at_[hidden])
Date: 2012-03-01 18:44:38
In my specific case there actually ended up being one small
modification I had to make because bind copied the object:
// bind version:
for_each(values.begin(), values.end(),
bind(boost::ref(avg), bind(&value_wrapper_type::getValue, _1))
);
Cheers!
Andrew Hundt
On Thu, Mar 1, 2012 at 6:13 PM, Andrew Hundt <athundt_at_[hidden]> wrote:
> Aha! I had to add:
>
> typedef void result_type;
>
> to the body of the avg object. Then your example:
>
> for_each(values.begin(), values.end(),
> bind(avg, bind(&value_wrapper_type::getValue, _1))
> );
>
> Worked perfectly! Thank you very much for the help Mathias, it is
> greatly appreciated.
>
> Cheers!
> Andrew Hundt
>
>
>
> On Thu, Mar 1, 2012 at 2:44 PM, Mathias Gaunard
> <mathias.gaunard_at_[hidden]> wrote:
>> 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 mailing list
>> Boost-users_at_[hidden]
>> http://lists.boost.org/mailman/listinfo.cgi/boost-users
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