如何在cout中将原始转义字符打印为\ t和\ n? [英] How can print raw escape characters as \t and \n in cout?

查看:262
本文介绍了如何在cout中将原始转义字符打印为\ t和\ n?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

推荐答案

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

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;

结果:

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

这篇关于如何在cout中将原始转义字符打印为\ t和\ n?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆