Boost logo

Boost Users :

Subject: Re: [Boost-users] Range_iterator - char* - reverse
From: Eric MALENFANT (Eric.Malenfant_at_[hidden])
Date: 2010-12-10 14:23:19


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 "--"


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net