C++的函数适配器

扩展函数的参数接口(假如函数有一个参数 再扩展一个接口 据可以传递两个参数)

绑定参数

bind2nd 和bind1st区别
bind2nd:将外界数据 绑定到第二个参数
bind1st:将外界数据 绑定到第一个参数

注意:
binary_function 二元继承
unary_function 一元继承

//val 是for_each提供  tmp
//适配器2:公共继承binary_function
//适配器3:参数的萃取
//适配器4:对operator()进行const修饰
class MyPrint:public binary_function<int,int, void>
{
public:
    void operator()(int val,int tmp) const
    {
        cout<<val+tmp<<" ";
    }
};

void test05()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);
    v.push_back(50);

    //适配器1:bind2nd 或bind1st 绑定参数
    for_each(v.begin(), v.end(), bind2nd(MyPrint(),1000) );
    cout<<endl;
}

运行结果:
在这里插入图片描述

取反适配器

not1一元取反
not2二元取反

//取反适配器2:public unary_function
//取反适配器3:参数萃取
//取反适配器4:const修饰operator()
class MyGreaterThan3:public unary_function<int,bool>
{
public:
    //一元谓词
    bool operator()(int val)const
    {
        return val>3;
    }
};
void test06()
{
    vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    v.push_back(5);
    //找出第一个大于3的数
    vector<int>::iterator ret;
    ret = find_if(v.begin(),v.end(), MyGreaterThan3() );
    if(ret != v.end())
    {
        cout<<"*ret = "<<*ret<<endl;//4
    }

    //找出第一个小于3的数
    //取反适配器1:not1修饰
    ret = find_if(v.begin(),v.end(), not1(MyGreaterThan3()) );
    if(ret != v.end())
    {
        cout<<"*ret = "<<*ret<<endl;//4
    }
}

运行结果:
在这里插入图片描述

成员函数适配器mem_fun_ref

class Person
{
public:
    string name;
    int age;
    Person(string name,int age)
    {
        this->name = name;
        this->age = age;
    }
    void showPerson()
    {
        cout<<"name = "<<this->name<<",age="<<this->age<<endl;
    }
};
void myPrintPerson(Person &ob)
{
    cout<<"name = "<<ob.name<<",age="<<ob.age<<endl;
}
void test08()
{
    vector<Person> v;
    v.push_back(Person("德玛西亚",18));
    v.push_back(Person("狗头",28));
    v.push_back(Person("牛头",19));
    v.push_back(Person("小法",38));

    //遍历 myPrintPerson普通函数
    //for_each(v.begin(),v.end(), myPrintPerson );
    //遍历  Person成员函数
    //利用 mem_fun_ref 将Person内部成员函数适配
    for_each(v.begin(),v.end(), mem_fun_ref(&Person::showPerson) );
}

运行结果:
在这里插入图片描述

普通函数作为适配器 ptr_fun

//普通函数 作为适配器
void myPrintInt01(int val,int tmp)
{
    cout<<val+tmp<<" ";
}
void test01()
{
    vector<int> v;
    v.push_back(10);
    v.push_back(20);
    v.push_back(30);
    v.push_back(40);

    //普通函数 需要使用ptr_fun转换成函数适配器
    for_each(v.begin(),v.end(), bind2nd(ptr_fun(myPrintInt01),1000));
    cout<<endl;
}

运行结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值