For example, I want to write std::string my_str( "foobar\n" ); literally into std::ostream& with the backslash-n intact (no formatting).
Perhaps I need to convert my_str with a formatting function that converts backslash to double-backslash first? Is there a standard library function for that?
Or maybe there is a directive I can pass to std::ostream&?
The easiest way to do this for larger strings is with raw string literals:
If you want parentheses in it, use a delimiter:
I don’t know of anything that will let you keep the escape codes etc. in the string, but output it without, but
std::transformwith anstd::ostream_iterator<std::string>destination and a function that handles the cases you want should do it, as it does in this example: