I am trying to get ride of trailing newlines on a std::stringstream
object after flushing some stuff into it. For now I would try something like this:
const char* inputvalue = "My line\n";
std::stringstream stream;
stream << std::string{inputvalue};
stream.seekp( -1, std::ios_base::end );
if( stream.peek() == '\n' ) {
stream << " ";
} else {
stream.seekp( 0, std::ios_base::end );
}
Is this the most efficient way to do so i.e., fewer CPU instructions and time?
As a side note, can I completely remove the last newline other than putting a space character to get rid of the new line without sacrificing performance?