c++ bind函数

bind () 函数

std::bind()函数作为函数的适配器,它可以扩大函数是使用场合,使得函数更加灵活的被使用。
template<class F, class… Args>
bind(F&&f, Args&&… args);
参数:
f 可以是function object,函数指针,函数引用,成员函数指针,或者数据成员的指针。
返回值:
function object

bind 简单使用

placeholders 命名空间下的_1, _2, _3 指示函数参数数量和位置。

#include <iostream>
#include <functional>
using namespace std;

using namespace std::placeholders;

void func(int a, int b, int c)
{
    cout << (a -b -c) << endl;
}

int main(int argc, char *argv[])
{
    auto fn1 = bind(func, _1, 2, 3);
    auto fn2 = bind(func, 2, _1, 3);

    fn1(10);
    fn2(10);
    return 0;
}

运行结果:

5
-11

placeholder的位置决定了调用返回函数时的参数位置

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;

void func(int a, int b, int c)
{
    cout << (a - b -c) << endl;
}

int main(int argc, char *argv[])
{
    auto fn1= bind(func, _2, 2, _1);
    cout << "the value of function is :";
    fn1(1, 13);

    auto fn2 = bind(func, _1, 2, _2);
    cout << "the value of function after changing placeholder position is :";
    fn2(1, 13);
    return 0;
}

运行结果:

the value of function is :10
the value of function after changing placeholder position is :-14

placeholder的数量决定了返回函数的参数数量

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;

void func(int a, int b, int c)
{
    cout << (a - b -c) << endl;
}

int main(int argc, char *argv[])
{
    auto fn1= bind(func, _1, 2, 4);
    cout << "the value of function with 1 placeholder is :";
    fn1(10);

    auto fn2 = bind(func, _1, 2, _2);
    cout << "the value of function with 2 placeholder is:";
    fn2(13, 1);

    auto fn3 = bind(func, _1, _3, _2);
    cout << "the value of function with 3 placeholders:";
    fn3(13, 1, 4);
    return 0;
}

运行结果:

the value of function with 1 placeholder is :4
the value of function with 2 placeholder is:10
the value of function with 3 placeholders:8

bind 类静态函数和成员函数

#include <iostream>
#include <functional>

using namespace std;
using namespace std::placeholders;

class test_callback
{
public:
    test_callback(void):a(10),b(100){ }
    typedef function<void(int,int)> callback;
    void use_value(callback func) {
        cout << "value a is " << a << endl;
        cout << "value b is " << b << endl;
        func(a, b);
    }
private:
    int a;
    int b;
};

class client
{
public:
    client(){ this->value = 2; }
    static void print_sum(int a, int b, int c) {
        cout << a + b +c << endl;
    }
    void print_multiply(int a, int b, int c, int d) {
        cout << a * b * c * d << endl;
        cout << "client value is " << this->value << endl; 
    }
private:
    int value;
};


int main(int argc, char *argv[])
{
    test_callback test1;
    client client1;

    test1.use_value(bind(client::print_sum, _2, _1, 0));
    test1.use_value(bind(&client::print_multiply, &client1, _1, _2, 2, 3));

    return 0;
}

重点是在使用non static function时要加&获取nonstatic 成员函数地址。
运行结果:

value a is 10
value b is 100
110
value a is 10
value b is 100
6000
client value is 2

  • 10
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值