Remove spaces from a string in C++

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?


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;

Need Your Help

Enterprise App and the Enterprise App Client

java jpa ejb

I came aboard a new project with a new company and we are trying to use JPA to do some DB work. So we have an Ear with an EJB, a webservice, and then there is a app client in the ear that really d...

MVC5 Routing with two parameters in an Area


I have an area called News, and within that area I have a Post Controller. The post controller has the following action: