I am attempting to interpret one of the answers to the question "calculate mean and standard deviation from a vector of samples in C++ using boost" found at Calculate mean and standard deviation from a vector of samples in C++ using Boost.
The question is about how to calculate mean and standard deviation for a vector containing samples using boost. The answer given by David Nehme is as follows.
Using accumulators is the way to compute means and standard deviations in boost.
accumulator_set<double, stats<tag::variance> > acc;
for_each(a_vec.begin(), a_vec.end(), bind<void>(ref(acc), _1));
cout << mean(acc) << endl;
cout << sqrt(variance(acc)) << endl;
Can someone explain this answer? It looks a bit like a magical incantation to me.
I do not know what "bind(ref(acc), _1)" means. From reading the Boost bind documentation found at www.boost.org/doc/libs/1_55_0/libs/bind/bind.html, I can assume that bind refers to boost::bind, ref refers to boost::ref, and _1 refers to the _1 placeholder object defined in boost\bind\placeholders.hpp.
However, I do not know how all that fits together. The documentation on boost::bind is a little vague. It talks about using boost::bind with functions but acc is a variable.
What exactly does the for_each line do?