将boost accumulator_set存储在STL映射中

时间:2012-04-23 01:50:28

标签: c++ boost map

我想在stl地图中存储多个boost 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并将其存储(参考或对象)?

1 个答案:

答案 0 :(得分:2)

您可以使用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*/) ));
}