Boost logo

Boost Users :

Subject: Re: [Boost-users] Range_iterator - char* - reverse
From: Rao, Anant (Anant.Rao_at_[hidden])
Date: 2010-12-10 14:49:56


Hi Eric,
Yes, it was a typo on my part to use ++ when I shd have had a --.
But, I was talking about the compile error. That is, even if I had a '--', it would still complain.
The offending line is:

while( cp != ire )
{
  //Do something
  cp--;
}

Please bear with me as I (hopefully) make my requirement statement more precise:

Fwd:
iterator_range<char*> ir( &s[0], &s[ strlen(s) ] );

for (cp = (char*)ir.begin(); cp != (char*)ir.end(); ++cp)
{
       cout << "cp in ri [" << *cp << "]\n";
}

I want similar code (using 'cp') to traverse backwards using 'ir' or another container-in-reverse mode.

Your help much appreciated.
Thanks a lot again,
Anant

-----Original Message-----
From: boost-users-bounces_at_[hidden] [mailto:boost-users-bounces_at_[hidden]] On Behalf Of Eric MALENFANT
Sent: Friday, December 10, 2010 11:23 AM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] Range_iterator - char* - reverse

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 mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

The information contained in this email message and its attachments is intended only for the private and confidential use of the recipient(s) named above, unless the sender expressly agrees otherwise. Transmission of email over the Internet is not a secure communications medium. If you are requesting or have requested the transmittal of personal data, as defined in applicable privacy laws by means of email or in an attachment to email, you must select a more secure alternate means of transmittal that supports your obligations to protect such personal data. If the reader of this message is not the intended recipient and/or you have received this email in error, you must take no action based on the information in this email and you are hereby notified that any dissemination, misuse or copying or disclosure of this communication is strictly prohibited. If you have received this communication in error, please notify us immediately by email and delete the original message.


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