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

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 → 1,1 + 2 → 3,…,最终得 15。
若初始值为 10,结果就是 25。
第四个参数可传入任意接受两个参数的可调用对象(函数指针、lambda、函数对象),控制“如何合并”前一个结果与当前元素:
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>{});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 !"
初始值类型决定返回类型和中间计算类型。错误的类型会导致静默截断或溢出:
立即学习“C++免费学习笔记(深入)”;
std::vector<long long></long> 求和,若写 std::accumulate(..., 0),0 是 int,可能溢出或隐式转换丢失精度;应写 0LL 或 0LL。std::accumulate 本身不提供。它可以自然融入现代 C++ 流式表达习惯:
std::transform_iterator(C++20 起为 std::views::transform)组合做映射后归约:auto squares_sum = std::accumulate(
v.begin(), v.end(), 0LL,
[](long long acc, int x) { return acc + 1LL * x * x; }
); // 平方和
std::map<std::string, int> m = {{"a", 10}, {"b", 20}};
int total = std::accumulate(m.begin(), m.end(), 0,
[](int s, const auto& p) { return s + p.second; });
以上就是c++++的std::accumulate算法怎么用 快速实现序列求和、求积【STL技巧】的详细内容,更多请关注php中文网其它相关文章!
c++怎么学习?c++怎么入门?c++在哪学?c++怎么学才快?不用担心,这里为大家提供了c++速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号