/*
谓词
返回值是bool的仿函数
一元谓词:参数值1个
二元谓词:两个参数
*/
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class GFive
{
public:
bool operator()(int val)
{
return val>5;
}
};
class Mycompare
{
public:
bool operator()(int a,int b)
{
return a>b;
}
};
int main(){
/*一元谓词*/
vector<int> v;
v.push_back(12);
v.push_back(3);
v.push_back(7);
//查容器有没有大于5的
//find_if 按条件查询
vector<int>::iterator it = find_if(v.begin(),v.end(),GFive());//GFive匿名函数对象
if (it == v.end())
{
cout << "no find"<< endl;
}
else{
cout << "find:"<< *it << endl;
}
/*二元谓词*/
vector<int> v2;
v2.push_back(13);
v2.push_back(23);
v2.push_back(7);
v2.push_back(17);
//排序
sort(v2.begin(),v2.end());
for (vector<int>::iterator it = v2.begin();it != v2.end();it++)
{
cout << *it << " ";
}
cout << endl;
//使用函数对象,改变函数逻辑,从大到小
sort(v2.begin(),v2.end(),Mycompare());
for (vector<int>::iterator it = v2.begin();it != v2.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
37-【什么叫规矩 什么叫体统】谓词
最新推荐文章于 2022-12-28 10:28:16 发布