在 C++ 中没有找到重置累加器的“升压”方法,我遇到了一段似乎重置升压累加器的代码。但不明白它是如何实现的。代码如下 -
#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 行是做什么的?“清除”如何设法重置累加器?此外,是否有我缺少的标准提升方式以及实现上述代码所做的任何其他方式。