【发布时间】:2009-06-03 17:02:13
【问题描述】:
这种话怎么说来着?
static const string message = "This is a message.\n
It continues in the next line"
问题是,下一行没有被识别为字符串的一部分..
如何解决这个问题?或者是创建字符串数组然后初始化数组以保存每一行的唯一解决方案?
【问题讨论】:
这种话怎么说来着?
static const string message = "This is a message.\n
It continues in the next line"
问题是,下一行没有被识别为字符串的一部分..
如何解决这个问题?或者是创建字符串数组然后初始化数组以保存每一行的唯一解决方案?
【问题讨论】:
用自己的引号将每一行括起来:
static const string message = "This is a message.\n"
"It continues in the next line";
编译器会将它们组合成一个字符串。
【讨论】:
cpp 实现会合并字符串...我猜它们不符合规范。
您可以使用尾随斜杠或引用每一行,因此
"This is a message.\n \
It continues in the next line"
或
"This is a message."
"It continues in the next line"
【讨论】:
在 C++ 中和在 C 中一样,由空格分隔的字符串是隐式连接的,所以
"foo" "bar"
相当于:
"foobar"
所以你想要:
static const string message = "This is a message.\n"
"It continues in the next line";
【讨论】: