Remove spaces from a string in C++
c++, string
Solution
`std::string::erase` returns an iterator, but you don't have to use it. Your original string is modified.
string removeSpaces(string input)
{
input.erase(std::remove(input.begin(),input.end(),' '),input.end());
return input;
}
Problem
I am currently learning C++. I am trying to code a method to remove white spaces form a string and return the string with no spaces This is my code: ``` string removeSpaces(string input) { int length = input.length(); for (int i = 0; i < length; i++) { if(input[i] == ' ') input.erase(i, 1); } return input } ``` But this has a bug as it won't remove double or triple white spaces. I found this on the net ``` s.erase(remove(s.begin(),s.end(),' '),s.end()); ``` but apparently this is returning an `iterator` (if I understand well) Is there any way to convert the `iterator` back to my string `input`? Most important is this the right approach?