Re: Changing the direction of bi-directional iterator

From: AlesD (ales_d_at_seznam.cz)
Date: 05/02/04


Date: Sun, 02 May 2004 22:45:49 +0200

Leor Zolman wrote:
> On Sun, 02 May 2004 02:06:53 +0200, AlesD <ales_d@seznam.cz> wrote:
>
>
>>Hello,

(snip)

>>So my question is: "Is there any way how to do this (except own
>>implementation of course)?" Or more generally: "Is there any way how to
>>create normal (forward) iterator from reverse_iterator?"
>
>
> Yes, apply the base() member function to the reverse iterator. I've shown a
> way you can apply it in your code below.
>
> Replace this:
>
>> container_t result(c.rbegin(), found); // this is reversed tail
>> reverse(result.begin(), result.end()); // this takes time
>
> with:
> container_t result;
> copy(found.base(), c.end(), back_inserter(result));
>
> This is assuming all the other issues in the code you've shown are
> resolved; I'm going under the assumption you weren't interested in comments
> on other issues and just wanted to see how to deal with the issue at hand.
> HTH,
> -leor

Thank you very much for your advice. I would just like to add what I
discovered later in case someone follows this thread:

----------
reverse_bidirectional_iterator must meat following requierements:

reverse_bidirectional_iterator(i).base() == i
&*ri == &*(--ri.base())
----------

Note the decrement in second reqirement! Although it seems strange that
ri and ri.base() does not point to same thing it has it's reason which
is reversing of ranges. If [rbegin(), rend()) is valid range then
[rend().base(), rbegin().base()) is also valid range.

                Thanks again for your help