适配器

适配器

//An highlighted block
class My_Print:public binary_function<int,int,void>
	{
	public:
		void operator()(int v,int val) const
			{
			cout <<"第一个参数是:"  << v << "  第二个参数是:" << val << "两个数的和是:" << v + val << endl;
			}
	};

void test1()
	{
	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( My_Print(),num ));

	cout << "-------------------------------" << endl;

	for_each(v.begin(),v.end(),bind1st( My_Print(),num ));


	}
//第一步 绑定 数据 bind2nd 绑定第二个参数      bind1st  绑定第一个参数
//继承类 binary_function<参数1,参数2,返回值类型>
//加const修饰 operator()

在这里插入图片描述

//An highlighted block
class greaterThan5 : public unary_function<int,bool>
	{
	public:
		bool operator()(int a) const
			{
			return a >5;
			}
	};

//取反配置器
void test2()
	{
	//一元取反
	vector<int>v;
	for (int i =0;i<10;i++)
	{
		v.push_back(i);
	}
	//查找大于5的数
	vector<int>::iterator pos = find_if(v.begin(),v.end(),greaterThan5());
	if(pos != v.end())
		{
		cout << "大于5的数字为:" << *pos << endl;
		}
	else
		{
		cout << "没有找到" << endl;
		}
	//查找小于5的数
	pos = find_if(v.begin(),v.end(),not1(greaterThan5()));
	//pos = find_if(v.begin(),v.end(),not1(bind2nd(greater<int>(),5)));
	if(pos != v.end())
		{
		cout << "小于5的数字为:" << *pos << endl;
		}
	else
		{
		cout << "没有找到" << endl;
		}
	}
//一元取反适配器 not1     二元 not2  not2(less<int>())  相当于greater<int>()
//继承unary_function<参数1,返回值类型>
//const 修饰operator()

在这里插入图片描述

//An highlighted block
//函数指针适配器


void my_print(int v , int val)
	{
	cout << v + val<< endl;
	}
void test3()
	{
	vector<int>v;
	for (int i =0;i<10;i++)
		{
		v.push_back(i);
		}
	//for_each(v.begin(),v.end(),my_print);



	//将函数指针  适配为  函数对象
	//ptr_fun
	for_each(v.begin(),v.end(),bind2nd(ptr_fun(my_print) ,100));
	}

在这里插入图片描述

//An highlighted block
//成员函数适配器
//如果容器中存放的是对象指针,那么用mem_fun
//如果容器中存放的是对象实体,那么用mem_fun_ref
class Person
	{
	public:
		Person(string name,int age)
			{
			this ->_name = name;
			this ->_age = age;
			}
		void showPerson()
			{
			cout << "姓名:" << this->_name << "  年龄:" << this->_age<< endl;
			}
		void plusAge()
			{
			this ->_age = this ->_age + 100;
			}
		string _name;
		int _age;
	};
void my_print_person(Person p)
	{
	cout << "姓名:" << p._name << "  年龄:" << p._age << endl;
	}
void test4()
	{
	vector<Person> v;
	Person p1("lilei",19);
	Person p2("hanmeimei",29);
	Person p3("tom",17);
	Person p4("bob",66);

	v.push_back(p1);
	v.push_back(p2);
	v.push_back(p3);
	v.push_back(p4);

	for_each(v.begin(),v.end(),my_print_person);


	//成员函数适配器
	//mem_fun_ref
	cout << "成员函数中:" << endl;
	for_each(v.begin(),v.end(),mem_fun_ref(&Person::showPerson));
	cout << "---------------------------------------------" << endl;

	for_each(v.begin(),v.end(),mem_fun_ref(&Person::plusAge));
	for_each(v.begin(),v.end(),mem_fun_ref(&Person::showPerson));


	}![在这里插入图片描述](https://img-blog.csdnimg.cn/20190703155602931.PNG?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L2h1Z3VvMTk5Mg==,size_16,color_FFFFFF,t_70)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值