适配器从字面上讲,是将一个事物经过适配之后变成另外一个事物,使之能够复合我们的需求。为什么要适配,显然现有的东西不满足我们的需要。
比如说,我们需要调用一个有一个参数的函数,但是结果现在只有两个参数的函数,这时怎么办?我们可以通过适配器将两个参数的函数转换成一个参数的函数。stl中实现了几个不同用途的适配器,我们一一来看看如何使用,至于stl实现这些适配器的思路,我们稍后在下一篇文章再给大家分析并实现其思路。
1. bind1st bind2nd 绑定适配器
//函数适配器bind1st bind2nd
//现在我有这个需求 在遍历容器的时候,我希望将容器中的值全部加上100之后显示出来,怎么做哇?
struct myprint : public binary_function<int, int, void>{ //二元函数对象 所以需要继承 binary_fucntion<参数类型,参数类型,返回值类型>
void operator()(int v1, int v2) const{
cout << v1 + v2 << " ";
}
};
void test02(){
vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
//我们直接给函数对象绑定参数 编译阶段就会报错
//for_each(v.begin(), v.end(), bind2nd(myprint(),100));
//如果我们想使用绑定适配器,需要我们自己的函数对象继承binary_function 或者 unary_function
//根据我们函数对象是一元函数对象 还是二元函数对象
for_each(v.begin(), v.end(), bind2nd(myprint(), 100));
cout << endl;
//总结: bind1st和bind2nd区别?
//bind1st : 将参数绑定为函数对象的第一个参数
//bind2nd : 将参数绑定为函数对象的第二个参数
//bind1st bind2nd将二元函数对象转为一元函数对象
}
2. not1 not2 取反适配器
//函数对象适配器 not1 not2
struct myprint02 {
void operator()(int v1) const{
cout << v1 << " ";
}
};
void test03(){
vector<int> v;
v.push_back(2);
v.push_back(1);
v.push_back(5);
v.push_back(4);
vector<int>::iterator it = find_if(v.begin(), v.end(), not1(bind2nd(less_equal<int>(), 2)));
cout << "it:" << *it << endl;
sort(v.begin(), v.end(), not2(greater<int>()));
for_each(v.begin(), v.end(), myprint02());
cout << endl;
//not1 对一元函数对象取反
//not2 对二元函数对象取反
}
3. ptr_func 函数指针适配器
//如何给一个普通函数使用绑定适配器(bind1st bind2nd)绑定一个参数?(拓展)
//ptr_fun
void myprint04(int v1, int v2){
cout << v1 + v2 << " ";
}
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_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));
}