|
Boost Users : |
Subject: [Boost-users] Iterating over the output of a member function in std::for_each
From: Andrew Hundt (athundt_at_[hidden])
Date: 2012-03-01 11:52:42
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
I also have the question posted in stack overflow, with a full
implementation:
http://stackoverflow.com/questions/9507830/iterating-over-the-output-of-a-member-function-in-stdfor-each
Cheers!
Andrew Hundt
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