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
-
- # Include <boost/Bind. HPP>
-
- # Include <iostream>
-
- Using NamespaceSTD;
- Using NamespaceBoost;
-
-
- VoidFun (IntA,IntB ){
-
- Cout <a + B <Endl;
-
- }
-
- IntMain ()
-
- {
-
- 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
# 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
-
- # Include <boost/Bind. HPP>
-
- # Include <iostream>
-
- # Include <vector>
- # Include <algorithm>
-
- Using NamespaceBoost;
-
- Using NamespaceSTD;
-
-
- StructPoint
- {
-
- IntX, Y;
-
- Point (IntA = 0,IntB = 0): X (A), y (B ){}
-
- VoidPrint (){
- Cout <"("<X <","<Y <") \ N";
-
- }
-
- VoidSetx (IntA ){
- Cout <"Setx :"<A <Endl;
-
- }
-
- VoidSetxy (IntX,IntY ){
- Cout <"Setx :"<X <", Sety :"<Y <Endl;
-
- }
- void setxyz ( int X, int y, int Z) {
- Cout <"Setx :"<X <", Sety :"<Y <"Setz :"<Z <Endl;
-
- }
-
- };
-
-
- IntMain ()
-
- {
- Point P1, P2;
-
- BIND (& point: setx, P1, _ 1) (10 );
-
- BIND (& point: setxy, P1, _ 1, _ 2) (10, 20 );
-
- BIND (& point: setxyz, P2, _ 1, _ 2, _ 3) (10, 20, 30 );
-
- Vector <point> V (10 );
-
- // For_each only requires _ 1.
-
- For_each (V. Begin (), V. End (), BIND (& point: print, _ 1 ));
-
- For_each (V. Begin (), V. End (), BIND (& point: setx, _ 1, 10 ));
- For_each (V. Begin (), V. End (), BIND (& point: setxy, _, 10, 20 ));
-
- For_each (V. Begin (), V. End (), BIND (& point: setxyz, _, 10, 20, 30 ));
-
- }
-
-
- Setx: 10
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20 setz: 30
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
-
- (0, 0)
- (0, 0)
-
- (0, 0)
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
-
- Setx: 10, sety: 20 setz: 30
- setx: 10, sety: 20 setz: 30