1 #include <iostream>
2 #include <functional>
3 using namespace std;
4 using namespace std::placeholders;
5
6 int add(
int a,
int b)
7 {
8 return a +
b;
9 }
10
11 class myclass
12 {
13 public:
14 int operator()(
int a,
int b)
15 {
16 cout <<
"a = " << a <<
" b = " << b <<
endl;
17 return a +
b;
18 }
19 };
20
21 void main()
22 {
23 //函数包装器
24 auto fun = bind(add,
10,
1);
25 cout << fun() <<
endl;
26 //lambda表达式与函数包装器
27 auto fun1 = bind([](
int a,
int b)->
int {
return a + b; },
100, _1);
28 cout << fun1(
122) <<
endl;
29
30 myclass my1;
31 cout << my1(
1,
1) <<
endl;
32 //伪函数和绑定
33 //绑定从左到右,可以指定可以不指定
34 auto fun3 = bind(my1,
100, _1);
35 fun3(
4);
36 cin.
get();
37 }
转载于:https://www.cnblogs.com/xiaochi/p/8555773.html
转载请注明原文地址: https://win8.8miu.com/read-27254.html