名稱空間
變體
操作

std::accumulate

來自 cppreference.com
< cpp‎ | 演算法
 
 
演算法庫
有約束演算法與針對範圍的演算法 (C++20)
有約束的演算法,例如 ranges::copyranges::sort 等……
執行策略 (C++17)
排序及相關操作
劃分操作
排序操作
二分搜尋操作
(於已劃分範圍上)
集合操作(於已排序範圍上)
歸併操作(於已排序範圍上)
堆操作
最小/最大值操作
(C++11)
(C++17)
字典序比較操作
排列操作
C 庫
數值操作
accumulate
(C++17)
未初始化記憶體上的操作
 
 
定義於標頭檔案 <numeric>
template< class InputIt, class T >
T accumulate( InputIt first, InputIt last, T init );
(1) (C++20 起為 constexpr)
template< class InputIt, class T, class BinaryOp >
T accumulate( InputIt first, InputIt last, T init, BinaryOp op );
(2) (C++20 起為 constexpr)

計算給定值 init 和範圍 [firstlast) 中元素的和。

1) 將累加器 acc (型別為 T) 初始化為初始值 init,然後對於範圍 [firstlast) 中的每個迭代器 i,按順序使用 acc = acc + *i(C++20 前)acc = std::move(acc) + *i(C++20 起) 修改它。
2) 將累加器 acc (型別為 T) 初始化為初始值 init,然後對於範圍 [firstlast) 中的每個迭代器 i,按順序使用 acc = op(acc, *i)(C++20 前)acc = op(std::move(acc), *i)(C++20 起) 修改它。

如果滿足以下任何條件,則行為是未定義的:

  • T 不是 CopyConstructible
  • T 不是 CopyAssignable
  • op 修改 [firstlast) 的任何元素。
  • op 使 [firstlast] 中的任何迭代器或子範圍失效。

目錄

[編輯] 引數

first, last - 定義要累加的元素 範圍 的迭代器對
init - 累加器的初始值
op - 將應用的二元操作函式物件。

函式的簽名應等效於以下內容

 Ret fun(const Type1 &a, const Type2 &b);

簽名不需要有 const &
型別  Type1 必須能使型別為 T 的物件隱式轉換為  Type1。型別  Type2 必須能使型別為 InputIt 的物件被解引用,然後隱式轉換為  Type2。型別 Ret 必須能使型別為 T 的物件被賦予型別為 Ret 的值。

型別要求
-
InputIt 必須滿足 LegacyInputIterator 的要求。

[編輯] 返回值

所有修改後的 acc

[編輯] 可能的實現

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

[編輯] 注意

std::accumulate 執行左 摺疊。為了執行右摺疊,必須反轉二元運算子的引數順序,並使用反向迭代器。

如果任由型別推導,op 將對與 init 相同型別的值進行操作,這可能導致迭代器元素的不必要型別轉換。例如,當 v 的型別為 std::vector<double> 時,std::accumulate(v.begin(), v.end(), 0) 可能不會得到期望的結果。

[編輯] 示例

#include <functional>
#include <iostream>
#include <numeric>
#include <string>
#include <vector>
 
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]), // start with first element
                                    dash_fold);
 
    // Right fold using reverse iterators
    std::string rs = std::accumulate(std::next(v.rbegin()), v.rend(),
                                     std::to_string(v.back()), // start with last element
                                     dash_fold);
 
    std::cout << "sum: " << sum << '\n'
              << "product: " << product << '\n'
              << "dash-separated string: " << s << '\n'
              << "dash-separated string (right-folded): " << rs << '\n';
}

輸出

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

[編輯] 缺陷報告

下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。

缺陷報告 應用於 釋出時的行為 正確的行為
LWG 242 C++98 op 不得有副作用 它不能修改所涉及的範圍

[編輯] 參閱

計算一個範圍內相鄰元素之間的差值
(函式模板) [編輯]
計算兩個元素範圍的內積
(函式模板) [編輯]
計算一個範圍元素的部分和
(函式模板) [編輯]
(C++17)
類似於 std::accumulate,但順序不同
(函式模板) [編輯]
對一個範圍的元素進行左摺疊
(演算法函式物件)[編輯]