C++之常用算法

news2024/9/24 21:20:55

C++之常用算法

在这里插入图片描述

for_each

transform

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>

class Tranfor 
{
public:
	int operator()(int var)
	{
		return var;
	}
};

class MyPrint
{
public:
	void operator()(int var)
	{
		cout << var<<" " ;
	}
};
void test()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}

	vector<int>vTarget;//目标容器
	vTarget.resize(v.size());//目标容器需要提前开辟空间

	transform(v.begin(),v.end(),vTarget.begin(),Tranfor());

	//遍历目标容器
	for_each(vTarget.begin(), vTarget.end(),MyPrint());
	cout << endl;
}

int main()
{
	test();

	system("pause");
	return 0;
}

在这里插入图片描述

常用查找算法

find

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//查找内置数据类型

void test()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	
	vector<int>::iterator it = find(v.begin(),v.end(),5);
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" <<  *it << endl;
	}
}

//查找自定义数据类型
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	//重载== 底层find知道该如何对比Person数据类型
	bool operator==(const Person&p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

void test2()
{
	//定义容器
	vector<Person>v1;
	//创建数据(对象)
	Person p1("aaa", 20);
	Person p2("bbb", 30);
	Person p3("ccc", 40);
	Person p4("ddd", 50);

	//将数据放入容器
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);

	//查到对象数据
	vector<Person>::iterator it = find(v1.begin(), v1.end(), p2);
	if (it == v1.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;
	}
}

int main()
{
	test2();

	system("pause");
	return 0;
}

在这里插入图片描述

find_if

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//查找内置数据类型
class Greater
{
public:
	bool operator()(int val)
	{
		return val > 5;
	}
};

void test()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	
	vector<int>::iterator it = find_if(v.begin(),v.end(), Greater());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" <<  *it << endl;
	}
}

//查找自定义数据类型
class Person
{
public:
	Person(string name,int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
class GreatAge
{
public:
	bool operator()(Person&p)
	{
		return p.m_Age > 20;
	}
};
void test2()
{
	//定义容器
	vector<Person>v1;
	//创建数据(对象)
	Person p1("aaa", 20);
	Person p2("bbb", 30);
	Person p3("ccc", 40);
	Person p4("ddd", 50);

	//将数据放入容器
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);

	//查到对象数据
	vector<Person>::iterator it = find_if(v1.begin(), v1.end(), GreatAge());
	if (it == v1.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了  名字为:" << it->m_Name << "年龄为:" << it->m_Age << endl;
	}
}

int main()
{
	test2();

	system("pause");
	return 0;
}

在这里插入图片描述

adjacent_find

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

void test()
{
	vector<int>v;

	v.push_back(9);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	v.push_back(5);

	vector<int>::iterator it = adjacent_find(v.begin(),v.end());
	if (it == v.end())
	{
		cout << "没有找到" << endl;
	}
	else
	{
		cout << "找到了" <<  *it << endl;
	}
}


int main()
{
	test();

	system("pause");
	return 0;
}

在这里插入图片描述

binary_search

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

void test()
{
	vector<int>v;
	for (int i = 0;i < 10;i++)
	{
		v.push_back(i);
	}
	//注意:容器必须是有序的序列
	bool rat =  binary_search(v.begin(), v.end(), 9);
	if (rat == true)
	{
		cout << "找到了" << endl;
	}
	else
	{
		cout << "没有找到" << endl;
	}
}


int main()
{
	test();

	system("pause");
	return 0;
}

在这里插入图片描述

count

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>

void test()
{
	vector<int>v;
	v.push_back(30);
	v.push_back(50);
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);

	int num = count(v.begin(), v.end(), 50);
	
	cout << "找到元素50" << num << endl;
}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	//重载== 底层find知道该如何对比Person数据类型
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	string m_Name;
	int m_Age;
};

void test2()
{
	//定义容器
	vector<Person>v1;
	//创建数据(对象)
	Person p1("aaa", 20);
	Person p2("bbb", 30);
	Person p3("ccc", 40);
	Person p4("ddd", 50);

	//将数据放入容器
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);

	//查到对象数据
	Person p5("ddd", 50);
	int num = count(v1.begin(), v1.end(), p5);
	cout << "和ddd同岁的人" << num << endl;
}
int main()
{
	test2();

	system("pause");
	return 0;
}

在这里插入图片描述
在这里插入图片描述

count_if

在这里插入图片描述

#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
class Greater
{
public:
	bool operator()(int val)
	{
		return val > 40;
	}
};
void test()
{
	vector<int>v;
	v.push_back(30);
	v.push_back(50);
	v.push_back(30);
	v.push_back(20);
	v.push_back(50);

	int num = count_if(v.begin(), v.end(), Greater());
	
	cout << "找到大于40的个数" << num << endl;
}

//查找自定义数据类型
class Person
{
public:
	Person(string name, int age)
	{
		this->m_Name = name;
		this->m_Age = age;
	}
	string m_Name;
	int m_Age;
};
class GreatAge
{
public:
	bool operator()(Person& p)
	{
		return p.m_Age > 20;
	}
};
void test2()
{
	//定义容器
	vector<Person>v1;
	//创建数据(对象)
	Person p1("aaa", 20);
	Person p2("bbb", 30);
	Person p3("ccc", 40);
	Person p4("ddd", 50);

	//将数据放入容器
	v1.push_back(p1);
	v1.push_back(p2);
	v1.push_back(p3);
	v1.push_back(p4);

	int num = count_if(v1.begin(), v1.end(), GreatAge());
	cout << "岁数大于20的个数" << num << endl;
}
int main()
{
	test2();

	system("pause");
	return 0;
}

在这里插入图片描述

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

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

相关文章

Shell判断:流程控制—if(二)

一、多分支结构 1、语法&#xff1a; if 条件测试1 then 命令序列 elif 条件测试2 then 命令序列 elif 条件测试3 then 命令序列.... else 命令序列 fi 2、示例&am…

入股合作协议要不要写章程

公司章程&#xff0c;是注册公司的基本文件&#xff0c;也公司必备的规定公司组织及活动基本规则的书面文件&#xff0c;是公司成立的必不可少的基础&#xff0c;也是公司赖以生存的灵魂。那么&#xff0c;这次要和大家讨论的是有关于入股合作协议要不要写章程的问题了。 入股合…

【开发流程】持续集成、持续交付、持续部署

一、开发工作流程 假设把开发流程分为以下几个阶段&#xff1a; 编码 -> 构建 -> 集成 -> 测试 -> 交付 -> 部署 如上图所示&#xff0c;持续集成、持续交付、持续部署有着不同的软件自动交付周期。 二、持续集成、持续交付、持续部署 1、持续集成 持续集成…

【Java 进阶篇】JQuery 事件绑定:`on` 与 `off` 的奇妙舞曲

在前端开发的舞台上&#xff0c;用户与页面的互动是一场精彩的表演。而 JQuery&#xff0c;作为 JavaScript 的一种封装库&#xff0c;为这场表演提供了更为便捷和优雅的事件绑定方式。其中&#xff0c;on 和 off 两位主角&#xff0c;正是这场奇妙舞曲中的核心演员。在这篇博客…

【STM32】IIC(Inter Integrated Cirruit--集成电路总线)

【单片机】14-I2C通信之EEPROM-CSDN博客 一、IIC总线协议介绍 1.IIC简介 同步&#xff08;有时钟频率&#xff09;&#xff0c;半双工&#xff08;同一个时间只能接收或者发送&#xff09;&#xff0c;串行&#xff08;一个字节一个字节传输&#xff09;&#xff0c;高位读取…

卡尔曼滤波器在车流量检测中的应用

目录 1. 作者介绍2. 卡尔曼滤波器2.1 卡尔曼滤波概述2.2 标志性发展2.3 卡尔曼公式理解 3. 车流量检测3.1 背景介绍3.2 实现过程3.2.1 YOLOv3网络模型结构3.2.2 SORT算法3.2.3 基于虚拟线圈法的车辆统计 4. 算法实现4.1 Kalman.py4.2 完整代码4.3 结果展示 1. 作者介绍 吴思雨…

左支座零件的机械加工工艺规程及工艺装备设计【计算机辅助设计与制造CAD】

wx供重浩&#xff1a;创享日记 对话框发送&#xff1a;左支座 获取完整CAD工程源文件论文报告说明书等 一、论文目录 二、论文部分内容 设计任务 1.完成左支座零件—毛坯合图及左支座零件图 2.完成左支座零件工艺规程设计 3.完成左支座零件加工工艺卡 4.机床专用夹具装备总图 …

小迪笔记(1)——操作系统文件下载反弹SHELL防火墙绕过

名词解释 POC&#xff1a;验证漏洞存在的代码&#xff1b; EXP&#xff1a;利用漏洞的代码&#xff1b; payload&#xff1a;漏洞利用载荷&#xff0c; shellcode&#xff1a;漏洞代码&#xff0c; webshell&#xff1a;特指网站后门&#xff1b; 木马&#xff1a;强调控制…

华为流量统计的2种配置方法

第一种就是用实打实的配置去设置&#xff0c;配置比较复杂 比如华为防火墙流量统计&#xff1a; acl 3000 rule permit ip source 192.168.0.1 0.0.0.0 dest 10.0.0.1 0.0.0.0 diagnose firewall statistic acl 3000 enable dis firewall statistics acl //流量查看 另一种最…

C语言实现带头双向循环链表

文章目录 写在前面1. 链表节点的定义2. 链表的初始化3. 插入数据3.1 头插3.2 尾插3.3 在指定位置的前面插入数据 4 删除数据4.1 头删4.2 尾删4.3 删除指定位置的数据 5 查找并修改数据5. 链表的销毁 写在前面 上面文章用C语言实现了单链表的增删查改&#xff0c;我们知道&…

Python将已标注的两张图片进行上下拼接并修改、合并其对应的Labelme标注文件

Python将已标注的两张图片进行上下拼接并修改、合并其对应的Labelme标注文件 前言前提条件相关介绍实验环境上下拼接图片并修改、合并其对应的Labelme标注文件代码实现输出结果 前言 由于本人水平有限&#xff0c;难免出现错漏&#xff0c;敬请批评改正。更多精彩内容&#xff…

手写消息队列(基于RabbitMQ)

一、什么是消息队列&#xff1f; 提到消息队列是否唤醒了你脑海深处的记忆&#xff1f;回看前面的这篇文章&#xff1a;《Java 多线程系列Ⅳ&#xff08;单例模式阻塞式队列定时器线程池&#xff09;》&#xff0c;其中我们在介绍阻塞队列时说过&#xff0c;阻塞队列最大的用途…

【Linux】:体系结构与进程概念

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关Linux体系结构和进程的知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入…

「Java开发指南」如何在Spring中使用JAX-WS注释器?

本文将指导您如何使用JAX-WS注释器从Spring服务生成JAX-WS Web服务&#xff0c;在本教程中&#xff0c;您将学习如何&#xff1a; 为Spring服务启用JAX-WS部署应用程序并测试服务 所有与Spring scaffolding相关的任务都需要MyEclipse Spring或Bling授权。 MyEclipse v2023.1…

『力扣刷题本』:二叉树的中序遍历

一、题目 给定一个二叉树的根节点 root &#xff0c;返回 它的 中序 遍历 。 示例 1&#xff1a; 输入&#xff1a;root [1,null,2,3] 输出&#xff1a;[1,3,2]示例 2&#xff1a; 输入&#xff1a;root [] 输出&#xff1a;[]示例 3&#xff1a; 输入&#xff1a;root [1…

【NI-DAQmx入门】校准

1.设备定期校准的理由 随着时间的推移电子器件的特性会发生自然漂移&#xff0c;可能会导致测量结果的不准确性。防止出现良品和差品筛选出错的情况满足行业国际标准降低设备出现故障的风险使测量结果更具备参考性 2.查找NI设备的校准间隔。 定期校准会使DAQ设备的精度保持在…

Linux远程工具专家推荐(二)

8. Apache Guacamole Apache Guacamole 是一款免费开源的无客户端远程桌面网关&#xff0c;支持 VNC、RDP 和 SSH 等标准协议。无需插件或客户端软件&#xff1b;只需使用 HTML5 Web 应用程序&#xff08;例如 Web 浏览器&#xff09;即可。 这意味着您的计算机的使用不受任何一…

线性表--链表-1

文章目录 主要内容一.链表练习题1.设计一个递归算法&#xff0c;删除不带头结点的单链表 L 中所有值为 X 的结点代码如下&#xff08;示例&#xff09;: 2.设 L为带头结点的单链表&#xff0c;编写算法实现从尾到头反向输出每个结点的值代码如下&#xff08;示例&#xff09;: …

Jave 定时任务:使用Timer类执行定时任务为何会发生任务阻塞?如何解决?

IDE&#xff1a;IntelliJ IDEA 2022.2.3 x64 操作系统&#xff1a;win10 x64 位 家庭版 JDK: 1.8 文章目录 一、Timer类是什么&#xff1f;二、Timer类主要由哪些部分组成&#xff1f;1.TaskQueue2. TimerThread 三、示例代码分析四、自定义TimerTask为什么会发生任务相互阻塞的…

度加创作工具 演示

度加创作工具 功能图功能测试文比润色测试经验分享测试测试输出测试输出工具地址功能图 功能测试 文比润色测试 经验分享测试 测试输出 在人工智能领域,我们一直在追求一个终极目标:让机器能够像人类一样,能够理解、学习和解决各种复杂问题。而要实现这个目标,我们需要将…