An example of using base iterator in C++

The code below demonstrates how to access underlying range iterator by transformed iterator:

struct A
{
    int x;
};

struct B
{
    A* a;
};

std::vector<B> v;

int main()
{
    auto a_range = v | std::views::transform(std::mem_fn(&B::a));

    auto i = std::ranges::find(a_range.begin(), a_range.end(), 5, std::mem_fn(&A::x));

    A* a = *i;

    B& b = *(i.base());
}

1 Response to An example of using base iterator in C++

  1. dmitriano says:

    https://stackoverflow.com/questions/65912645/is-there-any-possible-way-to-get-origin-value-using-transformed-iterator
    It’s possible to get an iterator to the underlying range by calling .base().

Leave a Reply

Your email address will not be published. Required fields are marked *