C++之常用算法

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;
}
复制代码
#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;
}
相关推荐
CoovallyAIHub2 分钟前
单目深度估计重大突破:无需标签,精度超越 SOTA!西湖大学团队提出多教师蒸馏新方案
深度学习·算法·计算机视觉
CoovallyAIHub5 分钟前
从FCOS3D到PGD:看深度估计如何快速搭建你的3D检测项目
深度学习·算法·计算机视觉
偷偷的卷33 分钟前
【算法笔记 day three】滑动窗口(其他类型)
数据结构·笔记·python·学习·算法·leetcode
北京地铁1号线39 分钟前
Zero-Shot(零样本学习),One-Shot(单样本学习),Few-Shot(少样本学习)概述
人工智能·算法·大模型
大白的编程日记.1 小时前
【计算机基础理论知识】C++篇(二)
开发语言·c++·学习
C语言小火车1 小时前
野指针:C/C++内存管理的“幽灵陷阱”与系统化规避策略
c语言·c++·学习·指针
凤年徐1 小时前
【数据结构】时间复杂度和空间复杂度
c语言·数据结构·c++·笔记·算法
kualcal1 小时前
代码随想录17|二叉树的层序遍历|翻转二叉树|对称二叉树
数据结构·算法
踏莎行hyx1 小时前
使用langchain连接llama.cpp部署的本地deepseek大模型开发简单的LLM应用
c++·ai·langchain·大模型·llama.cpp·deepseek
山河木马1 小时前
前端学C++可太简单了:双冒号 :: 操作符
前端·javascript·c++