C++提高笔记(七)---STL常用算法(排序、拷贝和替换、算术生成、集合)

(由于上篇笔记篇幅过长,故开新篇  继续记录算法笔记)

2.3常用排序算法

学习目标:掌握常用的排序算法

算法简介:

sort           //对容器内元素进行排序
random_shuffle //洗牌 指定范围内的元素随机调整次序
merge          //容器元素合并,并存储到另一容器中
reverse        //反转指定范围的元素

2.3.1 sort

功能描述:对容器内元素进行排序

sort属于开发中最常用的算法之一,需熟练掌握

函数原型:

sort(iterator beg, iterator end, _Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
//常用排序算法 sort
void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);//利用sort进行升序 默认就是升序sort(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;//利用sort进行降序sort(v.begin(), v.end(),greater<int>());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

10 20 30 40 50
50 40 30 20 10
请按任意键继续. . .

2.3.2 random_shuffle

功能描述:洗牌 指定范围内的元素随机调整次序

函数原型:

random_shuffle(iterator beg, iterator end);
// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<ctime>
//常用排序算法 random_shuffle
void myPrint(int val)
{cout << val << " ";
}void test01()
{//随机数种子 不然每次洗牌之后的顺序都一样srand((unsigned int)time(NULL));vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//利用洗牌算法 打乱顺序random_shuffle(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

9 2 8 1 5 6 4 0 7 3
请按任意键继续. . .

2.3.3 merge

功能描述:两个容器元素合并,并存储到另一容器中

注意:两个容器必须是有序的(而且顺序必须相同,不能一个升序,一个降序)

函数原型:

merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用排序算法 merge
void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 10);}//目标容器vector<int>vTarget;//需要提前给目标容器分配空间//否则会报错vTarget.resize(v1.size() + v2.size());merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());for_each(vTarget.begin(), vTarget.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
请按任意键继续. . .

2.3.4 reverse

功能描述:将容器内元素进行反转

函数原型:

reverse(iterator beg, iterator end);
// 反转指定范围的元素
// beg 开始迭代器
// end 结束迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用排序算法 reverse
void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}cout << "反转前:" << endl;for_each(v1.begin(), v1.end(), myPrint);reverse(v1.begin(), v1.end());cout << endl;cout << "反转后:" << endl;for_each(v1.begin(), v1.end(), myPrint);cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

反转前:
0 1 2 3 4 5 6 7 8 9
反转后:
9 8 7 6 5 4 3 2 1 0
请按任意键继续. . .

2.4常用拷贝和替换算法

学习目标:掌握常用的拷贝和替换算法

算法简介:

copy       //容器内指定范围的元素拷贝到另一容器中
replace    //将容器内指定范围的旧元素修改为新元素
replace_if //容器内指定范围满足条件的元素替换为新元素
swap       //互换两个容器的元素

2.4.1 copy

功能描述:容器内指定范围的元素拷贝到另一容器中

函数原型:

copy(iterator beg, iterator end, iterator dest);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// dest 目标起始迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用拷贝和替换算法 copy
void myPrint(int val)
{cout << val << " ";
}void test01()
{vector<int>v1;for (int i = 0; i < 10; i++){v1.push_back(i);}vector<int>v2;v2.resize(v1.size());copy(v1.begin(), v1.end(), v2.begin());for_each(v2.begin(), v2.end(), myPrint);cout << endl; 
}int main()
{test01();system("pause");return 0;
}

输出结果:

0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .

2.4.2 replace

功能描述:将容器内指定范围的旧元素修改为新元素

函数原型:

replace(iterator beg, iterator end, oldvalue, newvalue);
// 将区间内旧元素 替换成 新元素
// beg 开始迭代器
// end 结束迭代器
// oldvalue 旧元素
// newvalue 新元素
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用拷贝和替换算法 replace
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v;v.push_back(20);v.push_back(30);v.push_back(50);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(10);v.push_back(20);cout << "替换前:" << endl;for_each(v.begin(), v.end(), MyPrint());//将20替换为99replace(v.begin(), v.end(), 20, 99);cout << endl;cout << "替换后:" << endl;for_each(v.begin(), v.end(), MyPrint());cout << endl; 
}int main()
{test01();system("pause");return 0;
}

输出结果:

替换前:
20 30 50 30 40 20 10 20
替换后:
99 30 50 30 40 99 10 99
请按任意键继续. . .

2.4.3 replace_if

功能描述:将区间内满足条件的元素,替换成指定元素

注意:利用仿函数可以灵活筛选满足的条件

函数原型:

replace_if(iterator beg, iterator end, _pred, newvalue);
// 按条件替换元素,满足条件的替换成指定元素
// beg 开始迭代器
// end 结束迭代器
// _pred 谓词
// newvalue 替换的新元素
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用拷贝和替换算法 replace_if
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};class Greater03
{
public:bool operator()(int val){return val >= 30;}
};void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(20);v.push_back(40);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(30);cout << "替换前:" << endl;for_each(v.begin(), v.end(), MyPrint());//将大于等于30替换为99//利用仿函数可以灵活筛选满足的条件replace_if(v.begin(), v.end(), Greater03(), 99);cout << endl;cout << "替换后:" << endl;for_each(v.begin(), v.end(), MyPrint());cout << endl; 
}int main()
{test01();system("pause");return 0;
}

输出结果:

替换前:
10 40 20 40 30 50 20 30
替换后:
10 99 20 99 99 99 20 99
请按任意键继续. . .

2.4.4 swap

功能描述:互换两个容器的元素

注意:一定要是同种容器

函数原型:

swap(container c1, container c2);
// 互换两个容器的元素
// c1容器1
// c2容器2
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用拷贝和替换算法 swap
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);v2.push_back(i + 100);}cout << "交换前:" << endl;for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;cout << "------------------" << endl;cout << "交换后:" << endl;swap(v1, v2);for_each(v1.begin(), v1.end(), MyPrint());cout << endl;for_each(v2.begin(), v2.end(), MyPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

交换前:
0 1 2 3 4 5 6 7 8 9
100 101 102 103 104 105 106 107 108 109
------------------
交换后:
100 101 102 103 104 105 106 107 108 109
0 1 2 3 4 5 6 7 8 9
请按任意键继续. . .

2.5常用算术生成算法

学习目标:掌握常用的算术生成算法

注意:算术生成算法属于小型算法,使用时包含的头文件为#include<numeric>

算法简介:

accumulate // 计算容器元素累计总和
fill       // 向容器中添加元素

2.5.1 accumulate

功能描述:计算区间内 容器元素累计总和

函数原型:

accumulate(iterator beg, iterator end, value);
// 计算容器元素累计总和
// beg 开始迭代器
// end 结束迭代器
// value 起始值
#include <iostream>
using namespace std;
#include<vector>
#include<numeric>//别忘了包含此头文件
//常用算术生成算法 accumulate
void test01()
{vector<int>v;for (int i = 0; i <= 100; i++){v.push_back(i);}//参数3是个起始累加值int total = accumulate(v.begin(), v.end(), 0);cout << "total = " << total << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

total = 5050
请按任意键继续. . .

2.5.2 fill

功能描述:向容器中填充指定的元素

函数原型:

fill(iterator beg, iterator end, value);
// 向容器中填充元素
// beg 开始迭代器
// end 结束迭代器
// value 填充的值
#include <iostream>
using namespace std;
#include<vector>
#include<numeric>//别忘了包含此头文件
#include<algorithm>
//常用算术生成算法 fill
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v;v.resize(10);//后期重新填充fill(v.begin(), v.end(), 100);for_each(v.begin(), v.end(), MyPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

100 100 100 100 100 100 100 100 100 100
请按任意键继续. . .

2.6常用集合算法

学习目标:掌握常用的集合算法

算法简介:

set_intersection // 求两个容器的交集
set_union        //求两个容器的并集
set_difference   // 求两个容器的差集

2.6.1 set_intersection

功能描述:求两个容器的交集

注意:

        两个集合必须是有序序列

        目标容器开辟空间需要从两个容器中取小值

        set_intersection返回值是交集中最后一个元素的位置

函数原型:

set_intersection(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的交集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用集合算法 set_intersection
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);     //0-9v2.push_back(i + 5); //5-14}vector<int>vTarget;//目标容器需要提前开辟空间//最特殊情况,大容器包含小容器,开辟空间,取小容器的size即可vTarget.resize(min(v1.size(), v2.size()));//获取交集//返回值是个迭代器,是最后一个元素的位置vector<int>::iterator itEnd = set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());//所以这里遍历的时候,容器末尾是itEndfor_each(vTarget.begin(), itEnd, MyPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

5 6 7 8 9
请按任意键继续. . .

2.6.2 set_union

功能描述:求两个集合的并集

注意:

两个集合必须是有序序列

目标容器开辟空间需要两个容器相加

set_union返回值既是并集中最后一个元素的位置

函数原型:

set_union(iterator begl, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的并集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代翳
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用集合算法 set_union
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);     //0-9v2.push_back(i + 5); //5-14}vector<int>vTarget;//目标容器需要提前开辟空间//最特殊情况,大容器加上小容器,开辟空间,取两个容器的size和即可vTarget.resize(v1.size() + v2.size());//获取并集//返回值是个迭代器,是最后一个元素的位置vector<int>::iterator itEnd = set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());//所以这里遍历的时候,容器末尾是itEndfor_each(vTarget.begin(), itEnd, MyPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
请按任意键继续. . .

2.6.3 set_difference

功能描述:求两个集合的差集

注意:

求差集的两个集合必须的有序序列

目标容器开辟空间需要从两个容器取较大值

set_difference返回值既是差集中最后一个元素的位置

函数原型:

set_difference(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 求两个集合的差集
// 注意:两个集合必须是有序序列
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用集合算法 set_difference
class MyPrint
{
public:void operator()(int val){cout << val << " ";}
};void test01()
{vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(i);     //0-9v2.push_back(i + 5); //5-14}vector<int>vTarget;//目标容器需要提前开辟空间//最特殊情况,两容器没有交集,开辟空间,取两个容器的最大size即可vTarget.resize(max(v1.size(), v2.size()));//获取差集//返回值是个迭代器,是最后一个元素的位置cout << "v1和v2的差集为:" << endl;vector<int>::iterator itEnd = set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());//所以这里遍历的时候,容器末尾是itEndfor_each(vTarget.begin(), itEnd, MyPrint());cout << endl;cout << "v2和v1的差集为:" << endl;itEnd = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), vTarget.begin());//所以这里遍历的时候,容器末尾是itEndfor_each(vTarget.begin(), itEnd, MyPrint());cout << endl;
}int main()
{test01();system("pause");return 0;
}

输出结果:

v1和v2的差集为:
0 1 2 3 4
v2和v1的差集为:
10 11 12 13 14
请按任意键继续. . .

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

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

相关文章

【进阶版讲解如何系统地自学Python?】一篇文章带你了解

如何系统地自学Python? 1. 前言2. 确定学习目标3. 学习Python的基础4. 实践编程5. 理解面向对象编程6. 掌握Python库和框架7. 开始一个个人项目8. 加深理解9. 参与社区10. 不断挑战自己11. 总结和回顾 1. 前言 自学Python需要计划、资源和实践&#xff0c;以下是一个系统性自…

springboot + neo4j 功能使用

集成 添加依赖 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-neo4j</artifactId></dependency> spring:# neo4j 图数据库neo4j:uri: bolt://localhost:7687authentication:username: …

首页效果炫酷的wordpress免费主题模板

视频背景免费WP主题 简洁大气的视频背景wordpress主题&#xff0c;找大视频背景的主题可以看看这个。 https://www.wpniu.com/themes/193.html 红色全屏大图WP主题 非常经典的一款免费wordpress主题&#xff0c;红色全屏大图满足多行业使用。 https://www.wpniu.com/themes…

贴片电感的工艺结构原理及选型参数总结

🏡《总目录》 目录 1,概述2,工作原理3,结构特点3.1,耐电流叠加特性3.2,耐冲击噪音低3.3,全屏蔽结构绿色环保性好3.4,损耗低、耐热性好3.5,作业频带宽4,工艺流程4.1,线圈绕制4.2,磁芯制作4.3,导线制作4.4,封装

蓝桥杯算法基础(11):十大排序算法(冒泡排序)c语言般版

十大排序算法合集&#xff08;c语言般&#xff09; 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归并排序 堆排序 计数排序 桶排序 基数排序 分类: 交换类 1.冒泡排序 2.快速排序 分配类 1.计数排序 2.基数排序 选择类 1.选择排序 归并类 1.归并排序 插入类 1.插入…

9.登入页面

登入页面 在pages中新建页面login 修改代码 <template><view></view> </template><script setup></script><style lang"scss"></style>添加头像组件 官网 https://vkuviewdoc.fsq.pub/components/avatar.html …

Oracle中使用coe_load_sql_profile脚本固定执行计划

coe_load_sql_profile.sql 是Oracle数据库环境下用于迁移或固定SQL执行计划的一个脚本&#xff0c;它可以帮助DBA将特定SQL语句的高效执行计划转化为SQL Profile&#xff0c;并将其应用到目标数据库中。 SQL Profile是一种Oracle数据库中用来指导优化器选择特定执行计划的方法。…

【靶机测试--PHOTOGRAPHER: 1【php提权】】

前期准备 靶机下载地址&#xff1a; https://vulnhub.com/entry/photographer-1%2C519/ 信息收集 nmap 扫描同网段 ┌──(root㉿kali)-[/home/test/桌面] └─# nmap -sP 192.168.47.0/24 --min-rate 3333 Starting Nmap 7.92 ( https://nmap.org ) at 2024-03-19 07:37 …

SpringCloud Alibaba Nacos 服务注册和配置中心

一、前言 接下来是开展一系列的 SpringCloud 的学习之旅&#xff0c;从传统的模块之间调用&#xff0c;一步步的升级为 SpringCloud 模块之间的调用&#xff0c;此篇文章为第十二篇&#xff0c;即介绍 SpringCloud Alibaba Nacos 服务注册和配置中心。 二、Nacos 简介 2.1 为…

SpringBoot 监控 SQL 运行情况

Druid 数据库连接池相信很多小伙伴都用过,个人感觉 Druid 是阿里比较成功的开源项目了,不像 Fastjson 那么多槽点,Druid 各方面一直都比较出色,功能齐全,使用也方便,基本的用法就不说了,今天我们来看看 Druid 中的监控功能。 准备工作 首先我们来创建一个 Spring Boot…

Android API 30及更高版本网络权限设置

目录 一、网络权限设置二、配置步骤1、在 AndroidManifest.xml 文件中添加网络权限声明2、在 AndroidManifest.xml 文件中的 application 节点下配置网络安全策略 一、网络权限设置 在 Android API 30 及更高版本中&#xff0c;Google 引入了更严格的网络安全策略&#xff0c;…

wireshark数据捕获实验简述

Wireshark是一款开源的网络协议分析工具&#xff0c;它可以用于捕获和分析网络数据包。是一款很受欢迎的“网络显微镜”。 实验拓扑图&#xff1a; 实验基础配置&#xff1a; 服务器&#xff1a; ip:172.16.1.88 mask:255.255.255.0 r1: sys sysname r1 undo info enable in…

YOLOv5目标检测学习(6):源码解析之:训练部分train.py

文章目录 前言一、导入相关包与配置二、主函数main2.1 checks&#xff1a;检查rank值来判断是否打印参数、检查git仓库、检查包的安装2.2 判断是否恢复上一次模型训练提问&#xff1a;opt.data, opt.cfg, opt.hyp, opt.weights, opt.project各是什么&#xff1f; 2.3 DDP mode&…

【数据结构】哈希表与哈希桶

&#x1f440;樊梓慕&#xff1a;个人主页 &#x1f3a5;个人专栏&#xff1a;《C语言》《数据结构》《蓝桥杯试题》《LeetCode刷题笔记》《实训项目》《C》《Linux》《算法》 &#x1f31d;每一个不曾起舞的日子&#xff0c;都是对生命的辜负 目录 前言 1.概念 2.哈希冲突…

mysql查询条件包含IS NULL、IS NOT NULL、!=、like %* 、like %*%,不能使用索引查询,只能使用全表扫描,是真的吗???

不知道是啥原因也不知道啥时候, 江湖上流传着这么一个说法 mysql查询条件包含IS NULL、IS NOT NULL、!、like %* 、like %*%,不能使用索引查询&#xff0c;只能使用全表扫描。 刚入行时我也是这么认为的&#xff0c;还奉为真理&#xff01; 但是时间工作中你会发现还是走索引…

Linux 常用运维使用指令

查询占用 CPU 最高的前 10 个进程 ps aux|grep -v PID|sort -rn -k 3|head 查询占用内存最大的前 10 个进程 ps aux|grep -v PID|sort -rn -k 4|head Linux du 获取文件比较大的前十 du -h / | sort -rh | head -n 10 解释&#xff1a; du: 磁盘使用情况命令。 -h: 参数…

C++基础入门(命名空间,函数,引用)

文章目录 前言1,命名空间2,函数函数重载缺省参数内联函数 3,引用尾声 前言 欢迎来到这篇关于C的入门博客&#xff01;C是一门强大而又广泛应用的编程语言&#xff0c;作为一门面向对象的编程语言&#xff0c;C可以让你更好地组织和管理代码&#xff0c;提高代码的重用性和可维…

带你学会深度学习之卷积神经网络[CNN] - 3

前言 本文不讲述如泛化&#xff0c;前向后向传播&#xff0c;过拟合等基础概念。 本文图片来源于网络&#xff0c;图片所有者可以随时联系笔者删除。 CNN&#xff0c;常用于计算机视觉&#xff0c;是计算机视觉方面常见的基础模型&#xff0c;后面发展的有很多其他变种&…

实验7-3-6 字符串转换成十进制整数(PTA)

题目&#xff1a; 输入一个以#结束的字符串&#xff0c;本题要求滤去所有的非十六进制字符&#xff08;不分大小写&#xff09;&#xff0c;组成一个新的表示十六进制数字的字符串&#xff0c;然后将其转换为十进制数后输出。如果在第一个十六进制字符之前存在字符“-”&#…

图书推荐|图解算法:C语言实现+视频教学版

零负担理解数据结构及其算法的设计&#xff0c;零基础也能快速上手编程。 本书内容 《图解算法&#xff1a;C语言实现视频教学版》是一本综合讲述数据结构及其算法的入门书&#xff0c;力求简洁、清晰、严谨、且易于学习和掌握。 《图解算法&#xff1a;C语言实现视频教学版》…