当前位置: 首页 > article >正文

C++系列-STL中find相关的算法

STL中find相关的算法

  • 💢find相关的算法
    • 💢💢find,find_if举例
    • 💢💢find_first_of举例
    • 💢💢find_end举例
    • 💢💢adjacent_find举例


秋词二首
刘禹锡刘禹锡〔唐代〕

自古逢秋悲寂寥,我言秋日胜春朝。
晴空一鹤排云上,便引诗情到碧霄


💢find相关的算法

Column 1Column 2
find(beg, end, val)利用==运算符,对[begin, end)的元素与val进行比较,当匹配时结束搜索,返回该元素的迭代器
find_if(beg, end, pred)使用谓词代替find中的==操作符,执行find
find_first_of(beg1, end1, beg2, end2)在[beg1, end1)范围内查找[beg2, end2)中任意一个元素的第一次出现,返回该元素[beg1, end1)中的迭代器
find_first_of(beg1, end1, beg2, end2, pred)使用谓词代替==操作符,执行find_first_of
find_end(beg1, end1, beg2, end2)在[beg1, end1)范围内查找[beg2, end2)这一串元素的最后一次出现,返回序列匹配到的在第一个序列中的迭代器
find_end(beg1, end1, beg2, end2, pred)使用谓词代替==执行执行find_end, 返回值同find_end
adjacent_find(beg, end)在[beg, end)范围内查找相邻相同的元素,找到则返回第一个元素的迭代器,否则返回end迭代器
adjacent_find(beg, end, pred)使用谓词代替==,找到则返回第一个元素的迭代器,否则返回end迭代器

💢💢find,find_if举例

👉 👉 👉find和find_if只从头找第一个,找到第一个满足条件的就返回。
find(vec1.begin(), vec1.end(), 3),从vec1中找==3的元素,返回其对应的迭代器。
find_if(vec3.begin(), vec3.end(), [](int val) -> bool {return val > 10;}),从vec3中满足后面的谓词的元素,即元素值大于10,返回器对应的迭代器。

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

template<class T>
class MyPrint
{
public:
	void operator()(T& val)
	{
		cout << val << " ";
	}
};

void test01()
{
	// find举例
	vector<int> vec1{1, 2, 3, 4, 5, 3, 3};
	// find(beg, end, val) find等于3
	vector<int>::iterator it = find(vec1.begin(), vec1.end(), 3);
	if (it != vec1.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it:vec1.end())中的元素: " << endl;
		for_each(it, vec1.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void test02()
{
	// find_if举例
	vector<int> vec3{1, 3, 10, 11, 12, 3};
	// find_if(beg, end, pred),一元谓词判断是否大于10
	vector<int>::iterator it = find_if(vec3.begin(), vec3.end(), [](int val) -> bool {return val > 10;});
	if (it != vec3.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it:vec3.end())中的元素: " << endl;
		for_each(it, vec3.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void main()
{
	test01();
	test02();
	system("pause");
}

result:
*it:3
[it:vec1.end())中的元素:
3 4 5 3 3

*it:11
[it:vec3.end())中的元素:
11 12 3

💢💢find_first_of举例

👉 👉 👉find_first_of只从头找第一个,可能第二个序列中有多个元素在第一个序列中都能找到,但是以它们在第一个序列中最靠前的一个为最终找到的结果。
示例中第二个序列vec12{8, 5, 7, 10, 12, 3},10能在第一个序列vec11{1, 3, 10, 11, 12, 3}中找到,3也可以,但是3更靠前,所以返回第一个序列中3对应的迭代器。

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


template<class T>
class MyPrint
{
public:
	void operator()(T& val)
	{
		cout << val << " ";
	}
};

void test03()
{
	// find_first_of举例
	vector<int> vec11{1, 3, 10, 11, 12, 3};
	vector<int> vec12{8, 5, 7, 10, 12, 3};
	vector<int>::iterator it = find_first_of(vec11.begin(), vec11.end(), vec12.begin(), vec12.end());
	if (it != vec11.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it:vec12.end())中的元素: " << endl;
		for_each(it, vec11.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void test04()
{
	// find_first_of举例, pred的目标,第二组里的任意一个元素大于第一组的任意一个元素(都从头开始找),如果满足就算找到了,返回第一组元素对应的迭代器
	vector<int> vec11{18, 3, 10, 11, 12, 3};
	vector<int> vec12{0, 1};
	vector<int>::iterator it = find_first_of(vec11.begin(), vec11.end(), vec12.begin(), vec12.end(), [](int val1, int val2) ->bool {return val2 > val1;});
	if (it != vec11.end())
	{
		cout << "找到了,*it:" << *it << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void main()
{
	test03();
	test04();
	system("pause");
}

result:
*it:3
[it:vec12.end())中的元素:
3 10 11 12 3

找不到

💢💢find_end举例

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


template<class T>
class MyPrint
{
public:
	void operator()(T& val)
	{
		cout << val << " ";
	}
};

void test01()
{
	// find_end举例,以第二个容器为基准,从第一个容器中找是否有连续的元素和第二个容器的元素完全相同,
	// 也就是说看第二个容器是否是第一个的子序列,并找出最后一次匹配的迭代器(此迭代器为第一个序列的迭代器)
	vector<int> vec11{1, 3, 10, 11, 12, 3, 10, 5};
	vector<int> vec12{3, 10};
	vector<int>::iterator it = find_end(vec11.begin(), vec11.end(), vec12.begin(), vec12.end());
	if (it != vec11.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it:vec12.end())中的元素: " << endl;
		for_each(it, vec11.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void test02()
{
	// find_end举例,以第二个容器为基准,从第一个容器中是否有连续的元素均满足小于第二个容器中的元素,
	// 找到最后一组,并把对应的第一个容器对应的迭代器返回。
	vector<int> vec11{1, 3, 10, 11, 12, 3, 10, 5, 15};
	vector<int> vec12{3, 10};
	vector<int>::iterator it = find_end(vec11.begin(), vec11.end(), vec12.begin(), vec12.end(), [](int val1, int val2) -> bool {return val1>=val2;});
	if (it != vec11.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it:vec12.end())中的元素: " << endl;
		for_each(it, vec11.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void main()
{
	test01();
	test02();
	system("pause");
}

result:
*it:3
[it:vec12.end())中的元素:
3 10 5

*it:5
[it:vec12.end())中的元素:
5 15


💢💢adjacent_find举例

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


template<class T>
class MyPrint
{
public:
	void operator()(T& val)
	{
		cout << val << " ";
	}
};

void test01()
{
	// adjacent_find举例,查找是否有相邻的元素相等,返回相等的第一个元素的迭代器
	vector<int> vec1{1, 3, 10, 10, 12, 10, 10, 5};
	vector<int>::iterator it = adjacent_find(vec1.begin(), vec1.end());
	if (it != vec1.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it到vec1.end())中的元素: " << endl;
		for_each(it, vec1.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void test02()
{
	// adjacent_find举例,查找是否有相邻的元素中的第一个整除第二个,返回满足条件的第一个元素的迭代器
	vector<int> vec1{1, 3, 10, 4, 12, 6, 10, 5};
	vector<int>::iterator it = adjacent_find(vec1.begin(), vec1.end(), [](int val1, int val2) -> bool {return val1 % val2 == 0;});
	if (it != vec1.end())
	{
		cout << "*it:" << *it << endl;
		cout << "[it到vec1.end())中的元素: " << endl;
		for_each(it, vec1.end(), MyPrint<int>());
		cout << endl << endl;
	}
	else
	{
		cout << "找不到" << endl << endl;
	}
}

void main()
{
	test01();
	test02();
	system("pause");
}

result:
*it:10
[it到vec1.end())中的元素:
10 10 12 10 10 5

*it:12
[it到vec1.end())中的元素:
12 6 10 5

http://www.kler.cn/news/309262.html

相关文章:

  • 联合仿真(FMI,FMU)资料收集
  • 【mysql】逻辑运算符
  • QT实现TCP协议
  • 『功能项目』第二职业法师的平A【57】
  • nlohmann::json中有中文时调用dump转string抛出异常的问题
  • C++:类和对象全解
  • 【C++】日期类基础题
  • 笔记整理—内核!启动!—kernel部分(6)buxybox详解
  • 视觉检测中的深度学习应用
  • vue3 ref的用法及click事件的说明
  • 使用 uni-app 开发微信小程序的详细指南
  • go mod文件为啥又两个require
  • C#使用TCP-S7协议读写西门子PLC(四)
  • Qt常用控件——QDateTimeEdit
  • 【华为OD】2024D卷——生成哈夫曼树
  • CAD图纸加密软件哪个好?10款2024主流CAD图纸加密软件分享!
  • 如何利用Samba跨平台分享Ubuntu文件夹
  • 电路设计学习(一)
  • 【Day14-单例设计模式动态代理】
  • 一文吃透JVM面试八股文
  • 每日学习一个数据结构-DFA确定有限状态机
  • 【linux】VisiData:强大的命令行数据处理工具
  • 跟李沐学AI:序列到序列seq2seq
  • 本地部署大模型并使用知识库Windows下Ollama+Docker+MaxKB安装的记录
  • 影刀RPE学习——自动化
  • 地大信息-基础信息平台 GetImg 任意文件读取漏洞复现
  • http和https分别是什么?区别是什么?
  • GO GIN SSE DEMO
  • Springboot项目打war包运行及错误解决
  • SpringCloud Alibaba入门简介