16.函数包装器

 1 #include <iostream>
 2 #include <functional>
 3 using namespace std;
 4 using std::function;//函数包装
 5 
 6 void go()
 7 {
 8     cout << "go" << endl;
 9 }
10 
11 int add(int a, int b)
12 {
13     return a + b;
14 }
15 
16 void main()
17 {
18     function<void(void)> fun1 = go;
19     fun1();
20     //用lambda表达式包装
21     function<void(void)> fun2 = []() {cout << "hello" << endl; };
22     fun2();
23 
24     function<int(int, int)> fun3 = add;
25     cout << fun3(10, 19) << endl;
26     cin.get();
27 }

 

posted @ 2018-03-11 14:48  喵小喵~  阅读(94)  评论(0编辑  收藏  举报