c++11新特性-原字符串

时间:2022-10-04 14:45:42

c++11引入原字符串是为了简化含有转义字符的字符串的书写,使代码读起来更清晰

eg:一个字符串“\r”ow\char\n”
旧的表示形式:       string a="\\r\"ow\\\\char\\n";
而用原字符串表示为: string a=R"(\r"ow\\char\n)";

c++code:

#ifndef ROWCHAR_CPP
#define ROWCHAR_CPP
#include<iostream>
using namespace std;

int main(){
string string1="rowchar\n";
cout << "char*(rowchar\\n)的结果:" << string1 << endl;

string string2="\\row\\\\ch\\\nar\\n";
cout << "char*(\\\\row\\\\\\\\ch\\\\\\nar\\\\n)的结果:" << string2 << endl;

//不加分隔符:R"(rowstring)"
string rowstring1=R"(rowchar\n)";
cout << "char*R\"(rowchar\\n)\"的结果:" << rowstring1 << endl;

string rowstring2=R"(\\row\\\\ch\\\nar\\n)";
cout << "char*R\"(\\\\row\\\\\\\\ch\\\\\\nar\\\\n)\"的结果:" << rowstring2 << endl;

//加分隔符:R"xxx(rowstring)xxx"
string rowstring3=R"hh("(rowchar)")hh";
cout << rowstring3 << endl;
}

#endif

运行结果:

c++11新特性-原字符串