命名空间
变体
操作

std::accumulate

来自 cppreference.cn
< 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,但顺序不同
(函数模板) [编辑]
对一个范围的元素进行左折叠
(算法函数对象)[编辑]