美文网首页
七、函数对象适配器(bind、ptr_fun、mem_fun)

七、函数对象适配器(bind、ptr_fun、mem_fun)

作者: 木鱼_cc | 来源:发表于2018-07-02 15:39 被阅读0次

函数对象适配器是完成一些配接工作,这些配接包括绑定(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;
}

相关文章

  • 七、函数对象适配器(bind、ptr_fun、mem_fun)

    函数对象适配器是完成一些配接工作,这些配接包括绑定(bind),否定(negate),以及对一般函数或成员函数的修...

  • std::bind函数

    std::bind 关于bind的用法:可将bind函数看作是一个通用的函数适配器,它接受一个可调用对象,生成一个...

  • bind函数模版

    一、std::bind包装器/适配器介绍 1、函数模版bind生成f的可调用函数对象包装器,调用此包装器等价于...

  • C++ STL mem_fun / mem_fun_ref /

    说明 这些函数都作用如其名称:member function,都是为了取得成员函数对象。 mem_fun和mem_...

  • C++高级 C++算法源码全盘阅读与算法包实战

    1.C++函数适配器find_if 查找值equal_to 比较两个值是否相等bind2nd 函数适配器 2.算法...

  • Function(){}.bind()

    bind()方法主要就是将函数绑定到某个对象,bind()会创建一个函数,函数体内的this对象的值会被绑定到传入...

  • 贪吃蛇

    函数也是对象任何函数也有prototype属性,我们说只有对象才有属性和方法,所以函数也是对象,函数还有bind(...

  • 函数对象(仿函数)

    1.1 知识点 函数对象概述 预定义函数对象 辅助函数对象 适配器 函数对象使用方法 1.2 实验环境 g++ u...

  • apply, call, bind

    apply, call, bind 都是用来改变函数的this对象的指向 apply, call, bind 第一...

  • JS常见手写代码题(一)

    1、call bind applycall 、bind 、apply三个都是用来改变函数的this对象的指向,第一...

网友评论

      本文标题:七、函数对象适配器(bind、ptr_fun、mem_fun)

      本文链接:https://www.haomeiwen.com/subject/pucluftx.html