STL常用算法——C++

news2025/4/23 12:25:48

1.概述

2.常用遍历算法

1.简介

2.for_each

方式一:传入普通函数(printf1)

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void printf1(int a)
{
	cout << a << " ";
}
int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), printf1);
	cout << endl;
	return 0;
}

方式2:利用仿函数,传入匿名函数对象

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class person
{
public:
	void operator()(int a)
	{
		cout << a << " ";
	}
};
int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	for_each(v.begin(), v.end(), person());
	cout << endl;
	return 0;
}

注意:函数只需要传入函数名就行,但是仿函数需要传入函数对象。

for_each在实际开发中是最常用的一个算法,需要熟练掌握

3.transform

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class transform1
{
public:
	int operator()(int a)
	{
		return a;
	}
};
class print
{
public:
	void operator()(int a)
	{
		cout<<a<<" ";
	}
};
int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>v1;
	v1.resize(v.size());
	transform(v.begin(), v.end(), v1.begin(), transform1());
	for_each(v.begin(), v.end(), print());
	cout << endl;
	return 0;
}

注意:转运之前,需要将目标对象用resize()也开辟相同的空间。

3.常用查找算法

1.find

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class person
{
public:
	person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	bool operator==(const person& p)
	{
		if (this->age == p.age && this->name == p.name)
		{
			return true;
		}
		else return false;
	}
	string name;
	int age;
};
int main()
{
	vector<person> v;
	person p1("1", 12);
	person p2("2", 13);
	person p3("3", 14);
	person p4("4", 15);
	person p5("5", 16);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	vector<person>::iterator it = find(v.begin(), v.end(), p2);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" << endl;
	}
	return 0;
}

或者也可以将查找条件改为

    person pp(”11“,13);

    vector<person>::iterator it = find(v.begin(), v.end(), pp);

注意:find的返回值是迭代器

2.find_if

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}
	vector<int>::iterator it=find_if(v.begin(), v.end(), person());
	if (it == v.begin())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了:" << *it << endl;
	}
	return 0;
}

3.abjacent_find

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(3);
	v.push_back(4);
	vector<int>::iterator it=adjacent_find(v.begin(), v.end());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了:" << *it << endl;
	}
	return 0;
}

4.binary_search

底层原理就是二分查找,二分查找需要提前排序好才能用,二分查找需要的算力少。

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
int main()
{
	vector<int> v;
	for(int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	bool ret = binary_search(v.begin(), v.end(),9);
	if (ret)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没找到" << endl;
	}
	return 0;
}

如果是无序序列,结果未知

5.count

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class person
{
public:
	person(string name, int age)
	{
		this->age = age;
		this->name = name;
	}
	bool operator==(const person& p)
	{
		if (p.age == this->age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	int age;
	string name;
};
int main()
{
	vector<person> v;
	person p1("刘备", 35);
	person p2("关羽", 35);
	person p3("张飞", 35);
	person p4("赵云", 20);
	person p5("刘备", 40);
	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);
	v.push_back(p5);
	person p("诸葛亮", 35);
	int a = count(v.begin(), v.end(), p);
	cout << "同年龄的有:" << a << "个" << endl;
	return 0;
}

关于为什么operator要加const

const Person p1("刘备", 35);

Person p2("关羽", 35);

if (p1 == p2)

{

// 编译错误:无法将 const 对象传递给非 const 引用参数

cout << "年龄相同" << endl;

}

在运算符重载中,参数通常也会被声明为const。这样做的目的是为了扩大函数的适用范围,使其既能接受const对象,也能接受非const对象。

总结:对于统计自定义数据类型的时候,需要配合重载operator==

           对于形参是自定义类型时,要加上const

6.count_if

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
class person
{
public:
	bool operator()(int val)
	{
		return val == 2;
	}
};
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(2);
	v.push_back(2);
	v.push_back(5);
	int a = count_if(v.begin(), v.end(), person());
	cout << "同年龄的有:" << a << "个" << endl;
	return 0;
}

4.常用排序算法

1.sort

pred是谓词

生序

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void print(int a)
{
	cout << a << " ";
}
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(6);
	v.push_back(3);
	v.push_back(5);
	sort(v.begin(), v.end());
	for_each(v.begin(), v.end(), print);
	return 0;
}

降序(利用内建函数对象greater实现降序排序,要包含头文件functional)

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void fun(int a)
{
	cout << a << " ";
}
int main()
{
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(6);
	v.push_back(3);
	v.push_back(5);
	sort(v.begin(), v.end(), greater<int>());
	for_each(v.begin(), v.end(),fun);
	return 0;
}

2.random_shuffle

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void fun(int a)
{
	cout << a << " ";
}
int main()
{
	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(),fun);
	return 0;
}

可以加入时间种子来达到真随机效果(srand(unsigned int)time(NULL);)

不过需要包含头文件#include<time.h>

3.merge

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void fun(int a)
{
	cout << a << " ";
}
int main()
{
	vector<int> v1;
	vector<int> v2;
	vector<int> v3;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+1);
	}
	v3.resize(v1.size() + v2.size());
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(), v3.end(),fun);
	return 0;
}

不过要给目标容器提前开辟内存空间用resize

而且合并之后还是一个有序序列

merge合并的两个序列必须是有序序列

4.reverse

5.常用拷贝和替换算法

1.copy

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void fun(int a)
{
	cout << a << " ";
}
int main()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	v2.resize(v1.size());
	copy(v1.begin(), v1.end(), v2.begin());
	for_each(v2.begin(), v2.end(),fun);
	return 0;
}

2.replace

3.replace_if

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	replace_if(v1.begin(), v1.end(), person(), 1000);
	for_each(v1.begin(), v1.end(),fun);
	return 0;
}

可以利用仿函数灵活的设置筛选条件

4.swap

swap(v1,v2);

其中两个容器的大小和数值都会交换,不过交换的容器要同种类型

6.常用算术生成算法

1.accumulate

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
#include<numeric>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	for (int i = 0; i <= 100; i++)
	{
		v1.push_back(i);
	}
	int val=accumulate(v1.begin(),v1.end(),0);
	/*for_each(v1.begin(), v1.end(),fun);*/
	cout << val << endl;
	return 0;
}

比较实用,记住要添加头文件#include<numeric>

2.fill

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
#include<numeric>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
	}
	fill(v1.begin(),v1.end(),99);
	for_each(v1.begin(), v1.end(),fun);
	cout << endl;
	vector<int> v2;
	v2.resize(10);
	fill(v2.begin(), v2.end(), 11);
	for_each(v2.begin(), v2.end(), fun);
	return 0;
}

如果已经有了数值,会把原来的数值顶替掉

7.常用集合算法

1.set_intersection

交集:即两个容器中数值相同的元素

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
#include<numeric>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+3);
	}
	vector<int>v3;
	v3.resize(min(v1.size(), v2.size()));
	vector<int>::iterator it=set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(),it,fun);
	cout << endl;
	return 0;
}

注意:

set_intersection其中它会返回交际的最后一个元素的迭代器,遍历输出交集的时候不要写end(),要写它返回的迭代器,这样就不会输出无用元素。

存储交集的目标容器需要提前开辟空间,最特殊的情况是大容器包含小容器,即交集就是小容器,所以取小容器的大小即可。

min()包含在algorithm头文件中,找出最小值

2.set_union

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
#include<numeric>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+3);
	}
	vector<int>v3;
	v3.resize(v1.size()+v2.size());
	vector<int>::iterator it=set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(),it,fun);
	cout << endl;
	return 0;
}

最坏的情况是一个相同的都没有,所以开辟大小直接把两个容器的大小加起来就行

注意:两个容器必须是有序序列

3.set_different

有两种差集的情况:(谁在前先求谁的差集)

#include<stdio.h>
using namespace std;
#include<string>
#include<vector>
#include<functional>
#include<algorithm>
#include <iostream>
#include<numeric>
void fun(int a)
{
	cout << a << " ";
}
class person
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};
int main()
{
	vector<int> v1;
	vector<int> v2;
	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i+3);
	}
	v2.push_back(99);
	vector<int>v3;
	v3.resize(max(v1.size(),v2.size()));
	vector<int>::iterator it=set_difference(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
	for_each(v3.begin(),it,fun);
	cout << endl;
	vector<int>::iterator it1 = set_difference(v2.begin(), v2.end(), v1.begin(), v1.end(), v3.begin());
	for_each(v3.begin(), it1, fun);
	cout << endl;
	return 0;
}

注意:两个容器必须是有序序列

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

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

相关文章

UofTCTF-2025-web-复现

感兴趣朋友可以去我博客里看&#xff0c;画风更好看 UofTCTF-2025-web-复现 文章目录 scavenger-huntprismatic-blogscode-dbprepared-1prepared-2timeless scavenger-hunt 国外的一些ctf简单题就喜欢把flag藏在注释里&#xff0c;开源代码找到第一部分的flag 抓个包返回数据…

[密码学基础]GB与GM国密标准深度解析:定位、差异与协同发展

[密码学基础]GB与GM国密标准深度解析&#xff1a;定位、差异与协同发展 导语 在国产密码技术自主可控的浪潮下&#xff0c;GB&#xff08;国家标准&#xff09;与GM&#xff08;密码行业标准&#xff09;共同构建了我国商用密码的技术规范体系。二者在制定主体、法律效力、技术…

15.第二阶段x64游戏实战-分析怪物血量(遍历周围)

免责声明&#xff1a;内容仅供学习参考&#xff0c;请合法利用知识&#xff0c;禁止进行违法犯罪活动&#xff01; 本次游戏没法给 内容参考于&#xff1a;微尘网络安全 上一个内容&#xff1a;14.第二阶段x64游戏实战-分析人物的名字 如果想实现自动打怪&#xff0c;那肯定…

HarmonyOS 基础语法概述 UI范式

ArkUI框架 - UI范式 ArkTS的基本组成 装饰器&#xff1a; 用于装饰类、结构、方法以及变量&#xff0c;并赋予其特殊的含义。如上述示例中Entry、Component和State都是装饰器&#xff0c;Component表示自定义组件&#xff0c;Entry表示该自定义组件为入口组件&#xff0c;Stat…

专题讨论2:树与查找

在讨论前先回顾一下定义&#xff1a; BST树的定义 二叉搜索树是一种特殊的二叉树&#xff0c;对于树中的任意一个节点&#xff1a; 若它存在左子树&#xff0c;那么左子树中所有节点的值都小于该节点的值。 若它存在右子树&#xff0c;那么右子树中所有节点的值都大于该节点…

django之数据的翻页和搜索功能

数据的翻页和搜素功能 目录 1.实现搜素功能 2.实现翻页功能 一、实现搜素功能 我们到bootstrap官网, 点击组件, 然后找到输入框组, 并点击作为额外元素的按钮。 我们需要使用上面红色框里面的组件, 就是搜素组件, 代码部分就是下面红色框框出来的部分。 把这里的代码复制…

unity脚本-FBX自动化模型面数校验

根据目前模型资源平均面数预算进行脚本制作&#xff0c;自动化校验模型面数是否符合规范。 *注&#xff1a;文件格式为.cs。需要放置在unity资源文件夹Assets>Editor下。 测试效果&#xff08;拖一个fbx文件进unity时自动检测&#xff09;&#xff1a; 以下为完整代码 us…

C++用于保留浮点数的两位小数,使用宏定义方法(可兼容低版本Visual Studio)

文章目录 一、 描述二、 样例二、 结果输出 一、 描述 这个宏定义&#xff08;可放入.h头文件里&#xff09;使用基本的数学运算&#xff0c;几乎兼容所有版本的VS&#xff0c;以下可对正数做四舍五入&#xff1a; #define ROUND_TO_TWO(x) ( (floor((x) * 100 0.5) / 100) …

(51单片机)LCD显示温度(DS18B20教程)(LCD1602教程)(延时函数教程)(单总线教程)

演示视频&#xff1a; LCD显示温度 源代码 如上图将9个文放在Keli5 中即可&#xff0c;然后烧录在单片机中就行了 烧录软件用的是STC-ISP&#xff0c;不知道怎么安装的可以去看江科大的视频&#xff1a; 【51单片机入门教程-2020版 程序全程纯手打 从零开始入门】https://www.…

服务器运维:服务器流量的二八法则是什么意思?

文章目录 用户行为角度时间分布角度应用场景角度 服务器流量的二八法则&#xff0c;又称 80/20 法则&#xff0c;源自意大利经济学家帕累托提出的帕累托法则&#xff0c;该法则指出在很多情况下&#xff0c;80% 的结果是由 20% 的因素所决定的。在服务器流量领域&#xff0c;二…

【LeetCode】嚼烂热题100【持续更新】

2、字母异位词分组 方法一&#xff1a;排序哈希表 思路&#xff1a;对每个字符串排序&#xff0c;排序后的字符串作为键插入到哈希表中&#xff0c;值为List<String>形式存储单词原型&#xff0c;键为排序后的字符串。 Map<String, List<String>> m new Ha…

赛灵思 XC7K325T-2FFG900I FPGA Xilinx Kintex‑7

XC7K325T-2FFG900I 是 Xilinx Kintex‑7 系列中一款工业级 (I) 高性能 FPGA&#xff0c;基于 28 nm HKMG HPL 工艺制程&#xff0c;核心电压标称 1.0 V&#xff0c;I/O 电压可在 0.97 V–1.03 V 之间灵活配置&#xff0c;并可在 –40 C 至 100 C 温度范围内稳定运行。该器件提供…

k8s-1.28.10 安装metrics-server

1.简介 Metrics Server是一个集群范围的资源使用情况的数据聚合器。作为一个应用部署在集群中。Metric server从每个节点上KubeletAPI收集指标&#xff0c;通过Kubernetes聚合器注册在Master APIServer中。为集群提供Node、Pods资源利用率指标。 2.下载yaml文件 wget https:/…

基于外部中中断机制,实现以下功能: 1.按键1,按下和释放后,点亮LED 2.按键2,按下和释放后,熄灭LED 3.按键3,按下和释放后,使得LED闪烁

题目&#xff1a; 参照外部中断的原理和代码示例,再结合之前已经实现的按键切换LED状态的实验&#xff0c;用外部中断改进其实现。 请自行参考文档《中断》当中&#xff0c;有关按键切换LED状态的内容, 自行连接电路图&#xff0c;基于外部中断机制&#xff0c;实现以下功能&am…

【我的创作纪念日】 --- 与CSDN走过的第365天

个人主页&#xff1a;夜晚中的人海 不积跬步&#xff0c;无以至千里&#xff1b;不积小流&#xff0c;无以成江海。-《荀子》 文章目录 &#x1f389;一、机缘&#x1f680;二、收获&#x1f3a1;三、 日常⭐四、成就&#x1f3e0;五、憧憬 &#x1f389;一、机缘 光阴似箭&am…

鸿蒙生态新利器:华为ArkUI-X混合开发框架深度解析

鸿蒙生态新利器&#xff1a;华为ArkUI-X混合开发框架深度解析 作者&#xff1a;王老汉 | 鸿蒙生态开发者 | 2025年4月 &#x1f4e2; 前言&#xff1a;开发者们的新机遇 各位鸿蒙开发者朋友们&#xff0c;是否还在为多平台开发重复造轮子而苦恼&#xff1f;今天给大家介绍一位…

‌信号调制与解调技术基础解析

调制解调技术是通信系统中实现基带信号与高频载波信号相互转换的主要技术&#xff0c;通过调整信号特性使其适应不同信道环境&#xff0c;保障信息传输的效率和可靠性。 调制与解调的基本概念 调制&#xff08;Modulation&#xff09;‌ 将低频基带信号&#xff08;如语音或数…

【扫描件批量改名】批量识别扫描件PDF指定区域内容,用识别的内容修改PDF文件名,基于C++和腾讯OCR的实现方案,超详细

批量识别扫描件PDF指定区域内容并重命名文件方案 应用场景 本方案适用于以下场景: 企业档案数字化管理:批量处理扫描的合同、发票等文件,按内容自动分类命名财务票据处理:自动识别票据上的关键信息(如发票号码、日期)用于归档医疗记录管理:从扫描的检查报告中提取患者I…

序列决策问题(Sequential Decision-Making Problem)

序列决策问题&#xff08;Sequential Decision-Making Problem&#xff09;是强化学习&#xff08;Reinforcement Learning, RL&#xff09;的核心研究内容&#xff0c;其核心思想是&#xff1a;​​智能体&#xff08;Agent&#xff09;需要在连续的时间步骤中&#xff0c;通过…

L2-1、打造稳定可控的 AI 输出 —— Prompt 模板与格式控制

一、为什么需要 Prompt 模板&#xff1f; 在与 AI 模型交互时&#xff0c;我们经常会遇到输出不稳定、格式混乱的问题。Prompt 模板帮助我们解决这些问题&#xff0c;通过结构化的输入指令来获得可预测且一致的输出结果。 模板的作用主要体现在&#xff1a; 固定输出格式&am…