Should I increment or decrement the reverse iterator?

896 views Asked by At

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.

1

There are 1 answers

0
Mark Ransom On BEST ANSWER

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 ++.