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

get cycled_view iterator from base iterator #1698

Closed Dejauxvue closed 2 years ago

Dejauxvue commented 2 years ago

I want a cycled_view iterator to an element of a list that I found using ranges::find. I think, a solution should look something like this:

std::list<int> li{1, 2, 3};
auto find_2 = ranges::find(li, 2);
auto cycle_list = li | ranges::views::cycle;
decltype(cycle_list)::iterator (find_2); // it doesn't work like this

I cannot search cycle_list directly, because it doesn't terminate, if the element cannot be found. An alternative would be to search li for the element and only search cycle_list for the element, if it was found in li, but this seems inefficient.

Is there a proper solution to this problem using ranges?

Thanks in advance

Dejauxvue commented 2 years ago

I found a solution that works for me:

std::list<int> li{1, 2, 3};
auto cycle_list = li | ranges::views::cycle | ranges::views::take(li.size());
auto ret = ranges::find(cycle_list,2).base();