c++(适配器)

适配器

#include <vector>
#include <algorithm>
#include <functional>
//第一步 绑定  数据 bind2nd
//继承类 binary_function<参数类型1,参数类型2,返回值类型>
//加const修饰 operator()
class MyPrint :public binary_function<int,int,void>
{
public:
	void operator()(int val,int start) const
	{
		cout << val << endl;
		cout << start << endl;
		cout << val+start << endl;
	}
};
//适配器
void test01()
{
	vector<int> v;
	for (int i = 0; i < 10; i++)
		v.push_back(i);
	int num;
	cout << "请输入起始值: " << endl;
	cin >> num;
	//绑定数据
	for_each(v.begin(), v.end(),bind2nd(MyPrint(),num));
	//for_each(v.begin(), v.end(),bind1st(MyPrint(),num));
	//区别是在上面小括号重载上形参颠倒了
	//for_each(v.begin(), v.end(), [](int val) {cout << val << endl; });
}

posted on 2021-04-29 11:10  lodger47  阅读(20)  评论(0编辑  收藏  举报

导航