C++ Remove new line from multiline string

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

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

12条回答
  •  隐瞒了意图╮
    2020-12-08 06:35

    If its anywhere in the string than you can't do better than O(n).

    And the only way is to search for '\n' in the string and erase it.

    for(int i=0;i

    For more newlines than:

    int n=0;
    for(int i=0;i

    It erases all the newlines once.

提交回复
热议问题