c++11_原始字面量

1. 目的

  • 解决字符串中被错认为是转义字符或其他特殊字符的问题
  • 优雅地输出多行字符串

2. 格式

R"xxx()xxx"
其中xxx为注释

3. 例子

//原始字面量R"xxx()xxx"
#include <iostream>
#include<string>

using namespace std;
int main() {
    string str = "F:\practice\test.txt";
    cout << "一个反斜杠:" << str << endl;
    string str1 = "F:\\practice\\test.txt";
    cout << "两个反斜杠:" << str1 << endl;
    string str2 = R"("F:\practice\test.txt")";
    cout << "原始字面量:" << str2 << endl;

    string str3 = R"(<html>
    <div>
    <p>
    amazing
    </p>
    </div>
    </html>)";
    cout << "原始字面量多行字符串:" << str3;
    system("pause");
    return 0;
}

image

4. 注意事项

  • 括号前后注释必须相同,可以省略,建议省略
  • 如果必须写注释,注释中不要带括号,否则可能会出错。
  • 括号内的内容里可以出现括号
    总结:不要注释,括号内可以含有括号
posted @   云梦士  阅读(64)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!
点击右上角即可分享
微信分享提示