Calling std::list::erase(iterator pos) with a reverse_iterator?

D

Dalbosco J-F

Hi,

Sorry if this has already been answered.

Given a std::list and a reverse_iterator is there a way to erase the
element pointed to by the reverse_iterator via the erase method?
Apparently the erase method takes a normal iterator and it fails to
compile if I pass it a reverse_iterator. On the other hand, I don't
wan't to use myList.remove( *(myReverseIterator)) for performance reason.

Can anyone help?
Thanks by advance,
JF.
 
A

Azumanga

Dalbosco said:
Hi,

Sorry if this has already been answered.

Given a std::list and a reverse_iterator is there a way to erase the
element pointed to by the reverse_iterator via the erase method?
Apparently the erase method takes a normal iterator and it fails to
compile if I pass it a reverse_iterator. On the other hand, I don't
wan't to use myList.remove( *(myReverseIterator)) for performance reason.

reverse_iterator has a method called "base" which will give you back
the base iterator. You can use this.

Chris
 
A

Azumanga

Dalbosco said:
Hi,

Sorry if this has already been answered.

Given a std::list and a reverse_iterator is there a way to erase the
element pointed to by the reverse_iterator via the erase method?
Apparently the erase method takes a normal iterator and it fails to
compile if I pass it a reverse_iterator. On the other hand, I don't
wan't to use myList.remove( *(myReverseIterator)) for performance reason.

Apologises for two posts in a short period of time, I just realised my
previous post had an error.

In the standard, you'll notice that:

Iterator base() const; returns "current", while operator* is defined
as:

" iterator tmp = current; return *--tmp;"

Therefore, *reverse_it and *(reverse_it.base()) aren't the same thing.
*reverse_it and *(--reverse_it.base()) are.

Reverse iterators are strange..

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top