C++ 标准库中的 <algorithm> 头文件算法总结

C++ 常用 <algorithm> 算法概览

  C++ 标准库中的 <algorithm> 头文件提供了大量有用的算法,主要用于操作容器(如 vector, list, array 等)。这些算法通常通过迭代器来操作容器元素。

1. 非修改序列操作

std::all_of, std::any_of, std::none_of

#include <algorithm>
#include <vector>std::vector<int> v = {1, 2, 3, 4, 5};// 检查所有元素是否满足条件
bool all_even = std::all_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 检查是否有任一元素满足条件
bool any_even = std::any_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 检查是否没有元素满足条件
bool none_even = std::none_of(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

std::for_each

std::vector<int> v = {1, 2, 3, 4, 5};// 对每个元素执行操作
std::for_each(v.begin(), v.end(), [](int &n){ n *= 2; });

std::count, std::count_if

std::vector<int> v = {1, 2, 3, 4, 5};// 计算等于3的元素个数
int count_3 = std::count(v.begin(), v.end(), 3);// 计算满足条件的元素个数
int count_even = std::count_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

std::find, std::find_if, std::find_if_not

std::vector<int> v = {1, 2, 3, 4, 5};// 查找值为3的元素
auto it = std::find(v.begin(), v.end(), 3);// 查找第一个偶数
auto it_even = std::find_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });// 查找第一个非偶数
auto it_not_even = std::find_if_not(v.begin(), v.end(), [](int i){ return i % 2 == 0; });

2. 修改序列操作

std::copy, std::copy_if

std::vector<int> src = {1, 2, 3, 4, 5};
std::vector<int> dst(5);// 复制元素
std::copy(src.begin(), src.end(), dst.begin());// 条件复制
std::vector<int> dst_even;
std::copy_if(src.begin(), src.end(), std::back_inserter(dst_even), [](int i){ return i % 2 == 0; });

std::fill, std::fill_n

std::vector<int> v(5);// 填充所有元素为42
std::fill(v.begin(), v.end(), 42);// 填充前3个元素为10
std::fill_n(v.begin(), 3, 10);

std::transform

std::vector<int> v = {1, 2, 3, 4, 5};
std::vector<int> result(v.size());// 对每个元素应用函数
std::transform(v.begin(), v.end(), result.begin(), [](int i){ return i * 2; });

std::replace, std::replace_if

std::vector<int> v = {1, 2, 3, 4, 5};// 替换所有3为10
std::replace(v.begin(), v.end(), 3, 10);// 替换所有偶数为0
std::replace_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; }, 0);

std::remove, std::remove_if

std::vector<int> v = {1, 2, 3, 4, 5, 6};// "移除"所有3(实际上是移动到最后,返回新的逻辑end)
auto new_end = std::remove(v.begin(), v.end(), 3);
v.erase(new_end, v.end()); // 真正删除// "移除"所有偶数
new_end = std::remove_if(v.begin(), v.end(), [](int i){ return i % 2 == 0; });
v.erase(new_end, v.end());

3. 排序和相关操作

std::sort, std::stable_sort

std::vector<int> v = {5, 3, 1, 4, 2};// 默认升序排序
std::sort(v.begin(), v.end());// 自定义排序
std::sort(v.begin(), v.end(), [](int a, int b){ return a > b; }); // 降序// 稳定排序(保持相等元素的相对顺序)
std::stable_sort(v.begin(), v.end());

std::partial_sort

std::vector<int> v = {5, 6, 1, 3, 2, 4};// 部分排序(前3个最小的元素)
std::partial_sort(v.begin(), v.begin() + 3, v.end());

std::nth_element

std::vector<int> v = {5, 6, 1, 3, 2, 4};// 使第n个元素处于正确位置
std::nth_element(v.begin(), v.begin() + 2, v.end());
// v[2]现在是排序后的正确元素,前面的都<=它,后面的都>=它

std::is_sorted, std::is_sorted_until

std::vector<int> v = {1, 2, 3, 4, 5};// 检查是否已排序
bool sorted = std::is_sorted(v.begin(), v.end());// 查找第一个破坏排序的元素
auto it = std::is_sorted_until(v.begin(), v.end());

4. 二分搜索(必须在已排序的序列上使用)

std::lower_bound, std::upper_bound, std::equal_range

std::vector<int> v = {1, 2, 2, 3, 4, 5};// 查找第一个不小于3的元素
auto low = std::lower_bound(v.begin(), v.end(), 3);// 查找第一个大于3的元素
auto up = std::upper_bound(v.begin(), v.end(), 3);// 查找等于3的范围
auto range = std::equal_range(v.begin(), v.end(), 3);

std::binary_search

std::vector<int> v = {1, 2, 3, 4, 5};// 检查元素是否存在
bool found = std::binary_search(v.begin(), v.end(), 3);

5. 集合操作(必须在已排序的序列上使用)

std::merge

std::vector<int> v1 = {1, 3, 5};
std::vector<int> v2 = {2, 4, 6};
std::vector<int> dst(v1.size() + v2.size());// 合并两个已排序的序列
std::merge(v1.begin(), v1.end(), v2.begin(), v2.end(), dst.begin());

std::includes, std::set_difference, std::set_intersection, std::set_union, std::set_symmetric_difference

std::vector<int> v1 = {1, 2, 3, 4, 5};
std::vector<int> v2 = {2, 4, 6};std::vector<int> result;// 检查v1是否包含v2的所有元素
bool includes = std::includes(v1.begin(), v1.end(), v2.begin(), v2.end());// 差集(v1 - v2)
std::set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(result));// 交集
result.clear();
std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));// 并集
result.clear();
std::set_union(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));// 对称差集(在v1或v2中但不同时在两者中)
result.clear();
std::set_symmetric_difference(v1.begin(), v1.end(), v2.begin(), v2.end(),std::back_inserter(result));

6. 堆操作

std::make_heap, std::push_heap, std::pop_heap, std::sort_heap

std::vector<int> v = {3, 1, 4, 1, 5, 9};// 构建最大堆
std::make_heap(v.begin(), v.end());// 添加元素到堆
v.push_back(6);
std::push_heap(v.begin(), v.end());// 移除堆顶元素
std::pop_heap(v.begin(), v.end());
v.pop_back();// 堆排序
std::sort_heap(v.begin(), v.end());

7. 最小/最大值操作

std::min_element, std::max_element, std::minmax_element

std::vector<int> v = {3, 1, 4, 1, 5, 9};// 查找最小元素
auto min_it = std::min_element(v.begin(), v.end());// 查找最大元素
auto max_it = std::max_element(v.begin(), v.end());// 同时查找最小和最大元素
auto minmax = std::minmax_element(v.begin(), v.end());

std::clamp (C++17)

int value = 15;
// 将值限制在10-20范围内
int clamped = std::clamp(value, 10, 20); // 返回15
clamped = std::clamp(5, 10, 20); // 返回10
clamped = std::clamp(25, 10, 20); // 返回20

8. 排列操作

std::next_permutation, std::prev_permutation

std::vector<int> v = {1, 2, 3};// 生成下一个排列
do {// 处理当前排列
} while (std::next_permutation(v.begin(), v.end()));// 生成前一个排列
std::prev_permutation(v.begin(), v.end());

9. 其他有用算法

std::accumulate (来自 <numeric>)

#include <numeric>
std::vector<int> v = {1, 2, 3, 4, 5};// 求和
int sum = std::accumulate(v.begin(), v.end(), 0);// 自定义操作(如乘积)
int product = std::accumulate(v.begin(), v.end(), 1, [](int a, int b){ return a * b; });

std::inner_product (来自 <numeric>)

std::vector<int> v1 = {1, 2, 3};
std::vector<int> v2 = {4, 5, 6};// 点积
int dot = std::inner_product(v1.begin(), v1.end(), v2.begin(), 0);

std::iota (来自 <numeric>)

std::vector<int> v(5);// 填充序列值
std::iota(v.begin(), v.end(), 10); // v = {10, 11, 12, 13, 14}

这些算法可以大大提高C++编程效率,避免了手动编写循环的繁琐工作,同时通常比手写循环更高效。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/75763.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

程序化广告行业(84/89):4A广告代理公司与行业资质解读

程序化广告行业&#xff08;84/89&#xff09;&#xff1a;4A广告代理公司与行业资质解读 大家好&#xff01;在探索程序化广告行业的道路上&#xff0c;每一次知识的分享都是我们共同进步的阶梯。一直以来&#xff0c;我都希望能和大家携手前行&#xff0c;深入了解这个充满机…

deepin使用autokey添加微信快捷键一键显隐ctrl+alt+w

打开deepin商店&#xff0c;搜索快捷键&#xff0c;找到autokey 快捷键管理&#xff0c;点击安装 点击右键新建文件夹 点击右键新建脚本 打开脚本并添加以下内容 import subprocess import time# ------------------ 配置项 ------------------ WM_CLASS "wechat…

文件内容课堂总结

Spark SQL是Spark用于结构化数据处理的模块&#xff0c;前身是Shark。Shark基于Hive开发&#xff0c;虽提升了SQL-on-Hadoop效率&#xff0c;但对Hive依赖过多。2014年6月1日Shark项目停止开发&#xff0c;团队将资源投入Spark SQL项目。Spark SQL具有诸多优点&#xff0c;如摆…

Zotero PDF Translate 翻译插件使用OpenAI API配置教程

PDF Translate&#xff1a;提升 Zotero 内置 PDF 阅读器的翻译功能 “PDF Translate” 是一款为 Zotero 设计的插件&#xff0c;旨在方便用户在 Zotero 内置的 PDF 阅读器中进行划词或段落翻译&#xff0c;辅助阅读外文文献。 一、 安装插件 下载插件&#xff1a; 访问 PDF T…

火山引擎旗下的产品

用户问的是火山引擎旗下的产品&#xff0c;我需要详细列出各个类别下的产品。首先&#xff0c;我得确认火山引擎有哪些主要业务领域&#xff0c;比如云计算、大数据、人工智能这些。然后&#xff0c;每个领域下具体有哪些产品呢&#xff1f;比如云计算方面可能有云服务器、容器…

C/C++程序中实现Python绑定多种技术路线

在C/C程序中实现Python绑定有多种技术路线&#xff0c;选择合适的方法取决于项目需求、性能要求和开发效率。以下是常见的几种方案&#xff0c;按易用性排序&#xff1a; 1. PyBind11&#xff08;推荐首选&#xff09; 特点&#xff1a;现代C库&#xff0c;语法简洁&#xff0…

【位运算】消失的两个数字

文章目录 面试题 17.19. 消失的两个数字解题思路 面试题 17.19. 消失的两个数字 面试题 17.19. 消失的两个数字 ​ 给定一个数组&#xff0c;包含从 1 到 N 所有的整数&#xff0c;但其中缺了两个数字。你能在 O(N) 时间内只用 O(1) 的空间找到它们吗&#xff1f; ​ 以任意…

自然语言处理Hugging Face Transformers

Hugging Face Transformers 是一个基于 PyTorch 和 TensorFlow 的开源库&#xff0c;专注于 最先进的自然语言处理&#xff08;NLP&#xff09;模型&#xff0c;如 BERT、GPT、RoBERTa、T5 等。它提供了 预训练模型、微调工具和推理 API&#xff0c;广泛应用于文本分类、机器翻…

vue开发基础流程 (后20)

创建项目命令&#xff1b; 或者 vue create my - vue - router - project这个是创建带路由的项目 22.组件组成 比如说一个页面吧&#xff0c;他三个组件&#xff0c;template就是用来放所有的标签&#xff0c;script用来放业务逻辑&#xff0c;style用来放样式&#xff0c;c…

高性能内存kv数据库Redis

引言 在当今数据驱动的时代&#xff0c;高效的数据存储和检索对于各类应用程序至关重要。Redis&#xff08;Remote Dictionary Server&#xff09;作为一款开源的内存键值数据库&#xff0c;凭借其出色的性能、丰富的数据结构和灵活的特性&#xff0c;在众多场景中得到了广泛应…

自动化测试概念篇

文章目录 目录1. 自动化1.1 自动化概念1.1.1 回归测试 1.2 自动化分类1.3 自动化测试金字塔 2. web自动化测试2.1 驱动2.1.1 安装驱动管理2.1.2 selenium库 3. Selenium3.1 一个简单的web自动化示例3.2 selenium驱动浏览器的工作原理 目录 自动化web自动化测试Selenium 1. 自…

《AI大模型应知应会100篇》第17篇:大模型的偏见与公平性问题

第17篇&#xff1a;大模型的偏见与公平性问题 摘要 在人工智能迅速发展的今天&#xff0c;大型语言模型&#xff08;LLM&#xff09;已经深入到我们的日常生活和工作中。然而&#xff0c;这些模型并非完美无缺&#xff0c;它们可能携带并放大数据中的偏见&#xff0c;导致不公…

【踩坑】GitHub Actions 运行的 Linux 环境中,文件名是大小写敏感的

在使用 VuePress 搭建个人博客并部署到 GitHub Pages 的过程中&#xff0c;我遇到了一个颇为棘手的问题&#xff1a;本地打包一切正常&#xff0c;但在 GitHub Actions 自动执行打包流程时&#xff0c;却提示找不到 README.md 文件&#xff0c;导致整个流程失败。经过一番深入排…

C# 13新特性 - .NET 9

转载&#xff1a; C# 13 中的新增功能 | Microsoft Learn C# 13 包括以下新增功能。 可以使用最新的 Visual Studio 2022 版本或 .NET 9 SDK 尝试这些功能&#xff1a;Introduced in Visual Studio 2022 Version 17.12 and newer when using C# 13 C# 13 中的新增功能 | Micr…

numpy.ma.masked_where:屏蔽满足条件的数组

1.函数功能 屏蔽满足条件的数组内容&#xff0c;返回值为掩码数组 2.语法结构 np.ma.masked_where(condition, a, copyTrue)3. 参数 参数含义condition屏蔽条件a要操作的数组copy布尔值&#xff0c;取值为True时&#xff0c;结果复制数组(原始数据不变)&#xff0c;否则返回…

【Redis】数据结构和内部编码

先来复习一下之前学过的几个基本的全局命令&#xff1a; keys&#xff1a;用来查看匹配规则的keyexists&#xff1a;用来判定执行key是否存在del&#xff1a;删除指定的keyexpire&#xff1a;给key设置过期时间ttl&#xff1a;查询key的过期时间type&#xff1a;查询key对应的…

OBOO鸥柏如何以智能教育室内外触摸屏一体机AI变革硬件

在AI技术蓬勃发展的当下&#xff0c;OBOO鸥柏室外触摸屏一体机通过融入AI科技&#xff0c;为教育领域带来了翻天覆地的变化。这款一体机不仅为高校和大学校园提供了革命性的数字化教学解决方案&#xff0c;更引领了引体向上成绩提升一体机带训室外终端屏幕设备的新潮流。其创新…

从零搭建高并发体育直播网站:架构设计、核心技术与性能优化实战

本文从技术视角拆解体育直播网站开发全流程&#xff0c;涵盖高并发架构设计、低延迟视频流传输、实时弹幕系统实现等核心模块&#xff0c;并附可复用的代码片段与优化方案。适合中高级开发者进阶实战参考。 一、需求分析与技术选型 1. 典型业务场景 核心需求&#xff1a;支持1…

【Python内置函数的深度解析与应用】id

目录 前言&#xff1a;技术背景与价值当前技术痛点解决方案概述目标读者说明 一、技术原理剖析核心概念图解关键技术模块技术选型对比 二、实战演示环境配置要求核心代码实现1. 基础身份验证2. 不可变对象优化3. 对象生命周期追踪 运行结果验证 三、性能对比测试方法论量化数据…

3.vtkProp 和vtkProp3D

文章目录 vtkProp 和vtkProp3D使用vtkProp3D使用vtkPro vtkProp 和vtkProp3D vtkProp 和 vtkProp3D 都是VTK&#xff08;Visualization Toolkit&#xff09;库中的类&#xff0c;它们用于在渲染场景中表示可视化元素。理解这两个类的区别和用途对于有效地使用VTK进行三维数据可…