Infinite loop with string iterator in C++ -


while i'm willing parse instructions contained in strings, trying clean string it's spaces , tabs character, in order seek instructions in it. unfortunately, loop going infinite loop, , can't find why i'm refreshing iterator on each erasing of char...

any please?

void  myclass::parseinstructions(std::string& line) {     std::string::iterator        it;      (it = line.begin(); != line.end(); ++it)         if (((*it) == ' ') || ((*it) == '\t'))             = line.erase(it); } 

the flow of code:

it = line.begin();  while (it != line.end()) {     if (((*it) == ' ') || ((*it) == '\t'))         = line.erase(it);     ++it; } 

correct code:

it = line.begin();  while (it != line.end()) {     if (((*it) == ' ') || ((*it) == '\t'))         = line.erase(it);     else // <---- important         ++it; } 

right you'll miss 2 whitespace characters in row, , when final character whitespace you'll move right past end.

or use std::remove_copy_if, should have lower complexity.


Comments

Popular posts from this blog

apache - Add omitted ? to URLs -

redirect - bbPress Forum - rewrite to wwww.mysite prohibits login -

php - How can I stop spam on my custom forum/blog? -