std::accumulate
来自 cppreference.cn
定义于头文件 <numeric> |
||
template< class InputIt, class T > T accumulate( InputIt first, InputIt last, T init ); |
(1) | (constexpr since C++20) |
template< class InputIt, class T, class BinaryOp > T accumulate( InputIt first, InputIt last, T init, BinaryOp op ); |
(2) | (constexpr since C++20) |
计算给定值 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++ 标准。
DR | 应用于 | 已发布行为 | 正确行为 |
---|---|---|---|
LWG 242 | C++98 | op 可能没有副作用 | 它不能修改所涉及的范围 |
[编辑] 参见
计算范围内相邻元素之间的差值 (函数模板) | |
计算两个元素范围的内积 (函数模板) | |
计算元素范围的部分和 (函数模板) | |
(C++17) |
类似于 std::accumulate,但顺序不同 (函数模板) |
(C++23) |
左折叠元素范围 (算法函数对象) |