To remove manual logic in my code, I use that construct:
std::ranges::drop_view a { std::ranges::take_view(my_range, my_range.size() -X), Y};
with X and Y values I pass at runtime.
Even though I check the algorithms, I could not find a shorter way that has the following constraints:
- don't go beyond or below the range I want, and don't do anything if the range has 0 elements -> no overflow
- non owning -> no copies
ranges::subranges doesn't meet those requirements.
Thanks
CodePudding user response:
You can compose take_view
and drop_view
into a new adaptor
auto take_and_drop = [](auto n, auto m) {
return std::views::take(n) | std::views::drop(m);
};
auto a = my_range | take_and_drop(my_range.size() - X, Y);