001谓词的概念与一元谓词例子

#include<iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
/*
 * 返回bool类型的仿函数称为谓词
如果operator()接受一个参数,那么叫做一元谓词
如果operator()接受两个参数,那么叫做二元谓词
 */

//一元谓词
class GreatFive
{
public:

	bool operator()(int val)
	{
		return val>5;
	}
};
void test01()
{
	vector<int> v;
	for (int i=0;i<10;i++)
	{
		v.push_back(i);
		//查找大于5的第一个元素的位置
		
	}
	vector<int>::iterator it=find_if(v.begin(),v.end(),GreatFive());//使用一个匿名的函数对象
		//谓词英语 predicate
		if (it==v.end())
		{
			cout<<"没找到"<<endl;
		}
		else
		{
			cout<<"找到了,这个元素是:"<<*it<<endl;
		}
}
using namespace std;
int main(void)
{
	test01();
	system("pause");
	return 0;
}
/*
 * 找到了,这个元素是:6
请按任意键继续. . .

 */

猜你喜欢

转载自blog.csdn.net/baixiaolong1993/article/details/89640572
今日推荐