有没有办法把报价放在一个cout?

时间:2016-01-21 07:05:21

标签: c++

我试图制作一个简单的计算器,我想在你第一次运行程序时在指令中显示引号。

2 个答案:

答案 0 :(得分:4)

另一个解决方案是使用原始字符串:

#include <string>
#include <iostream>

int main()
{
    std::cout << R"_(A raw string with " inside (and \ as well))_" << std::endl;
    return 0;
}

Live example

输出:

  

带有“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.
  
      
  1. 字符串文字是由双引号括起来的字符序列(在2.14.3中定义),可选地以Ru8u8R为前缀,   uuRUURLLR,如"..."R"(...)" ,   u8"..."u8R"**(...)**"u"..."uR"*˜(...)*˜"U"...",   <{1}},UR"zzz(...)zzz"L"..."

  2.   
  3. 前缀中包含LR"(...)"的字符串文字是原始字符串文字R用作分隔符。终止   原始字符串的d-char-sequence是相同的字符序列   作为最初的d-char-sequenced-char-sequence应由d-char-sequence组成   最多16个字符。

  4.   
  5. [注意:原始字符串中允许使用字符()。因此,R"delimiter((a|b))delimiter"相当于"(a|b)"。 -结束   注意]
  6.   
  7. [注意:原始字符串文字中的源文件换行会在生成的执行string-literal中生成换行符。假设没有   在下面的例子中,行的开头是空格   断言将成功:

    const char *p = R"(a\
    b
    c)";
    assert(std::strcmp(p, "a\\\nb\nc") == 0);
    
         

    - 结束说明]

  8.   
  9. [示例:原始字符串

    R"a(
    )\
    a"
    )a"
    
         

    相当于"\n)\\\na\"\n"。原始字符串

    R"(??)"
    
         

    相当于"\?\?"。原始字符串

    R"#(
    )??="
    )#"
    
         

    相当于"\n)\?\?=\"\n"。 - 例子]

  10.   

答案 1 :(得分:3)

使用\"。也称为escape sequences