C++におけるaccumulateの使い方

17276 ワード

std::accumulate
ヘッダファイルに定義
template< class InputIt, class T >
constexpr T accumulate( InputIt first, InputIt last, T init );
template< class InputIt, class T, class BinaryOperation >
constexpr T accumulate( InputIt first, InputIt last, T init, BinaryOperation op );

与えられた値initと与えられた範囲[first,last)の要素の和を計算します.第1のバージョンはoperator+、第2のバージョンは二元関数opで要素を加算し、std::moveを左側の演算数(C++20から)に適用します.
opは、エンド反復器を含む範囲に関連する任意の反復器を不正化せず、その範囲に関連する任意の要素および*lastを変更しない必要があります.(C++11から)
パラメータ
first,last−要件和の要素範囲init−和の初期値op−が使用される二元関数オブジェクト.現在の蓄積値a(initに初期化)と現在の要素bの二元演算子を受信する.この関数の署名は、次のように等価です.
 Ret fun(const Type1 &a, const Type2 &b);
          。        a (     init )      b       。

この関数の署名は、次のように等価です.
Ret fun(const Type1 &a, const Type2 &b);
署名にはconst&は必要ありません.タイプType 1は、TタイプのオブジェクトがType 1に暗黙的に変換できるようにしなければならない.タイプType 2は、InputItタイプのオブジェクトがリファレンス解除後にType 2に暗黙的に変換できるようにする必要があります.タイプRetは、TタイプオブジェクトにRetタイプ値を付与できるようにしなければならない.​
タイプ要求InputItは、レガシー入力反復器(LegacyInputIterator)の要求を満たす必要があります.Tはコピー可能付与(CopyAssignable)とコピー可能構造(CopyConstructible)の要件を満たさなければならない.
戻り値
  • 所与の値と所与の範囲内の要素の和.
  • 所与の範囲がop上で左に折り畳む結果
  • 注釈
    std::accumulateを左に折ります.右に折りたたむには、二元演算子のパラメータ順序を逆転し、逆反復器を使用する必要があります.
    実現可能
    バージョン1
    template<class InputIt, class T>
    constexpr // C++20  
    T accumulate(InputIt first, InputIt last, T init)
    {
        for (; first != last; ++first) {
            init = std::move(init) + *first; // C++20    std::move
        }
        return init;
    }
    

    バージョン2
    template<class InputIt, class T, class BinaryOperation>
    constexpr // C++20  
    T accumulate(InputIt first, InputIt last, T init, 
                 BinaryOperation op)
    {
        for (; first != last; ++first) {
            init = op(std::move(init), *first); // C++20    std::move
        }
        return init;
    }
    

    #include 
    #include 
    #include 
    #include 
    #include 
     
    int main()
    {
        std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
     
        int sum = std::accumulate(v.begin(), v.end(), 0);
     
        int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>());
     
        auto dash_fold = [](std::string a, int b) {
                             return std::move(a) + '-' + std::to_string(b);
                         };
     
        std::string s = std::accumulate(std::next(v.begin()), v.end(),
                                        std::to_string(v[0]), //       
                                        dash_fold);
     
        //           
        std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),
                                         std::to_string(v.back()), //       
                                         dash_fold);
     
        std::cout << "sum: " << sum << '
    '
    << "product: " << product << '
    '
    << "dash-separated string: " << s << '
    '
    << "dash-separated string (right-folded): " << rs << '
    '
    ; }

    しゅつりょく
    sum: 55
    product: 3628800
    dash-separated string: 1-2-3-4-5-6-7-8-9-10
    dash-separated string (right-folded): 10-9-8-7-6-5-4-3-2-1