问题
how can print escape characters without further processing and as \t
or \n
or ... in std::cout
?
I dont want to process text manually before sending it to output?
Is there any switch to std::cout
for this purpose?
回答1:
Basically a raw string literal is a string in which the escape characters (like \n \t or \" ) of C++ are not processed. A raw string literal starts with R"( and ends in )", let's see it in an example the difference between a normal string and a raw string in C++:
string raw_str=R"(First line.\nSecond line.\nEnd of message.\n)";
cout<<raw_str<<endl;
result:
~$ ./a.out
First line.\nSecond line.\nEnd of message.\n
回答2:
If you add one extra slash there as \\t you can see \t in the output of std::cout
For example: cout<<"\\t hello" will print \t hello.
I hope this helps
来源:https://stackoverflow.com/questions/38843417/how-can-print-raw-escape-characters-as-t-and-n-in-cout