重置升压累加器c ++ [英] reset boost accumulator c++

查看:98
本文介绍了重置升压累加器c ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++中没有找到重置累加器的提升"方式,我遇到了一段似乎重置了升压累加器的代码.但是不知道它是如何实现的.代码如下-

Having not found a 'boost' way of resetting an accumulator in C++, I came across a piece of code that seems to reset a boost accumulator. But don't understand how it is achieving it. The code is as below -

#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
using namespace boost::accumulators;

template< typename DEFAULT_INITIALIZABLE >
inline void clear( DEFAULT_INITIALIZABLE& object )
{
        object.DEFAULT_INITIALIZABLE::~DEFAULT_INITIALIZABLE() ;
        ::new ( boost::addressof(object) ) DEFAULT_INITIALIZABLE() ;
}

int main()
{
    // Define an accumulator set for calculating the mean 
    accumulator_set<double, stats<tag::mean> > acc;

    float tmp = 1.2;
    // push in some data ...
    acc(tmp);
    acc(2.3);
    acc(3.4);
    acc(4.5);

    // Display the results ...
    std::cout << "Mean:   " << mean(acc) << std::endl;
    // clear the accumulator
    clear(acc);
    std::cout << "Mean:   " << mean(acc) << std::endl;
    // push new elements again
    acc(1.2);
    acc(2.3);
    acc(3.4);
    acc(4.5);
    std::cout << "Mean:   " << mean(acc) << std::endl;

    return 0;
}

第7到12行做什么?清除"如何设法重置累加器?另外,有没有一种我缺少的标准提升方法以及实现上述代码已完成的其他任何方法.

what do lines 7 to 12 do ? How does 'clear' manage to reset the accumulator ? Also, is there a standard boost way that I am missing and any other ways of achieving what the code above has done.

推荐答案

要重新初始化对象,只需执行以下操作:

To re-initialize an object just do:

acc = {};

它的作用是 {} 创建一个默认初始化的临时对象,该对象被分配给 acc .

What it does is {} creates a default-initialized temporary object which gets assigned to acc.

这篇关于重置升压累加器c ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆