简体   繁体   English

如何初始化升压滚动 window 累加器?

[英]How to initialize a boost rolling window accumulator?

I would like to initialize a boost rolling window accumulator without having to do an assignment inside a function call.我想初始化一个升压滚动 window 累加器,而不必在 function 调用中进行分配。

This is what I see everyone do:这是我看到每个人所做的:

boost::accumulators::accumulator_set<double, boost::accumulators::stats<boost::accumulators::tag::rolling_mean>> acc(boost::accumulators::tag::rolling_window::window_size = 10);

How could I make the same accumulator without having the assignment inside the constructor call above?如果在上面的构造函数调用中没有赋值,我怎么能制作相同的累加器?

It's not an assignment, it's a named-argument idiom .这不是作业,而是命名参数成语 C++ doesn't have that, really, so this why it looks like an assignment: it's an Expression Template C++ 没有那个,真的,所以这就是为什么它看起来像一个赋值:它是一个表达式模板

You could of course figure out the type and use it, but that would not make any difference and just make it harder to use the library correctly:您当然可以找出类型并使用它,但这不会有任何区别,只会使正确使用库变得更加困难:

boost::parameter::aux::tagged_argument_list_of_1<
    boost::parameter::aux::tagged_argument<
        boost::accumulators::tag::rolling_window_size_<0>, const int>>
    init(10);

ba::accumulator_set<double, ba::stats<ba::tag::rolling_mean>> acc(init);

I don't know about you, but I prefer the named-argument expression.我不了解你,但我更喜欢命名参数表达式。


You can obviously write a helper function to remove the library details:您显然可以编写一个帮助程序 function 来删除库详细信息:

auto make_accum(int window) {
    return ba::accumulator_set<
        double,
        ba::stats<ba::tag::rolling_mean>> (ba::tag::rolling_window::window_size = window);
}

int main() {
    auto acc = make_accum(10);
}

This simply made the named argument into a positional argument using the knowledge about the statistics in in your set.这只是使用有关您集合中的统计信息的知识将命名参数变成位置参数。

If you're worried about generic code, just pass the expression as the initializer in generic cases.如果您担心泛型代码,只需在泛型情况下将表达式作为初始化程序传递。 That's how the library istelf is implemented:这就是库 istelf 的实现方式:

template <typename Stats, typename... Init> auto generic_accum(Init const&... init) {
    return ba::accumulator_set<double, Stats> (init...);
}

Demo All 3 Approaches演示所有 3 种方法

Live On Coliru住在科利鲁

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/statistics/rolling_mean.hpp>

namespace ba = boost::accumulators;

template <typename Stats, typename... Init> auto generic_accum(Init const&... init) {
    return ba::accumulator_set<double, Stats> (init...);
}

auto make_accum(int window) {
    return ba::accumulator_set<
        double,
        ba::stats<ba::tag::rolling_mean>> (ba::tag::rolling_window::window_size = window);
}

int main() {
    {
        boost::parameter::aux::tagged_argument_list_of_1<
            boost::parameter::aux::tagged_argument<
            boost::accumulators::tag::rolling_window_size_<0>, const int>>
            init(10);

        ba::accumulator_set<double, ba::stats<ba::tag::rolling_mean>>
            acc(init);
    }

    {
        auto acc = make_accum(10);
    }

    {
        auto acc = generic_accum<ba::stats<ba::tag::rolling_mean>>(ba::tag::rolling_window::window_size = 10);
    }
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM