c++algorithmreverse-iterator

Should I increment or decrement the reverse iterator?


As given here, a good way to iterate backwards through a list is to use rbegin(), as below:

list<DVFGfxObj*>::reverse_iterator iter = m_Objs.rbegin();
for( ; iter != m_Objs.rend(); ++iter) {
}

Unfortunately, I cannot remember whether to ++iter or --iter. Because we are going backwards, using --iter, too, seems logical to me.

I am seeking an intuitive explanation so that I can remember it forever. I do not wish to look it up every single time.


Solution

  • The only reason to have a reverse iterator is to go backwards through the sequence. If you were OK with using -- you could just use a regular iterator. Always use ++.