C 中存在转义字符例如\n或\t。当我们尝试打印转义字符时它们不会显示在输出中。为了在输出屏幕上显示转义字符我们使用了R(带转义字符的字符串)作为原始字符串字面量。在字符串前面使用 R 后转义字符将显示在输出中。定义方式R xxx(原始字符串)xxx其中两边的xxx要一样包括长度、顺序xxx在编译时会被忽略对括号中的字符串没有影响一般xxx用途相当于注释这个字符串的用途因此一般不用指定原始字符串必须用括号括起来。例如当我们要打印一个路径时由于路径字符串中常常包含一些特殊字符传统方法通常需要使用转义字符 \ 来处理。但如果使用原始字符串字面量就可以轻松解决这个问题。#include iostream using namespace std; int main(){ string str E:\wwh\c\temp\cpp_new_features; cout str endl; string str1 E:\\wwh\\c\\temp\\cpp_new_features; cout str1 endl; string str2 R(E:\wwh\c\temp\cpp_new_features); cout str2 endl; string str3 R123abc(E:\wwh\c\temp\cpp_new_features)123abc; cout str3 endl; system(pause); return 0; }输出结果E:wwhc empcpp_new_features E:\wwh\c\temp\cpp_new_features E:\wwh\c\temp\cpp_new_features E:\wwh\c\temp\cpp_new_features第一条语句中\h 和 \w 转义失败对应地字符串会原样输出;第二条语句中是在没有原始字面量的时候比较常见的操作第一个反斜杠对第二个反斜杠的转义\\表示’\第三条语句中使用了原始字面量 R() 中的内容来描述路径的字符串因此无需做任何处理第四条语句中括号两边加入xxx不同会报错编译会忽略。在 C11 之前如果一个字符串分别写到了不同行里需要加连接符\这种方式不仅繁琐还破坏了表达式的原始含义如果使用原始字面量就变得简单很多很强直观可读性强。我们通过一个输出 HTML 标签的例子体会一下原始字面量。#include iostream using namespace std; int main(){ string str html\ head\ title\ 原始字面量\ /title\ /head\ body\ p\ C11字符串原始字面量\ /p\ /body\ /html; cout str endl; string str1 html \n\ head\n\ title\n\ 原始字面量\n\ / title\n\ / head\n\ body\n\ p\n\ C11字符串原始字面量\n\ /p\n\ /body\n\ /html; cout str1 endl; string str2 R(html head title 原始字面量 /title /head body p C11字符串原始字面量 /p /body /html ); cout str2 endl; system(pause); return 0; }输出结果html head title 原始字面量 /title /head body p C11字符串原始字面量 /p /body /html html head title 原始字面量 / title / head body p C11字符串原始字面量 /p /body /html html head title 原始字面量 /title /head body p C11字符串原始字面量 /p /body /html