-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloop_refactor.cpp
More file actions
61 lines (51 loc) · 1.71 KB
/
loop_refactor.cpp
File metadata and controls
61 lines (51 loc) · 1.71 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
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using Data = std::string;
bool filter(const Data &data) { return data.size() > 5 && data[0] == 't'; }
bool find_predicate1(const std::vector<Data> &data) {
// C++03 style
std::vector<Data>::const_iterator it = data.begin();
for (; it != data.end(); ++it) {
if (filter(*it))
return true;
}
return false;
}
bool find_predicate2(const std::vector<Data> &data) {
// C++11 style with auto
for (auto it = data.begin(); it != data.end(); ++it) {
if (filter(*it))
return true;
}
return false;
}
bool find_predicate3(const std::vector<Data> &data) {
// C++11 style with range based for loop
for (auto const &data : data) {
if (filter(data))
return true;
}
return false;
}
bool find_predicate4(const std::vector<Data> &data) {
// C++11 style using STL algorithm (and begin/end function not members)
return std::find_if(std::begin(data), std::end(data), filter) != std::end(data);
}
bool find_predicate5(const std::vector<Data> &data) {
// C++11 style with embedded lamda function
return std::find_if(begin(data), end(data), [](const Data &data) {
return data.size() > 5 && data[0] == 't';
}) != end(data);
}
int main() {
std::vector<std::string> words = {"undef", "nothing", "stocking", "floor",
"suit", "turkey", "limit", "measure",
"goldfish", "west", "talk"};
std::cout << find_predicate1(words) << "\n";
std::cout << find_predicate2(words) << "\n";
std::cout << find_predicate3(words) << "\n";
std::cout << find_predicate4(words) << "\n";
std::cout << find_predicate5(words) << "\n";
}