是否可以在原始字符串文字中插入转义序列?
Is it possible to insert escape sequence in a raw string literal?
如果我希望它在我的文件中消耗更少的行怎么办?
auto rsl = R"rsl(==== WELCOME ===
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque sed scelerisque purus. Nulla facilisi.
Suspendisse laoreet mattis urna sit amet suscipit.
===== Thanks for your attention ====)rsl";
我可以在这个原始字符串文字上添加转义字符,例如:
auto rsl = R"rsl(==== WELCOME ===\n
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque sed scelerisque purus. Nulla facilisi.
Suspendisse laoreet mattis urna sit amet suscipit.\n
===== Thanks for your attention ====)rsl";
或者更好的情况,如果我不得不懒惰地将一些编码的 utf-8 文字复制粘贴到原始字符串文字中,如下所示:
auto u8 = u8"UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422 \n";
auto u8Rs = u8R"u8R(UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422
some additional stuff I want to add
to the previous string literal
because requirements slightly changed
or something)u8R";
因为在这些情况下输出会大不相同
虽然通常最好坚持使用一种文字或另一种文字,但您可以在串联中混合使用原始文字和非原始文字:
auto u8 = u8"UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422 \n";
auto u8Rs = u8R"u8R(UTF-8 encoded string literal: )u8R" u8"\u041F\u0420\u0418\u0412\u0415\u0422" u8R"u8R(
some additional stuff I want to add
to the previous string literal
because requirements slightly changed
or something)u8R";
是的,很丑。我会认真考虑它是否比单个非原始文字的替代方案更难看。在保存垂直编辑器 space 的情况下,我会说不要。使用原始文字,让人们假设他们看到的就是他们得到的,而不是隐藏额外的换行符。
如果我希望它在我的文件中消耗更少的行怎么办?
auto rsl = R"rsl(==== WELCOME ===
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque sed scelerisque purus. Nulla facilisi.
Suspendisse laoreet mattis urna sit amet suscipit.
===== Thanks for your attention ====)rsl";
我可以在这个原始字符串文字上添加转义字符,例如:
auto rsl = R"rsl(==== WELCOME ===\n
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Pellentesque sed scelerisque purus. Nulla facilisi.
Suspendisse laoreet mattis urna sit amet suscipit.\n
===== Thanks for your attention ====)rsl";
或者更好的情况,如果我不得不懒惰地将一些编码的 utf-8 文字复制粘贴到原始字符串文字中,如下所示:
auto u8 = u8"UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422 \n";
auto u8Rs = u8R"u8R(UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422
some additional stuff I want to add
to the previous string literal
because requirements slightly changed
or something)u8R";
因为在这些情况下输出会大不相同
虽然通常最好坚持使用一种文字或另一种文字,但您可以在串联中混合使用原始文字和非原始文字:
auto u8 = u8"UTF-8 encoded string literal: \u041F\u0420\u0418\u0412\u0415\u0422 \n";
auto u8Rs = u8R"u8R(UTF-8 encoded string literal: )u8R" u8"\u041F\u0420\u0418\u0412\u0415\u0422" u8R"u8R(
some additional stuff I want to add
to the previous string literal
because requirements slightly changed
or something)u8R";
是的,很丑。我会认真考虑它是否比单个非原始文字的替代方案更难看。在保存垂直编辑器 space 的情况下,我会说不要。使用原始文字,让人们假设他们看到的就是他们得到的,而不是隐藏额外的换行符。