The boost docs give this as an example of how to use boost::accumulate:
// The data for which we wish to calculate statistical properties:
std::vector< double > data( /* stuff */ );
// The accumulator set which will calculate the properties for us:
accumulator_set< double, features< tag::tail<left> > > acc(
tag::tail<left>::cache_size = 4 );
// Use std::for_each to accumulate the statistical properties:
std::for_each( data.begin(), data.end(), bind<void>( ref(acc), _1 ) );
Is there a more elegant way to write this code with range based loops or lambdas in C++11/14 ?
There are two methods I can think of, as follows:
std::vector< double > data = {2.1, 2.2, 3.3, 4.4, 5.5};
accumulator_set< double, features< tag::tail<left> > > acc(tag::tail<left>::cache_size = 4);
for_each(data.begin(), data.end(), [&acc](double y){ acc(y); });
or
std::vector< double > data = {2.1, 2.2, 3.3, 4.4, 5.5};
accumulator_set< double, features< tag::tail<left> > > acc(tag::tail<left>::cache_size = 4);
for (auto y : data)
acc(y);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With