如何在 cout 中将原始转义字符打印为 \t 和 \n?

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

如何在不进一步处理的情况下将转义字符打印为 \t\n 或 ... 在 std::cout 中? 我不想在将文本发送到输出之前手动处理文本? 是否有为此目的切换到 std::cout

如果你在那里添加一个额外的斜线作为 \\t 你可以在 std::cout

的输出中看到 \t

例如:cout<<"\\t hello" 将打印 \t hello。

希望对您有所帮助

基本上,原始字符串文字是一个未处理 C++ 转义字符(如 \n \t 或 \" )的字符串。原始字符串文字以 R"( 开头并以 )" 结尾,让我们在示例中查看 C++ 中普通字符串和原始字符串之间的区别:

string raw_str=R"(First line.\nSecond line.\nEnd of message.\n)";
cout<<raw_str<<endl;

结果:

~$ ./a.out
First line.\nSecond line.\nEnd of message.\n