【C++】【STL算法】那些STL算法替代的循环

有一句谚语说:"如果你显式使用循环的话,说明你不了解STL算法。"STL算法有很多自己典型使用场景:数据处理和转换,搜索和过滤,排序与分组,数学计算,集合操作等。下面示例如何将代码中的循环,用STL算法替代,主要有std::for_each()/std::find()/std::find_if()/std::count_if()/std::any_of()/std::all_of()/std::none_of()/std::transform()/std::copy_if()/std::remove_if()/std::accumulate()/std::sort()/std::unique()/std::replace_if()。

1. 遍历并执行操作

cpp 复制代码
//目标:对容器中每个元素执行某个动作

//传统循环
for (int x : v) { std::cout << x << " "; }

//STL 算法
std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });

//说明
//std::for_each 明确表达"对每个元素执行操作"的语义
//更容易被并行化(std::for_each(std::execution::par, ...))

2. 查找元素

cpp 复制代码
//目标:判断容器中是否存在某个值

//传统循环

bool found = false; for (int x : v) { if (x == 10) { found = true; break; } }

//STL 算法

auto it = std::find(v.begin(), v.end(), 10); bool found = (it != v.end());

3. 条件查找(第一个满足条件的元素)

cpp 复制代码
//传统循环

int result = -1; for (int x : v) { if (x > 10) { result = x; break; } }

//STL 算法

auto it = std::find_if(v.begin(), v.end(), [](int x) { return x > 10; });

4. 统计数量

cpp 复制代码
//目标:统计满足条件的元素个数

//传统循环

int count = 0; for (int x : v) { if (x % 2 == 0) { ++count; } }

//STL 算法

int count = std::count_if(v.begin(), v.end(), [](int x) { return x % 2 == 0; });

5. 判断"是否存在 / 是否全部满足"

cpp 复制代码
//是否存在

bool has_negative = std::any_of(v.begin(), v.end(), [](int x) { return x < 0; });

//是否全部满足

bool all_positive = std::all_of(v.begin(), v.end(), [](int x) { return x > 0; });

//是否全部不满足

bool none_zero = std::none_of(v.begin(), v.end(), [](int x) { return x == 0; });

6. 修改元素(就地变换)

cpp 复制代码
//目标:对每个元素进行变换

//传统循环

for (int& x : v) { x *= 2; }

//STL 算法

std::transform(v.begin(), v.end(), v.begin(), [](int x) { return x * 2; });

7. 拷贝 + 条件过滤

cpp 复制代码
//目标:筛选元素到新容器

//传统循环
std::vector<int> result; for (int x : v) { if (x > 0) { result.push_back(x); } }

//STL 算法
std::vector<int> result; std::copy_if(v.begin(), v.end(), std::back_inserter(result), [](int x) { return x > 0; });

8. 删除元素(erase-remove 惯用法)

cpp 复制代码
//目标:从容器中删除满足条件的元素

//传统循环(容易出错)

for (auto it = v.begin(); it != v.end(); ) { if (*it < 0) it = v.erase(it); else ++it; }

//STL 算法

v.erase(std::remove_if(v.begin(), v.end(), [](int x) { return x < 0; }), v.end());

9. 累加 / 聚合

cpp 复制代码
//目标:求和或聚合计算

//传统循环

int sum = 0; for (int x : v) { sum += x; }

//STL 算法

int sum = std::accumulate(v.begin(), v.end(), 0);

//自定义聚合

int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<>());

10. 排序 + 自定义规则

cpp 复制代码
//传统循环
//实际上很少手写排序循环(复杂且易错)

//STL 算法
std::sort(v.begin(), v.end(), [](int a, int b) { return a > b; });

11. 相邻元素处理

cpp 复制代码
//目标:消除连续重复值
//STL 算法
v.erase(std::unique(v.begin(), v.end()), v.end());

12. 条件替换

cpp 复制代码
//传统循环
for (int& x : v) { if (x < 0) x = 0; }
//STL 算法
std::replace_if(v.begin(), v.end(), [](int x) { return x < 0; }, 0);
相关推荐
YuTaoShao5 分钟前
【LeetCode 每日一题】1653. 使字符串平衡的最少删除次数——(解法一)前后缀分解
算法·leetcode·职场和发展
BD_Marathon10 分钟前
设计模式——依赖倒转原则
java·开发语言·设计模式
VT.馒头12 分钟前
【力扣】2727. 判断对象是否为空
javascript·数据结构·算法·leetcode·职场和发展
goodluckyaa21 分钟前
LCR 006. 两数之和 II - 输入有序数组
算法
孤狼warrior22 分钟前
YOLO目标检测 一千字解析yolo最初的摸样 模型下载,数据集构建及模型训练代码
人工智能·python·深度学习·算法·yolo·目标检测·目标跟踪
devmoon29 分钟前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
Evand J32 分钟前
TDOA(到达时间差)的GDOP和CRLB计算的MATLAB例程,论文复现,附参考文献。GDOP:几何精度因子&CRLB:克拉美罗下界
开发语言·matlab·tdoa·crlb·gdop
野犬寒鸦33 分钟前
从零起步学习并发编程 || 第七章:ThreadLocal深层解析及常见问题解决方案
java·服务器·开发语言·jvm·后端·学习
云姜.36 分钟前
java抽象类和接口
java·开发语言
xyq20241 小时前
Pandas 安装指南
开发语言