Connecting Tech Pros Worldwide Forums | Help | Site Map

How to get a reverse_iterator

* Tong *
Guest
 
Posts: n/a
#1: Aug 10 '08
Hi,

Simple question, how to get a reverse_iterator?

As you can see from the following code, I managed to answer the first half
of the question myself, but can't figure out the second half:

string line("FIRST,MIDDLE,LAST");

// find first element in a comma-separated list
string::iterator comma = find(line.begin(), line.end(), ',');
cout << string(line.begin(), comma) << endl;

cout << string(++comma, line.end()) << endl;

// get reverse_iterator from iterator
string::reverse_iterator commar = string::reverse_iterator(comma);
cout << string(line.rbegin(), commar) << endl;

// get reverse_iterator from reverse_iterator. wrong. how to fix?
cout << string(++comma, string::reverse_iterator(line.rbegin())) << endl;


Please help.

Thanks

tong

Ian Collins
Guest
 
Posts: n/a
#2: Aug 10 '08

re: How to get a reverse_iterator


* Tong * wrote:
Quote:
Hi,
>
Simple question, how to get a reverse_iterator?
>
As you can see from the following code, I managed to answer the first half
of the question myself, but can't figure out the second half:
>
string line("FIRST,MIDDLE,LAST");
>
// find first element in a comma-separated list
string::iterator comma = find(line.begin(), line.end(), ',');
cout << string(line.begin(), comma) << endl;
>
cout << string(++comma, line.end()) << endl;
>
// get reverse_iterator from iterator
string::reverse_iterator commar = string::reverse_iterator(comma);
cout << string(line.rbegin(), commar) << endl;
>
// get reverse_iterator from reverse_iterator. wrong. how to fix?
cout << string(++comma, string::reverse_iterator(line.rbegin())) << endl;
>
It's not the initialising of the iterator that's the problem, it's the
attempt to construct a string from an iterator and a reverse_iterator.

--
Ian Collins.
mastier_na_vsje_ruki
Guest
 
Posts: n/a
#3: Aug 10 '08

re: How to get a reverse_iterator


* * // get reverse_iterator from reverse_iterator. wrong. how to fix?
Quote:
* * cout << string(++comma, string::reverse_iterator(line.rbegin())) << endl;
Both arguments for string constructor should be either iterators or
reverse_iterators.
Besides I don't see the point of calling string::reverse_iterator copy
constructor with line.rbegin(). It won't convert the reverse_iterator
-- if that's what you've expected -- it'll just create another
reverse_iterator (only silently casting from const_reverse_iterator).
What's more - even if you changed ++comma to ++commar in the last
line, the string's ctor would fail because of invalid iterator range.

regards,
slaimi.
Ensemble
Guest
 
Posts: n/a
#4: Aug 10 '08

re: How to get a reverse_iterator


On Sat, 09 Aug 2008 20:21:10 -0700, mastier_na_vsje_ruki wrote:
Quote:
Quote:
>Â* Â* // get reverse_iterator from reverse_iterator. wrong. how to fix?
>Â* Â* cout << string(++comma, string::reverse_iterator(line.rbegin())) << endl;
>
Both arguments for string constructor should be either iterators or
reverse_iterators.
Besides I don't see the point of calling string::reverse_iterator copy
constructor with line.rbegin(). It won't convert the reverse_iterator
-- if that's what you've expected -- it'll just create another
reverse_iterator (only silently casting from const_reverse_iterator).
So, as oppose to double-negative gives you positive, double reverse
iterator doesn't give you a normal iterator, correct?

thx
Ian Collins
Guest
 
Posts: n/a
#5: Aug 10 '08

re: How to get a reverse_iterator


Ensemble wrote:
Quote:
On Sat, 09 Aug 2008 20:21:10 -0700, mastier_na_vsje_ruki wrote:
>
Quote:
Quote:
>> // get reverse_iterator from reverse_iterator. wrong. how to fix?
>> cout << string(++comma, string::reverse_iterator(line.rbegin())) << endl;
>Both arguments for string constructor should be either iterators or
>reverse_iterators.
>Besides I don't see the point of calling string::reverse_iterator copy
>constructor with line.rbegin(). It won't convert the reverse_iterator
>-- if that's what you've expected -- it'll just create another
>reverse_iterator (only silently casting from const_reverse_iterator).
>
So, as oppose to double-negative gives you positive, double reverse
iterator doesn't give you a normal iterator, correct?
>
Correct.

--
Ian Collins.
Closed Thread