函数对象适配器是完成一些配接工作,这些配接包括绑定(bind),否定(negate),以及对一般函数或成员函数的修饰,使其成为函数对象,重点掌握函数对象适配器
- bind1st: 将参数绑定为函数对象的第一个参数
- bind2nd: 将参数绑定为函数对象的第二个参数
- not1: 对一元函数对象取反
- not2: 将二元函数对象取反
- ptr_fun: 将普通函数修饰成函数对象
- mem_fun: 修饰成员函数
- mem_fun_ref: 修饰成员函数
预定义函数对象
仿函数适配器 bind1st bind2nd
仿函数适配器 not1 not2
仿函数适配器 ptr_fun
成员函数适配器 mem_fun mem_fun_ref
#include <iostream>
#include <functional>
#include <vector>
#include <algorithm>
using namespace std;
//functional 全包含
//bind1st bind2nd
//第一步 需要让你自定义函数对象去继承父类 binary_function unary_function
//两个参数用 binary_function 一个参数用unary_function
class print:public binary_function<int,int,void>{//参数1 参数2 返回值
public:
void operator()(int v,int v2) const{//一定要加const
cout<<"v:"<<v<<" v2:"<<v2<<endl;
if (v > v2)
//如果我要大于1,大于2 ... 是不是要写print01 print02....能不能把这个5当做参数传进来?
{
cout<<v<<" ";
}
}
};
void test01(){
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i);
}
print p;
for_each(v.beigin(),v.end(),bind2nd(p,10));
//bind2nd(T &t,TYPE &key)
//为什么是bind2nd而不是bind1st?其实都可以,底层会自动把v的元素绑定到另一个位置上,放心使用
//bind1st bind2nd绑定适配器 调用绑定适配器 统统编程一元函数对象
cout<<endl;
}
//not1 not2 取反适配器
class mycompare:public unary_function<int,bool>{//参数1 返回值类型
public:
bool operator()(int v) const{
return v > 5;
}
};
void test02(){
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i);
}
mycompare myp;
vector<int>::iterator pos = find_if(v.begin(),v.end(),not1(myp));//返回迭代器
if (pos != v.end())
{
cout<<"找到:"<<*pos<<endl;
}
else{
cout<<"没有找到"<<endl;
}
//sort算法
greater<int> mygreater;
//sort(v.begin(),v.end(),less<int>());//从小到大排序 greater从大到小
sort(v.begin(),v.end(),mygreater);
for(vector<int>::iterator it = v.begin();it != v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
sort(v.begin(),v.end(),not2(mygreater));
for(vector<int>::iterator it = v.begin();it != v.end();it++){
cout<<*it<<" ";
}
cout<<endl;
}
//ptr_fun 普通函数适配器 将普通函数转化为函数对象
void print02(int v,int v2){//不用函数对象 用普通函数
//我要打印大于5 6 7 8 的数,怎么办?新增v2 用ptr_fun
if(v > v2){
cout<<v<<" ";
}
}
//对普通函数进行绑定参数
//1 需要将普通函数转化为函数对象 ptr_fun
//2 可以用bind适配器了
void test03()
{
vector<int> v;
for (int i = 0; i < 10; ++i)
{
v.push_back(i);
}
//for_each(v.begin(),v.end(),print02);
ptr_fun(print02);
for_each(v.begin(),v.end(),bind2nd(print02,5));
}
//mem_fun mem_fun_ref
class Teacher{
public:
Teacher(int id,int age):id(id),age(age){}
int id;
int age;
void print(){
cout<<"成员方法:"<<this->id<<" "<<this->age<<endl;
}
};
class print04{
public:
void operator()(Teacher t){
cout<<t.id<<" "<<t.age<<endl;
}
}
void test04(){
vector<Teacher> v;
Teacher t1(1,2),t2(3,4),t3(5,6);
v.push_back(t1);
v.push_back(t2);
v.push_back(t3);
//函数对象方式
//for_each(v.begin(),v.end(),print04());
//希望用类的成员方法打印类对象
for_each(v.begin(),v.end(),mem_fun_ref(&Teacher::print));//member functon reference 成员方法引用
//如果你的容器中放的是类对象实体 用mem_fun_ref
//如果你的容器中放的是类对象指针 用mem_fun
vector<Teacher*> v2;
Teacher *tp1 = new Teacher(1,2);
Teacher *tp2 = new Teacher(3,4);
Teacher *tp3 = new Teacher(5,6);
v2.push_back(tp1);
v2.push_back(tp2);
v2.push_back(tp3);
for_each(v2.begin(),v2.end(),mem_fun(&Teacher::print));
}
int main(){
//test01();
//test02();
//test03();
test04();
return 0;
}