
DeĀ : Rao, Anant
So, there's absolutely no way to use the same ptr to traverse backwards? I tried the options based on my limited knowledge, but if there're other options, please let me know (using the same ptr).
It's ironic a simple pointer could go fwd, but not back.
Well, it can go forward or back, but you have to ask it. "++" to go forward, "--" to go back.
char *cp; boost::range_reverse_iterator< char*>::type irb = boost::rbegin( ir ); boost::range_reverse_iterator< char*>::type ire = boost::rend( ir ); for (; irb != ire; ++irb) { cout << "reverse cp [" << *irb << "]\n"; }
This works like a charm - both in fwd and reverse traversal.
This works because irb is a reverse iterator, with operator++ defined to go "backwards". That behavior will not change because the initial value happens to come from a reverse_iterator. A char* is a char*, regardless of the source of its initial value.
Option 1: for (cp = boost::rbegin(ir); cp != boost::rend(ir); ++cp) { cout << "cp reverse in ri [" << *cp << "]\n"; }
cp is a char* here, and "++" on a char* is defined to increment it, moving it "forward". To move it "backward", use "--"