Boost: bind

Source: Internet
Author: User

BIND is not a separate class or function, but a very large family. Based on the number of bound parameters and the type of the called object to be bound, there are a total of 10 different forms, but their names are all bind.
The first parameter accepted by BIND must be a callable object F, including functions, function pointers, function objects, and member functions. Then bind can accept a maximum of nine parameters, the number of parameters must be the same as that of F.
_ 1, _ 2 can be up to 9, which is a placeholder. All parameters required by the function must be provided in the binding expression, either real parameters or placeholders. Placeholders cannot exceed the number of function parameters.
Bind a common function:

C ++ Code
  1. # Include <boost/Bind. HPP>
  2. # Include <iostream>
  3. Using NamespaceSTD;
  4. Using NamespaceBoost;
  5. VoidFun (IntA,IntB ){
  6. Cout <a + B <Endl;
  7. }
  8. IntMain ()
  9. {
  10. BIND (fun, 1, 2 )();// Fun (1, 2)
  11. BIND (fun, _ 1, _ 2) (1, 2 );// Fun (1, 2)
  12. BIND (fun, _ 2, _ 1) (1, 2 );// Fun (2, 1)
  13. BIND (fun, _ 2, _ 2) (1, 2 );// Fun (2, 2)
  14. BIND (fun, _ 1, 3) (1 );// Fun (1, 3)
  15. }
  16. 3
  17. 3
  18. 3
  19. 4
  20. 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

Bind a member function:

C ++ code
  1. # Include <boost/Bind. HPP>
  2. # Include <iostream>
  3. # Include <vector>
  4. # Include <algorithm>
  5. Using NamespaceBoost;
  6. Using NamespaceSTD;
  7. StructPoint
  8. {
  9. IntX, Y;
  10. Point (IntA = 0,IntB = 0): X (A), y (B ){}
  11. VoidPrint (){
  12. Cout <"("<X <","<Y <") \ N";
  13. }
  14. VoidSetx (IntA ){
  15. Cout <"Setx :"<A <Endl;
  16. }
  17. VoidSetxy (IntX,IntY ){
  18. Cout <"Setx :"<X <", Sety :"<Y <Endl;
  19. }
  20. void setxyz ( int X, int y, int Z) {
  21. Cout <"Setx :"<X <", Sety :"<Y <"Setz :"<Z <Endl;
  22. }
  23. };
  24. IntMain ()
  25. {
  26. Point P1, P2;
  27. BIND (& point: setx, P1, _ 1) (10 );
  28. BIND (& point: setxy, P1, _ 1, _ 2) (10, 20 );
  29. BIND (& point: setxyz, P2, _ 1, _ 2, _ 3) (10, 20, 30 );
  30. Vector <point> V (10 );
  31. // For_each only requires _ 1.
  32. For_each (V. Begin (), V. End (), BIND (& point: print, _ 1 ));
  33. For_each (V. Begin (), V. End (), BIND (& point: setx, _ 1, 10 ));
  34. For_each (V. Begin (), V. End (), BIND (& point: setxy, _, 10, 20 ));
  35. For_each (V. Begin (), V. End (), BIND (& point: setxyz, _, 10, 20, 30 ));
  36. }
  37. Setx: 10
  38. Setx: 10, sety: 20
  39. Setx: 10, sety: 20 setz: 30
  40. (0, 0)
  41. (0, 0)
  42. (0, 0)
  43. (0, 0)
  44. (0, 0)
  45. (0, 0)
  46. (0, 0)
  47. (0, 0)
  48. (0, 0)
  49. (0, 0)
  50. Setx: 10
  51. Setx: 10
  52. Setx: 10
  53. Setx: 10
  54. Setx: 10
  55. Setx: 10
  56. Setx: 10
  57. Setx: 10
  58. Setx: 10
  59. Setx: 10
  60. Setx: 10, sety: 20
  61. Setx: 10, sety: 20
  62. Setx: 10, sety: 20
  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 setz: 30
  71. Setx: 10, sety: 20 setz: 30
  72. Setx: 10, sety: 20 setz: 30
  73. Setx: 10, sety: 20 setz: 30
  74. Setx: 10, sety: 20 setz: 30
  75. Setx: 10, sety: 20 setz: 30
  76. Setx: 10, sety: 20 setz: 30
  77. Setx: 10, sety: 20 setz: 30
  78. Setx: 10, sety: 20 setz: 30
  79. setx: 10, sety: 20 setz: 30

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.