有没有办法有多行纯文本,常量字面量在c++, à la Perl?也许是使用#include文件的一些解析技巧?我想不出一个,但那太好了。我知道是c++ 0x。
当前回答
在c++ 11中,你有原始字符串字面值。有点像这里-文本在shell和脚本语言,如Python、Perl和Ruby。
const char * vogon_poem = R"V0G0N(
O freddled gruntbuggly thy micturations are to me
As plured gabbleblochits on a lurgid bee.
Groop, I implore thee my foonting turlingdromes.
And hooptiously drangle me with crinkly bindlewurdles,
Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.
(by Prostetnic Vogon Jeltz; see p. 56/57)
)V0G0N";
字符串中的所有空格、缩进和换行符都被保留。
这些也可以是utf-8|16|32或wchar_t(带有常用的前缀)。
我应该指出,这里实际上不需要转义序列V0G0N。它的存在将允许将)“放入字符串中。换句话说,我可以把
"(by Prostetnic Vogon Jeltz; see p. 56/57)"
(注意额外的引号)和上面的字符串仍然是正确的。否则我还不如用
const char * vogon_poem = R"( ... )";
引号内的括号仍然是需要的。
其他回答
// C++11.
std::string index_html=R"html(
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>VIPSDK MONITOR</title>
<meta http-equiv="refresh" content="10">
</head>
<style type="text/css">
</style>
</html>
)html";
你可以这样做:
const char *text = "This is my string it is "
"very long";
在c++ 11中,你有原始字符串字面值。有点像这里-文本在shell和脚本语言,如Python、Perl和Ruby。
const char * vogon_poem = R"V0G0N(
O freddled gruntbuggly thy micturations are to me
As plured gabbleblochits on a lurgid bee.
Groop, I implore thee my foonting turlingdromes.
And hooptiously drangle me with crinkly bindlewurdles,
Or I will rend thee in the gobberwarts with my blurlecruncheon, see if I don't.
(by Prostetnic Vogon Jeltz; see p. 56/57)
)V0G0N";
字符串中的所有空格、缩进和换行符都被保留。
这些也可以是utf-8|16|32或wchar_t(带有常用的前缀)。
我应该指出,这里实际上不需要转义序列V0G0N。它的存在将允许将)“放入字符串中。换句话说,我可以把
"(by Prostetnic Vogon Jeltz; see p. 56/57)"
(注意额外的引号)和上面的字符串仍然是正确的。否则我还不如用
const char * vogon_poem = R"( ... )";
引号内的括号仍然是需要的。
#定义MULTILINE(…)#__VA_ARGS__ 消耗括号之间的所有东西。 将任意数量的连续空白字符替换为单个空格。
你还可以这样做:
const char *longString = R""""(
This is
a very
long
string
)"""";