find和find_if算法(C++)

1 find

功能描述:

  • 查找指定元素,找到返回指定元素的迭代器,找不到返回结束迭代器end()

函数原型:

  • find(iterator beg, iterator end, value);

    // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

    // beg 开始迭代器

    // end 结束迭代器

    // value 查找的元素



在这里插入图片描述




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



/*- `find(iterator beg, iterator end, value);  `

  // 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

  // beg 开始迭代器

  // end 结束迭代器

  // value 查找的元素

*/

// 遍历函数
void Myprint1(int n) {

	cout << n << "    ";
}

// 查找内置数据类型
void test01() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(201);
	v1.push_back(3);
	v1.push_back(6);
	v1.push_back(58);
	v1.push_back(79);
	cout << "查找内置数据类型" << endl;
	cout << "容器v1里的元素如下:" << endl;
	for_each(v1.begin(), v1.end(), Myprint1);
	cout << endl << endl;

	// 查找5这个元素是否存在
	vector<int>::iterator it1=find(v1.begin(), v1.end(), 5);

	// 查找6这个元素是否存在
	vector<int>::iterator it2 = find(v1.begin(), v1.end(), 6);

	if (it1 == v1.end())
	{
		cout << "容器v1里没有5这个元素!!" << endl << endl;
	}
	else {

		cout << "已找到元素5!!" << endl;
		cout << "它在第 " << (it1 - v1.begin()) + 1 << "个位置" << endl << endl;
	}


	if (it2 == v1.end())
	{
		cout << "容器v1里没有6这个元素!!" << endl << endl;
	}
	else {

		cout << "已找到元素6!!" << endl;
		cout << "它在第 " << (it2 - v1.begin()) + 1 << "个位置" << endl << endl;
	}
	cout << endl << endl;

}

// 查找自定义数据类型
class Person {
public:

	Person(string name,int age):m_Age(age),m_Name(name){}

	int m_Age;
	string m_Name;



	// 重载==
    // 不然怎么比较两个对象是否相等呢
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}



};

void Myprint2(Person p) {
	cout << "姓名:" << p.m_Name << "     "
		<< "年龄:" << p.m_Age << endl;
}


void test02() {
	cout << "查找自定义数据类型" << endl;

	vector<Person> v2;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("fff", 50);


	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	cout << "容器v2里的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;

	// 查找p2是否在容器v2里
	vector<Person>::iterator it1 = find(v2.begin(), v2.end(), p2);

	// 查找p5是否在容器v2里
	vector<Person>::iterator it2 = find(v2.begin(), v2.end(), p5);

	if (it1 == v2.end())
	{
		cout << "容器v2里没有p2!!" << endl << endl;
	}
	else {

		cout << "已找到p2!!" << endl;
		cout << "它在第 " << (it1 - v2.begin()) + 1 << "个位置" << endl ;
		cout << "它的姓名为:" << (*it1).m_Name << "    "
			<< "它的年龄为:" << it1->m_Age << endl << endl;
	}


	if (it2 == v2.end())
	{
		cout << "容器v2里没有p5!!" << endl << endl;
	}
	else {

		cout << "已找到元素6!!" << endl;
		//cout << "它在第 " << (it2 - v2.begin()) + 1 << "个位置" << endl ;
		cout << "它的姓名为:" << (*it2).m_Name << "    "
			<< "它的年龄为:" << it2->m_Age << endl << endl;
	}
	cout << endl << endl;



}




int main() {

	test01();

	test02();

	return 0;
}

在这里插入图片描述



注意查找自定义数据类型时,一定要重载==号。STL内置的函数底层实现时,不会识别自定义的数据类型。

// 查找自定义数据类型
class Person {
public:

	Person(string name,int age):m_Age(age),m_Name(name){}

	int m_Age;
	string m_Name;



	// 重载==
    // 不然怎么比较两个对象是否相等呢
	bool operator==(const Person& p)
	{
		if (this->m_Name == p.m_Name && this->m_Age == p.m_Age)
		{
			return true;
		}
		return false;
	}
	


};





2 find_if

功能描述:

  • 按条件查找元素

函数原型:

  • find_if(iterator beg, iterator end, _Pred);

    按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置

    beg //开始迭代器

    end //结束迭代器

    _Pred //函数或者谓词(返回bool类型的仿函数)



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




void Myprint1(int n) {
	cout << n << "    ";
}


class Person {
public:
	Person(string name, int age) :m_Name(name), m_Age(age) {

	}

	// 重载=
	bool operator=(Person& p) {

		if (m_Name == p.m_Name && m_Age == p.m_Age) {
			return 1;
		}
		return 0;
	}



		int m_Age;
	string m_Name;



};






class MyFind1 {
public:
	bool operator()(int a) {
		return a > 10;
	}
	

};

// 不一定要用谓词,普通函数也可以
/*bool Mycompare1(int a) {
	return a > 10;

}*/


void test01() {

	vector<int> v1;
	v1.push_back(10);
	v1.push_back(201);
	v1.push_back(3);
	v1.push_back(6);
	v1.push_back(58);
	v1.push_back(79);
	cout << "查找内置数据类型" << endl;
	cout << "容器v1里的元素如下:" << endl;
	for_each(v1.begin(), v1.end(), Myprint1);
	cout << endl << endl;
	vector<int>::iterator it1 = find_if(v1.begin(), v1.end(),MyFind1());
//	vector<int>::iterator it1 = find_if(v1.begin(), v1.end(), Mycompare1);
	if (it1 == v1.end()) {
		cout << "容器里没有大于10的数!!" << endl << endl;
	}
	else {
		cout << "已找到一个容器里大于10的元素!!" << endl;
		cout <<"它的值为:"<< * it1 << endl ;
		cout << "它在容器里第" << (it1 - v1.begin()) + 1 << "个位置" << endl << endl;

	}
	cout << endl << endl;
}

void Myprint2(Person p) {
	cout<<"姓名:"<<p.m_Name<<"    "
		<<"年龄:"<<p.m_Age<<endl;

}

class MyFind2 {
public:
	bool operator()(Person p) {

		return p.m_Age > 28;

	}


};


// 不一定要用谓词,普通函数也可以
bool Mycompare2(Person p) {
	return p.m_Age > 40;

}



// find_if查找自定义数据类型
void test02() {
	cout << "查找自定义数据类型" << endl;

	vector<Person> v2;

	//创建数据
	Person p1("aaa", 10);
	Person p2("bbb", 20);
	Person p3("ccc", 30);
	Person p4("ddd", 40);
	Person p5("fff", 50);


	v2.push_back(p1);
	v2.push_back(p2);
	v2.push_back(p3);
	v2.push_back(p4);
	cout << "容器v2里的元素如下:" << endl;
	for_each(v2.begin(), v2.end(), Myprint2); cout << endl << endl;


	vector<Person>::iterator it2 = find_if(v2.begin(), v2.end(), MyFind2());
	if (it2 == v2.end()) {
		cout << "容器v2里没有年龄大于28的对象!!" << endl << endl;
	}
	else {
		cout << "已找到一个容器里年龄大于28的元素!!" << endl;
		cout << "它的姓名为:" << it2->m_Name<<"    "
			<< "它的年龄为:"<<(*it2).m_Age<<endl;
		cout << "它在容器里第" << (it2 - v2.begin()) + 1 << "个位置" << endl << endl;

	}
	cout << endl << endl;


	
		vector<Person>::iterator it3 = find_if(v2.begin(), v2.end(), Mycompare2);
	if (it3 == v2.end()) {
		cout << "容器v2里没有年龄大于40的对象!!" << endl << endl;
	}
	else {
		cout << "已找到一个容器里年龄大于40的元素!!" << endl;
		cout << "它的姓名为:" << it3->m_Name << "    "
			<< "它的年龄为:" << (*it3).m_Age << endl;
		cout << "它在容器里第" << (it3 - v2.begin()) + 1 << "个位置" << endl << endl;

	}
	cout << endl << endl;






}



int main() {

	test01();
	test02();
	system("pause");
	return 0;

}


在这里插入图片描述
总结:find_if按条件查找使查找更加灵活,提供的仿函数可以改变不同的策略






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++中,`std::find_if` 是一个算法函数,用于在给定范围内查找满足指定条件的元素。该函数需要三个参数:范围的起始迭代器、范围的结束迭代器和一个谓词(条件)函数。 以下是 `std::find_if` 的函数签名: ```cpp template<class InputIt, class UnaryPredicate> InputIt find_if(InputIt first, InputIt last, UnaryPredicate p); ``` 其中: - `InputIt` 是迭代器类型,表示范围的起始和结束位置。 - `UnaryPredicate` 是一个可调用对象,用于确定元素是否满足条件。它接受一个参数并返回一个 `bool` 类型的结果。 以下是一个使用 `std::find_if` 函数的示例代码,演示如何查找一个数组中第一个大于 5 的元素: ```cpp #include <iostream> #include <vector> #include <algorithm> bool isGreaterThan5(int num) { return num > 5; } int main() { std::vector<int> numbers = {2, 4, 6, 8, 10}; auto it = std::find_if(numbers.begin(), numbers.end(), isGreaterThan5); if (it != numbers.end()) { std::cout << "First element greater than 5: " << *it << std::endl; } else { std::cout << "No element greater than 5 found." << std::endl; } return 0; } ``` 输出结果为: ``` First element greater than 5: 6 ``` 在这个示例中,我们定义了一个 `isGreaterThan5` 函数作为谓词,用于确定元素是否大于 5。然后,我们使用 `std::find_if` 函数在 `numbers` 向量中查找第一个满足条件的元素,并将结果存储在迭代器 `it` 中。最后,我们输出找到的元素(如果存在)或相应的提示信息。 你可以根据自己的需求编写不同的谓词函数来满足不同的查找条件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值