我们可以像这样用多行定义一个字符串:
const char* text1 = "part 1"
"part 2"
"part 3"
"part 4";
const char* text2 = "part 1\
part 2\
part 3\
part 4";
用原始文字怎么样,我都试过了,没有人工作
std::string text1 = R"part 1"+
R"part 2"+
R"part 3"+
R"part 4";
std::string text2 = R"part 1"
R"part 2"
R"part 3"
R"part 4";
std::string text3 = R"part 1\
part 2\
part 3\
part 4";
std::string text4 = R"part 1
part 2
part 3
part 4";
最佳答案
请注意,原始字符串文字由 R"(
和 )"
分隔(或者您可以通过在引号和括号之间添加字符来添加分隔符,如果您需要额外的“独特性”)。
#include <iostream>
#include <ostream>
#include <string>
int main ()
{
// raw-string literal example with the literal made up of separate, concatenated literals
std::string s = R"(abc)"
R"( followed by not a newline: \n)"
" which is then followed by a non-raw literal that's concatenated \n with"
" an embedded non-raw newline";
std::cout << s << std::endl;
return 0;
}
关于C++ 多行字符串原始文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20508534/