How can print raw escape characters as \t and \n in cout?

ε祈祈猫儿з 提交于 2019-12-13 08:37:50

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!