Skip to content

Commit a6942ea

Browse files
committed
converts testrange to a set of examples
1 parent afb2b62 commit a6942ea

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

examples/range_examples.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <range.hpp>
2+
3+
#include <iostream>
4+
5+
int main() {
6+
std::cout << "range(10): { ";
7+
for (auto i : iter::range(10)) {
8+
std::cout << i << ' ';
9+
}
10+
std::cout << "}\n";
11+
12+
// range works with a start and exclusive stop
13+
std::cout << "range(20, 30): { ";
14+
for (auto i : iter::range(20, 30)) {
15+
std::cout << i << ' ';
16+
}
17+
std::cout << "}\n";
18+
19+
// range supports a step size (prints 50, 52 ... 58)
20+
std::cout << "range(50, 60, 2): { ";
21+
for (auto i : iter::range(50, 60, 2)) {
22+
std::cout << i << ' ';
23+
}
24+
std::cout << "}\n";
25+
26+
// ranges can cover negative values
27+
std::cout << "range(-10, 10, 2): { ";
28+
for (auto i : iter::range(-10, 10, 2)) {
29+
std::cout << i << ' ';
30+
}
31+
std::cout << "}\n";
32+
33+
// the step size doesn't need to evenly divide the distance
34+
std::cout << "range(0, 5, 4): { ";
35+
for (auto i : iter::range(0, 5, 4)) {
36+
std::cout << i << ' ';
37+
}
38+
std::cout << "}\n";
39+
40+
// ranges can go down with a negative step
41+
std::cout << "range(-1, -10, -2): { ";
42+
for (auto i : iter::range(-1, -10, -2)) {
43+
std::cout << i << ' ';
44+
}
45+
std::cout << "}\n";
46+
47+
// ranges can work with floats and other types that act like numbers
48+
// the normal concerns with comparing floats still hold here
49+
std::cout << "range(5.0, 9.9, 0.5): { ";
50+
for (auto i : iter::range(5.0, 9.9, 0.5)) {
51+
std::cout << i << ' ';
52+
}
53+
std::cout << "}\n";
54+
55+
// ranges of course support unsigned values
56+
std::cout << "range(10u): { ";
57+
for (auto i : iter::range(10u)) {
58+
std::cout << i << ' ';
59+
}
60+
std::cout << "}\n";
61+
}

0 commit comments

Comments
 (0)