Jump to content
  • Advertisement
Sign in to follow this  
Couvillion

Is this a safe way to manipulate std::string?

This topic is 4815 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've got some code for removing whitespace from a string. It compiles and runs using gcc 3.4.4, but I'm wondering if I'm just lucky:
	using namespace std;
	using namespace boost::lambda;
	//String with space and tab whitespace:
	string foo = "a b 	c	d	    ef   g";
	cout << "Old foo: " << foo << endl;
	//Pack all non-white space to the front, and chop off whitespace.
        //Is this safe?
	foo.erase(remove_if(foo.begin(), foo.end(), _1 == ' ' || _1 == '\t'), foo.end());
	cout << "New foo: " << foo << endl;
I'm not sure what string::end() gives you. Does it remain the same, as long as the string stays the same length? If so the call to erase() should be okay. What I'm worried about is that foo.end() called as the foo.erase() parameter may be evaluated first and then invalidated by the call to remove_if, in which case I may get weird behavior. Anybody know if the erase line would work for any string, or would it blow up sometime?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
If you're worried about order of operations, just rewrite it to perform the work in two lines instead of one, to eliminate the ambiguity.

Share this post


Link to post
Share on other sites
You're perfectly safe. remove_if does not (indeed cannot) modify the size of the underlying container, it merely moves elements around.

Enigma

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
You're perfectly safe. remove_if does not (indeed cannot) modify the size of the underlying container, it merely moves elements around.

Enigma


Thanks! That's what I thought, but I wasn't sure if the act of manipulating the objects invalidated the end() iterator.

Answering the previous poster, the only reason didn't split it into two lines is because I hate typing, unless it's using forums to get strangers to do my job for me.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by Couvillion
Answering the previous poster, the only reason didn't split it into two lines is because I hate typing
Yet you use C++ for string manipulation...
Anyway, wouldn't this be shorter:
erase_all(foo, " ");
erase_all(foo, "\t");

erase_all is part of boost also.

Share this post


Link to post
Share on other sites
Quote:
Original post by Anonymous Poster
Quote:
Original post by Couvillion
Answering the previous poster, the only reason didn't split it into two lines is because I hate typing
Yet you use C++ for string manipulation...
Anyway, wouldn't this be shorter:
erase_all(foo, " ");
erase_all(foo, "\t");

erase_all is part of boost also.


Same functionality with few characters, so, yeah, that's better, using the typing criterion.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!