在字符串中使用 " 字符

问题描述

你好,我想在这样的字符串变量中使用字符“:

std::string hello = """;

有没有可能做这样的事情还是我在胡说八道? 提前致谢!

解决方法

逃避吧:

std::string hello = "\"";
,

您有多种方法,包括:

  • 转义:"\""
  • 原始字符串:R"(")"(C++11 起)
,

您可以使用转义字符,例如

    std::string hello( "\"" );

    std::string hello = "\"";

或使用接受字符文字的构造函数,如

std::string hello( 1,'"' );

或者你甚至可以使用像

这样的原始字符串文字
std::string hello( R"(")" );

std::string hello = R"(")";