ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.06k stars 437 forks source link

Access n-th in a view #1743

Open ghost opened 1 year ago

ghost commented 1 year ago

Hi, I would like to use range but without a for loop.

auto view_ = my_container | ::ranges::views::cycle | ::ranges::views::take( my_container.size() + 1 ) | ::ranges::views::drop( drop_count );
for ( auto &e : view_ ){/*do something*/; break; }

I would like to iterate "a la coroutine" my view manually as in view_() or something of the like. That would give me the current value and increase the internal counter to the next element.

That would enable me to play with multiple views. So, if my_container is vector<int> a{0,1,2,3}, I would have

view_(0); // 0 with  starting offset, here 0
view_(); // 1 and so on taking the cycle into account 

Right now, I have to recreate my view with a new drop_count value, which seems inefficient.

How to achieve that cleanly?

Thanks