C++ Remove new line from multiline string

c++, multiline, string

Solution

#include <algorithm>
#include <string>

std::string str;

str.erase(std::remove(str.begin(), str.end(), '\n'), str.cend());

The behavior of `std::remove` may not quite be what you'd expect.

A call to remove is typically followed by a call to a container's erase method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.

See an explanation of it here.

Problem

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

Original source

Related problems