std::accumulate用于累加容器元素,需包含头文件、指定左闭右开区间及初始值,类型决定返回类型,支持自定义二元操作但需注意参数顺序与性能陷阱。

std::accumulate 基本用法:对 vector 求和
直接调用 std::accumulate 即可,它默认用 + 运算符累加。注意必须传入初始值(哪怕为 0),否则无法推导返回类型。
- 头文件是
<numeric></numeric>,不是<algorithm></algorithm> - 第三个参数是初始值,类型决定返回类型:传
0得int,传0.0得double - 迭代器范围是左闭右开:
[first, last),别把end()写成end() - 1
#include <numeric>
#include <vector>
#include <iostream>
<p>int main() {
std::vector<int> v = {1, 2, 3, 4, 5};
int sum = std::accumulate(v.begin(), v.end(), 0); // → 15
std::cout << sum << "\n";
}处理浮点数或自定义类型时的精度与初始化陷阱
用 0 初始化 vector<double></double> 会导致整型提升,可能引发隐式转换警告或精度截断;更严重的是,若容器为空,返回值完全取决于你给的初始值——它不会“自动适配”。
- 对
vector<double></double>,务必用0.0或0.0f初始化 - 对自定义类,需确保该类支持
operator+,且初始值类型能与元素类型运算 - 空容器时
std::accumulate直接返回初始值,不报错也不提示
std::vector<double> d = {1.1, 2.2, 3.3};
double s1 = std::accumulate(d.begin(), d.end(), 0); // 警告:int→double,不推荐
double s2 = std::accumulate(d.begin(), d.end(), 0.0); // 正确用二元函数替代默认 +:实现乘积、字符串拼接等
std::accumulate 第四个参数可传入任意二元可调用对象,不只是加法。但要注意:这个函数接收「当前累加结果」和「下一个元素」,顺序不能反。
- 求乘积:用
std::multiplies{}或 lambda[](auto a, auto b) { return a * b; } - 字符串拼接:初始值必须是
std::string{},否则char*+std::string编译失败 - lambda 捕获需谨慎:若在循环中反复调用,避免意外捕获外部变量
std::vector<int> nums = {2, 3, 4};
int product = std::accumulate(nums.begin(), nums.end(), 1, std::multiplies<>{}); // → 24
<p>std::vector<std::string> words = {"hello", "world"};
std::string joined = std::accumulate(words.begin(), words.end(), std::string{},
[](const std::string& a, const std::string& b) {
return a.empty() ? b : a + " " + b;
}); // → "hello world"性能与边界注意事项:不要在 accumulate 里做重操作
std::accumulate 是线性遍历,时间复杂度 O(n),但它本身不控制执行顺序——在并行版本(std::reduce)出现前,所有运算严格按迭代器顺序进行。这意味着:
立即学习“C++免费学习笔记(深入)”;
- 如果二元操作有副作用(如打印、修改全局状态),行为是确定的,但不推荐
- 避免在 lambda 中做耗时操作(如文件读写、内存分配),会显著拖慢整体速度
- 对
std::list或其他非随机访问容器,std::accumulate仍可用,但迭代器移动成本更高 - 没有内置溢出检查:
int累加超限时静默回绕,需自行判断范围或换用std::safe_numerics等库
最常被忽略的一点:它不验证迭代器有效性。传入悬垂迭代器或 begin > end,行为未定义,调试时很难定位。









