I would avoid using inverse iterators (in general, and in particular for anything other than sequential transversal). Forward and reverse processing iterators work differently, in the case of a direct iterator in the list, the iterator tracks the node that you access through operator* , but in the opposite case, the iterator tracks the next element in the list. The dereferencing act of the reverse iterator takes the predecessor node passed by the iterator and extracts the value from it. Graphically (f is the forward iterator, r is the inverse iterator)
f 1 2 4 r
Both the forward iterator f and the reverse iterator r will return 2 when dereferencing, but the node trace is different. When you insert using r, you insert between 2 and 4, but the main iterator remains on the node pointer, holding 4:
f 1 2 3 4 r
Now, if you are looking for r, the same process applies as above. The predecessor of the current node is received, and the value is printed, except that now the predecessor of 4 is 3, and this is what you get.
Is this the expected behavior. If so, how can you flip the iterator to insert items into the list (or is it useless in this regard)?
Yes, this is the expected behavior. How to use a reverse iterator to insert items into a list? Understanding how it works.
source share