文章目录 
概述 包装器可以对一个可调用对象进行包装
使用包装器可以避免模板实例化出多份代码,可以提高效率
function function的使用 function可以使用构造函数进行初始化,也能用赋值进行初始化
仿函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 struct  Functor double  operator () (double  i)     {return  i / 2 ;int  main () double  m=10.1 ;double (double )> f2 = Functor (); f2 (m)<<endl;return  0 ;
Lambda表达式 1 2 3 4 5 6 7 8 int  main () double  m=10.1 ;double (double )> f2 = [](double  i){return  i/2 ;};f2 (m)<<endl;return  0 ;
函数指针 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 void  l (int & x) 2 ;"222" <<endl;void  demo4 () int  x=2 ;void (int &)> s=l;s (x);
类成员函数 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class  Plus public :static  double  fI (double  x)     {return  x;double  f (double  i)     {return  i / 2 ;int  main () double (double )> f3 = &Plus::fI; f2 (10 ) << endl;double (Plus, double )> ff = &Plus::f; ff (Plus (), 123 ) << endl;return  0 ;
function的实战 逆波兰表达式
根据 逆波兰表示法,求表达式的值。
 
题解:如果是操作数就入栈,如果是操作符,就把栈顶的两个数取出来进行处理
 
我们可以使用map,命令和函数可以进行映射,映射到的为function,因为function对于函数指针,lambda表达式,仿函数都可以包装,所以就可以了,一个命令对应一个函数
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 class  Solution public :int  evalRPN (vector<string> &tokens)      {int (int , int )>> opMapFunc; "+" ] = [](int  a, int  b)return  a + b; }; "-" ] = [](int  a, int  b)return  a - b; }; "*" ] = [](int  a, int  b)return  a * b; }; "/" ] = [](int  a, int  b)return  a / b; }; int > s;int  i = 0 ;int  top;for  (int  i = 0 ; i < tokens.size (); i++)if  (opMapFunc.find (str) == opMapFunc.end ())push (stoi (str)); else int  left = s.top ();pop ();int  right = s.top ();pop ();push (opMapFunc[str](left, right));return  s.top ();
bind 
bind可以对function绑定的函数调整参数的顺序
bind可以调整参数的个数(比如将包装之后的函数固定一个参数为某个东西,就不需要我们再手动去添加)
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 int  sub (int  a, int  b) return  a - b;class  subber public :int  Sub (int  a, int  b)      {return  a - b;void  demo3 () int (int , int )> f = sub;f (10 , 3 ) << endl;int (int , int )> f1 = bind (sub,placeholders::_1,placeholders::_2);f1 (10 , 3 ) << endl;int (int , int )> f3 = bind (sub,placeholders::_2,placeholders::_1);f3 (10 , 3 ) << endl;int (subber,int , int )> f4 = &subber::Sub;f4 (subber (),10 , 3 ) << endl;int (int , int )> f5 = bind (&subber::Sub,subber (),placeholders::_1,placeholders::_2);f5 (10 , 3 ) << endl;int (int )> f6=bind (&subber::Sub,subber (),100 ,placeholders::_1);f6 (20 )<<endl;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 class  A {public :A ()=default ;void  l (int  &x) 2 ;"222"  << endl;void  ll (int  &x) 2 ;"222"  << endl;int  main () int , function<void (int  &)>> lll = {bind (&A::ll,A (), placeholders::_1)},2 , bind (&A::l,A () ,placeholders::_1)}};return  0 ;