C ++字符串文字转义字符 [英] C++ string literals escape character
问题描述
字符串文字中的转义字符 \
有什么规则?是否有转义的所有字符的列表?特别是,当我在gedit的字符串文字中使用 \
,并且跟随它由任何三个数字,它们不同地给它们上色。我试图创建一个 std :: string
从字符构造的字符 0
后跟空字符 \0
),后跟字符 0
。但是,语法高亮提醒我,也许这将创建一个像 0
后跟空字符( \00
,aka \0
),也就是说,只有两个字符。
只是这一个问题,是这个最好的方法:
std :: string(0\0 0,3)//字符串连接
字符串字面量一般?例如
控制字符:
(十六进制代码假定与ASCII兼容的字符编码。)
-
\a
=\x07
= alert(bell) -
\b
=\x08
= backspace -
\t
=\x09
=水平标签 -
\\\
=
\x0A
=换行符(或换行符) -
\v
=\x0B
=垂直标签 -
\f
=\x0C
=表单Feed -
\r
=\x0D
=回车 -
\e
=\x1B
= escape(非标准GCC扩展)
标点符号:
-
\
=引号''
) -
\'
= apostrophe'
) -
\?
=问号以避免三字母) -
-
\\
=反斜杠
数字字符引用:
-
\
+最多3个八进制数字
-
\x
+任意多个十六进制数字 -
\u
+ 4个十六进制数字(Unicode BMP,C ++ 11中的新增) -
\U
+ 8个十六进制数字(Unicode astral planes,new in C ++ 11)
\0
= \00
= \000
=空字符的八进制表达式
如果你想在 \0
然后是,我建议字符串连接。注意,字面之间的空格是可选的,所以你可以写\00
。
What are the rules for the escape character \
in string literals? Is there a list of all the characters that are escaped? In particular, when I use \
in a string literal in gedit, and follow it by any three numbers, it colors them differently. I was trying to create a std::string
constructed from a literal with the character 0
followed by the null character (\0
), followed by the character 0
. However, the syntax highlighting alerted me that maybe this would create something like the character 0
followed by the null character (\00
, aka \0
), which is to say, only two characters.
For the solution to just this one problem, is this the best way to do it:
std::string ("0\0" "0", 3) // String concatenation
And is there some reference for what the escape character does in string literals in general? What is '\a', for instance?
Control characters:
(Hex codes assume an ASCII-compatible character encoding.)
\a
=\x07
= alert (bell)\b
=\x08
= backspace\t
=\x09
= horizonal tab\n
=\x0A
= newline (or line feed)\v
=\x0B
= vertical tab\f
=\x0C
= form feed\r
=\x0D
= carriage return\e
=\x1B
= escape (non-standard GCC extension)
Punctuation characters:
\"
= quotation mark (backslash not required for'"'
)\'
= apostrophe (backslash not required for"'"
)\?
= question mark (used to avoid trigraphs)\\
= backslash
Numeric character references:
\
+ up to 3 octal digits\x
+ any number of hex digits\u
+ 4 hex digits (Unicode BMP, new in C++11)\U
+ 8 hex digits (Unicode astral planes, new in C++11)
\0
= \00
= \000
= octal ecape for null character
If you do want an actual digit character after a \0
, then yes, I recommend string concatenation. Note that the whitespace between the parts of the literal is optional, so you can write "\0""0"
.
这篇关于C ++字符串文字转义字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!