函数对象
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
bool func(int val1,int val2)
{
return val1 > val2;
}
void test()
{
vectorv;
v.push_back(2);
v.push_back(7);
v.push_back(4);
//sort(v.begin(), v.end(),func);//降序
sort(v.begin(), v.end(), greater());
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
//[](int val) {cout << val << " "; }[]是匿名函数
}
//函数对象与普通函数的区别
//1:函数对象能有自己的状态
//2:普通函数没有类型,但函数对象有
//3:函数对象比普通函数更有效率,(成员函数自动申请为内联函数)
void test01()
{
plusmyplus;
cout << myplus(3, 5) << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
适配器
#define _CRT_SECURE_NO_WARNINGS
#include
#include
#include
#include
using namespace std;
void print(int val)
{
cout << val << " ";
}
struct myfunc
{
bool operator()(int val)
{
cout << val << " "<
struct myfunc2 :public binary_function
{
void operator()(int val1, int val2)const
{
cout << val1 + val2 << " ";//第三步实现函数体
}
};
void test()
{
vectorv;
v.push_back(2);
v.push_back(34);
v.push_back(122);
//for_each(v.begin(), v.end(), print);
for_each(v.begin(), v.end(), myfunc());
//返回时每个都加十
for_each(v.begin(), v.end(), bind2nd(myfunc2(),10));//第四步:用bind2nd绑定函数对象
}
//bind1st把10绑定给第一个参数,bind2nd把10绑定给第二个参数
//函数对象适配器not1 not2取反
//1:not1和not2区别:not1针对一元函数对象,not2针对二元函数对象
//第一步:继承
struct mynotfunc:public unary_function
{
bool operator()(int val)const//第二步:变常函数
{
return val >= 20;
}
};
int myfind(int val)
{
return val > 20;
}
void test02()
{
vectorv;
v.push_back(2);
v.push_back(34);
v.push_back(122);
vector::iterator it=find_if(v.begin(), v.end(),not1( mynotfunc()));//第三步:适配
//vector::iterator it = find_if(v.begin(), v.end(), myfind);
if (it == v.end())
{
cout << " 查找失败" << endl;
}
else
{
cout << " 查找成功" << *it << endl;//结果:打印出2
}
}
//not2的使用
struct myprint
{
bool operator()(int val1)
{
cout << val1 << " ";
return true;
}
};
struct mysort
{
bool operator()(int val1, int val2)
{
return val1 > val2;
}
};
void test03()
{
vectorv;
v.push_back(2);
v.push_back(34);
v.push_back(122);
//sort(v.begin(), v.end(),mysort());
//debug下会报错
sort(v.begin(), v.end(), not2(less()));
//for_each(v.begin(), v.end(), myprint());
for_each(v.begin(), v.end(), [](int val) {cout << val << " "; });
}
//普通函数进行适配
//第一步:把一个参数变两个参数
void myprint1(int val,int val2)
{
cout << val+val2 << " ";
}
void test04()
{
vectorv;
v.push_back(2);
v.push_back(34);
v.push_back(122);
//第二步:把普通函数变函数对象ptr_fun
for_each(v.begin(), v.end(), bind2nd(ptr_fun(myprint1),100));
}
//成员函数适配
class maker
{
public:
maker(string name, int age)
{
this->name = name;
this->age = age;
}
void myprint2()
{
cout << "name:" << name << " " << "age:" <v;
v.push_back(maker("aaa", 10));
v.push_back((maker("huihui", 18)));
v.push_back(maker("hhh", 20));
//for_each(v.begin(), v.end(), myprint2);
//当容器存储的是对象,用mem_fun_ref来适配它的成员函数
for_each(v.begin(), v.end(),mem_fun_ref( & maker::myprint2));
vectorv2;
v2.push_back(new maker("aaa", 10));
v2.push_back((new maker("huihui", 18)));
v2.push_back(new maker("hhh", 20));
//当容器存储的是对象指针,用mem_fun来适配它的成员函数
for_each(v2.begin(), v2.end(), mem_fun(&maker::myprint2));
}
int main()
{
test05();
system("pause");
return 0;
}