在 boost 库中,我们使用这样的累加器:
acc(1); // push things into acc
cout << max( acc ) << endl; // get its result
为什么我们不能像这样定义它的接口:
acc.push(1);
cout << acc.max() << endl;
那么为什么 boost 库中的累加器有一个类似函数的接口呢?它有什么好处?
在 boost 库中,我们使用这样的累加器:
acc(1); // push things into acc
cout << max( acc ) << endl; // get its result
为什么我们不能像这样定义它的接口:
acc.push(1);
cout << acc.max() << endl;
那么为什么 boost 库中的累加器有一个类似函数的接口呢?它有什么好处?
这是我的猜测
运算符()
用于推送而不是的push()
原因是因为acc(value)
读取“累积另一个值”acc.push(value)
这听起来比“向累加器推送一个值”更自然
除了累加器可以接收可选的特征,比如协变量或权重,这样结果看起来和听起来都比acc.push(value, feature)
. 文档中的一些示例:
acc( 1.2, covariate1 = 12 );
acc( 2.3, covariate1 = -23 );
acc( 3.4, covariate1 = 34 );
acc( 4.5, covariate1 = -45 );
acc(1, weight = 2); // 1 * 2
acc(2, weight = 4); // 2 * 4
acc(3, weight = 6); // + 3 * 6
acc(1, weight = 2, covariate1 = 3);
acc(0, weight = 4, covariate1 = 4);
acc(2, weight = 9, covariate1 = 8);
使用thenmax(acc)
而不是,acc.max()
因为它允许可扩展性,同时仍保持一致性。通过编写一个接收累加器的新函数,您将能够对累加列表执行其他操作
std::cout << "Mean: " << mean(acc) << std::endl;
std::cout << "Moment: " << accumulators::moment<2>(acc) << std::endl;
如果使用了成员方法,那么只有有限数量的默认操作,例如acc.mean()
, acc.max()
,其余的必须像函数一样使用,例如product(acc)
, rolling_moment<2>(acc)
, kurtosis(acc)
, skewness(acc)
...