参数有一个的返回值为bool的数据类型,为一元谓词
参数有两个的返回值为bool的数据类型,二元谓词
#include
#include
#include
#include
using namespace std;
//仿函数 返回值为bool的数据类型 为谓词
class stu {
public:
//参数有一个的时一元谓词
bool operator()(int val) {
return val > 5;
}
bool operator()(int val1, int val2) {
return val1 > val2;
}
};
void show(vector <int> ret) {
for (auto a : ret)
cout << a << " ";
}
int main() {
vector <int>ret;
for (int i = 0; i < 10; i++) {
ret.push_back(rand() % 100);
}
//vector ::iterator
auto isok = find_if(ret.begin(), ret.end(), stu());
if (isok == ret.end())
cout << "没找到" << endl;
else
cout << "找到了" << *isok << endl;
sort(ret.begin(), ret.end(), stu());
show(ret);
}
negate
#include
#include
#include
#include
using namespace std;
void test01() {
negate <int> n;//取反
cout << n(50)<<endl;
}
void test02() {
plus <int> n;
cout << n(10, 20)<<endl;
}
int main() {
test01();
test02();
}
大于 greater
大于等于 geater_equal
小于 less
小于等于 less_equal
等于 equal_to
不能与 not_equal_to
#include
#include
#include
#include
using namespace std;
void show(vector <int> &ret) {
for (vector<int>::iterator it = ret.begin(); it != ret.end(); it++)
cout << *it << " ";
cout << endl;
}
vector <int> init() {
vector <int> ret;
for(int i=0;i<10;i++)
ret.push_back(rand() % 50);
return ret;
}
int main() {
vector <int> ret;
ret = init();
show(ret);
//内建函数对象 greater用于排序
sort(ret.begin(), ret.end(), greater<int>());
show(ret);
}
logical_and 和
logical_not 非
logical_or 或
#include
#include
#include
#include
using namespace std;
void show(vector <bool>ret) {
for (vector<bool>::iterator it = ret.begin(); it != ret.end(); it++)
cout << *it << " ";
cout << endl;
}
vector <bool> init() {
vector <bool> ret;
for(int i=0;i<10;i++)
ret.push_back(rand() % 2);
return ret;
}
int main() {
vector <bool> ret;
ret = init();
show(ret);
//内建函数对象 greater用于排序
vector <bool>ret1(ret);
transform(ret.begin(), ret.end(),ret1.begin(),logical_not<bool>());
show(ret1);
}