函数适配器

1.初识函数适配器
1)使用bind2nd进行绑定
2)需要继承 public binary_function<参数类型1,参数类型2,返回值类型>
3)使用const修饰operator()

class myPrint:public binary_function<int,int,void>
{
public:
	void operator()(int val,int start) const //限定数据
	{
		cout<<"val: "<<val<<"start:"<<start<<"val+start= "<<val+start<<endl;
	}
};

void text01()
{

	vector<int>v;
	for (int i=0;i<10;i++)
	{
		v.push_back(i);
	}
	cout<<"尊贵的用户请输入起始值:"<<endl;
	int num;
	cin>>num;

	for_each(v.begin(),v.end(),bind2nd(myPrint(),num));
}

2.取反函数适配器
1)使用not1进行取反
2)继承public unary_function<int,bool>
3)使用const修饰operator()

#include<iostream>
#include<vector>
#include<algorithm>
#include<functional>
using namespace std;
struct MyPrint:public binary_function<int,int,bool>
{
	bool operator()(int v1,int v2)const
	{
		return v1>v2;
	}
};
struct MyPrint02
{
	void operator()(int val)
	{
		cout<<val<<" ";
	}
};
struct MyGreater6:public unary_function<int,bool>
{
	bool operator()(int v)const
	{
		return v>5;
	}
};
void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(20);
	v.push_back(30);
	v.push_back(40);
	//一元谓词取反用not1
	//二元谓词取反用not2
	sort(v.begin(),v.end(),not2(MyPrint()));

	for_each(v.begin(),v.end(),MyPrint02());cout<<endl;

	vector<int>::iterator it=find_if(v.begin(),v.end(),not1(MyGreater6()));
	//判断有没有找到需要的值
	if(it==v.end())
	{
		cout<<"没有找到!"<<endl;
	}
	else
	{
		cout<<*it<<endl;
	}
}
int main()
{
	test01();
	return 0;
}

3.prt_fun// 将普通函数适配成函数对象,2然后通过绑定器绑定参数

//如何给一个普通函数使用绑定适配器(bind1st bind2nd)绑定一个参数?(拓展)
//ptr_fun
void myprint04(int v1, int v2){
	cout<<"v1:"<<v1<<" v2:"<<v2<<endl;
	cout << v1 + v2 << endl;
}
void test04(){
 
	vector<int> v;
	v.push_back(2);
	v.push_back(1);
	v.push_back(5);
	v.push_back(4);
 
 
	//1 将普通函数适配成函数对象
	//2 然后通过绑定器绑定参数
	for_each(v.begin(), v.end(), bind2nd(ptr_fun(myprint04), 100));
	cout << endl;
 
	//总结: ptr_fun 将普通函数转变为函数对象
}

4.mem_fun mem_func_ref 成员函数适配器

mem_fun mem_func_ref 成员函数适配器
//mem_fun mem_fun_ref
//如果我们容器中存储的是对象或者对象指针,如果能指定某个成员函数处理成员数据。
class student{
public:
	student(string name, int age) :name(name), age(age){}
	void print(){
		cout << "name:" << name << " age:" << age << endl;;
	}
	void print2(int a){
		cout << "name:" << name << " age:" << age << " a:" << a << endl;
	}
	int age;
	string name;
};
 
void test05(){
 
	//mem_fun : 如果存储的是对象指针,需要使用mem_fun
	vector<student*> v;
	student* s1 = new student("zhaosi", 10);
	student* s2 = new student("liuneng", 20);
	student* s3 = new student("shenyang", 30);
	student* s4 = new student("xiaobao", 40);
 
	v.push_back(s1);
	v.push_back(s2);
	v.push_back(s3);
	v.push_back(s4);
 //&号不能忘记 必须有(格式比较特殊)
	for_each(v.begin(), v.end(), mem_fun(&student::print));
	cout << "-----------------------------" << endl;
 
	//mem_fun_ref : 如果容器中存储的是对象,需要使用mem_fun_ref
 
	vector<student> v2;
	v2.push_back(student("zhaosi", 50));
	v2.push_back(student("liuneng", 60));
	v2.push_back(student("shenyang", 70));
	v2.push_back(student("xiaobao", 80));
 
	for_each(v2.begin(), v2.end(), mem_fun_ref(&student::print));
 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值