我试图制作一个简单的计算器,我想在你第一次运行程序时在指令中显示引号。
答案 0 :(得分:4)
另一个解决方案是使用原始字符串:
#include <string>
#include <iostream>
int main()
{
std::cout << R"_(A raw string with " inside (and \ as well))_" << std::endl;
return 0;
}
输出:
带有“inside(和\)
的原始字符串
根据标准2.14.5 [lex.string]:
string-literal: encoding-prefixopt "s-char-sequenceopt" encoding-prefixopt R raw-string encoding-prefix: u8 u U L s-char-sequence: s-char s-char-sequence s-char s-char: any member of the source character set except the double-quote ", backslash \, or new-line character escape-sequence universal-character-name raw-string: " d-char-sequenceopt ( r-char-sequenceopt) d-char-sequenceopt " r-char-sequence: r-char r-char-sequence r-char r-char: any member of the source character set, except a right parenthesis ) followed by the initial d-char-sequence (which may be empty) followed by a double quote ". d-char-sequence: d-char d-char-sequence d-char d-char: any member of the basic source character set except: space, the left parenthesis (, the right parenthesis ), the backslash \, and the control characters representing horizontal tab, vertical tab, form feed, and newline.
字符串文字是由双引号括起来的字符序列(在2.14.3中定义),可选地以
R
,u8
,u8R
为前缀,u
,uR
,U
,UR
,L
或LR
,如"..."
,R"(...)"
,u8"..."
,u8R"**(...)**"
,u"..."
,uR"*˜(...)*˜"
,U"..."
, <{1}},UR"zzz(...)zzz"
或L"..."
。前缀中包含
LR"(...)"
的字符串文字是原始字符串文字。R
用作分隔符。终止 原始字符串的d-char-sequence
是相同的字符序列 作为最初的d-char-sequence
。d-char-sequence
应由d-char-sequence
组成 最多16个字符。- [注意:原始字符串中允许使用字符
(
和)
。因此,R"delimiter((a|b))delimiter"
相当于"(a|b)"
。 -结束 注意][注意:原始字符串文字中的源文件换行会在生成的执行
string-literal
中生成换行符。假设没有 在下面的例子中,行的开头是空格 断言将成功:const char *p = R"(a\ b c)"; assert(std::strcmp(p, "a\\\nb\nc") == 0);
- 结束说明]
- 醇>
[示例:原始字符串
R"a( )\ a" )a"
相当于
"\n)\\\na\"\n"
。原始字符串R"(??)"
相当于
"\?\?"
。原始字符串R"#( )??=" )#"
相当于
"\n)\?\?=\"\n"
。 - 例子]
答案 1 :(得分:3)
使用\"
。也称为escape sequences。