谓词
谓词是指昔通函数或重载的operator(拨回值是bool类型的函数对象(仿函数)。如果operator接受一个参数,那么叫做一元谓词,如果接受两个参数,那么叫做二元谓词,谓词可作为一个判断式。
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class MoreThan20
{
public:
bool operator()(int val)
{
return val > 20;
}
};
//一元谓词
void test01()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//查找第一个大于20的数字
//vector<int>::iterator it = find_if(v.begin(),v.end(),MoreThan20());
auto it = find_if(v.begin(), v.end(), MoreThan20());
cout << *it << endl;
}
class MyCompare
{
public:
bool operator()(int v1,int v2)
{
return v1 > v2;
}
};
//二元谓词
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
sort(v.begin(),v.end(),MyCompare());
for (auto it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
test02();
return 0;
}
?内建函数对象
STL内建了一些函数对象。分为:算数类函数对象,关系运算类函数对象,逻辑运算类仿函数。这些仿函数所产生的对象,用法和一般函数完全相同,当然我们还可以产生无名的临时对象来履行函数功能。使用内建函数对象,需要引入头文件#include<functional>。
#include <iostream>
#include <vector>
using namespace std;
//内建函数头文件
#include <functional>
#include <algorithm>
//算术类函数对象
void test01()
{
//template<class T> T negate<T> //取反仿函数
negate<int>n;
cout << n(10) << endl;
//加法仿函数
plus<int>p;
cout << p(1,1) << endl;
}
//关系运算类函数对象
void test02()
{
vector<int>v;
v.push_back(10);
v.push_back(20);
v.push_back(30);
v.push_back(40);
v.push_back(50);
//内建函数大于
sort(v.begin(),v.end(),greater<int>());
/*for (auto it = v.begin();it != v.end();it++)
{
cout << *it << " ";
}
cout << endl;*/
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
int main()
{
test02();
return 0;
}
?适配器使用
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
#include <string>
using namespace std;
class MyPrint:public binary_function<int,int,void>
{
public:
void operator()(int v,int start) const
{
cout << v + start<< " ";
}
};
//绑定适配器
void test01()
{
vector<int>v;
for (int i = 0;i<10;i++)
{
v.push_back(i);
}
//for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
int num;
cin >> num;
for_each(v.begin(), v.end(),bind2nd(MyPrint(),num));
}
class MoreThanFive : public unary_function<int,bool>
{
public:
bool operator()(int val) const
{
return val > 5;
}
};
//一元取反适配器 not1
void test02()
{
//一元取反
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//查找大于5的数字
auto pos = find_if(v.begin(), v.end(), not1(MoreThanFive()));
cout << *pos << endl;
}
void MyPrint03(int v,int start)
{
cout << v + start << " ";
}
//函数指针适配器
void test03()
{
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
//for_each(v.begin(), v.end(), MyPrint03);
for_each(v.begin(), v.end(), bind2nd(ptr_fun(MyPrint03),100));
}
//成员函数适配器
class Person
{
public:
string m_Name;
int m_Age;
public:
Person(string name,int age):m_Name(name),m_Age(age){}
void showPerson()
{
cout << "成员函数中:姓名:" << m_Name << "年龄:" << m_Age << endl;
}
};
void MyPrintPerson(Person &p)
{
cout << "姓名:" << p.m_Name << " " << "年龄:" << p.m_Age << endl;
}
void test04()
{
vector<Person>v;
Person p1("aaa", 10);
Person p2("bbb", 15);
Person p3("ccc", 13);
Person p4("ddd", 19);
v.push_back(p1);
v.push_back(p2);
v.push_back(p3);
v.push_back(p4);
//for_each(v.begin(), v.end(), MyPrintPerson);
//成员函数适配器
for_each(v.begin(), v.end(),mem_fun_ref(&Person::showPerson));
}
int main()
{
test04();
return 0;
}
|