std::accumulate
來自 cppreference.com
定義於標頭檔案 <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 和範圍 [
first,
last)
中元素的和。
1) 將累加器 acc (型別為
T
) 初始化為初始值 init,然後對於範圍 [
first,
last)
中的每個迭代器 i,按順序使用 acc = acc + *i(C++20 前)acc = std::move(acc) + *i(C++20 起) 修改它。2) 將累加器 acc (型別為
T
) 初始化為初始值 init,然後對於範圍 [
first,
last)
中的每個迭代器 i,按順序使用 acc = op(acc, *i)(C++20 前)acc = op(std::move(acc), *i)(C++20 起) 修改它。如果滿足以下任何條件,則行為是未定義的:
-
T
不是 CopyConstructible。 -
T
不是 CopyAssignable。 - op 修改
[
first,
last)
的任何元素。 - op 使
[
first,
last]
中的任何迭代器或子範圍失效。
目錄 |
[編輯] 引數
first, last | - | 定義要累加的元素 範圍 的迭代器對 |
init | - | 累加器的初始值 |
op | - | 將應用的二元操作函式物件。 函式的簽名應等效於以下內容 Ret fun(const Type1 &a, const Type2 &b); 簽名不需要有 const &。 |
型別要求 | ||
-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,但順序不同 (函式模板) |
(C++23) |
對一個範圍的元素進行左摺疊 (演算法函式物件) |