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

STL中find相关的算法


秋词二首

刘禹锡刘禹锡〔唐代〕

自古逢秋悲寂寥,我言秋日胜春朝。

晴空一鹤排云上,便引诗情到碧霄


💢find相关的算法

Column 1 Column 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,返回器对应的迭代器。

cpp 复制代码
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对应的迭代器。

cpp 复制代码
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举例

cpp 复制代码
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举例

cpp 复制代码
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
相关推荐
keke1035 分钟前
Java【14_2】接口(Comparable和Comparator)、内部类
java·开发语言
CN.LG1 小时前
Java 乘号来重复字符串的功能
java·开发语言
L_cl1 小时前
【Python 算法零基础 3.递推】
算法
萌新下岸多多关照1 小时前
Java中synchronized 关键字
java·开发语言
中国lanwp1 小时前
使用Maven部署WebLogic应用
java·maven
醍醐三叶1 小时前
C++文件操作--2 二进制文件操作
开发语言·c++
int型码农1 小时前
数据结构第七章(四)-B树和B+树
数据结构·b树·算法·b+树
li星野1 小时前
C++:C++内存管理
开发语言·c++
开开心心就好1 小时前
Word图片格式调整与转换工具
java·javascript·spring·eclipse·pdf·word·excel
溟洵1 小时前
【C++ Qt】布局管理器
开发语言·c++·qt