Hi,
I need to iterate over a char
string and I wrote the code as follows:
char *s = "abcdefg";
iterator_range<char*> ir(
&s[0], &s[ strlen(s) ] );
char *cp;
for (cp = (char*)ir.begin();
cp != (char*)ir.end(); ++cp)
{
cout << "cp in ri
[" << *cp << "]\n";
}
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.
However,
what I want is – in reverse traversal also, I want to use the same pointer (“cp”
in this case). In other words, I want to use the same pointer to traverse in
both directions.
Here’s
what I want to be able to write, but get a compile error with both attempts and
I was wondering if you could help.
Option
1:
for (cp = boost::rbegin(ir); cp != boost::rend(ir);
++cp)
{
cout <<
"cp reverse in ri [" << *cp << "]\n";
}
Option 2:
while( cp != ire )
{
//Do something
cp++
}
Thanks so much in advance,
Anant