Skip to content

Commit d558d3d

Browse files
committed
Add docs: build instructions and examples in README
1 parent 7ddb37f commit d558d3d

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

README.md

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,43 @@ The README itself is licesed with CC0 1.0 Universal. Copy the contents and incor
2525

2626
## Examples
2727

28-
Full runable examples can be found in `examples/` - please check [./examples/README.md](./examples/README.md).
28+
Full runable examples can be found in `examples/` - please check [./examples/README.md](./examples/README.md) for building the code on local setup or on Compiler Explorer.
2929

30-
### TODO - first example
30+
### Repetead Chars Iterator
3131

32-
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R4)](https://wg21.link/P2727R4):
32+
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R)](https://wg21.link/P2727R4): define a random access iterator that iterates over a sequence of characters repeated indefinitely.
3333

3434
```cpp
35-
TODO
35+
// Create a repeated_chars_iterator that iterates over the sequence "foo" repeated indefinitely:
36+
// "foofoofoofoofoofoo...". Will actually extract a prefix of the sequence and insert it into a std::string.
37+
constexpr const std::string_view target = "foo";
38+
constexpr const auto len = 7; // Number of extracted characters from the sequence.
39+
40+
// Create iterators that iterate over the sequence "foofoofoofoofoofoo...".
41+
repeated_chars_iterator it_first(target.data(), target.size(), 0); // target.size() == 3 is the length of "foo", 0 is this iterator's position.
42+
repeated_chars_iterator it_last(target.data(), target.size(), len); // Same as above, but now the iterator's position is 7.
43+
44+
std::string extracted_result;
45+
std::copy(it_first, it_last, std::back_inserter(extracted_result));
46+
assert(extracted_result.size() == len);
47+
std::cout << extracted_result << "\n"; // Expected output at STDOUT: "foofoof"
3648
```
3749
38-
### TODO - second example
50+
### Filter Integer Iterator
3951
40-
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R4)](https://wg21.link/P2727R4):
52+
The next code snippet shows iterator interface support added in [`std::iterator_interface` (P2727R4)](https://wg21.link/P2727R4): define a forward iterator that iterates over a sequence of integers, skipping those that do not satisfy a predicate.
4153
4254
```cpp
43-
TODO
55+
// Create a filtered_int_iterator that iterates over the sequence {1, 2, 3, 4, 10, 11, 101, 200, 0},
56+
// skipping odd numbers. 0 is not skipped, so it will be the last element in the sequence.
57+
std::array a = {1, 2, 3, 4, 10, 11, 101, 200, 0};
58+
filtered_int_iterator it{std::begin(a), std::end(a), [](int i) { return i % 2 == 0; }};
59+
60+
while (*it) {
61+
std::cout << *it << " ";
62+
++it;
63+
}
64+
std::cout << "\n";
4465
```
4566

4667
## How to Build
@@ -69,9 +90,9 @@ Example of installation on `Ubuntu 24.04`:
6990
apt-get install -y cmake make ninja-build
7091

7192
# Example of toolchains:
72-
apt-get install \
73-
g++-14 gcc-14 gcc-13 g++-14 \
74-
clang-18 clang++-18 clang-17 clang++-17
93+
apt-get install \
94+
g++-14 gcc-14 gcc-13 g++-14 \
95+
clang-18 clang++-18 clang-17 clang++-17
7596
```
7697

7798
### Instructions

examples/filter_int_iterator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ struct filtered_int_iterator
5555
};
5656

5757
int main() {
58-
// Create a filtered_int_iterator that iterates over the sequence {1, 2, 3, 4, 10, 11, 101, 200, 0}, skipping odd numbers.
59-
// 0 is not skipped, so it will be the last element in the sequence.
58+
// Create a filtered_int_iterator that iterates over the sequence {1, 2, 3, 4, 10, 11, 101, 200, 0},
59+
// skipping odd numbers. 0 is not skipped, so it will be the last element in the sequence.
6060
std::array a = {1, 2, 3, 4, 10, 11, 101, 200, 0};
6161
filtered_int_iterator it{std::begin(a), std::end(a), [](int i) { return i % 2 == 0; }};
6262

examples/repeated_chars_iterator.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ int main() {
5757
std::string extracted_result;
5858
std::copy(it_first, it_last, std::back_inserter(extracted_result));
5959
assert(extracted_result.size() == len);
60-
std::cout << extracted_result << "\n"; // Expected output at STDOUT: "foofoof"
60+
std::cout << extracted_result << "\n"; // Expected output at STDOUT: "foofoof"
6161

6262
return 0;
6363
}

0 commit comments

Comments
 (0)