ericniebler / range-v3

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

Can the base iterator of a cycle view iterator be accessed? #1686

Closed Dejauxvue closed 2 years ago

Dejauxvue commented 2 years ago

I am trying to iterate a range in a cycle and do something with each element depending on its position in the original range. Similar to this:

std::vector<int> v{0, 1, 2, 3, 4};

auto cycle_view_of_v = v | ranges::views::cycle;

for (auto iter = cycle_view_of_v.begin(); iter != cycle_view_of_v.end(); ++iter) {
    if (iter.base() == v.begin())
        std::cout << *iter << std::endl;
}

However, iter.base() does not compile. Is there a possibility to access this base iterator of iter?

I use range-v3 0.11.0#1 built with vcpkg and MSVC v142.

Edit: code formatting

dvirtz commented 2 years ago

you can use views::enumerate if you need the position:

auto cycle_view_of_v = v | ranges::views::enumerate | ranges::views::cycle;

for (auto&& [item, index] : cycle_view_of_v | ranges::views::take(10)) {
    if (index == 0) {
        std::cout << item << std::endl;
    }
}

https://gcc.godbolt.org/z/6x1fPdYnf

On Fri, 28 Jan 2022 at 17:15, Dejauxvue @.***> wrote:

I am trying to iterate a range in a cycle and do something with each element depending on its position in the original range. Similar to this:

` std::vector v{0, 1, 2, 3, 4};

auto cycle_view_of_v = v | ranges::views::cycle;

for (auto iter = cycle_view_of_v.begin(); iter != cycle_view_of_v.end(); ++iter) { if (iter.base() == v.begin()) std::cout << *iter << std::endl; }`

However, iter.base() does not compile. Is there a possibility to access this base iterator of iter?

I use range-v3 0.11.0#1 built with vcpkg and MSVC v142.

— Reply to this email directly, view it on GitHub https://github.com/ericniebler/range-v3/issues/1686, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB2ORDP6U2YFXXT5WCJL4ITUYKXKXANCNFSM5NBALRCQ . You are receiving this because you are subscribed to this thread.Message ID: @.***>

Dejauxvue commented 2 years ago

That is definitly a good workaround, I will probably do it this way, thank you!

But, is there a good reason, whyiter.base() is not possible? I feel like it should be, conceptually.