【C++11】原始字面量

目的

  1. 解决字符串中转义字符,特殊字符需要繁杂的方式才能输出的问题
  2. 省去字符串换行连接符

表示方式

string str = R"xxx(原始字符串)xxx"

细节

原始字符串两侧可以添加其他字符串,但需要注意:

  1. 两侧字符串必须一致
  2. 所添加的字符串会被忽略

举例

//原始字面量R"()"
#include
using namespace std;
int main(){
    string str= "F:\hello\world\test.txt";
    cout << "单个反斜杠:" << str << endl;
    string str1 = "F:\\hello\\world\\test.txt";
    cout << "双反斜杠:" << str1 << endl;
    string str2 = R"(F:\hello\world\test.txt)";
    cout << "原始字面量:" << str2 << endl;

    string str3 = "\
    
\

\ amazing\

\
\ "
; cout << "换行需要连接符\\:" << str3 << endl; string str4 = R"(

amazing

)"
; cout << "原始字面量可以直接换行省略连接符:" << str4; system("pause"); return 0; }

运行结果

【C++11】原始字面量_第1张图片

你可能感兴趣的:(C++,c++11)