rlb*_*ond 383 c++ string-literals
有什么方法可以在C++中使用多行纯文本,常量文字,还有Perl?也许有一些#include文件的解析技巧?我想不出一个,但男孩,那会很好.我知道它将在C++ 0x中.
unw*_*ind 550
嗯...排序.最简单的方法是使用编译器连接相邻字符串文字的事实:
const char *text =
"This text is pretty long, but will be "
"concatenated into just a single string. "
"The disadvantage is that you have to quote "
"each part, and newlines must be literal as "
"usual.";
Run Code Online (Sandbox Code Playgroud)
缩进无关紧要,因为它不在引号内.
您也可以这样做,只要您注意逃避嵌入式换行符.不这样做,就像我的第一个答案那样,将无法编译:
const char *text2 = "Here, on the other hand, I've gone crazy \ and really let the literal span several lines, \ without bothering with quoting each line's \ content. This works, but you can't indent.";
再次注意每行末尾的反斜杠,它们必须紧接在行结束之前,它们正在转换源中的换行符,以便所有内容都像新行一样.您没有在反斜杠的位置获取字符串中的换行符.使用这种形式,你显然不能缩进文本,因为缩进将成为字符串的一部分,用随机空格拼写它.
ems*_*msr 359
在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";
Run Code Online (Sandbox Code Playgroud)
保留字符串中的所有空格和缩进以及换行符.
这些也可以是utf-8 | 16 | 32或wchar_t(使用通常的前缀).
我应该指出,这里实际上并不需要转义序列V0G0N.它的存在将允许"在弦内".换句话说,我可以放
"(by Prostetnic Vogon Jeltz; see p. 56/57)"
Run Code Online (Sandbox Code Playgroud)
(注意额外的引号)和上面的字符串仍然是正确的.否则我也可以使用
const char * vogon_poem = R"( ... )";
Run Code Online (Sandbox Code Playgroud)
引号内的parens仍然需要.
bcm*_*inc 24
输入多行字符串的一种可能方便的方法是使用宏.仅当引号和括号平衡且不包含"顶级"逗号时才有效:
#define MULTI_LINE_STRING(a) #a
const char *text = MULTI_LINE_STRING(
Using this trick(,) you don't need to use quotes.
Though newlines and multiple white spaces
will be replaced by a single whitespace.
);
printf("[[%s]]\n",text);
Run Code Online (Sandbox Code Playgroud)
用gcc 4.6或g ++ 4.6编译,产生: [[Using this trick(,) you don't need to use quotes. Though newlines and multiple white spaces will be replaced by a single whitespace.]]
请注意,,除非它包含在括号或引号中,否则不能在字符串中.单引号是可能的,但会创建编译器警告.
编辑:如评论中所述,#define MULTI_LINE_STRING(...) #__VA_ARGS__允许使用,.
Eri*_*ric 13
你可以这样做:
const char *text = "This is my string it is "
"very long";
Run Code Online (Sandbox Code Playgroud)
由于一盎司的经验值得大量理论,我尝试了一个小测试程序MULTILINE:
#define MULTILINE(...) #__VA_ARGS__
const char *mstr[] =
{
MULTILINE(1, 2, 3), // "1, 2, 3"
MULTILINE(1,2,3), // "1,2,3"
MULTILINE(1 , 2 , 3), // "1 , 2 , 3"
MULTILINE( 1 , 2 , 3 ), // "1 , 2 , 3"
MULTILINE((1, 2, 3)), // "(1, 2, 3)"
MULTILINE(1
2
3), // "1 2 3"
MULTILINE(1\n2\n3\n), // "1\n2\n3\n"
MULTILINE(1\n
2\n
3\n), // "1\n 2\n 3\n"
MULTILINE(1, "2" \3) // "1, \"2\" \3"
};
Run Code Online (Sandbox Code Playgroud)
编译此片段cpp -P -std=c++11 filename以重现.
背后的诀窍#__VA_ARGS__是__VA_ARGS__不处理逗号分隔符.所以你可以把它传递给stringizing运算符.修剪前导和尾随空格,然后将单词之间的空格(包括换行符)压缩为单个空格.括号需要平衡.我认为这些缺点解释了为什么C++ 11的设计者#__VA_ARGS__看到了对原始字符串文字的需求.
您也可以这样做:
const char *longString = R""""(
This is
a very
long
string
)"""";
Run Code Online (Sandbox Code Playgroud)
只是为了阐明@smsr在@ unwind的答案中的评论,如果一个人不幸有一个C++ 11编译器(比如GCC 4.2.1),并且想要在字符串中嵌入换行符(char*或类字符串),可以写这样的东西:
const char *text =
"This text is pretty long, but will be\n"
"concatenated into just a single string.\n"
"The disadvantage is that you have to quote\n"
"each part, and newlines must be literal as\n"
"usual.";
Run Code Online (Sandbox Code Playgroud)
非常明显,是的,但@ emsr的简短评论并没有在我第一次读到这篇文章时突然出现,所以我必须自己发现这个.希望我能在几分钟内拯救别人.
小智 5
// 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";
Run Code Online (Sandbox Code Playgroud)