Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

More elegant boost accumulation in C++11?

Tags:

c++

c++11

boost

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 ?

like image 369
Thomas Johnson Avatar asked Dec 19 '25 06:12

Thomas Johnson


1 Answers

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);
like image 50
pertre Avatar answered Dec 20 '25 19:12

pertre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!