1 #include <iostream> 2 #include <boost/accumulators/accumulators.hpp> 3 #include <boost/accumulators/statistics/stats.hpp> 4 #include <boost/accumulators/statistics/mean.hpp> 5 #include <boost/accumulators/statistics/moment.hpp> 6 using namespace boost::accumulators; 7 8 int main() 9 { 10 // Define an accumulator set for calculating the mean and the 11 // 2nd moment ... 12 accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;//建立對象 13 14 // push in some data ... 15 acc(1.2); 16 acc(2.3); 17 acc(3.4); 18 acc(4.5); 19 20 // Display the results ... 21 std::cout << "Mean: " << mean(acc) << std::endl; 22 std::cout << "Moment: " << accumulators::moment<2>(acc) << std::endl; 23 24 return 0; 25 }
運行結果:html
Mean: 2.85 Moment: 9.635
計算步驟:
1 acc(1.2);// 1.2 2 acc(2.3);// + 2.3 3 acc(3.4);// + 3.4 4 acc(4.5);// + 4.5 5 // -------- 6 // 11.4/4=2.85(mean) 7 acc(1.2);// 1.2^2 8 acc(2.3);// + 2.3^2 9 acc(3.4);// + 3.4^2 10 acc(4.5);// + 4.5^2 11 // -------- 12 // 38.54/4=2.85(moment)
總結:accumulators用於增量統計的庫,也是一個用於增量計算的可擴展的累加器框架。還有不少有用的函數,能夠參見reference。
Reference:http://www.boost.org/doc/libs/1_64_0/doc/html/accumulators/user_s_guide.html#accumulators.user_s_guide.the_accumulators_framework.extending_the_accumulators_framework