在 STL 地图中存储提升accumulator_set

Store boost accumulator_set in STL map

本文关键字:accumulator set 存储 STL 地图      更新时间:2023-10-16

我想在 stl 映射中存储多个提升accumulataor_set。

我读到的所有示例都使用 accumulator_set 作为局部变量:

accumulator_set<int, stats<tag::rolling_mean> > acc(tag::rolling_window::window_size = 5);
acc(1);
acc(2);
acc(3);
cout << rolling_mean(acc);
但是我想将

accumulator_set存储在地图中。我试图编写这样的代码,但我卡住了:

map<int, accumulator_set<long, stats<tag::rolling_mean> > > avg;
void update(int id, long data){
    if(avg.count(id)==0){
        //key doesn't exist in map
        avg[id]= ;// How to create acc as in above example and store it in map?
    }
    accumulator_set<long, stats<tag::rolling_mean> > &acc = avg[id];
    acc(data);
}
void read(int id){
    cout << rolling_mean(avg[id]) ;
}

如何创建上例中的accumulator_set并将其(引用或对象)存储在map中?

您可以使用 insert():

typedef accumulator_set<long, stats<tag::rolling_mean> > acc_set_t;
if(avg.count(id)==0){
    //key doesn't exist in map
    avg.insert( std::make_pair(id, acc_set_t(/*init parameters here*/) ));
}