17370845950

c++的std::accumulate算法怎么用 快速实现序列求和、求积【STL技巧】
std::accumulate是C++中通用归约算法,支持求和、求积、字符串拼接等;需注意初始值类型匹配、溢出风险及浮点精度问题;可结合lambda、函数对象及C++20视图实现灵活折叠。

std::accumulate 是 C++ 标准库中 头文件提供的通用归约算法,最常用的是求和,但远不止于此——它能灵活实现求和、求积、拼接字符串、找最大值、自定义折叠逻辑等。

基础用法:默认求和

对数值序列做累加,最简形式只需传入起始/结束迭代器和初始值:

#include 
#include 
#include 

std::vector v = {1, 2, 3, 4, 5}; int sum = std::accumulate(v.begin(), v.end(), 0); // 结果为 15

注意第三个参数是“初始值”,不是“起始下标”。它参与第一次运算:
0 + 1 → 11 + 2 → 3,…,最终得 15。
若初始值为 10,结果就是 25。

指定二元操作:实现求积、拼接等

第四个参数可传入任意接受两个参数的可调用对象(函数指针、lambda、函数对象),控制“如何合并”前一个结果与当前元素:

  • 求积
    int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies{});
    或用 lambda:
    int product = std::accumulate(v.begin(), v.end(), 1, [](int a, int b) { return a * b; });
  • 字符串拼接(需注意效率,适合小量):
    std::vector words = {"Hello", "World", "!"};
    std::string s = std::accumulate(words.begin(), words.end(), std::string{},
                                    [](const std::string& a, const std::string& b) {
                                        return a.empty() ? b : a + " " + b;
                                    }); // "Hello World !"
    

注意事项:类型匹配与溢出风险

初始值类型决定返回类型和中间计算类型。错误的类型会导致静默截断或溢出:

  • std::vector 求和,若写 std::accumulate(..., 0),0 是 int,可能溢出或隐式转换丢失精度;应写 0LL 0LL
  • 浮点数累加存在精度误差,顺序敏感(不满足结合律)。如需更高精度,可考虑 Kahan 求和,但 std::accumulate 本身不提供。
  • 空容器时,直接返回初始值,不会报错——这是安全的设计。

进阶技巧:配合其他 STL 组件使用

它可以自然融入现代 C++ 流式表达习惯:

  • std::transform_iterator(C++20 起为 std::views::transform)组合做映射后归约:
  • C++20 示例(需编译器支持):
    auto squares_sum = std::accumulate(
            v.begin(), v.end(), 0LL,
            [](long long acc, int x) { return acc + 1LL * x * x; }
        ); // 平方和
    
  • 对 map 的 value 求和(需先提取):
    std::map m = {{"a", 10}, {"b", 20}};
    int total = std::accumulate(m.begin(), m.end(), 0,
                                 [](int s, const auto& p) { return s + p.second; });