zoukankan      html  css  js  c++  java
  • boost::bind

    bind并不是一个单独的类或函数,而是非常庞大的家族,依据绑定的参数个数和要绑定的调用对象类型,总共有十个不同的形式,但它们的名字都叫bind.
    bind接受的第一个参数必须是一个可调用对象f,包括函数,函数指针,函数对象和成员函数,之后bind接受最多9个参数,参数的数量必须与f的参数数量相等
    _1,_2这些一直可以到9,是占位符,必须在绑定表达式中提供函数要求的所有参数,无论是真实参数还是占位符均可以。占位符不可以超过函数参数数量。
    绑定普通函数:

    C++代码 复制代码 收藏代码
    1. #include<boost/bind.hpp>   
    2. #include<iostream>   
    3. using namespace std;   
    4. using namespace boost;   
    5.   
    6. void fun(int a,int b){   
    7.         cout << a+b << endl;   
    8. }   
    9.   
    10. int main()   
    11. {   
    12.         bind(fun,1,2)();//fun(1,2)   
    13.         bind(fun,_1,_2)(1,2);//fun(1,2)   
    14.         bind(fun,_2,_1)(1,2);//fun(2,1)   
    15.         bind(fun,_2,_2)(1,2);//fun(2,2)   
    16.         bind(fun,_1,3)(1);//fun(1,3)   
    17. }   
    18.   
    19.   
    20. 3   
    21. 3   
    22. 3   
    23. 4   
    24. 4  
    #include<boost/bind.hpp>
    #include<iostream>
    using namespace std;
    using namespace boost;
    
    void fun(int a,int b){
            cout << a+b << endl;
    }
    
    int main()
    {
            bind(fun,1,2)();//fun(1,2)
            bind(fun,_1,_2)(1,2);//fun(1,2)
            bind(fun,_2,_1)(1,2);//fun(2,1)
            bind(fun,_2,_2)(1,2);//fun(2,2)
            bind(fun,_1,3)(1);//fun(1,3)
    }
    
    
    3
    3
    3
    4
    4
    


    绑定成员函数:

    C++代码 复制代码 收藏代码
    1. #include<boost/bind.hpp>   
    2. #include<iostream>   
    3. #include<vector>   
    4. #include<algorithm>   
    5. using namespace boost;   
    6. using namespace std;   
    7.   
    8. struct point   
    9. {   
    10.     int x,y;   
    11.     point(int a=0,int b=0):x(a),y(b){}   
    12.     void print(){   
    13.         cout << "(" << x << "," << y << ")\n";   
    14.     }   
    15.     void setX(int a){   
    16.         cout << "setX:" << a << endl;   
    17.     }   
    18.     void setXY(int x,int y){   
    19.         cout << "setX:" << x << ",setY:" << y << endl;   
    20.     }   
    21.     void setXYZ(int x,int y,int z){   
    22.         cout << "setX:" << x << ",setY:" << y << "setZ:" << z << endl;   
    23.     }   
    24. };   
    25.   
    26. int main()   
    27. {   
    28.     point p1,p2;   
    29.     bind(&point::setX,p1,_1)(10);   
    30.     bind(&point::setXY,p1,_1,_2)(10,20);   
    31.     bind(&point::setXYZ,p2,_1,_2,_3)(10,20,30);   
    32.     vector<point> v(10);   
    33.     //for_each的时候只需要_1就可以了   
    34.     for_each(v.begin(),v.end(),bind(&point::print,_1));   
    35.     for_each(v.begin(),v.end(),bind(&point::setX,_1,10));   
    36.     for_each(v.begin(),v.end(),bind(&point::setXY,_1,10,20));   
    37.     for_each(v.begin(),v.end(),bind(&point::setXYZ,_1,10,20,30));   
    38. }   
    39.   
    40. setX:10   
    41. setX:10,setY:20   
    42. setX:10,setY:20setZ:30   
    43. (0,0)   
    44. (0,0)   
    45. (0,0)   
    46. (0,0)   
    47. (0,0)   
    48. (0,0)   
    49. (0,0)   
    50. (0,0)   
    51. (0,0)   
    52. (0,0)   
    53. setX:10   
    54. setX:10   
    55. setX:10   
    56. setX:10   
    57. setX:10   
    58. setX:10   
    59. setX:10   
    60. setX:10   
    61. setX:10   
    62. setX:10   
    63. setX:10,setY:20   
    64. setX:10,setY:20   
    65. setX:10,setY:20   
    66. setX:10,setY:20   
    67. setX:10,setY:20   
    68. setX:10,setY:20   
    69. setX:10,setY:20   
    70. setX:10,setY:20   
    71. setX:10,setY:20   
    72. setX:10,setY:20   
    73. setX:10,setY:20setZ:30   
    74. setX:10,setY:20setZ:30   
    75. setX:10,setY:20setZ:30   
    76. setX:10,setY:20setZ:30   
    77. setX:10,setY:20setZ:30   
    78. setX:10,setY:20setZ:30   
    79. setX:10,setY:20setZ:30   
    80. setX:10,setY:20setZ:30   
    81. setX:10,setY:20setZ:30   
    82. setX:10,setY:20setZ:30  
  • 相关阅读:
    Ubuntu adb devices :???????????? no permissions (verify udev rules) 解决方法
    ubuntu 关闭显示器的命令
    ubuntu android studio kvm
    ubuntu 14.04版本更改文件夹背景色为草绿色
    ubuntu 创建桌面快捷方式
    Ubuntu 如何更改用户密码
    ubuntu 14.04 返回到经典桌面方法
    ubuntu 信使(iptux) 创建桌面快捷方式
    Eclipse failed to get the required ADT version number from the sdk
    Eclipse '<>' operator is not allowed for source level below 1.7
  • 原文地址:https://www.cnblogs.com/lzjsky/p/2169820.html
Copyright © 2011-2022 走看看