473,378 Members | 1,434 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

How to get a reverse_iterator

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
Aug 10 '08 #1
4 2064
* Tong * wrote:
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.
Aug 10 '08 #2
* * // 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).
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.
Aug 10 '08 #3
On Sat, 09 Aug 2008 20:21:10 -0700, mastier_na_vsje_ruki wrote:
>Â* Â* // 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
Aug 10 '08 #4
Ensemble wrote:
On Sat, 09 Aug 2008 20:21:10 -0700, mastier_na_vsje_ruki wrote:
>> // 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.
Aug 10 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: praetorian | last post by:
high all i iterate my list with reverse_iterator and at some point i decide that certain element needs to be erased. how do i do that ? this is the code i tried inside iteration loop (_it is...
6
by: Boogie El Aceitoso | last post by:
Hi, I'm writng a function that works an a std::string and should accept either an iterator or a reverse_iterator. How can I do this? O:-)
3
by: ben | last post by:
If I have a reverse_iterator to an element, does the standard library provide a way to find the iterator equivalent or do I have to roll my own?
3
by: Eric Twietmeyer | last post by:
Hello, In playing with a reverse_iterator issue today, I realized that I could use its base() method to get at the underlying iterator. However, I realized that in the implementation shipping...
13
by: jason | last post by:
My question is regaring STL iterator/reverse_iterator. I would like to write a function which does the following int CFoo::calculate(std::vector<int>::iterator itBegin,...
3
by: Dalbosco J-F | last post by:
Hi, Sorry if this has already been answered. Given a std::list and a reverse_iterator is there a way to erase the element pointed to by the reverse_iterator via the erase method? Apparently...
3
by: noone | last post by:
string operator()(const bool clean=true) { string rv; MPEGQUEUE::reverse_iterator i=thequeue.rbegin(); MPEGQUEUE::reverse_iterator t=thequeue.rend(); while (i!=thequeue.rend()) { if...
3
by: bb | last post by:
Hi, Please could you clarify why 'implicit conversion' does not take place while assigning an iterator to reverse_iterator. However, it happens while initializing/constructing. e.g. typedef...
20
by: Jess | last post by:
Hello, I think the two reverse_iterators are the same, except that the const_ version doesn't allow me to change the value pointed to by the iterators. However, I have a program that works for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.