【发布时间】:2014-03-10 04:38:30
【问题描述】:
我正在尝试解释问题“使用 boost 从 C++ 中的样本向量计算均值和标准差”的答案之一,该问题位于 Calculate mean and standard deviation from a vector of samples in C++ using Boost。
问题是关于如何使用 boost 计算包含样本的向量的均值和标准差。 David Nehme给出的答案如下。
在 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;
有人可以解释这个答案吗?这对我来说有点像魔法咒语。
我不知道“bind(ref(acc), _1)”是什么意思。通过阅读www.boost.org/doc/libs/1_55_0/libs/bind/bind.html 中的 Boost 绑定文档,我可以假设 bind 指的是 boost::bind,ref 指的是 boost::ref,_1 指的是 boost\bind\placeholders.hpp 中定义的 _1 占位符对象。
但是,我不知道这一切是如何结合在一起的。 boost::bind 的文档有点模糊。它谈到了将 boost::bind 与函数一起使用,但 acc 是一个变量。
for_each 行具体是做什么的?
【问题讨论】: