Quick C++ Question, can't erase from a std::string

otherwise

Member
Nov 20, 2005
52
0
0
Hello all. I am writing a quick and dirty parser. I have code that wants to consume everything up to the end of a tag. I write this to find an #INDUCTION tag:

myCopy.erase(0, myCopy.find("#INDUCTION") + 10);

This works wonderfully, unless myCopy.find("#INDUCTION") == 0. Then it erases nothing. It works properly in all other cases.

I thought size_t(0) might be special, but this works fine:

Code:
size_t test = 0;
test += 9;

+ static_cast<std::size_t>(9) in my actual code doesn't work either.

vvvvvvvvvvvvvvvvvvv The response right below this post probably doesn't make sense because I edited the code.
 

Sc4freak

Guest
Oct 22, 2004
953
0
0
You're using erase() incorrectly. The first parameter is the index, whereas the second parameter is the length of characters you want to erase. Also, your usage is potentially dangerous. You're finding its index in one string, but then using that index to erase from what looks to be a different string. Also, string::find can return string::npos, which indicates that the input was not found. Supplying this to erase() will cause a crash.

But nonetheless, this is what you want:
jar.erase(myCopy.find("#INDUCTION"), 10);
 

otherwise

Member
Nov 20, 2005
52
0
0
Thanks for the reply, I miswrote the sample code. It is supposed to be myCopy.erase(0,myCopy.find("#INDUCTION") + 10). Editing the op now.

find() is guaranteed not to be string::npos because of code elsewhere, basically this is in a functor that only runs if a regexp guarantees a certain format (which #INDUCTION) is part of. It could probably be written safer in case the regexp is buggy, but I'm not too worried about the string::npos case right now.

What I'm trying to do is, when it sees #INDUCTION, it erases everything up to #INDUCTION. The code you provided seems like it would erase the next 10 characters after the tag.

The code pretty much does what I want it to do if the tag isn't at the start of the string. If it's at the start of the string though, it simply does nothing. For example:

"....... #INDUCTION f(x)->...." transforms to "f(x)->...." like I want it to.

"#INDUCTION f(x)->....." would remain the same.
 

Sc4freak

Guest
Oct 22, 2004
953
0
0
Oh, right. I misread your post. Well, I can't see any problem with the code.

If it isn't working, the problem probably lies elsewhere. Step through using a debugger to ensure that it's doing everything correctly. If you're using Visual Studio, set a breakpoint just before your code and the program will break into the debugger once it hits the line of code with the breakpoint on it. Then you can use the step into/over/out buttons to go through each line of code to see exactly what's going on.
 

otherwise

Member
Nov 20, 2005
52
0
0
Well, it works now!

I didn't change a thing. All that has changed is I rebooted after installing some updates.

I'm a little puzzles exactly what happened, but for now I'll let it slide.
 

tfinch2

Lifer
Feb 3, 2004
22,114
1
0
Originally posted by: otherwise
Well, it works now!

I didn't change a thing. All that has changed is I rebooted after installing some updates.

I'm a little puzzles exactly what happened, but for now I'll let it slide.

:confused: