boost::bind

來源:互聯網
上載者:User

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)}33344

綁定成員函數:

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  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.