c++的std::accumulate算法怎么用 快速实现序列求和、求积【STL技巧】

admin 百科 12
std::accumulate是C++中通用归约算法,支持求和、求积、字符串拼接等;需注意初始值类型匹配、溢出风险及浮点精度问题;可结合lambda、函数对象及C++20视图实现灵活折叠。

c++的std::accumulate算法怎么用 快速实现序列求和、求积【STL技巧】-第1张图片-佛山资讯网

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

基础用法:默认求和

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

#include <numeric>
#include <vector>
#include <iostream>
<p>std::vector<int> 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<int>{});

    登录后复制

    或用 lambda:

    int product = std::accumulate(v.begin(), v.end(), 1, [](int a, int b) { return a * b; });

    登录后复制

  • 字符串拼接(需注意效率,适合小量):

    std::vector<std::string> 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 !"
    

    登录后复制

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

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

标签: word c++ ios stream 标准库 隐式转换

发布评论 0条评论)

还木有评论哦,快来抢沙发吧~