C++ Remove new line from multiline string

前端 未结 12 2371
暗喜
暗喜 2020-12-08 06:14

Whats the most efficient way of removing a \'newline\' from a std::string?

12条回答
  •  半阙折子戏
    2020-12-08 06:50

    #include 
    #include 
    
    std::string str;
    
    str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
    

    The behavior of std::remove may not quite be what you'd expect. See an explanation of it here.

提交回复
热议问题