|
1 | 1 | #include <Lz/basic_iterable.hpp>
|
2 | 2 | #include <Lz/c_string.hpp>
|
| 3 | +#include <vector> |
3 | 4 |
|
4 | 5 | int main() {
|
5 | 6 | // - Iterable as template parameter
|
6 | 7 | // - Iterable as function argument
|
7 | 8 | // - Iterable has a .size() method because arr has a size
|
8 | 9 | int arr[] = { 1, 2, 3, 4, 5 };
|
9 |
| - lz::basic_iterable<int[5]> iterable_iterable = lz::to_iterable(arr); |
| 10 | + lz::basic_iterable<int[5]> iterable_iterable(arr); |
10 | 11 | std::cout << iterable_iterable.size() << '\n'; // Output: 5
|
11 | 12 |
|
12 | 13 | // - Iterator as template parameter
|
13 | 14 | // - Iterable as function argument
|
14 | 15 | // - Iterable has size() method because std::begin(arr) and std::end(arr) are random access iterators
|
15 |
| - lz::basic_iterable<int*> iterator_iterable = lz::to_iterable(arr); |
| 16 | + lz::basic_iterable<int*> iterator_iterable(arr); |
16 | 17 | std::cout << iterator_iterable.size() << '\n'; // Output: 5
|
17 | 18 |
|
18 | 19 | // - Iterable as template parameter
|
19 | 20 | // - Iterator as function argument
|
20 | 21 | // - Iterable has size() method because std::begin(arr) and std::end(arr) are random access iterators
|
21 |
| - lz::basic_iterable<int[5]> iterable_iterator = lz::to_iterable(std::begin(arr), std::end(arr)); |
| 22 | + lz::basic_iterable<int[5]> iterable_iterator(std::begin(arr), std::end(arr)); |
22 | 23 | std::cout << iterable_iterator.size() << '\n'; // Output: 5
|
23 | 24 |
|
24 | 25 | // - Iterator as template parameter
|
25 | 26 | // - Iterator as function argument
|
26 | 27 | // - Iterable has size() method because std::begin(arr) and std::end(arr) are random access iterators
|
27 |
| - lz::basic_iterable<int*> iterator_iterator = lz::to_iterable(std::begin(arr), std::end(arr)); |
| 28 | + lz::basic_iterable<int*> iterator_iterator(std::begin(arr), std::end(arr)); |
28 | 29 | std::cout << iterator_iterator.size() << '\n'; // Output: 5
|
29 | 30 |
|
30 | 31 | // You can also work with sentinels:
|
31 | 32 | auto cstr = lz::c_string("Hello, world!");
|
32 | 33 | using it = decltype(cstr.begin());
|
33 | 34 | using sentinel = decltype(cstr.end());
|
34 |
| - lz::basic_iterable<it, sentinel> iterable = lz::to_iterable(cstr.begin(), cstr.end()); |
| 35 | + lz::basic_iterable<it, sentinel> iterable(cstr.begin(), cstr.end()); |
| 36 | + std::cout << "c_string iterable does not contain a size() method\n"; |
35 | 37 | // iterable does not contain a size() method because cstr does not have a size and is also not random access
|
| 38 | + |
| 39 | + // This also creates a sized iterable |
| 40 | + using it = decltype(cstr.begin()); |
| 41 | + using sentinel = decltype(cstr.end()); |
| 42 | + // We need to specify that it is a sized iterable |
| 43 | + lz::sized_iterable<it, sentinel> sized_cstr_iterable(cstr.begin(), 5); |
| 44 | + std::cout << sized_cstr_iterable.size() << '\n'; // Output: 5 |
| 45 | + // iterable now contains "Hello" |
| 46 | + |
| 47 | + // The following would get an error because cstr is not random access and there's no way to get the size |
| 48 | + // lz::sized_iterable<it, sentinel> sized_cstr_iterable(cstr.begin(), cstr.end()); |
| 49 | + |
| 50 | + // With STL containers you can use the following: |
| 51 | + std::vector<int> vec = { 1, 2, 3, 4, 5 }; |
| 52 | + lz::basic_iterable<std::vector<int>::iterator> vec_iterable(vec.begin(), vec.end()); |
| 53 | + std::cout << vec_iterable.size() << '\n'; // Output: 5 |
36 | 54 | }
|
0 commit comments