C++ 多行字符串原始文字

标签 c++

我们可以像这样用多行定义一个字符串:

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/

相关文章:

c++ - isalnum 的范围解析运算符

c++ - Maya c++ API,移动对象?

c++ - 如何在QT中实时更新GL()

c++ - 采样率偏差和声音播放位置

c++ - 创建一个库?

c++ - 如何在gdb/lldb调试中打印模板成员函数返回的值

c++ - 你真的可以用 WaitFor...Object(s) 来等待条件变量吗?

python - 如何防止 Python 2.7 的 <Python.h> 在 SWIG 生成的 Python 包装器中包含 <inttypes.h>?

C++:在常量指针指向的对象中调用非常量函数(错误 C2662)

c++ - 将成员函数作为模板参数传递