使用空Boost累加器

Using empty Boost accumulators

本文关键字:累加器 Boost      更新时间:2023-10-16

我很好奇,从这个代码片段中获得的平均值是多少?累加器为空。

boost::accumulators::accumulator_set<
    int,
    boost::accumulators::features<boost::accumulators::tag::mean>
> Accumulator;
int Mean = boost::accumulators::mean(Accumulator);

当我测试它时,平均值是非零的。有没有什么方法能告诉我平均值是对一个空数据集取的?为什么"平均值"的结果值不为零?

我在文档中寻找累加器库,但无法找到这个问题的答案。

任何值都是空值集的有效平均值。也就是说,x * 0 = 0对任何x都成立。

您可以将count特性添加到accumulator_set并查询它是否为0。

您不需要添加count特性,因为mean累加器是基于countsum累加器

from boost User's Guide:

均值取决于累加器和累加器…平均累加器的结果仅仅是总和累加器的结果除以计数累加器的结果。

所以你只需要验证count大于0:

bool isEmpty = boost::accumulators::count(Accumulator) == 0;