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

运行结果如下图所示:
C++中使用R“()“标记符书写多行字符串_第1张图片

string s_JavaScript = @"
            ";

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

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

const char* name005 = "line1\n"
		"line2\n"
		"line3\n";

不过庆幸的是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;
}

运行结果如下图所示:
C++中使用R“()“标记符书写多行字符串_第2张图片

你可能感兴趣的:(c++,开发语言)