简体   繁体   English

为什么 C++ boost 库中的累加器有类似函数的接口?

[英]Why do the accumulators from the C++ boost library have a function-like interface?

In the boost library, we use accumulators like this:在 boost 库中,我们使用这样的累加器:

acc(1); // push things into acc
cout << max( acc ) << endl; // get its result

Why can't we define its interface like this:为什么我们不能像这样定义它的接口:

acc.push(1);
cout << acc.max() << endl;

So why do the accumulators from the boost library have a function-like interface?那么为什么 boost 库中的累加器有一个类似函数的接口呢? What is the advantage of it?它有什么好处?

Here's my guess这是我的猜测

The reason that operator () is used for pushing instead of push() is because acc(value) reads "accumulate another value" which sounds more natural than acc.push(value) which is "push a value to the accumulator" operator ()用于推送而不是push()的原因是因为acc(value)读取“累积另一个值” ,这听起来比acc.push(value)更自然,后者是“向累加器推送一个值”

Besides the accumulator can receive optional features like covariate or weight and that way probably the result looks and sounds better than acc.push(value, feature) .此外,累加器可以接收可选的特征,如协变量权重,这样结果看起来和听起来都比acc.push(value, feature)更好。 Some examples from the documentation : 文档中的一些示例:

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);

And then max(acc) is used instead of acc.max() because it allows extensibility while still maintaining consistency .然后使用max(acc)代替acc.max()因为它允许可扩展性同时仍然保持一致性 By writing a new function that receives an accumulator you'll be able to do other things to the accumulated list通过编写一个接收累加器的新函数,您将能够对累加列表执行其他操作

std::cout << "Mean:   " << mean(acc) << std::endl;
std::cout << "Moment: " << accumulators::moment<2>(acc) << std::endl;

If a member method was used then there'll only be a limited number of default operations like acc.mean() , acc.max() , the remaining things must be used like a function such as product(acc) , rolling_moment<2>(acc) , kurtosis(acc) , skewness(acc) ... which breaks consistency如果使用了成员方法,那么只有有限数量的默认操作,如acc.mean()acc.max() ,其余的必须像函数一样使用product(acc)rolling_moment<2>(acc) , kurtosis(acc) , skewness(acc) ...这会破坏一致性

Here are two reasons:这里有两个原因:

  1. You can pass them to algorithms, such as for_each :您可以将它们传递给算法,例如for_each

     acc = std::for_each(range.begin(), range.end(), acc);
  2. This gives all accumulators a uniform interface.这为所有累加器提供了统一的接口。 This is helpful to combine them eg.这有助于将它们结合起来,例如。 with accumulator_set .accumulator_set

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Boost :: spirit如何解析和调用类似c ++函数的表达式 - Boost::spirit how to parse and call c++ function-like expressions c 和 c++ 中类似函数的奇怪语法 - strange function-like syntax in c and c++ C ++:类似于复杂函数的宏定义 - C++: complex function-like macro definition 无法在C ++中创建类似函数的模板 - Unable to create a function-like template in C++ 如何使用C ++中的D3DCompile()定义类似函数的宏? - How to define a function-like macro with D3DCompile() from C++? C ++编写类似函数的宏 - C++ writing a proper function-like macro C ++ Boost Graph-为什么要有一个附加顶点? - C++ Boost Graph - Why do I have an additional vertex? 如何在 c++ UE4 中修复错误“类似函数的宏调用‘TEXT’的参数过多” - How to fix error 'too many arguments for function-like macro invocation 'TEXT'' in c++ UE4 在C ++中模拟__name__ == __main__是否导致错误“未定义类似函数的宏” - Emulating if __name__ == __main__ in c++ causes error “function-like macro is not defined” 有没有办法编写一个类似 C/C++ 函数的宏来测试是否定义了类似对象的宏并生成使用它的代码? - Is there a way to write a C/C++ function-like macro that tests if an object-like macro is defined and generates code uses it?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM