• C++中使用R“()“标记符书写多行字符串


    在C#中使用@表示的字符串能够跨越数行。用于在C#中写JS或SQL代码比较方便。

    string sqlInsert = @"INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 2, 'a04006', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 3, 'a04007', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 1, 'a99501', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 2, 'a99502', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 3, 'a99500', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 4, 'a99505', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 5, 'a99504', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 6, 'a99503', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 23, 'a24901', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 27, 'a24904', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 28, 'a24905', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 29, 'a24042', '.3');
                                    INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 36, 'a25002', '.3');)";
    
    Console.WriteLine(sqlInsert);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    运行结果如下图所示:
    CSharp

    string s_JavaScript = @"
                ";
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    那么在C++中有没有比较方便的方式书写SQL脚本呢?因为在实际编程中,对于那种较长的SQL脚本,我们如果在代码中一行写的话有时不容易阅读和理解。在油管上看到C++博主The Cherno的一篇String Literals in C++的视频,里面提到了使用R"()"标记符书写多行字符串的用法。

    原始的C/C++语言可以按照下面那样书写多行的字符串

    const char* name005 = "line1\n"
    		"line2\n"
    		"line3\n";
    
    • 1
    • 2
    • 3

    不过庆幸的是C++中提供了R"()"的方式书写多行字符串,如下所示:

    #include 
    #include 
    
    int main()
    {
       std::string sqlInsert = R"(INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 1, 'a04005', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 2, 'a04006', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 24, 0, 1, 3, 'a04007', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 1, 'a99501', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 2, 'a99502', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 3, 'a99500', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 4, 'a99505', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 5, 'a99504', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 16, 0, 1, 6, 'a99503', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 23, 'a24901', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 27, 'a24904', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 28, 'a24905', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 29, 'a24042', '.3');
    							INSERT INTO tb_param(protocol, slave, number, ptype, pid, name, format)  VALUES(2, 22, 0, 1, 36, 'a25002', '.3');)";
    
    	std::cout << sqlInsert << std::endl;
    
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    运行结果如下图所示:
    C++ R标记符

  • 相关阅读:
    leetcode做题笔记135. 分发糖果
    2019 java面试题基础
    互联网发展从红利到实力,行业内卷“升级”
    柒微自动发卡系统源码
    使用 Velocity 模板引擎的 Spring Boot 应用
    Java 8 新特性 Ⅲ
    FITC荧光素标记氨基半乳糖/氨基葡萄糖定制合成
    电脑一键重装系统后如何打开事件查看器
    MySQL--MHA高可用方案
    抽象类和接口概念和区别
  • 原文地址:https://blog.csdn.net/ccf19881030/article/details/132768650