Writing your own range adaptor
The standard library contains a series of range adaptors that can be used for solving many different tasks. More are being added in newer versions of the standard. However, there can be situations when you’d like to create your own range adaptor to use with others from the range library. This is not actually a trivial task. For this reason, in this final section of the chapter, we will explore the steps you need to follow to write such a range adaptor.
For this purpose, we will consider a range adaptor that takes every Nth element of a range and skips the others. We will call this adaptor step_view
. We can use it to write code as follows:
for (auto i : std::views::iota(1, 10) | views::step(1)) std::cout << i << '\n'; for (auto i : std::views::iota(1, 10) | views::step(2)) std::cout << i << '\n'; for (auto i : std::views::iota(1, 10) | views::step(3)) ...