首页 > 其他 > 详细

4、原始字面量

时间:2021-07-26 09:14:48      阅读:26      评论:0      收藏:0      [点我收藏+]

一、介绍 

  平时使用‘\‘去做转译,   在 C++11之后, 出现了原始字面量,定义方式为:R "xxx(原始字符串)xxx",表示括号里的字符串是原始的字符串,不需要做转译。其中()两边的字符串可以省略。当有时,两边必须相同。

二、使用

  案例1:

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str = "D:\hello\world\test.text";
    cout << str << endl;
    string str1 = "D:\\hello\\world\\test.text";
    cout << str1 << endl;
    string str2 = R"(D:\hello\world\test.text)";
    cout << str2 << endl;

    return 0;
}

结果:

技术分享图片

 

  案例2:在 C++11 之前如果一个字符串分别写到了不同的行里边,需要加连接符,这种方式不仅繁琐,还破坏了表达式的原始含义,如果使用原始字面量就变得简单很多,很强直观,可读性强。

  

#include<iostream>
#include<string>
using namespace std;
int main()
{
    string str = R"(<html>
        <head>
        <title>
        海贼王
        </title>
        </head>
        <body>
        <p>
        我是要成为海贼王的男人!!!
        </p>
        </body>
        </html>)";
    cout << str << endl;

    return 0;
}

 

4、原始字面量

原文:https://www.cnblogs.com/zwj-199306231519/p/15059482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!