-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathrange_for.cxx
37 lines (31 loc) · 913 Bytes
/
range_for.cxx
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
#include <vector>
#include <cstdio>
int main() {
// Loop over all odd indices and break when i > 10.
for(int i : @range(1::2)...) {
printf("%d ", i);
if(i > 10)
break;
}
printf("\n");
// The same as above, but put the end index in the range.
for(int i : @range(1:10:2)...)
printf("%d ", i);
printf("\n");
int items[] { 5, 2, 2, 3, 1, 0, 9, 8 };
// Loop over all but the first item.
for(int i : items[1:]...)
printf("%d ", i);
printf("\n");
// Loop over items in reverse order.
for(int i : items[::-1]...)
printf("%d ", i);
printf("\n");
// Bind to the range expression which adds consecutive elements.
// The items array has 8 elements, but this loop runs through 7 elements,
// because the slice expression items[1:] starts at index 1 (so only has
// 7 elements).
for(int x : items[:] + items[1:]...)
printf("%d ", x);
printf("\n");
}