Remove spaces from std::string in C++

后端 未结 17 1667
说谎
说谎 2020-11-22 16:47

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?

17条回答
  •  生来不讨喜
    2020-11-22 17:35

       #include 
       using namespace std;
    
       int main() {
           .
           .
           s.erase( remove( s.begin(), s.end(), ' ' ), s.end() );
           .
           .
       }
    

    Source:

    Reference taken from this forum.

提交回复
热议问题