-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.cpp
More file actions
280 lines (189 loc) · 7.43 KB
/
algorithms.cpp
File metadata and controls
280 lines (189 loc) · 7.43 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include <iterator>
#include <functional>
#include <algorithm>
#include <numeric>
#include <vector>
#include <iostream>
#include <string>
#include <cctype>
// all - none - any...of
std::vector<int> v = {0,1,2,3,4,5,6,7,8,9};
void test_all_none_any_of() {
//all : all list should pass the test
auto all = std::all_of(v.begin(), v.end(), [](int p) { return p % 2 == 0; });
// any : at least 1 should pass the test
auto any = std::any_of(v.begin(), v.end(), [](int p) { return p % 2 == 0; });
// none: none of the list should pass the test
auto none = std::none_of(v.begin(), v.end(), [](int p) { return p% 2 ==0; });
std::cout << std::boolalpha << "all:" << all << " any:" << any << " none:" << none << std::endl;
}
void test_for_each() {
std::for_each(v.begin(), v.end(), [](int p) { std::cout << p << ' '; });
std::cout << std::endl;
}
void test_count() {
// count, count_if
auto c1 = std::count(v.begin(), v.end(), 3);
auto c2 = std::count_if(v.begin(), v.end(), [](auto i) { return i %2 == 0; });
std::cout << c1 << ":" << c2 << std::endl;
}
void test_find() {
// find, find_if, find_if_not
// string, map, unordered_map...have member finds ( faster and specialized: prefer them)
// vector...deque... dont have finds
//
auto itr = std::find(v.begin(), v.end(), 3);
if (itr != v.end()) std::cout << "found" << std::endl;
itr = std::find_if(v.begin(), v.end(), [](auto value) { return value == 3; }); // if 3
if (itr != v.end()) std::cout << "found" << std::endl;
itr = std::find_if_not(v.begin(), v.end(), [](auto value) { return value == 3; }); // if not 3
if ( itr != v.end()) std::cout << "found" << std::endl;
}
void test_find_first_of() {
// find_first_of(beg1,end1,beg2,end2)->itr1
std::string text = "sample text;sample:...";
std::string delims =" ;:,.";
auto itr = std::find_first_of(text.begin(), text.end(), delims.begin(), delims.end());
if (itr != text.end())
std::cout << "found:" << std::distance(text.begin(), itr) << std::endl;
}
void test_search() {
// search(beg1,end1, beg2, end2) -> itr1
std::string text = "sample text fsafdsaf";
std::string key = "fsa";
auto result = std::search(text.begin(), text.end(), key.begin(), key.end());
if (result != text.end())
std::cout << "found" << std::endl;
}
// search for a sequence of sth like 4 4 => 4444
void test_search_n() {
// search_n(beg1, end1, count, value) -> iter
// searches count numbered sequence in beg-end with value => value
std::string text = "aaaabbbcc";
auto itr = std::search_n(text.begin(), text.end(), 3, 'a');
if (itr != text.end()) std::cout << "found:" << std::distance(text.begin(), itr) << std::endl;
itr = std::search_n(text.begin(), text.end(), 4, 'b');
if (itr == text.end())
std::cout << "not found" << std::endl;
}
void test_mismatch() {
// mismatch
std::string s = "test";
std::string t = "tes";
auto result = std::mismatch(s.begin(), s.end(), t.begin());
auto result2 = std::mismatch(s.begin(), s.end(), s.rbegin());
std::cout << std::distance(s.begin(), result.first) << std::endl;
std::cout << std::distance(s.begin(), result2.first) << std::endl;
}
void test_copy() {
// copy, copy_if
// target can be : itr or back_insert, inserter... or iterator like ostream_iterator
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout," "));
std::cout << std::endl;
std::vector<int> v2(v.size());
// array to array copy so prepare size before
std::copy(v.begin(), v.end(), v.begin()) ;
std::cout << v2.size() << std::endl;
std::vector<int> v3;
std::copy(v.begin(), v.end(), std::back_inserter(v3));
std::cout << v3.size() << std::endl;
std::vector<int> v4;
std::copy(v.begin(), v.end(), std::inserter(v4, v4.end()));
std::cout << v4.size() << std::endl;
}
void test_transform() {
// transform(beg1, end1, beg2, pred)
// transform(beg1, end1, beg2, beg3, pred)
std::vector<int> v1 = {0,1,2,3,4};
std::vector<int> v2;
std::transform(v1.begin(), v1.end(), std::back_inserter(v2), [](int val) { return val * 2; });
std::copy(v2.begin(), v2.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
std::vector<int> v3;
std::transform(v1.begin(), v1.end(),
v2.begin(), std::back_inserter(v3),
std::plus<>());
std::copy(v3.begin(), v3.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
// lower & upper
//
std::string s = "fdsfdsafdsafdsaf";
std::cout << s << " : ";
std::transform(s.begin(), s.end(), s.begin(), toupper);
std::cout << s << " : ";
std::transform(s.begin(), s.end(), s.begin(), tolower);
std::cout << s << std::endl;
}
void test_unique() {
std::vector<int> v1 = {0,1,1,1,1,2,2,2,3};
auto it = std::unique(v1.begin(), v1.end());
v1.erase(it, v1.end());
std::copy(v1.begin(), v1.end(), std::ostream_iterator<int>(std::cout, " "));
std::string s = "aaaa";
auto itr = std::unique(s.begin(), s.end());
if ( itr == s.end()) throw;
std::cout << std::distance(s.begin(), itr) << std::endl;
s.erase(itr, s.end());
std::cout << s << std::endl;
}
void test_rotate() {
// rotate(beg, next_beg, end)
std::string s = "abcde";
// rotate to left
std::rotate(s.begin(), s.begin() + 2, s.end());
std::cout << s << std::endl;
// rotate to right
std::rotate(s.rbegin(), s.rbegin() + 2, s.rend());
std::cout << s << std::endl;
}
void test_generate(){
std::vector<int> v(10);
std::generate(v.begin(), v.end(), [counter = 0] () mutable { return counter++; });
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
}
void test_reduce() {
std::cout << "reduce:" << std::reduce(v.begin(), v.end(), 0) << std::endl;
std::cout << "min_reduce:" << std::reduce(v.begin(), v.end(), 100, std::minus<>()) << std::endl;
}
void test_equal() {
// equal(beg1, end1, beg2)
// checks if two container equal
std::string s = "abcba";
std::string s2 = "fafafa";
auto res1 = std::equal(s.begin(), s.begin() + s.size()/2, s.rbegin());
auto res2 = std::equal(s2.begin(), s2.begin() + s2.size()/2, s2.rbegin());
std::cout << s << " " << std::boolalpha << res1 << " " << s2 << " " << res2 << std::endl;
}
void test_accumulate() {
std::cout << std::accumulate(v.begin(), v.end(), 0) << std::endl;
std::cout << std::accumulate(v.begin(), v.end(), 1, std::multiplies<>()) << std::endl;
}
void test_inner_product() {
// beg1, end1, beg2, init, plus<>, multiplies
// init + beg1 * beg2
std::vector v1 = {1,2,3};
std::vector v2 = {4,5,6};
std::cout << std::inner_product(v1.begin(), v1.end(), v2.begin(), 0) << std::endl;
std::cout << std::inner_product(v1.begin(), v1.end(), v2.begin(), 0,
[](auto a, auto b) { return a +b; },
[](auto a, auto b) { return a*b; }) << std::endl;
}
int main() {
test_all_none_any_of();
test_for_each();
test_count();
test_mismatch();
test_find();
test_find_first_of();
test_search();
test_search_n();
test_copy();
test_transform();
test_unique();
test_equal();
test_rotate();
test_reduce();
test_generate();
test_accumulate();
test_inner_product();
}