Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
字符串文字C++;? 我需要在C++中执行下面的C代码(如果可能的话)。我必须构造一个长字符串,里面有很多奇怪的引号和其他东西 const String _literal = @"I can use "quotes" inside here"; C++中没有原始字符串文字。您需要转义字符串文本 std::string str = "I can use \"quotes\" inside here";_C++_String - Fatal编程技术网

字符串文字C++;? 我需要在C++中执行下面的C代码(如果可能的话)。我必须构造一个长字符串,里面有很多奇怪的引号和其他东西 const String _literal = @"I can use "quotes" inside here"; C++中没有原始字符串文字。您需要转义字符串文本 std::string str = "I can use \"quotes\" inside here";

字符串文字C++;? 我需要在C++中执行下面的C代码(如果可能的话)。我必须构造一个长字符串,里面有很多奇怪的引号和其他东西 const String _literal = @"I can use "quotes" inside here"; C++中没有原始字符串文字。您需要转义字符串文本 std::string str = "I can use \"quotes\" inside here";,c++,string,C++,String,C++0x在可用时提供原始字符串文字: R"C:\mypath" P> >不应该用任何一个前导下划线来命名任何东西,因为C++中保留了这样的标识符。< P> C++中没有这样的机制。你必须用老式的方法,用逃犯 不过,您可能可以使用脚本语言使转义部分更容易一些。例如,Ruby中的%Q运算符在irb中使用时将返回一个正确转义的双引号字符串: irb(main):003:0> %Q{hello "world" and stuff with tabs} => "hello

C++0x在可用时提供原始字符串文字:

R"C:\mypath"


<> P> >不应该用任何一个前导下划线来命名任何东西,因为C++中保留了这样的标识符。

< P> C++中没有这样的机制。你必须用老式的方法,用逃犯

不过,您可能可以使用脚本语言使转义部分更容易一些。例如,Ruby中的
%Q
运算符在
irb
中使用时将返回一个正确转义的双引号字符串:

irb(main):003:0> %Q{hello "world" and stuff     with    tabs}
=> "hello \"world\" and stuff\twith\ttabs"

<> C++中没有C的“@”等价物。实现此目的的唯一方法是正确地转义字符串:

const char *_literal = "I can use \"quotes\" inside here";

这在C++03(当前标准)中不可用

这是C++0x标准草案的一部分,但目前还没有现成的版本

现在,您只需明确引用它:

const std::string _literal = "I have to escape my quotes in \"C++03\"";
一旦C++0x成为现实,您将能够编写:

const std::string _literal = R"(but "C++0x" has raw string literals)";
当您需要
)“
文字:

const std::string _literal = R"DELIM(more "(raw string)" fun)DELIM";

@paxdiablo-不客气。另一方面,我前面的示例不正确。原始字符串使用括号而不是方括号作为分隔符。此外,原始字符串比我的示例显示的更灵活(您可以添加分隔符以便放置
在实际字符串中。为什么不从文件中加载这么长的字符串呢?以下划线开头然后后跟大写字母的名称是为实现保留的,以供任何使用,因此您根本不能使用它们。一般用下划线开头的名字只保留为全局命名空间中的名称。@布瑞恩-原始字符串使用括号<代码> r(c:\MyPATH)“< /Cord>”,你是否真的建议在C++程序中嵌入Ruby解释器只是为了逃离字符串?@ JALF:绝对不是!我建议将字符串粘贴到Ruby中一次,然后将结果粘贴回他的源文件中。不过,我可以看出我的答案在哪里可以用那种可笑的方式来解释。