C++标准库---find&find_if

find: 根据equality操作符,循环查找[first,last)内的所有元素,找出第一个匹配"等同(equality)条件"者。如果找到,就返回一个InputIterator指向该元素,否则返回迭代器last。


template<class InputIterator,class T>
InputIterator find(InputIterator first,InputIterator last,const T& value)

find_if:根据指定的pred运算条件(以仿函数表示),循环查找[first,last)内的所有元素,找出第一个令pred运算结果true者。如果找到就返回一个InputIterator指向该元素,否则就返回迭代器last。

template<class InputIterator,class Predicate>

InputIterator find_if(InputIterator first,InputIterator last,Predicate pred)


代码示例:

//vector
#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
#include<string>

using namespace std;

class Student
{
public:
	Student(){}
	Student(string iname,float iscore):name(iname),score(iscore){}
	~Student(){}

	string name;
	float score;
};

class StudentAdapter:public unary_function<Student,bool>
{
private:
	string name;
public:
	explicit StudentAdapter(string iname):name(iname){}
	bool operator()(const Student& student)
	{
		return (student.name==name);
	}
};

int main()
{
	vector<Student> admin;
	vector<Student>::iterator pos;


	Student stu1("lanzhihui",89.1);
	Student stu2("wangdan",89.2);
	Student stu3("wangqian",89.3);

	admin.push_back(stu1);
	admin.push_back(stu2);
	admin.push_back(stu3);

	for(pos=admin.begin();pos!=admin.end();++pos)
	{
		cout<<pos->name<<" "<<pos->score<<endl;
	}
	cout<<endl;

	//实验find_if
	pos=find_if(admin.begin(),admin.end(),StudentAdapter("lanzhihui"));

	if(pos!=admin.end())
	{
		cout<<pos->name<<" "<<pos->score<<endl;
	}

	//以下实验find
	vector<int> coll;
	vector<int>::iterator pos1;
	for(int i=0;i<10;i++)
	{
		coll.push_back(i);
	}

	pos1=find(coll.begin(),coll.end(),5);

	if(pos1!=coll.end())
	{
		cout<<*pos1<<" is find"<<endl;
	}

	system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值