027 原生字符串字面量

C++ 中原生字符串的声明相当简单,只需要在字符串前加入前缀——R,并在引号中使用括号左右标识,就可以声明该字符串字面量为原生字符串了。

示例一:

    cout << R"(hello,\n
            world!)" << endl;
    cout << u8R"(hello,\n
            world!)" << endl;
    cout << uR"(hello,\n
            world!)" << endl;
    cout << UR"(hello,\n
            world!)" << endl;

输出:

hello,\n
            world!
hello,\n
            world!
0x10b93df12
0x10b93ddf4

示例二:

    cout << u8R"(\u4F60,\n
            \u597D)" << endl;
    cout << u8R"(你好)" << endl;
    cout << sizeof(u8R"(hello)") << "\t" << u8R"(hello)" << endl;
    cout << sizeof(uR"(hello)") << "\t" << uR"(hello)" << endl;
    cout << sizeof(UR"(hello)") << "\t" << UR"(hello)" << endl;

输出:

\u4F60,\n
            \u597D
你好
6   hello
12  0x101f35f3a
24  0x101f35e64
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容