Sign in to follow this  
bushimports

reversing the direction of an insert iterator

Recommended Posts

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

Share this post


Link to post
Share on other sites
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 variables
std::string Input;

//this is how I am using the insert function
Input.insert(TempInsertPt,1,key);
TempInsertPt++;


Much Thanks, Jody Bush

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this