有没有办法有多行纯文本,常量字面量在c++, à la Perl?也许是使用#include文件的一些解析技巧?我想不出一个,但那太好了。我知道是c++ 0x。
当前回答
为了在@unwind的回答中稍微说明一下@emsr的注释,如果一个人不够幸运,没有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.";
很明显,这是真的,但是@emsr的简短评论在我第一次读到这篇文章时并没有跳入我的脑海,所以我必须自己去发现。希望我为其他人节省了几分钟时间。
其他回答
你可以这样做:
const char *text = "This is my string it is "
"very long";
你还可以这样做:
const char *longString = R""""(
This is
a very
long
string
)"""";
#定义MULTILINE(…)#__VA_ARGS__ 消耗括号之间的所有东西。 将任意数量的连续空白字符替换为单个空格。
// 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";
为了在@unwind的回答中稍微说明一下@emsr的注释,如果一个人不够幸运,没有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.";
很明显,这是真的,但是@emsr的简短评论在我第一次读到这篇文章时并没有跳入我的脑海,所以我必须自己去发现。希望我为其他人节省了几分钟时间。