Introduction:
A function adapter is also a template technique that binds a function to an adapter to implement a function call, and can modify the parameters and move the position of the parameter. Powerful features
Here's a quick introduction to this powerful tool
1. For the normal function, direct its type < return value (parameter 1, parameter 2, ...). > So fill in the adapter as a function type
function< return Value (parameter 1, parameter 2, ...). ) > PF = &func;
With this definition, PF becomes the Func function, which can be called as in the following example.
1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 7 voidFooConst string&s)8 {9cout << S <<Endl;Ten } One A - intMainintargcConst Char*argv[]) - { the void(*pfunc) (Const string&) = &foo; -PFunc ("Bar"); - -function<void(Const string&) > F = &foo; +F"Bar"); - + A return 0; at}
2) Examples of two-parameter common functions
It's no different than the previous example, but it's a parameter.
1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 7 voidFoointIDoubled)8 {9cout << I << D <<Endl;Ten } One A - intMainintargcConst Char*argv[]) - { thefunction<void(int,Double) > F = &foo; -F A,4.5); - - + return 0; -}
3. For member functions inside a class, if there is no instance we cannot access its member functions so we have to use a powerful tool----"bind
Here is the interface of BIND, this bind is not Linux under the binding socket that bind, do not confuse, bind return value is a function adapter, parameter bind (parameter 1: The function to bind, parameter 2: the first parameter to fit the position or the constant to fill, Parameter 3:2nd The position of the parameter to be fitted or the constant to be filled, the position of the 3rd parameter to be fitted, or the constant to be filled
1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 7 classFoo8 {9 Public:Ten voidFoointi) {cout << i <<Endl;} One A Static voidBarDoubled) {cout << D <<Endl;} - - }; the - intMainintargcConst Char*argv[]) - { - //mem_fun Void (*) (FOO *, int) + Foo F; -(Mem_fun (&foo::foo)) (&f,123); + Afunction<void(int) > PF = Bind (&Foo::foo, at&F, - std::p laceholders::_1); -pf345); - -function<void(foo*,int) > PF2 = Bind (&Foo::foo, - std::p laceholders::_1, in std::p laceholders::_2); - toPF2 (&f,456); + -function<void(int, foo*) > PF3 = Bind (&Foo::foo, the std::p laceholders::_2, * std::p laceholders::_1); $ Panax NotoginsengPF3 (567, &f); - the + A return 0; the}
4. For non-class member functions can also use bind to bind to achieve the default parameters, the purpose of the parameter swap position
We can get familiar with BIND's interface through a few exercises.
1#include <iostream>2#include <string>3#include <vector>4#include <functional>5 using namespacestd;6 using namespacestd::p laceholders;7 8 voidTestintIDoubleDConst string&s)9 {Tencout <<"i ="<< I <<"d ="<< D <<"s ="<< s <<Endl; One } A intMainintargcConst Char*argv[]) - { -function<void(int,Double,Const string&) > f1 = &test; theF1 ( A,3.14,"Foo"); - - //1.void (*) (int, double) -function<void(int,Double) > F2 = +Std::bind (&Test, - _1, + _2, A "Foo"); at - //2.void (*) (double, int, const string &) -function<void(Double,int,Const string&) > F3 = -Std::bind (&Test, - _2, - _1, in _3); - to //3.void (*) (const string &, int) +function<void(Const string&,int) > F4 = -Std::bind (&Test, the _2, * 3.4, $ _1);Panax Notoginseng - the //4. Void (*) (const string &, int, double) +function<void(Const string&,int,Double) >f5 A= Std::bind (&Test, the _2, + _3, - _1); $ $ //5. Void (*) (int) -function<void(int) > F6 = -Bind (&Test, the _1, - 3.4,Wuyi "Bar"); the - //6 void (*) (const string &) Wufunction<void(Const string&) > F7 = -Bind (&Test, About A, $ 4.5, - _1); - - //7. Void (*) () Afunction<void() > F8 = +Bind (&Test, the A, - 4.5, $ "Bar"); the}
Through the above 7 small exercises, I believe that the simple function adapter is ready to handle.
Complete
C + + Learning Path: Function Adapter