intmain() { // Character literals auto c0 = 'A'; // char auto c1 = u8'A'; // char auto c2 = L'A'; // wchar_t auto c3 = u'A'; // char16_t auto c4 = U'A'; // char32_t
// Multicharacter literals auto m0 = 'abcd'; // int, value 0x61626364
// String literals auto s0 = "hello"; // const char* auto s1 = u8"hello"; // const char* before C++20, encoded as UTF-8, // const char8_t* in C++20 auto s2 = L"hello"; // const wchar_t* auto s3 = u"hello"; // const char16_t*, encoded as UTF-16 auto s4 = U"hello"; // const char32_t*, encoded as UTF-32 }
2. 原始字符串
1 2 3 4 5 6 7 8 9 10
intmain() { // Raw string literals containing unescaped \ and " auto R0 = R"("Hello \ world")"; // const char* auto R1 = u8R"("Hello \ world")"; // const char* before C++20, encoded as UTF-8, // const char8_t* in C++20 auto R2 = LR"("Hello \ world")"; // const wchar_t* auto R3 = uR"("Hello \ world")"; // const char16_t*, encoded as UTF-16 auto R4 = UR"("Hello \ world")"; // const char32_t*, encoded as UTF-32 }
3. 字符串后缀
字符串后缀就是在字符串后面加s,如:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
auto S0 = "hello"s; // std::string auto S1 = u8"hello"s; // std::string before C++20, std::u8string in C++20 auto S2 = L"hello"s; // std::wstring auto S3 = u"hello"s; // std::u16string auto S4 = U"hello"s; // std::u32string std::string_view sv = "abc\0\0def"sv;
// 和原始字符串一起使用 auto S5 = R"("Hello \ world")"s; // std::string from a raw const char* auto S6 = u8R"("Hello \ world")"s; // std::string from a raw const char* before C++20, encoded as UTF-8, // std::u8string in C++20 auto S7 = LR"("Hello \ world")"s; // std::wstring from a raw const wchar_t* auto S8 = uR"("Hello \ world")"s; // std::u16string from a raw const char16_t*, encoded as UTF-16 auto S9 = UR"("Hello \ world")"s; // std::u32string from a raw const char32_t*, encoded as UTF-32