forked from chronolaw/boost_guide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbind.cpp
More file actions
219 lines (158 loc) · 4.26 KB
/
Copy pathbind.cpp
File metadata and controls
219 lines (158 loc) · 4.26 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// Copyright (c) 2015
// Author: Chrono Law
#include <std.hpp>
//using namespace std;
#include <boost/bind.hpp>
using namespace boost;
//////////////////////////////////////////
int f(int a, int b)
{ return a + b;}
int g(int a, int b, int c)
{ return a + b * c;}
//typedef int (*f_type)(int, int);
//typedef int (*g_type)(int, int, int);
typedef decltype(&f) f_type;
typedef decltype(&g) g_type;
void case1()
{
std::cout << bind(f,1,2)() << std::endl;
std::cout << bind(g,1,2,3)() << std::endl;
//f_type fp = f;
//std::cout << bind(fp, _1, _2)(1,2) << std::endl;
int x = 1, y = 2, z = 3;
bind(f, _1, 9)(x) ;
bind(f, _1, _2)(x, y) ;
bind(f, _2, _1)(x, y) ;
bind(f, _1, _1)(x, y) ;
bind(g, _1, 8, _2)(x, y) ;
bind(g, _3, _2, _2)(x, y, z) ;
f_type pf = f;
g_type pg = g;
std::cout << bind(pf, _1, 9)(x) << std::endl;
std::cout << bind(pg, _3, _2, _2)(x, y, z) << std::endl;
}
//////////////////////////////////////////
struct demo
{
int f(int a, int b)
{ return a + b; }
};
struct point
{
int x, y;
point(int a = 0, int b = 0):x(a),y(b){}
void print()
{ std::cout << "(" << x << "," << y << ")\n"; }
};
void case2()
{
demo a,&ra=a;
demo *p = &a;
std::cout << bind(&demo::f, a, _1, 20)(10) << std::endl;
std::cout << bind(&demo::f, ra, _2, _1)(10, 20) << std::endl;
std::cout << bind(&demo::f, p, _1, _2)(10, 20) << std::endl;
std::vector<point> v(10);
std::for_each(v.begin(), v.end(), bind(&point::print, _1));
}
//////////////////////////////////////////
void case3()
{
std::vector<point> v(10);
std::vector<int> v2(10);
std::transform(v.begin(), v.end(), v2.begin(), bind(&point::x, _1));
for(auto x : v2) //foreach循环输出值
std::cout << x << ",";
typedef std::pair<int, std::string> pair_t;
pair_t p(123, "string");
std::cout << bind(&pair_t::first , p)() << std::endl;
std::cout << bind(&pair_t::second, p)() << std::endl;
}
//////////////////////////////////////////
struct func
{
int operator()(int a, int b)
{ return a + b; }
};
void case4()
{
bind(std::greater<int>(), _1, 10);
bind(std::plus<int>(), _1, _2);
bind(std::modulus<int>(), _1, 3);
std::cout << bind<int>(func(), _1, _2)(10,20) << std::endl;
}
//////////////////////////////////////////
void case5()
{
int x = 10;
std::cout << bind(g,_1, cref(x), ref(x))(10) << std::endl;
func af;
std::cout << bind<int>(ref(af), _1, _2)(10, 20) << std::endl;
}
//////////////////////////////////////////
void case6()
{
int x = 10;
auto r = ref(x);
{
int *y = new int(20);
r = ref(*y);
std::cout << r << std::endl;
std::cout << bind(g, r, 1, 1)() << std::endl;
delete y;
}
std::cout << bind(g, r, 1, 1)() << std::endl;
}
//////////////////////////////////////////
#include <boost/rational.hpp>
void case7()
{
typedef rational<int> ri;
std::vector<ri> v = {ri(1,2),ri(3,4),ri(5,6)};
std::remove_if(v.begin(), v.end(), bind(&ri::numerator, _1) == 1 );
assert(v[0].numerator() == 3);
assert(std::find_if(v.begin(), v.end(), bind(&ri::numerator, _1) == 1) == v.end());
auto pos = std::find_if(v.begin(), v.end(),
bind(&ri::numerator, _1) >3 && bind(&ri::denominator, _1) < 8);
std::cout << *pos << std::endl;
pos = find_if(v.begin(), v.end(),
[](ri &r)
{
return r.numerator() >3 && r.denominator() < 8;
});
std::cout << *pos << std::endl;
}
//////////////////////////////////////////
void case8()
{
auto lf = [](int x)
{
return f(x, 9);
};
assert(lf(10) == bind(f, _1, 9)(10));
}
//////////////////////////////////////////
int f(double a, double b)
{ return a * b;}
typedef int (*f_type1)(int, int);
typedef int (*f_type2)(double, double);
void case9()
{
//std::cout << bind(f,1,2)() << std::endl;
f_type1 pf1 = f;
f_type2 pf2 = f;
std::cout << bind(pf1,1,2)() << std::endl;
std::cout << bind(pf2,1,2)() << std::endl;
}
//////////////////////////////////////////
int main()
{
case1();
case2();
case3();
case4();
case5();
case6();
case7();
case8();
case9();
}