Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 227
J. Doe Avatar asked Oct 18 '25 23:10

J. Doe


1 Answers

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

like image 110
Mark Ransom Avatar answered Oct 21 '25 13:10

Mark Ransom