Sunday, 15 September 2013

c++ - How to call erase with a reverse iterator -


I am trying to do something like:

 for  (std :: List & lt; cursor :: and & gt; :: reverse_iterator i = m_CursorStack.rbegin (); i! = M_CursorStack.rend (); ++ i) {if (* i == pCursor) {m_CursorStack.erase (i ); break; }}  

However erasing takes an iterator and not a reverse itater. Is there a way to convert a reverse itater into a regular iterator or another way to remove this element from the list?

After some more research and testing I found the solution clearly [24.4.1 / 1] According to the relationship between i.base () and i is:

  & amp; * (Reverse_itreator (i)) == & amp; * (I - 1)  

(from A):

So you have to apply an offset if you get support (). The solution is therefore:

  m_CursorStack.erase (- (i.base ()));  

Edit

Update for C ++ 11.

The reverse_ititor i is unchanged:

  m_CursorStack.erase (std :: Next (i) .base ());  

The reverse_terator i is advanced:

  std :: advance (i, 1); M_CursorStack.erase (i.base ());  

I find it very clear from my previous solution, use whatever you need.


No comments:

Post a Comment