[C.C++] c++ bind函数

83 0
Honkers 7 小时前 | 显示全部楼层 |阅读模式

bind () 函数

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

bind 简单使用

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

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4. using namespace std::placeholders;
  5. void func(int a, int b, int c)
  6. {
  7. cout << (a -b -c) << endl;
  8. }
  9. int main(int argc, char *argv[])
  10. {
  11. auto fn1 = bind(func, _1, 2, 3);
  12. auto fn2 = bind(func, 2, _1, 3);
  13. fn1(10);
  14. fn2(10);
  15. return 0;
  16. }
复制代码

运行结果:

  1. 5
  2. -11
复制代码

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

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4. using namespace std::placeholders;
  5. void func(int a, int b, int c)
  6. {
  7. cout << (a - b -c) << endl;
  8. }
  9. int main(int argc, char *argv[])
  10. {
  11. auto fn1= bind(func, _2, 2, _1);
  12. cout << "the value of function is :";
  13. fn1(1, 13);
  14. auto fn2 = bind(func, _1, 2, _2);
  15. cout << "the value of function after changing placeholder position is :";
  16. fn2(1, 13);
  17. return 0;
  18. }
复制代码

运行结果:

  1. the value of function is :10
  2. the value of function after changing placeholder position is :-14
复制代码

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

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4. using namespace std::placeholders;
  5. void func(int a, int b, int c)
  6. {
  7. cout << (a - b -c) << endl;
  8. }
  9. int main(int argc, char *argv[])
  10. {
  11. auto fn1= bind(func, _1, 2, 4);
  12. cout << "the value of function with 1 placeholder is :";
  13. fn1(10);
  14. auto fn2 = bind(func, _1, 2, _2);
  15. cout << "the value of function with 2 placeholder is:";
  16. fn2(13, 1);
  17. auto fn3 = bind(func, _1, _3, _2);
  18. cout << "the value of function with 3 placeholders:";
  19. fn3(13, 1, 4);
  20. return 0;
  21. }
复制代码

运行结果:

  1. the value of function with 1 placeholder is :4
  2. the value of function with 2 placeholder is:10
  3. the value of function with 3 placeholders:8
复制代码

bind 类静态函数和成员函数

  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4. using namespace std::placeholders;
  5. class test_callback
  6. {
  7. public:
  8. test_callback(void):a(10),b(100){ }
  9. typedef function<void(int,int)> callback;
  10. void use_value(callback func) {
  11. cout << "value a is " << a << endl;
  12. cout << "value b is " << b << endl;
  13. func(a, b);
  14. }
  15. private:
  16. int a;
  17. int b;
  18. };
  19. class client
  20. {
  21. public:
  22. client(){ this->value = 2; }
  23. static void print_sum(int a, int b, int c) {
  24. cout << a + b +c << endl;
  25. }
  26. void print_multiply(int a, int b, int c, int d) {
  27. cout << a * b * c * d << endl;
  28. cout << "client value is " << this->value << endl;
  29. }
  30. private:
  31. int value;
  32. };
  33. int main(int argc, char *argv[])
  34. {
  35. test_callback test1;
  36. client client1;
  37. test1.use_value(bind(client::print_sum, _2, _1, 0));
  38. test1.use_value(bind(&client::print_multiply, &client1, _1, _2, 2, 3));
  39. return 0;
  40. }
复制代码

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

  1. value a is 10
  2. value b is 100
  3. 110
  4. value a is 10
  5. value b is 100
  6. 6000
  7. client value is 2
复制代码
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Honkers

特级红客

关注
  • 3259
    主题
  • 36
    粉丝
  • 0
    关注
这家伙很懒,什么都没留下!

中国红客联盟公众号

联系站长QQ:5520533

admin@chnhonker.com
Copyright © 2001-2025 Discuz Team. Powered by Discuz! X3.5 ( 粤ICP备13060014号 )|天天打卡 本站已运行