-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathRangeBasedForLoop.cpp
More file actions
146 lines (120 loc) · 4.54 KB
/
RangeBasedForLoop.cpp
File metadata and controls
146 lines (120 loc) · 4.54 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
// =====================================================================================
// RangeBasedForLoop.cpp
// =====================================================================================
module modern_cpp:range_based_for_loop;
/*
* 12 ways to iterate through a vector
*/
namespace RangeBasedForLoop {
// global function
static void processElement(int n)
{
std::cout << n << " ";
}
// functor: "callable object" - class which implements operator()
class ElementProcessor
{
public:
void operator() (int n) const {
std::cout << n << " ";
}
};
static void test_iterations()
{
// container of integral data type
std::vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// =========================================================================
// "Classic style" examples
// a) Very, very classic style ... C-stylistic
for (std::size_t i{}; i != vec.size(); ++i) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
// b) Looks like C++, very classic style
std::for_each(
vec.cbegin(), // Iterator-object for begin of range
vec.cend(), // Iterator-object for end of range
processElement // function pointer
);
std::cout << std::endl;
// c) Same as b), modified ranges
std::for_each(
std::next(vec.cbegin()), // Iterator-object for begin of range
std::prev(vec.cend()), // Iterator-object for end of range
processElement // function pointer
);
std::cout << std::endl;
// d) Looks like C++, classic style, 'Functor'-stylistic
std::for_each(
vec.cbegin(), // Iterator-object for begin of range
vec.cend(), // Iterator-object for end of range
ElementProcessor{} // "callable" object
);
std::cout << std::endl;
// e) Using STL algorithm std::for_each with a lambda: Modern style
std::for_each(
vec.cbegin(), // Iterator-object for begin of range
vec.cend(), // Iterator-object for end of range
[](int n) { // anonymous method (lambda)
std::cout << n << " ";
}
);
std::cout << std::endl;
// =========================================================================
// Range-based 'for' Loop examples
// f) Using Range-based 'for' Loop: Very modern style
for (int n : vec) {
std::cout << n << " ";
if (n == 2) // break is possible
break;
}
std::cout << std::endl;
// g) Same as f), using 'const int&'
for (const int& n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
// h) Same as f): Using 'auto' keyword to cause type inference to be used
for (auto n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
// i) Same as g), using 'const auto&': type inference by reference
for (const auto& n : vec) {
std::cout << n << " ";
}
std::cout << std::endl;
// j) Real C++: How the compiler 'sees' a Range-based 'for' Loop
std::vector<int>::const_iterator begin = vec.cbegin();
std::vector<int>::const_iterator end = vec.cend();
while (begin != end) {
int n{ *begin };
std::cout << n << " ";
++begin;
}
std::cout << std::endl;
// k) Same as h), using 'auto'
auto begin2 = vec.cbegin();
auto end2 = vec.cend();
while (begin2 != end2) {
int n{ *begin2 };
std::cout << n << " ";
++begin2;
}
std::cout << std::endl;
// l) Range-based 'for' loop with initializer,
// we can now use the index variable inside the for statement
for (int index{}; int n : vec) {
std::cout << index << ": " << n << " " << std::endl;
++index;
}
}
}
void main_range_based_for_loop()
{
using namespace RangeBasedForLoop;
test_iterations();
}
// =====================================================================================
// End-of-File
// =====================================================================================