日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

C++?Boost?Accumulators累加器詳細講解_C 語言

作者:無水先生 ? 更新時間: 2022-12-25 編程語言

Boost.Accumulators?

? ? ? ? Boost.Accumulators 提供了處理樣本的類。例如,您可以找到最大或最小的樣本,或者計算所有樣本的總和。雖然標準庫支持其中一些操作,但 Boost.Accumulators 還支持統計計算,例如均值和標準差。

? ? ? ? 該庫稱為 Boost.Accumulators,因為累加器是一個基本概念。累加器是一個容器,每次插入一個值時都會計算出一個新的結果。該值不一定存儲在累加器中。相反,累加器在輸入新值時不斷更新中間結果。

? ? ? ? Boost.Accumulators 包含三個部分:

  • 框架提供了庫的整體結構。它提供類 boost::accumulators::accumulator_set,它總是與 Boost.Accumulators 一起使用。雖然您需要了解這個類和框架中的其他一些類,但細節并不重要,除非您想開發自己的累加器。頭文件 boost/accumulators/accumulators.hpp 使您可以訪問 boost::accumulators::accumulator_set 和框架中的其他類。
  • Boost.Accumulators 提供了許多執行計算的累加器。一旦包含 boost/accumulators/statistics.hpp,您就可以訪問和使用所有這些累加器。
  • Boost.Accumulators 提供運算符,例如,將一個 std::complex 類型的復數與一個 int 值相乘或將兩個向量相加。頭文件 boost/accumulators/numeric/functional.hpp 定義了 std::complex、std::valarray 和 std::vector 的運算符。您不需要自己包含頭文件,因為它包含在累加器的頭文件中。但是,您必須定義宏 BOOST_NUMERIC_FUNCTIONAL_STD_COMPLEX_SUPPORT、BOOST_NUMERIC_FUNCTIONAL_STD_VALARRAY_SUPPORT 和 BOOST_NUMERIC_FUNCTIONAL_STD_VECTOR_SUPPORT 以使運算符可用。

? ? ? ? Boost.Accumulators 提供的所有類和函數都在 boost::accumulators 或嵌套命名空間中定義。例如,所有累加器都在 boost::accumulators::tag 中定義。

示例 58.1。使用 boost::accumulators::tag::count 計數

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
  accumulator_set<int, features<tag::count>> acc;
  acc(4);
  acc(-6);
  acc(9);
  std::cout << count(acc) << '\n';
}

Example?58.1?

? ? ? ? 示例 58.1 使用 boost::accumulators::tag::count,這是一個簡單的累加器,用于計算傳遞給它的值的數量。因此,由于傳遞了三個值,此示例將 3 寫入標準輸出。要使用累加器,您需要訪問類 boost::accumulators::accumulator_set,這是一個模板,它期望將要處理的值的類型作為其第一個參數。示例 58.1 將 int 作為第一個參數傳遞。

? ? ? ? 第二個參數指定要使用的累加器。您可以使用多個累加器。類名 boost::accumulators::accumulator_set 表示可以管理任意數量的累加器。

? ? ? ? 嚴格來說,您指定的是特征,而不是累加器。特征定義了應該計算什么。你決定什么,而不是如何。功能可以有不同的實現。實現是累加器。

? ? ? ? 示例 58.1 使用 boost::accumulators::tag::count 選擇一個計算值的累加器。如果存在多個可以計算值的累加器,Boost.Accumulators 會選擇默認的累加器。

? ? ? ? 請注意,您不能將特征直接傳遞給 boost::accumulators::accumulator_set。您需要使用 boost::accumulators::features。

? ? ? ? boost::accumulators::accumulator_set 類型的對象可以像函數一樣使用。可以通過調用 operator() 來傳遞值。它們會立即得到處理。傳遞的值必須與作為第一個模板參數傳遞給 boost::accumulators::accumulator_set 的類型相同。

? ? ? ? 對于每個特征,都有一個同名的提取器。提取器接收累加器的當前結果。示例 58.1 使用提取器 boost::accumulators::count()。傳遞的唯一參數是 acc。 boost::accumulators::count() 返回 3。

? ? ? ? 示例 58.2。使用均值和方差

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
  accumulator_set<double, features<tag::mean, tag::variance>> acc;
  acc(8);
  acc(9);
  acc(10);
  acc(11);
  acc(12);
  std::cout << mean(acc) << '\n';
  std::cout << variance(acc) << '\n';
}

Example?58.2?

? ? ? ? 示例 58.2 使用兩個特征 boost::accumulators::tag::mean 和 boost::accumulators::tag::variance 來計算五個值的均值和方差。該示例將 10 和 2 寫入標準輸出。

? ? ? ? 方差為 2,因為 Boost.Accumulators 為五個值中的每一個分配了 0.2 的權重。使用 boost::accumulators::tag::variance 選擇的累加器使用權重。如果未明確設置權重,則所有值都具有相同的權重。

? ? ? ? 示例 58.3。計算加權方差

#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include <iostream>
using namespace boost::accumulators;
int main()
{
  accumulator_set<double, features<tag::mean, tag::variance>, int> acc;
  acc(8, weight = 1);
  acc(9, weight = 1);
  acc(10, weight = 4);
  acc(11, weight = 1);
  acc(12, weight = 1);
  std::cout << mean(acc) << '\n';
  std::cout << variance(acc) << '\n';
}

Example?58.3?

? ? ? ? 示例 58.3 將 int 作為第三個模板參數傳遞給 boost::accumulators::accumulator_set。此參數指定權重的數據類型。在此示例中,權重分配給每個值。

? ? ? ? Boost.Accumulators 使用 Boost.Parameter 以名稱/值對的形式傳遞附加參數,例如權重。權重的參數名稱是權重。您可以將參數視為變量并分配一個值。名稱/值對作為附加參數在每個值之后傳遞給累加器。

? ? ? ? 在示例 58.3 中,值 10 的權重為 4,而所有其他值的權重為 1。均值仍然是 10,因為權重對均值無關緊要。但是,方差現在是 1.25。與前面的示例相比,它有所減少,因為中間值的權重高于其他值。

? ? ? ? Boost.Accumulators 提供了更多的累加器。它們的用法與本章介紹的累加器相同。該庫的文檔包含對所有可用累加器的概述。

原文鏈接:https://yamagota.blog.csdn.net/article/details/128032406

欄目分類
最近更新