forked from DragonFive/cpp11
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpp11_13_bindFunction.cpp
59 lines (46 loc) · 1.49 KB
/
cpp11_13_bindFunction.cpp
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
54
55
56
57
58
59
/************************************************************************
* 代码标签 :
* 功能描述 :
* 类 名 称 :
* 版 本 号 : v1.0
* 说 明 :
* 作 者 : dragonfive
* 创建时间 :
* 更新时间 :
************************************************************************
* Copyright @dragonfive 2016 . All rights reserved.
************************************************************************/
#include <iostream>
#include <functional>
#include <algorithm>
#include <array>
using namespace std;
//下面这个命名空间是占位符的命名空间;
using namespace std::placeholders;
void testBind(string val1,string val2,string val3)
{
cout<<"val1="<<val1<<" val2="<<val2<<" val3="<<val3<<endl;
}
struct intAdd{
int operator() (int x, int y) const
{ return x+y ;};
};
int main()
{
//测试发现占位符表示的是占的这个位子在新名字函数调用时用它的第几个参数来填充
auto f1 = bind(testBind,_1,"val2","val3");
f1("val1");
auto f2 = bind(testBind,"val1",_1,"val3");
f2("val2");
//下面测试funciton函数,auto函数与结构体成员函数,所以说啊,使用funciton 是有道理的,
function<int(int,int)> f = intAdd();
auto af = intAdd();
cout<<f(4,2)<<endl;
cout<<intAdd()(4,2)<<endl;
cout<<af(4,2)<<endl;
array<int,4> intAr{8,4,2,2};
cout<<accumulate(intAr.begin(),intAr.end(),1,f)<<endl;
cout<<accumulate(intAr.begin(),intAr.end(),1,intAdd())<<endl;
cout<<accumulate(intAr.begin(),intAr.end(),1,af)<<endl;
return 0;
}