简体   繁体   English

C++ 无法识别的转义序列

[英]C++ Unrecognized escape sequence

I want to create a string that contains all possible special chars.我想创建一个包含所有可能的特殊字符的字符串。

However, the compiler gives me the warning "Unrecognized escape sequence" in this line:但是,编译器在这一行中给了我警告“无法识别的转义序列”

wstring s=L".,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£­€™℗@©®~µ´`'" + wstring(1,34);

Can anybody please tell me which one of the characters I may not add to this string the way I did?任何人都可以告诉我我不能像我那样将哪个字符添加到这个字符串中?

您必须将\\转义为\\\\ ,否则\\¯将被解释为(无效的)转义序列:

wstring s=L".,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£­€™℗@©®~µ´`'" + wstring(1,34);

Escape sequence is a character string that has a different meaning than the literal characters themselves.转义序列是一个字符串,其含义与文字字符本身不同。 In C and C++ the sequence begins with \\ so if your string contains a double quote or backslash it must be escaped properly using \\" and \\\\在 C 和 C++ 中,序列以\\开头,因此如果您的字符串包含双引号或反斜杠,则必须使用\\"\\\\正确转义

In long copy-pasted strings it may be difficult to spot those characters and it's also less maintainable in the future so it's recommended to use raw string literals with the prefix R so you don't need any escapes at all在复制粘贴的长字符串中,可能很难发现这些字符,而且将来的维护性也较差,因此建议使用带有前缀R原始字符串文字,这样您根本不需要任何转义

wstring s = LR"(.,;*:-_⁊#‘⁂‡…–«»¤¤¡=„+-¶~´:№\¯/?‽!¡-¢–”¥—†¿»¤{}«[-]()·^°$§%&«|⸗<´>²³£­€™℗@©®~µ´`')"
          + wstring(1,34);

A special delimiter string may be inserted outside the parentheses like this LR"delim(special string)delim" in case your raw string contains a )" sequence如果您的原始字符串包含)"序列,可以在括号外插入一个特殊的分隔符字符串,例如LR"delim(special string)delim"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM