• 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标记符

  • 相关阅读:
    使用Java接入小程序订阅消息!
    八月七日 SpringBoot自动装配
    基于MYSQL的新闻发布系统数据库设计项目实战
    路由器二次开发一步一步把工业路由器变成一个高端的可指定出网、节点和链路的路由器,包含详细过程及快捷脚本(三)
    修改http_charfinder.py使能在python311环境中运行
    百度文心一言API调用,千帆大模型获取API Key和API Secret图解
    UNI-APP_获取手机品牌
    学好Python-新手小白如何做?
    HTML5期末考核大作业——学生网页设计作业源码HTML+CSS+JavaScript 中华美德6页面带音乐文化
    DirectX12初始化四——初始化Direct3D(总代码)
  • 原文地址:https://blog.csdn.net/ccf19881030/article/details/132768650