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

2023-09-09 14:37:54 浏览数 (2)

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

代码语言:javascript复制
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);

运行结果如下图所示:

代码语言:javascript复制
string s_JavaScript = @"
            <script type=""type/javascript"">
            function doSomething()
            {
            }
            </script>";

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

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

代码语言:javascript复制
const char* name005 = "line1n"
		"line2n"
		"line3n";

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

代码语言:javascript复制
#include <iostream>
#include <string>

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;
}

运行结果如下图所示:

0 人点赞