reversing the direction of an insert iterator

Started by
7 comments, last by bushimports 13 years, 11 months ago
I am working on building a textbox from scratch in c++ and when I do an insert operation into a std::string ,the insert function returns a reverse iterator but I need it to go forward. How can I make the reverse iterator which is returned to me go forward. Much Thanks, Jody Bush
Advertisement
No overload of std::string::insert() should return a reverse iterator. How are you using insert()?
I am not overloading the insert function.Below is a code snippet of how I am using it.
//this is the declaration of the string in with my member variablesstd::string Input;//this is how I am using the insert function Input.insert(TempInsertPt,1,key);       TempInsertPt++; 

Much Thanks, Jody Bush
You aren't even using the return value of insert(). Why do you think it's returning a reverse iterator?
I don't guess I knew or did'nt think about it returning a value,how do I find out how to get the return value, and how do I use it? The reason I assumed that it was returning a reverse iterator is because the first character draws properly but every character after that draws behind the one before it instead of in front of it. Thanks again, Jody Bush
If you insert a character at position 1, then all characters except the first one will need to move to the right to make room for it.
Don't you simply want to use append instead?

You don't have to guess what any of the standard library functions return, they're well documented in numerous places on the web.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
The insert function inserts in front of the character given by the iterator/index. Perhaps you want to insert after the given character? In this case you should add 1 to the iterator.

But it seems to me you are not taking into account that the insert function can invalidate your iterator. You should be using the overload for inserting one character and returning an iterator to that.

TempInsertPt = Input.insert(TempInsertPt,key);++TempInsertPt;


(That is, assuming TempInsertPt is an iterator in the first place. Otherwise you should be fine with that.)

If you still can't figure it out, how about a small compilable example of what you are doing and what the result is? Also, if this is a graphical thing, do you have a cursor to show where characters should be inserted?
Asdlfajsdfljlasdf.

Please describe, in English, exactly what you are trying to do with the iterator - the entire process.

Anything else is just confusing the issue.
Ok guys, Thanks to all of you for your help. I finally got the text going the right direction while I was trying to strip the program down to the bare essentials so I could post it here so someone smarter or more experienced than me could maybe see what was wrong with it. I have a few other nitpicking issues to overcome before the text box is acceptably finished,but that one is fixed. Much Thanks to all of you again, Jody Bush

This topic is closed to new replies.

Advertisement