A
AlesD
Hello,
could you please help me with following problem:
I have reversible container which holds (unsorted) instances of objects
with common base class. From time to time I need to search for last
occurance of instance meeting some criteria. When I find it I need to
move all folowing instances ("tail") into different cointainer (of same
type) while preserving their order.
I'm using find_if with container's reverse_iterators for the search, but
don't know how to preform the movement. If I use container constructor
with reverse_iterators the order of items in new container would be
reversed. This can be solved by reversing the container, but it might
take time if the tail has many items.
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?"
Thanks in advance, Ales
----- ilustrative code -----
class base_t;
class container_t: list<base_t>
bool test(base_t& item);
container_t tail(container_t& c)
{
c::reverse_iterator found = find_if(c.rbegin(), c.rend(), test);
if (found == c.rend())
return container_t();
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time
return result;
}
could you please help me with following problem:
I have reversible container which holds (unsorted) instances of objects
with common base class. From time to time I need to search for last
occurance of instance meeting some criteria. When I find it I need to
move all folowing instances ("tail") into different cointainer (of same
type) while preserving their order.
I'm using find_if with container's reverse_iterators for the search, but
don't know how to preform the movement. If I use container constructor
with reverse_iterators the order of items in new container would be
reversed. This can be solved by reversing the container, but it might
take time if the tail has many items.
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?"
Thanks in advance, Ales
----- ilustrative code -----
class base_t;
class container_t: list<base_t>
bool test(base_t& item);
container_t tail(container_t& c)
{
c::reverse_iterator found = find_if(c.rbegin(), c.rend(), test);
if (found == c.rend())
return container_t();
container_t result(c.rbegin(), found); // this is reversed tail
reverse(result.begin(), result.end()); // this takes time
return result;
}