diff --git a/print_page/index.html b/print_page/index.html index 46bc430..78163ad 100644 --- a/print_page/index.html +++ b/print_page/index.html @@ -2497,7 +2497,7 @@

u8 字符串常量的作用

“你好” -运行字符集 (ANSI) +运行字符集 (默认是开发者电脑的“区域设置”,而不是客户的) const char [] @@ -2522,6 +2522,9 @@

u8 字符串常量的作用

+
+

只不过是开发者和客户往往处于同一个地区,所以 "你好" 看起来好像可以直接输入到 std::cout 中一样。实际上他只是开发者电脑的 ANSI,而不是客户电脑的 ANSI,如果直接拿来打印,会导致以 "" 常量形式写死的字符串会在客户电脑上出现乱码。除非这个字符串只包含 ASCII,因为所有 ANSI 都兼容 ASCII,才恰好避免了乱码。

+

选择你的阵营!

ANSI 阵营

diff --git a/search/search_index.json b/search/search_index.json index a7ac9b3..ae9c1f9 100644 --- a/search/search_index.json +++ b/search/search_index.json @@ -1 +1 @@ -{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"\u5c0f\u5f6d\u8001\u5e08\u73b0\u4ee3 C++ \u5927\u5178 \u5c0f\u5f6d\u5927\u5178\u662f\u4e00\u672c\u5173\u4e8e\u73b0\u4ee3 C++ \u7f16\u7a0b\u7684\u6743\u5a01\u6307\u5357\uff0c\u5b83\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u77e5\u8bc6\u5230\u9ad8\u7ea7\u6280\u5de7\u7684\u5185\u5bb9\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u9605\u8bfb\u3002\u672c\u4e66\u7531\u5c0f\u5f6d\u8001\u5e08\u4eb2\u81ea\u7f16\u5199\uff0c\u901a\u8fc7\u7b80\u5355\u6613\u61c2\u7684\u8bed\u8a00\u548c\u4e30\u5bcc\u7684\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 C++ \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u5b66\u4f1a\u5982\u4f55\u8fd0\u7528\u5b83\u4eec\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002 \u6562\u627f\u8bfa\uff1a\u571f\u6728\u8001\u54e5\u4e5f\u80fd\u770b\u61c2\uff01 \u524d\u8a00 \u63a8\u8350\u7528\u624b\u673a\u6216\u5e73\u677f \u7ad6\u5c4f \u89c2\u770b\uff0c\u53ef\u4ee5\u5728\u5e8a\u6216\u6c99\u53d1\u4e0a\u8eba\u7740\u3002 \u7528\u7535\u8111\u770b\u7684\u8bdd\uff0c\u53ef\u4ee5\u6309 WIN + \u2190 \uff0c\u628a\u672c\u4e66\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u653e\u5728\u5c4f\u5e55\u5de6\u4fa7\uff0c\u53f3\u4fa7\u662f\u4f60\u7684 IDE\u3002\u4e00\u8fb9\u770b\u4e00\u8fb9\u81ea\u5df1\u52a8\u624b\u505a\u5b9e\u9a8c\u3002 \u8bf7\u5750\u548c\u653e\u5bbd\u3002 \u53ef\u4ee5\u6309\u987a\u5e8f\u9605\u8bfb\uff0c\u4e5f\u53ef\u4ee5\u5728\u672c\u9875\u9762\u4e0a\u65b9\u5bfc\u822a\u680f\u7684\u201c\u7ae0\u8282\u5217\u8868\u201d\u4e2d\uff0c\u9009\u62e9\u611f\u5174\u8da3\u7684\u7ae0\u8282\u9605\u8bfb\u3002 \u672c\u4e66\u5b8c\u5168\u5f00\u6e90\u548c\u514d\u8d39\uff0cGitHub \u4ed3\u5e93\uff1a https://github.com/parallel101/cppguidebook \u5982\u679c\u4f60\u662f\u5728\u4ed8\u8d39\u7fa4\u4e2d\u201c\u4e70\u201d\u5230\u672c\u4e66\uff0c\u6216\u8005\u6253\u7740\u5c0f\u5f6d\u8001\u5e08\u540d\u53f7\u5356\u8bfe\uff0c\u8bf4\u660e\u4f60\u53ef\u80fd\u662f\u79c1\u6709\u5236\u7684\u53d7\u5bb3\u8005\u3002\u56e0\u4e3a\u5c0f\u5f6d\u8001\u5e08\u4ece\u6765\u6ca1\u6709\u4ed8\u8d39\u624d\u80fd\u770b\u7684\u8bfe\u7a0b\uff0c\u6240\u6709\u5c0f\u5f6d\u8001\u5e08\u8bfe\u7a0b\u90fd\u5bf9\u5168\u7403\u4e92\u8054\u7f51\u5f00\u653e\u3002 \u5982\u9700\u79bb\u7ebf\u67e5\u770b\uff0c\u53ef\u4ee5\u524d\u5f80 GitHub Release \u9875\u9762 \u4e0b\u8f7d PDF \u6587\u4ef6\u3002 \u5982\u679c\u4f60\u5728\u9605\u8bfb\u8fc7\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u53ef\u4ee5\u5728 GitHub Issues \u4e2d\u63d0\u51fa\uff0c\u5c0f\u5f6d\u8001\u5e08\u4f1a\u5c3d\u529b\u89e3\u7b54\u3002 \u4e5f\u53ef\u4ee5\u5728 B \u7ad9 \u53d1\u79c1\u4fe1\u7ed9\u5c0f\u5f6d\u8001\u5e08\u54e6\u3002 \u672c\u4e66\u8fd8\u5728\u6301\u7eed\u66f4\u65b0\u4e2d\u2026\u2026\u8981\u8ffd\u756a\u7684\u8bdd\uff0c\u53ef\u4ee5\u5728 GitHub \u70b9\u4e00\u4e0b\u53f3\u4e0a\u89d2\u7684 \u201cWatch\u201d \u6309\u94ae\uff0c\u6bcf\u5f53\u5c0f\u5f6d\u8001\u5e08\u63d0\u4ea4\u65b0 commit\uff0cGitHub \u4f1a\u5411\u4f60\u53d1\u9001\u4e00\u5cf0\u7535\u5b50\u90ae\u4ef6\uff0c\u63d0\u9192\u4f60\u5c0f\u5f6d\u8001\u5e08\u66f4\u65b0\u4e86\u3002 \u66f4\u65b0\u65f6\u95f4\uff1ahttps://github.com/parallel101/cppguidebook \u683c\u5f0f\u7ea6\u5b9a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u6e29\u99a8\u63d0\u793a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u53ef\u80fd\u72af\u9519\u7684\u8b66\u544a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u7b11\u8bdd\u6216\u8da3\u5473\u5bd3\u8a00\u6545\u4e8b \u7528\u8fd9\u79cd\u989c\u8272\u4e66\u5199\u7684\u662f\u8865\u5145\u8bf4\u660e\u7684\u8bfe\u5916\u9605\u8bfb\uff0c\u770b\u4e0d\u61c2\u4e5f\u6ca1\u5173\u7cfb \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u662f\u521d\u5b66\u8005\u53ef\u6682\u65f6\u4e0d\u7528\u7406\u89e3\u7684\u7ec6\u8282 \u672f\u8bed\u540d\u79f0: \u8fd9\u91cc\u662f\u672f\u8bed\u7684\u5b9a\u4e49\u3002 \u89c2\u524d\u987b\u77e5 \u4e0e\u5927\u591a\u6570\u73b0\u6709\u6559\u6750\u4e0d\u540c\u7684\u662f\uff0c\u672c\u8bfe\u7a0b\u5c06\u4f1a\u91c7\u7528\u201c\u5012\u53d9\u201d\u7684\u5f62\u5f0f\uff0c\u4ece\u6700\u65b0\u7684 C++23 \u8bb2\u8d77\uff01\u7136\u540e\u8bb2 C++20\u3001C++17\u3001C++14\u3001C++11\uff0c\u6162\u6162\u8bb2\u5230\u6700\u539f\u59cb\u7684 C++98\u3002 \u4e0d\u7528\u62c5\u5fc3\uff0c\u8d8a\u662f\u73b0\u4ee3\u7684 C++\uff0c\u5b66\u8d77\u6765\u53cd\u800c\u66f4\u5bb9\u6613\uff01\u53cd\u800c\u53e4\u4ee3 C++ \u624d \u53c8\u81ed\u53c8\u957f \u3002 \u5f88\u591a\u540c\u5b66\u60f3\u5f53\u7136\u5730\u8bef\u4ee5\u4e3a C++98 \u6700\u7b80\u5355\uff0c\u54fc\u54e7\u54fc\u54e7\u8d39\u8001\u5927\u52b2\u4ece C++98 \u5f00\u59cb\u5b66\uff0c\u624d\u662f\u9519\u8bef\u7684\u3002 \u4e3a\u4e86\u5e94\u4ed8\u7f3a\u80f3\u818a\u5c11\u817f\u7684 C++98\uff0c\u4eba\u4eec\u53d1\u660e\u4e86\u5404\u79cd \u7e41\u7410\u65e0\u8c13 \u7684\u5199\u6cd5\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u65e9\u5c31\u5df2\u7ecf\u88ab\u66f4 \u7b80\u6d01\u76f4\u89c2 \u7684\u5199\u6cd5\u66ff\u4ee3\u4e86\u3002 \u4f8b\u5982\u6240\u8c13\u7684 safe-bool idiom\uff0c\u5199\u8d77\u6765\u53c8\u81ed\u53c8\u957f\uff0cC++11 \u5f15\u5165\u4e00\u4e2a explicit \u5173\u952e\u5b57\u76f4\u63a5\u5c31\u79d2\u4e86\u3002\u7ed3\u679c\u8fd8\u6709\u4e00\u6279\u52b3\u4fdd\u6559\u6750\u5927\u5439\u7279\u5439 safe-bool idiom\uff0c\u5439\u5f97\u597d\u50cf\u662f\u4e2a\u4ec0\u4e48\u9ad8\u5927\u4e0a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u6837\uff0c\u4e0d\u8fc7\u662f\u4e2a\u5e94\u4ed8 C++98 \u8bed\u8a00\u7f3a\u9677\u7684\u8e69\u811a\u73a9\u610f\u3002 \u5c31\u597d\u6bd4\u4e00\u4e2a \u8001\u5916 \u60f3\u8981\u5b66\u4e60\u6c49\u8bed\uff0c\u4ed6\u9996\u5148\u80af\u5b9a\u662f\u4ece \u73b0\u4ee3\u6c49\u8bed \u5b66\u8d77\uff01\u800c\u4e0d\u662f\u4e0a\u6765\u5c31\u6559\u4ed6 \u6587\u8a00\u6587 \u3002 \u5373\u4f7f\u8fd9\u4e2a\u8001\u5916\u7684\u804c\u4e1a\u5c31\u662f\u201c\u8003\u53e4\u201d\uff0c\u6216\u8005\u4ed6\u5bf9\u201c\u53e4\u4ee3\u6587\u5b66\u201d\u611f\u5174\u8da3\uff0c\u4e5f\u4e0d\u53ef\u80fd\u81ea\u5b66\u6587\u8a00\u6587\u7684\u540c\u65f6\u5b8c\u5168\u8df3\u8fc7\u73b0\u4ee3\u6c49\u8bed\u3002 \u5f53\u6211\u4eec\u5b66\u4e60\u4e2d\u6587\u65f6\uff0c\u4f60\u80af\u5b9a\u5e0c\u671b\u5148\u5b66\u73b0\u4ee3\u6c49\u8bed\uff0c\u518d\u5b66\u6587\u8a00\u6587\uff0c\u518d\u5b66\u7532\u9aa8\u6587\uff0c\u518d\u5b66 brainf* * k\uff0c\u800c\u4e0d\u662f\u53cd\u8fc7\u6765\u3002 \u5bf9\u4e8e C++ \u521d\u5b66\u8005\u4e5f\u662f\u5982\u6b64\uff1a\u6211\u4eec\u9996\u5148\u5b66\u4f1a\u7b80\u5355\u660e\u4e86\u7684\uff0c\u7b26\u5408\u73b0\u4ee3\u4eba\u601d\u7ef4\u7684 C++23\uff0c\u518d\u9010\u6e10\u56de\u5230\u4e13\u4e3a\u4f3a\u5019\u201c\u53e4\u4ee3\u5f00\u53d1\u73af\u5883\u201d\u7684 C++98\u3002 \u4f60\u7684\u751f\u4ea7\u73af\u5883\u53ef\u80fd\u4e0d\u5141\u8bb8\u7528\u4e0a C++20 \u751a\u81f3 C++23 \u7684\u65b0\u6807\u51c6\u3002 \u522b\u62c5\u5fc3\uff0c\u5c0f\u5f6d\u8001\u5e08\u6559\u4f1a\u4f60 C++23 \u7684\u6b63\u5e38\u5199\u6cd5\u540e\uff0c\u4f1a\u8bb2\u89e3\u5982\u4f55\u5728 C++14\u3001C++98 \u4e2d\u5199\u51fa\u540c\u6837\u7684\u6548\u679c\u3002 \u8fd9\u6837\u4f60\u5b66\u4e60\u7684\u65f6\u5019\u601d\u8def\u6e05\u6670\uff0c\u4e0d\u7528\u88ab\u7e41\u7410\u7684 C++98 \u201c\u5947\u6280\u6deb\u5de7\u201d\u5e72\u6270\uff0c\u5b66\u8d77\u6765\u4e8b\u534a\u529f\u500d\uff1b\u4f46\u4e5f\u201c\u5403\u8fc7\u89c1\u8fc7\u201d\uff0c\u77e5\u9053\u53e4\u4ee3 C++98 \u7684\u5e94\u5bf9\u7b56\u7565\u3002 \u76ee\u524d\u4f01\u4e1a\u91cc\u4e3b\u6d41\u4f7f\u7528\u7684\u662f C++14 \u548c C++17\u3002\u4f8b\u5982\u8c37\u6b4c\u5c31\u660e\u786e\u89c4\u5b9a\u8981\u6c42 C++17\u3002 \u4e3e\u4e2a\u4f8b\u5b50 \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4f60\u53ef\u80fd\u770b\u4e0d\u61c2\uff0c\u4f46\u53ea\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5411\u4f60\u8bf4\u660e\uff1a\u8d8a\u662f\u65b0\u7684 C++ \u6807\u51c6\uff0c\u53cd\u800c\u8d8a\u5bb9\u6613\u5b66\uff01 \u4f8b\u5982\uff0c\u5728\u6a21\u677f\u5143\u7f16\u7a0b\u4e2d\uff0c\u8981\u68c0\u6d4b\u4e00\u4e2a\u7c7b\u578b T \u662f\u5426\u62e5\u6709 foo() \u8fd9\u4e00\u6210\u5458\u51fd\u6570\u3002\u5982\u679c\u5b58\u5728\uff0c\u624d\u4f1a\u8c03\u7528\u3002 \u5728 C++20 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f88\u65b9\u4fbf\u7684 requires \u8bed\u6cd5\uff0c\u8f7b\u677e\u68c0\u6d4b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u80fd\u5408\u6cd5\u901a\u8fc7\u7f16\u8bd1\u3002\u5982\u679c\u80fd\uff0c requires \u8bed\u53e5\u4f1a\u8fd4\u56de true \u3002\u7136\u540e\u7528\u4e00\u4e2a if constexpr \u8fdb\u884c\u7f16\u8bd1\u671f\u5206\u652f\u5224\u65ad\uff0c\u5373\u53ef\u5b9e\u73b0\u68c0\u6d4b\u5230\u5b58\u5728\u5219\u8c03\u7528\u3002 template void try_call_foo(T &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } \u4f46\u4ec5\u4ec5\u662f\u56de\u5230 C++17\uff0c\u6ca1\u6709 requires \u8bed\u6cd5\uff0c\u6211\u4eec\u53ea\u80fd\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a trait \u7c7b\uff0c\u5e76\u8fd0\u7528\u70e6\u4eba\u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u68c0\u6d4b\u8868\u8fbe\u5f0f\u662f\u5426\u7684\u5408\u6cd5\uff0c\u53c8\u81ed\u53c8\u957f\u3002 template struct has_foo { inline constexpr bool value = false; }; template struct has_foo().foo())>> { inline constexpr bool value = true; }; template void try_call_foo(T &t) { if constexpr (has_foo::value) { t.foo(); } } \u5982\u679c\u56de\u5230 C++14\uff0c\u60c5\u51b5\u5c31\u66f4\u7cdf\u7cd5\u4e86\uff01 if constexpr \u662f C++17 \u7684\u7279\u6027\uff0c\u6ca1\u6709\u4ed6\uff0c\u8981\u5b9e\u73b0\u7f16\u8bd1\u671f\u5206\u652f\uff0c\u6211\u4eec\u5c31\u5f97\u7528 enable_if_t \u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a try_call_foo \u51fd\u6570\uff0c\u4e92\u76f8\u91cd\u8f7d\uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo())>> { static constexpr bool value = true; }; template ::value, int> = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int> = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++11\uff0c\u60c5\u51b5\u8fdb\u4e00\u6b65\u6076\u5316\uff01 enable_if_t \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5c0f\u52a9\u624b\u5df2\u7ecf\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u4f7f\u7528\u6bd4\u4ed6\u66f4\u5e95\u5c42\u7684 enable_if \u6a21\u677f\u7c7b\uff0c\u624b\u52a8\u53d6\u51fa ::type \uff0c\u5e76\u4e14\u9700\u8981 typename \u4fee\u9970\uff0c\u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff01\u5e76\u4e14 void_t \u4e5f\u4e0d\u80fd\u7528\u4e86\uff0c\u8981\u7528\u9017\u53f7\u8868\u8fbe\u5f0f\u5c0f\u6280\u5de7\u624d\u80fd\u8ba9 decltype \u56fa\u5b9a\u8fd4\u56de void\u2026\u2026 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo(), (void)0)> { static constexpr bool value = true; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++98\uff0c\u90a3\u53c8\u8981\u7f6a\u52a0\u4e00\u7b49\uff01 enable_if \u548c \u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u7684\u5e2e\u624b\u7c7b\uff0c\u5728 C++98 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9e\u73b0 enable_if \u2026\u2026 declval \u4e5f\u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u4e2d\u7684\u5e2e\u624b\u51fd\u6570\u2026\u2026\u5047\u8bbe\u4f60\u81ea\u5df1\u597d\u4e0d\u5bb9\u6613\u5b9e\u73b0\u51fa\u6765\u4e86 enable_if \u548c declval \uff0c\u8fd8\u6ca1\u5b8c\uff1a\u56e0\u4e3a constexpr \u5728 C++98 \u4e2d\u4e5f\u4e0d\u5b58\u5728\u4e86\uff01\u4f60\u65e0\u6cd5\u5b9a\u4e49 value \u6210\u5458\u53d8\u91cf\u4e3a\u7f16\u8bd1\u671f\u5e38\u91cf\uff0c\u6211\u4eec\u53ea\u597d\u53c8\u7528\u4e00\u4e2a\u62bd\u8c61\u7684\u679a\u4e3e\u5c0f\u6280\u5de7\u6765\u5b9e\u73b0\u5b9a\u4e49\u7c7b\u6210\u5458\u5e38\u91cf\u7684\u6548\u679c\u3002 template struct has_foo { enum { value = 0 }; }; template struct has_foo().foo(), (void)0)> { enum { value = 1 }; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u6b64\u5197\u957f\u96be\u61c2\u7684\u62bd\u8c61 C++98 \u4ee3\u7801\uff0c\u4eff\u4f5b\u662f\u201c\u52a0\u5bc6\u201d\u8fc7\u7684\u4ee3\u7801\u4e00\u6837\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u5b9e\u73b0\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u6210\u5458\u51fd\u6570 foo\u2026\u2026 \u5982\u679c\u56de\u5230 C \u8bed\u8a00\uff0c\u90a3\u4e48\u4f60\u751a\u81f3\u90fd\u4e0d\u7528\u68c0\u6d4b\u4e86\u3002\u56e0\u4e3a\u4f1f\u5927\u7684 C \u8bed\u8a00\u8fde\u6210\u5458\u51fd\u6570\u90fd\u6ca1\u6709\uff0c\u4f55\u8c08\u201c\u68c0\u6d4b\u6210\u5458\u51fd\u6570\u662f\u5426\u5b58\u5728\u201d\uff1f \u53cd\u89c2 C++20 \u7684\u5199\u6cd5\uff0c\u4e00\u773c\u5c31\u770b\u660e\u767d\u4ee3\u7801\u7684\u903b\u8f91\u662f\u4ec0\u4e48\uff0c\u8868\u8fbe\u4f60\u8be5\u8868\u8fbe\u7684\uff0c\u800c\u4e0d\u662f\u8ff7\u5931\u4e8e\u4f3a\u5019\u5404\u79cd\u8bed\u8a00\u7f3a\u9677\uff0c\u5e72\u6270\u6211\u4eec\u5b66\u4e60\u3002 void try_call_foo(auto &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } // \u4ece\u6b8b\u5e9f\u7684 C++98 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u5c31\u88ab\u8fd9\u4e9b\u65e0\u8c13\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u626d\u66f2\u4e86\uff0c\u800c\u4f7f\u5f97\u771f\u6b63\u5e94\u8be5\u8868\u8fbe\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u6df9\u6ca1\u5728\u53c8\u81ed\u53c8\u957f\u7684\u53e4\u4ee3\u6280\u5de7\u4e2d\u3002 // \u4ece\u73b0\u4ee3\u7684 C++23 \u5b66\u8d77\uff0c\u5148\u77e5\u9053\u6b63\u5e38\u7684\u5199\u6cd5\u201c\u7406\u5e94\u201d\u662f\u4ec0\u4e48\u6837\u3002\u5de5\u4f5c\u4e2d\u7528\u4e0d\u4e0a C++23\uff1f\u6211\u4f1a\u5411\u4f60\u4ecb\u7ecd\uff0c\u5982\u679c\u8981\u5012\u9000\u56de C++14\uff0c\u53e4\u4ee3\u4eba\u90fd\u662f\u7528\u4ec0\u4e48\u201c\u5947\u6280\u6deb\u5de7\u201d\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 // \u8fd9\u6837\u4f60\u6700\u540e\u540c\u6837\u53ef\u4ee5\u9002\u5e94\u516c\u53f8\u8981\u6c42\u7684 C++14 \u73af\u5883\u3002\u4f46\u662f\u4ece C++23 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u53c8\u4e0d\u4f1a\u88ab\u5e94\u4ed8\u53e4\u4ee3\u8bed\u8a00\u7f3a\u9677\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u6270\u4e71\uff0c\u5b66\u8d77\u6765\u5c31\u4e8b\u534a\u529f\u500d\u3002 \u65e2\u7136\u73b0\u4ee3 C++ \u8fd9\u4e48\u597d\uff0c\u4e3a\u4ec0\u4e48\u5b66\u6821\u4e0d\u4ece\u73b0\u4ee3 C++ \u6559\u8d77\uff0c\u6559\u8d77\u6765\u8fd8\u8f7b\u677e\uff1f\u56e0\u4e3a\u52b3\u4fdd\u8001\u5e08\u4fdd\uff0c\u61d2\u5f97\u63a5\u89e6\u65b0\u77e5\u8bc6\uff0c\u8ba4\u4e3a\u201c\u7956\u5b97\u4e4b\u6cd5\u4e0d\u53ef\u53d8\u201d\uff0c\u201c\u7248\u53f7\u7a33\u5b9a\u538b\u5012\u4e00\u5207\u201d\u3002","title":"\u524d\u8a00"},{"location":"#c","text":"\u5c0f\u5f6d\u5927\u5178\u662f\u4e00\u672c\u5173\u4e8e\u73b0\u4ee3 C++ \u7f16\u7a0b\u7684\u6743\u5a01\u6307\u5357\uff0c\u5b83\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u77e5\u8bc6\u5230\u9ad8\u7ea7\u6280\u5de7\u7684\u5185\u5bb9\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u9605\u8bfb\u3002\u672c\u4e66\u7531\u5c0f\u5f6d\u8001\u5e08\u4eb2\u81ea\u7f16\u5199\uff0c\u901a\u8fc7\u7b80\u5355\u6613\u61c2\u7684\u8bed\u8a00\u548c\u4e30\u5bcc\u7684\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 C++ \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u5b66\u4f1a\u5982\u4f55\u8fd0\u7528\u5b83\u4eec\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002 \u6562\u627f\u8bfa\uff1a\u571f\u6728\u8001\u54e5\u4e5f\u80fd\u770b\u61c2\uff01","title":"\u5c0f\u5f6d\u8001\u5e08\u73b0\u4ee3 C++ \u5927\u5178"},{"location":"#_1","text":"\u63a8\u8350\u7528\u624b\u673a\u6216\u5e73\u677f \u7ad6\u5c4f \u89c2\u770b\uff0c\u53ef\u4ee5\u5728\u5e8a\u6216\u6c99\u53d1\u4e0a\u8eba\u7740\u3002 \u7528\u7535\u8111\u770b\u7684\u8bdd\uff0c\u53ef\u4ee5\u6309 WIN + \u2190 \uff0c\u628a\u672c\u4e66\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u653e\u5728\u5c4f\u5e55\u5de6\u4fa7\uff0c\u53f3\u4fa7\u662f\u4f60\u7684 IDE\u3002\u4e00\u8fb9\u770b\u4e00\u8fb9\u81ea\u5df1\u52a8\u624b\u505a\u5b9e\u9a8c\u3002 \u8bf7\u5750\u548c\u653e\u5bbd\u3002 \u53ef\u4ee5\u6309\u987a\u5e8f\u9605\u8bfb\uff0c\u4e5f\u53ef\u4ee5\u5728\u672c\u9875\u9762\u4e0a\u65b9\u5bfc\u822a\u680f\u7684\u201c\u7ae0\u8282\u5217\u8868\u201d\u4e2d\uff0c\u9009\u62e9\u611f\u5174\u8da3\u7684\u7ae0\u8282\u9605\u8bfb\u3002 \u672c\u4e66\u5b8c\u5168\u5f00\u6e90\u548c\u514d\u8d39\uff0cGitHub \u4ed3\u5e93\uff1a https://github.com/parallel101/cppguidebook \u5982\u679c\u4f60\u662f\u5728\u4ed8\u8d39\u7fa4\u4e2d\u201c\u4e70\u201d\u5230\u672c\u4e66\uff0c\u6216\u8005\u6253\u7740\u5c0f\u5f6d\u8001\u5e08\u540d\u53f7\u5356\u8bfe\uff0c\u8bf4\u660e\u4f60\u53ef\u80fd\u662f\u79c1\u6709\u5236\u7684\u53d7\u5bb3\u8005\u3002\u56e0\u4e3a\u5c0f\u5f6d\u8001\u5e08\u4ece\u6765\u6ca1\u6709\u4ed8\u8d39\u624d\u80fd\u770b\u7684\u8bfe\u7a0b\uff0c\u6240\u6709\u5c0f\u5f6d\u8001\u5e08\u8bfe\u7a0b\u90fd\u5bf9\u5168\u7403\u4e92\u8054\u7f51\u5f00\u653e\u3002 \u5982\u9700\u79bb\u7ebf\u67e5\u770b\uff0c\u53ef\u4ee5\u524d\u5f80 GitHub Release \u9875\u9762 \u4e0b\u8f7d PDF \u6587\u4ef6\u3002 \u5982\u679c\u4f60\u5728\u9605\u8bfb\u8fc7\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u53ef\u4ee5\u5728 GitHub Issues \u4e2d\u63d0\u51fa\uff0c\u5c0f\u5f6d\u8001\u5e08\u4f1a\u5c3d\u529b\u89e3\u7b54\u3002 \u4e5f\u53ef\u4ee5\u5728 B \u7ad9 \u53d1\u79c1\u4fe1\u7ed9\u5c0f\u5f6d\u8001\u5e08\u54e6\u3002 \u672c\u4e66\u8fd8\u5728\u6301\u7eed\u66f4\u65b0\u4e2d\u2026\u2026\u8981\u8ffd\u756a\u7684\u8bdd\uff0c\u53ef\u4ee5\u5728 GitHub \u70b9\u4e00\u4e0b\u53f3\u4e0a\u89d2\u7684 \u201cWatch\u201d \u6309\u94ae\uff0c\u6bcf\u5f53\u5c0f\u5f6d\u8001\u5e08\u63d0\u4ea4\u65b0 commit\uff0cGitHub \u4f1a\u5411\u4f60\u53d1\u9001\u4e00\u5cf0\u7535\u5b50\u90ae\u4ef6\uff0c\u63d0\u9192\u4f60\u5c0f\u5f6d\u8001\u5e08\u66f4\u65b0\u4e86\u3002 \u66f4\u65b0\u65f6\u95f4\uff1ahttps://github.com/parallel101/cppguidebook","title":"\u524d\u8a00"},{"location":"#_2","text":"\u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u6e29\u99a8\u63d0\u793a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u53ef\u80fd\u72af\u9519\u7684\u8b66\u544a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u7b11\u8bdd\u6216\u8da3\u5473\u5bd3\u8a00\u6545\u4e8b \u7528\u8fd9\u79cd\u989c\u8272\u4e66\u5199\u7684\u662f\u8865\u5145\u8bf4\u660e\u7684\u8bfe\u5916\u9605\u8bfb\uff0c\u770b\u4e0d\u61c2\u4e5f\u6ca1\u5173\u7cfb \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u662f\u521d\u5b66\u8005\u53ef\u6682\u65f6\u4e0d\u7528\u7406\u89e3\u7684\u7ec6\u8282 \u672f\u8bed\u540d\u79f0: \u8fd9\u91cc\u662f\u672f\u8bed\u7684\u5b9a\u4e49\u3002","title":"\u683c\u5f0f\u7ea6\u5b9a"},{"location":"#_3","text":"\u4e0e\u5927\u591a\u6570\u73b0\u6709\u6559\u6750\u4e0d\u540c\u7684\u662f\uff0c\u672c\u8bfe\u7a0b\u5c06\u4f1a\u91c7\u7528\u201c\u5012\u53d9\u201d\u7684\u5f62\u5f0f\uff0c\u4ece\u6700\u65b0\u7684 C++23 \u8bb2\u8d77\uff01\u7136\u540e\u8bb2 C++20\u3001C++17\u3001C++14\u3001C++11\uff0c\u6162\u6162\u8bb2\u5230\u6700\u539f\u59cb\u7684 C++98\u3002 \u4e0d\u7528\u62c5\u5fc3\uff0c\u8d8a\u662f\u73b0\u4ee3\u7684 C++\uff0c\u5b66\u8d77\u6765\u53cd\u800c\u66f4\u5bb9\u6613\uff01\u53cd\u800c\u53e4\u4ee3 C++ \u624d \u53c8\u81ed\u53c8\u957f \u3002 \u5f88\u591a\u540c\u5b66\u60f3\u5f53\u7136\u5730\u8bef\u4ee5\u4e3a C++98 \u6700\u7b80\u5355\uff0c\u54fc\u54e7\u54fc\u54e7\u8d39\u8001\u5927\u52b2\u4ece C++98 \u5f00\u59cb\u5b66\uff0c\u624d\u662f\u9519\u8bef\u7684\u3002 \u4e3a\u4e86\u5e94\u4ed8\u7f3a\u80f3\u818a\u5c11\u817f\u7684 C++98\uff0c\u4eba\u4eec\u53d1\u660e\u4e86\u5404\u79cd \u7e41\u7410\u65e0\u8c13 \u7684\u5199\u6cd5\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u65e9\u5c31\u5df2\u7ecf\u88ab\u66f4 \u7b80\u6d01\u76f4\u89c2 \u7684\u5199\u6cd5\u66ff\u4ee3\u4e86\u3002 \u4f8b\u5982\u6240\u8c13\u7684 safe-bool idiom\uff0c\u5199\u8d77\u6765\u53c8\u81ed\u53c8\u957f\uff0cC++11 \u5f15\u5165\u4e00\u4e2a explicit \u5173\u952e\u5b57\u76f4\u63a5\u5c31\u79d2\u4e86\u3002\u7ed3\u679c\u8fd8\u6709\u4e00\u6279\u52b3\u4fdd\u6559\u6750\u5927\u5439\u7279\u5439 safe-bool idiom\uff0c\u5439\u5f97\u597d\u50cf\u662f\u4e2a\u4ec0\u4e48\u9ad8\u5927\u4e0a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u6837\uff0c\u4e0d\u8fc7\u662f\u4e2a\u5e94\u4ed8 C++98 \u8bed\u8a00\u7f3a\u9677\u7684\u8e69\u811a\u73a9\u610f\u3002 \u5c31\u597d\u6bd4\u4e00\u4e2a \u8001\u5916 \u60f3\u8981\u5b66\u4e60\u6c49\u8bed\uff0c\u4ed6\u9996\u5148\u80af\u5b9a\u662f\u4ece \u73b0\u4ee3\u6c49\u8bed \u5b66\u8d77\uff01\u800c\u4e0d\u662f\u4e0a\u6765\u5c31\u6559\u4ed6 \u6587\u8a00\u6587 \u3002 \u5373\u4f7f\u8fd9\u4e2a\u8001\u5916\u7684\u804c\u4e1a\u5c31\u662f\u201c\u8003\u53e4\u201d\uff0c\u6216\u8005\u4ed6\u5bf9\u201c\u53e4\u4ee3\u6587\u5b66\u201d\u611f\u5174\u8da3\uff0c\u4e5f\u4e0d\u53ef\u80fd\u81ea\u5b66\u6587\u8a00\u6587\u7684\u540c\u65f6\u5b8c\u5168\u8df3\u8fc7\u73b0\u4ee3\u6c49\u8bed\u3002 \u5f53\u6211\u4eec\u5b66\u4e60\u4e2d\u6587\u65f6\uff0c\u4f60\u80af\u5b9a\u5e0c\u671b\u5148\u5b66\u73b0\u4ee3\u6c49\u8bed\uff0c\u518d\u5b66\u6587\u8a00\u6587\uff0c\u518d\u5b66\u7532\u9aa8\u6587\uff0c\u518d\u5b66 brainf* * k\uff0c\u800c\u4e0d\u662f\u53cd\u8fc7\u6765\u3002 \u5bf9\u4e8e C++ \u521d\u5b66\u8005\u4e5f\u662f\u5982\u6b64\uff1a\u6211\u4eec\u9996\u5148\u5b66\u4f1a\u7b80\u5355\u660e\u4e86\u7684\uff0c\u7b26\u5408\u73b0\u4ee3\u4eba\u601d\u7ef4\u7684 C++23\uff0c\u518d\u9010\u6e10\u56de\u5230\u4e13\u4e3a\u4f3a\u5019\u201c\u53e4\u4ee3\u5f00\u53d1\u73af\u5883\u201d\u7684 C++98\u3002 \u4f60\u7684\u751f\u4ea7\u73af\u5883\u53ef\u80fd\u4e0d\u5141\u8bb8\u7528\u4e0a C++20 \u751a\u81f3 C++23 \u7684\u65b0\u6807\u51c6\u3002 \u522b\u62c5\u5fc3\uff0c\u5c0f\u5f6d\u8001\u5e08\u6559\u4f1a\u4f60 C++23 \u7684\u6b63\u5e38\u5199\u6cd5\u540e\uff0c\u4f1a\u8bb2\u89e3\u5982\u4f55\u5728 C++14\u3001C++98 \u4e2d\u5199\u51fa\u540c\u6837\u7684\u6548\u679c\u3002 \u8fd9\u6837\u4f60\u5b66\u4e60\u7684\u65f6\u5019\u601d\u8def\u6e05\u6670\uff0c\u4e0d\u7528\u88ab\u7e41\u7410\u7684 C++98 \u201c\u5947\u6280\u6deb\u5de7\u201d\u5e72\u6270\uff0c\u5b66\u8d77\u6765\u4e8b\u534a\u529f\u500d\uff1b\u4f46\u4e5f\u201c\u5403\u8fc7\u89c1\u8fc7\u201d\uff0c\u77e5\u9053\u53e4\u4ee3 C++98 \u7684\u5e94\u5bf9\u7b56\u7565\u3002 \u76ee\u524d\u4f01\u4e1a\u91cc\u4e3b\u6d41\u4f7f\u7528\u7684\u662f C++14 \u548c C++17\u3002\u4f8b\u5982\u8c37\u6b4c\u5c31\u660e\u786e\u89c4\u5b9a\u8981\u6c42 C++17\u3002","title":"\u89c2\u524d\u987b\u77e5"},{"location":"#_4","text":"\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4f60\u53ef\u80fd\u770b\u4e0d\u61c2\uff0c\u4f46\u53ea\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5411\u4f60\u8bf4\u660e\uff1a\u8d8a\u662f\u65b0\u7684 C++ \u6807\u51c6\uff0c\u53cd\u800c\u8d8a\u5bb9\u6613\u5b66\uff01 \u4f8b\u5982\uff0c\u5728\u6a21\u677f\u5143\u7f16\u7a0b\u4e2d\uff0c\u8981\u68c0\u6d4b\u4e00\u4e2a\u7c7b\u578b T \u662f\u5426\u62e5\u6709 foo() \u8fd9\u4e00\u6210\u5458\u51fd\u6570\u3002\u5982\u679c\u5b58\u5728\uff0c\u624d\u4f1a\u8c03\u7528\u3002 \u5728 C++20 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f88\u65b9\u4fbf\u7684 requires \u8bed\u6cd5\uff0c\u8f7b\u677e\u68c0\u6d4b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u80fd\u5408\u6cd5\u901a\u8fc7\u7f16\u8bd1\u3002\u5982\u679c\u80fd\uff0c requires \u8bed\u53e5\u4f1a\u8fd4\u56de true \u3002\u7136\u540e\u7528\u4e00\u4e2a if constexpr \u8fdb\u884c\u7f16\u8bd1\u671f\u5206\u652f\u5224\u65ad\uff0c\u5373\u53ef\u5b9e\u73b0\u68c0\u6d4b\u5230\u5b58\u5728\u5219\u8c03\u7528\u3002 template void try_call_foo(T &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } \u4f46\u4ec5\u4ec5\u662f\u56de\u5230 C++17\uff0c\u6ca1\u6709 requires \u8bed\u6cd5\uff0c\u6211\u4eec\u53ea\u80fd\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a trait \u7c7b\uff0c\u5e76\u8fd0\u7528\u70e6\u4eba\u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u68c0\u6d4b\u8868\u8fbe\u5f0f\u662f\u5426\u7684\u5408\u6cd5\uff0c\u53c8\u81ed\u53c8\u957f\u3002 template struct has_foo { inline constexpr bool value = false; }; template struct has_foo().foo())>> { inline constexpr bool value = true; }; template void try_call_foo(T &t) { if constexpr (has_foo::value) { t.foo(); } } \u5982\u679c\u56de\u5230 C++14\uff0c\u60c5\u51b5\u5c31\u66f4\u7cdf\u7cd5\u4e86\uff01 if constexpr \u662f C++17 \u7684\u7279\u6027\uff0c\u6ca1\u6709\u4ed6\uff0c\u8981\u5b9e\u73b0\u7f16\u8bd1\u671f\u5206\u652f\uff0c\u6211\u4eec\u5c31\u5f97\u7528 enable_if_t \u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a try_call_foo \u51fd\u6570\uff0c\u4e92\u76f8\u91cd\u8f7d\uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo())>> { static constexpr bool value = true; }; template ::value, int> = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int> = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++11\uff0c\u60c5\u51b5\u8fdb\u4e00\u6b65\u6076\u5316\uff01 enable_if_t \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5c0f\u52a9\u624b\u5df2\u7ecf\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u4f7f\u7528\u6bd4\u4ed6\u66f4\u5e95\u5c42\u7684 enable_if \u6a21\u677f\u7c7b\uff0c\u624b\u52a8\u53d6\u51fa ::type \uff0c\u5e76\u4e14\u9700\u8981 typename \u4fee\u9970\uff0c\u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff01\u5e76\u4e14 void_t \u4e5f\u4e0d\u80fd\u7528\u4e86\uff0c\u8981\u7528\u9017\u53f7\u8868\u8fbe\u5f0f\u5c0f\u6280\u5de7\u624d\u80fd\u8ba9 decltype \u56fa\u5b9a\u8fd4\u56de void\u2026\u2026 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo(), (void)0)> { static constexpr bool value = true; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++98\uff0c\u90a3\u53c8\u8981\u7f6a\u52a0\u4e00\u7b49\uff01 enable_if \u548c \u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u7684\u5e2e\u624b\u7c7b\uff0c\u5728 C++98 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9e\u73b0 enable_if \u2026\u2026 declval \u4e5f\u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u4e2d\u7684\u5e2e\u624b\u51fd\u6570\u2026\u2026\u5047\u8bbe\u4f60\u81ea\u5df1\u597d\u4e0d\u5bb9\u6613\u5b9e\u73b0\u51fa\u6765\u4e86 enable_if \u548c declval \uff0c\u8fd8\u6ca1\u5b8c\uff1a\u56e0\u4e3a constexpr \u5728 C++98 \u4e2d\u4e5f\u4e0d\u5b58\u5728\u4e86\uff01\u4f60\u65e0\u6cd5\u5b9a\u4e49 value \u6210\u5458\u53d8\u91cf\u4e3a\u7f16\u8bd1\u671f\u5e38\u91cf\uff0c\u6211\u4eec\u53ea\u597d\u53c8\u7528\u4e00\u4e2a\u62bd\u8c61\u7684\u679a\u4e3e\u5c0f\u6280\u5de7\u6765\u5b9e\u73b0\u5b9a\u4e49\u7c7b\u6210\u5458\u5e38\u91cf\u7684\u6548\u679c\u3002 template struct has_foo { enum { value = 0 }; }; template struct has_foo().foo(), (void)0)> { enum { value = 1 }; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u6b64\u5197\u957f\u96be\u61c2\u7684\u62bd\u8c61 C++98 \u4ee3\u7801\uff0c\u4eff\u4f5b\u662f\u201c\u52a0\u5bc6\u201d\u8fc7\u7684\u4ee3\u7801\u4e00\u6837\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u5b9e\u73b0\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u6210\u5458\u51fd\u6570 foo\u2026\u2026 \u5982\u679c\u56de\u5230 C \u8bed\u8a00\uff0c\u90a3\u4e48\u4f60\u751a\u81f3\u90fd\u4e0d\u7528\u68c0\u6d4b\u4e86\u3002\u56e0\u4e3a\u4f1f\u5927\u7684 C \u8bed\u8a00\u8fde\u6210\u5458\u51fd\u6570\u90fd\u6ca1\u6709\uff0c\u4f55\u8c08\u201c\u68c0\u6d4b\u6210\u5458\u51fd\u6570\u662f\u5426\u5b58\u5728\u201d\uff1f \u53cd\u89c2 C++20 \u7684\u5199\u6cd5\uff0c\u4e00\u773c\u5c31\u770b\u660e\u767d\u4ee3\u7801\u7684\u903b\u8f91\u662f\u4ec0\u4e48\uff0c\u8868\u8fbe\u4f60\u8be5\u8868\u8fbe\u7684\uff0c\u800c\u4e0d\u662f\u8ff7\u5931\u4e8e\u4f3a\u5019\u5404\u79cd\u8bed\u8a00\u7f3a\u9677\uff0c\u5e72\u6270\u6211\u4eec\u5b66\u4e60\u3002 void try_call_foo(auto &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } // \u4ece\u6b8b\u5e9f\u7684 C++98 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u5c31\u88ab\u8fd9\u4e9b\u65e0\u8c13\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u626d\u66f2\u4e86\uff0c\u800c\u4f7f\u5f97\u771f\u6b63\u5e94\u8be5\u8868\u8fbe\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u6df9\u6ca1\u5728\u53c8\u81ed\u53c8\u957f\u7684\u53e4\u4ee3\u6280\u5de7\u4e2d\u3002 // \u4ece\u73b0\u4ee3\u7684 C++23 \u5b66\u8d77\uff0c\u5148\u77e5\u9053\u6b63\u5e38\u7684\u5199\u6cd5\u201c\u7406\u5e94\u201d\u662f\u4ec0\u4e48\u6837\u3002\u5de5\u4f5c\u4e2d\u7528\u4e0d\u4e0a C++23\uff1f\u6211\u4f1a\u5411\u4f60\u4ecb\u7ecd\uff0c\u5982\u679c\u8981\u5012\u9000\u56de C++14\uff0c\u53e4\u4ee3\u4eba\u90fd\u662f\u7528\u4ec0\u4e48\u201c\u5947\u6280\u6deb\u5de7\u201d\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 // \u8fd9\u6837\u4f60\u6700\u540e\u540c\u6837\u53ef\u4ee5\u9002\u5e94\u516c\u53f8\u8981\u6c42\u7684 C++14 \u73af\u5883\u3002\u4f46\u662f\u4ece C++23 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u53c8\u4e0d\u4f1a\u88ab\u5e94\u4ed8\u53e4\u4ee3\u8bed\u8a00\u7f3a\u9677\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u6270\u4e71\uff0c\u5b66\u8d77\u6765\u5c31\u4e8b\u534a\u529f\u500d\u3002 \u65e2\u7136\u73b0\u4ee3 C++ \u8fd9\u4e48\u597d\uff0c\u4e3a\u4ec0\u4e48\u5b66\u6821\u4e0d\u4ece\u73b0\u4ee3 C++ \u6559\u8d77\uff0c\u6559\u8d77\u6765\u8fd8\u8f7b\u677e\uff1f\u56e0\u4e3a\u52b3\u4fdd\u8001\u5e08\u4fdd\uff0c\u61d2\u5f97\u63a5\u89e6\u65b0\u77e5\u8bc6\uff0c\u8ba4\u4e3a\u201c\u7956\u5b97\u4e4b\u6cd5\u4e0d\u53ef\u53d8\u201d\uff0c\u201c\u7248\u53f7\u7a33\u5b9a\u538b\u5012\u4e00\u5207\u201d\u3002","title":"\u4e3e\u4e2a\u4f8b\u5b50"},{"location":"design/","text":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 \u4e0d\u8981\u53bb\u6307\u6325\u4e0b\u9762\u600e\u4e48\u505a\uff01 \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. \u4f60\u597d O(N) O(N) \u554a\uff01","title":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5)"},{"location":"design/#_1","text":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 \u4e0d\u8981\u53bb\u6307\u6325\u4e0b\u9762\u600e\u4e48\u505a\uff01","title":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5)"},{"location":"design/#_2","text":"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. \u4f60\u597d O(N) O(N) \u554a\uff01","title":"\u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9"},{"location":"functional/","text":"\u51fd\u6570\u5f0f\u7f16\u7a0b \u51fd\u6570\u5f0f\u7f16\u7a0b \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u7528\u51fd\u6570\u5c01\u88c5 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u4e8c\u6b21\u5c01\u88c5 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570 \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum = {}\", s); return 0; } \u8fd9\u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u7ec4\u6c42\u548c\u7684\u7b80\u5355\u7a0b\u5e8f\u3002 \u4f46\u662f\uff0c\u4ed6\u53ea\u80fd\u8ba1\u7b97\u6570\u7ec4 a \u7684\u6c42\u548c\uff0c\u65e0\u6cd5\u590d\u7528\u3002 \u5982\u679c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u6570\u7ec4 b \u4e5f\u9700\u8981\u6c42\u548c\u7684\u8bdd\uff0c\u5c31\u5f97\u628a\u6574\u4e2a\u6c42\u548c\u7684 for \u5faa\u73af\u91cd\u65b0\u5199\u4e00\u904d\uff1a int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum of a = {}\", s); std::vector b = {5, 6, 7, 8}; s = 0; for (int i = 0; i < a.size(); i++) { s += b[i]; } fmt::println(\"sum of b = {}\", s); return 0; } \u8fd9\u5c31\u51fa\u73b0\u4e86\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5927\u5fcc\uff1a\u4ee3\u7801\u91cd\u590d\u3002 \u4f8b\u5982\uff0c\u4f60\u6709\u5439\u7a7a\u8c03\u7684\u9700\u6c42\uff0c\u548c\u5145\u624b\u673a\u7684\u9700\u6c42\u3002\u4f60\u4e3a\u4e86\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u9700\u6c42\uff0c\u8d2d\u4e70\u4e86\u4e24\u53f0\u53d1\u7535\u673a\uff0c\u5206\u522b\u4e3a\u7a7a\u8c03\u548c\u624b\u673a\u4f9b\u7535\u3002\u7b2c\u4e8c\u5929\uff0c\u4f60\u53c8\u4ea7\u751f\u4e86\u73a9\u7535\u8111\u9700\u6c42\uff0c\u4e8e\u662f\u4f60\u53c8\u8d2d\u4e70\u4e00\u53f0\u53d1\u7535\u673a\uff0c\u4e13\u4e3a\u7535\u8111\u4f9b\u7535\u2026\u2026\u771f\u662f\u6d6a\u8d39\uff01 \u91cd\u590d\u7684\u4ee3\u7801\u4e0d\u4ec5\u5f71\u54cd\u4ee3\u7801\u7684 \u53ef\u8bfb\u6027 \uff0c\u4e5f\u589e\u52a0\u4e86 \u7ef4\u62a4 \u4ee3\u7801\u7684\u6210\u672c\u3002 \u770b\u8d77\u6765\u4e71\u7cdf\u7cdf\u7684\uff0c\u4fe1\u606f\u5bc6\u5ea6\u4f4e\uff0c\u8ba9\u4eba\u4e00\u773c\u770b\u4e0d\u51fa\u4ee3\u7801\u5728\u5e72\u4ec0\u4e48\u7684\u529f\u80fd \u5f88\u5bb9\u6613\u5199\u9519\uff0c\u770b\u8d70\u773c\uff0c\u96be\u8c03\u8bd5 \u590d\u5236\u7c98\u8d34\u8fc7\u7a0b\u4e2d\uff0c\u5bb9\u6613\u6f0f\u6539\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684 s += b[i] \u53ef\u80fd\u5199\u6210 s += a[i] \u800c\u81ea\u5df1\u4e0d\u53d1\u73b0 \u6539\u8d77\u6765\u4e0d\u65b9\u4fbf\uff0c\u5f53\u6211\u4eec\u7684\u9700\u6c42\u53d8\u66f4\u65f6\uff0c\u9700\u8981\u591a\u5904\u4fee\u6539\uff0c\u6bd4\u5982\u5f53\u6211\u9700\u8981\u6539\u4e3a\u8ba1\u7b97\u4e58\u79ef\u65f6\uff0c\u9700\u8981\u628a\u4e24\u4e2a\u5730\u65b9\u90fd\u6539\u6210 s *= \u6539\u4e86\u4ee5\u540e\u53ef\u80fd\u6f0f\u6539\u4e00\u90e8\u5206\uff0c\u7559\u4e0b Bug \u9690\u60a3 \u654f\u6377\u5f00\u53d1\u9700\u8981\u53cd\u590d\u4fee\u6539\u4ee3\u7801\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u8c03\u8bd5 += \u548c -= \u7684\u533a\u522b\uff0c\u770b\u7ed3\u679c\u53d8\u5316\uff0c\u5982\u679c\u4e00\u6b21\u5207\u6362\u9700\u8981\u6539\u591a\u5904\uff0c\u5c31\u5f71\u54cd\u4e86\u8c03\u8bd5\u901f\u5ea6 \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u5982\u679c\u4f60\u8fd8\u662f\u559c\u6b22\u201c\u4e00\u672c\u9053\u201d\u5199\u6cd5\u7684\u8bdd\uff0c\u4e0d\u59a8\u60f3\u60f3\u770b\uff0c\u5b8c\u5168\u4e0d\u7528\u4efb\u4f55\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u628a fmt::println \u548c std::vector \u8fd9\u4e9b\u51fd\u6570\u5168\u90e8\u62c6\u89e3\u6210\u4e00\u4e2a\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u90a3\u8fd9\u6574\u4e2a\u7a0b\u5e8f\u4f1a\u6709\u591a\u96be\u5199\uff1f int main() { #ifdef _WIN32 int *a = (int *)VirtualAlloc(NULL, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else int *a = (int *)mmap(NULL, 4 * sizeof(int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; int s = 0; for (int i = 0; i < 4; i++) { s += a[i]; } char buffer[64]; buffer[0] = 's'; buffer[1] = 'u'; buffer[2] = 'm'; buffer[3] = ' '; buffer[4] = '='; buffer[5] = ' '; // \u4f8b\u5982\uff0c\u5982\u679c\u8981\u4fee\u6539\u6b64\u5904\u7684\u63d0\u793a\u6587\u672c\uff0c\u751a\u81f3\u9700\u8981\u4fee\u6539\u540e\u9762\u7684 len \u53d8\u91cf... int len = 6; int x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif int *b = (int *)a; b[0] = 4; b[1] = 5; b[2] = 6; b[3] = 7; int s = 0; for (int i = 0; i < 4; i++) { s += b[i]; } len = 6; x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif #ifdef _WIN32 VirtualFree(a, 0, MEM_RELEASE); #else munmap(a); #endif return 0; } \u4e0d\u4ec5\u5b8c\u5168\u6ca1\u6709\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\uff0c\u751a\u81f3\u90fd\u6ca1\u6709\u53ef\u79fb\u690d\u6027\u3002 \u9664\u975e\u4f60\u53ea\u5199\u5e94\u4ed8\u5bfc\u5e08\u7684\u201c\u4e00\u6b21\u6027\u201d\u7a0b\u5e8f\uff0c\u4e00\u65e6\u8981\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u53ef\u907f\u514d\u7684\u8981\u81ea\u5df1\u5c01\u88c5\u51fd\u6570\u6216\u7c7b\u3002\u7f51\u4e0a\u6240\u6709\u9f13\u5439\u201c\u4e0d\u5c01\u88c5\u201d\u201c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u9762\u5b50\u5de5\u7a0b\u201d\u7684\u53cd\u667a\u8a00\u8bba\uff0c\u90fd\u662f\u6ca1\u6709\u505a\u8fc7\u5927\u578b\u9879\u76ee\u7684\u3002 \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u5f88\u591a\u8bbe\u8ba1\u6a21\u5f0f\u6559\u6750\u7247\u9762\u5f3a\u8c03 \u53ef\u8bfb\u6027 \uff0c\u4eff\u4f5b\u8bbe\u8ba1\u6a21\u5f0f\u5c31\u662f\u4e3a\u4e86\u201c\u4f18\u96c5\u201d\u201c\u9ad8\u5927\u4e0a\u201d\u201c\u7f8e\u5b66\u201d\uff1f\u4f7f\u5f97\u5f88\u591a\u4eba\u8ba4\u4e3a\uff0c\u201c\u6211\u8fd9\u4e2a\u662f\u81ea\u5df1\u7684\u9879\u76ee\uff0c\u4e0d\u7528\u7f8e\u5316\u7ed9\u9886\u5bfc\u770b\u201d\u800c\u62d2\u7edd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b9e\u9645\u4e0a\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4e3b\u8981\u4ef7\u503c\u5728\u4e8e \u65b9\u4fbf\u540e\u7eed\u4fee\u6539 *\uff01 \u4f8b\u5982 B \u7ad9\u4ee5\u524d\u53ea\u652f\u6301\u4e0a\u4f20\u666e\u901a\u89c6\u9891\uff0c\u73b0\u5728\u53d4\u53d4\u7a81\u7136\u63d0\u51fa\uff1a\u8981\u652f\u6301\u4e92\u52a8\u89c6\u9891\uff0c\u5145\u7535\u89c6\u9891\uff0c\u89c6\u9891\u5408\u96c6\uff0c\u8fd8\u5e9f\u9664\u4e86\u89c6\u9891\u5206 p\uff0c\u8fd8\u8981\u652f\u6301\u4e0a\u4f20\u77ed\u89c6\u9891\uff0c\u7ad6\u5c4f\u5f00\u5173\u7b49\u2026\u2026\u6bcf\u4e00\u4e2a\u53d4\u53d4\u7684\u8981\u6c42\uff0c\u90fd\u9700\u8981\u5927\u91cf\u7a0b\u5e8f\u5458\u4fee\u6539\u4ee3\u7801\uff0c\u65e0\u8bba\u6d89\u53ca\u524d\u7aef\u8fd8\u662f\u540e\u7aef\u3002 \u4e0e\u5efa\u7b51\u3001\u7ed8\u753b\u7b49\u9886\u57df\u4e0d\u540c\uff0c\u4e00\u6b21\u4ea4\u4ed8\u5b8c\u6bd5\u5c31\u53ef\u4ee5\u51e0\u4e4e\u6c38\u4e45\u4f7f\u7528\u3002\u800c\u8f6f\u4ef6\u5f00\u53d1\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u66f4\uff0c\u90fd\u5bfc\u81f4\u4ee3\u7801\u9700\u8981\u4fee\u6539\u3002\u5f00\u53d1\u4eba\u5458\u51e0\u4e4e\u9700\u8981\u4e00\u76f4\u56f4\u7ed5\u7740\u8f6f\u4ef6\u4ee3\u7801\uff0c\u4e0d\u65ad\u7684\u4fee\u6539\u3002\u8c03\u67e5\u8868\u660e\uff0c\u7a0b\u5e8f\u5458 90% \u7684\u65f6\u95f4\u82b1\u5728 \u6539\u4ee3\u7801 \u4e0a\uff0c \u5199\u4ee3\u7801 \u53ea\u5360 10%\u3002 \u8f6f\u4ef6\u5c31\u50cf\u751f\u7269\uff0c\u8981\u4e0d\u65ad\u8fdb\u5316\uff0c\u8f6f\u4ef6\u4e0d\u66f4\u65b0\u4e0d\u7ef4\u62a4\u4e86\u7b49\u4e8e\u6b7b\u3002\u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6\u9010\u6e10\u53d8\u5f97\u81c3\u80bf\u96be\u4ee5\u4fee\u6539\uff0c\u65e0\u6cd5\u9002\u5e94\u65b0\u9700\u6c42\uff0c\u90a3\u4ed6\u5c31\u50cf\u5df2\u7ecf\u5931\u53bb\u8fdb\u5316\u80fd\u529b\u7684\u751f\u7269\u79cd\u7fa4\uff0c\u5982\u300a\u4e09\u4f53\u300b\u4e16\u754c\u89c2\u4e2d\u201c\u5b89\u987f\u201d\u5230\u6fb3\u5927\u5229\u4e9a\u4fdd\u7559\u533a\u91cc\u201c\u7edd\u80b2\u201d\u7684\u4eba\u7c7b\uff0c\u88ab\u6dd8\u6c70\u53ea\u662f\u65f6\u95f4\u95ee\u9898\u3002 \u5982\u679c\u6211\u4eec\u80fd\u5728 \u5199\u4ee3\u7801 \u9636\u6bb5\uff0c\u5c31\u628a\u7a0b\u5e8f\u51c6\u5907\u5f97 \u6613\u4e8e\u540e\u7eed\u4fee\u6539 \uff0c\u90a3\u5c31\u53ef\u4ee5\u5728\u540e\u7eed 90% \u7684 \u6539\u4ee3\u7801 \u9636\u6bb5\u7701\u4e0b\u65e0\u6570\u65f6\u95f4\u3002 \u5982\u4f55\u8ba9\u4ee3\u7801\u6613\u4e8e\u4fee\u6539\uff1f\u524d\u4eba\u603b\u7ed3\u51fa\u4e00\u7cfb\u5217\u5e38\u7528\u7684\u5199\u6cd5\uff0c\u8fd9\u7c7b\u5199\u6cd5\u6709\u52a9\u4e8e\u8ba9\u540e\u7eed\u4fee\u6539\u66f4\u5bb9\u6613\uff0c\u5404\u81ea\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u5408\uff0c\u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u3002 \u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u6700\u57fa\u7840\u7684\u4e00\u70b9\uff0c\u5c31\u662f\u907f\u514d\u91cd\u590d\uff01 \u5f53\u4f60\u6709\u5f88\u591a\u5730\u65b9\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\u65f6\uff0c\u4e00\u65e6\u9700\u8981\u6d89\u53ca\u4fee\u6539\u8fd9\u90e8\u5206\u903b\u8f91\u65f6\uff0c\u5c31\u9700\u8981\u5230\u6bcf\u4e00\u4e2a\u51fa\u73b0\u4e86\u8fd9\u4e2a\u903b\u8f91\u7684\u4ee3\u7801\u4e2d\uff0c\u53bb\u9010\u4e00\u4fee\u6539\u3002 \u4f8b\u5982\u4f60\u7684\u540d\u5b57\uff0c\u5728\u51fa\u751f\u8bc1\uff0c\u8eab\u4efd\u8bc1\uff0c\u5b66\u751f\u8bc1\uff0c\u6bd5\u4e1a\u8bc1\uff0c\u623f\u4ea7\u8bc1\uff0c\u9a7e\u9a76\u8bc1\uff0c\u5404\u79cd\u5730\u65b9\u90fd\u51fa\u73b0\u4e86\u3002\u90a3\u4e48\u4f60\u8981\u6539\u540d\u7684\u8bdd\uff0c\u6240\u6709\u8fd9\u4e9b\u8bc1\u4ef6\u90fd\u9700\u8981\u91cd\u65b0\u5370\u5237\uff01\u5982\u679c\u80fd\u628a\u4ed6\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u201c\u7edf\u4e00\u8bc1\u201d\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u4fee\u6539\u201c\u7edf\u4e00\u8bc1\u201d\u4e0a\u7684\u540d\u5b57\u5c31\u884c\u4e86\u3002 \u4e0d\u8fc7\uff0c\u73b0\u5b9e\u4e2d\u5e76\u6ca1\u6709\u9891\u7e41\u6539\u540d\u5b57\u7684\u9700\u6c42\uff0c\u8fd9\u8bf4\u660e\uff1a \u5bf9\u4e8e\u4e0d\u5e38\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u53ef\u4ee5\u5bb9\u5fcd\u4e00\u5b9a\u7684\u91cd\u590d\u3002 \u8d8a\u662f\u672a\u6765\u6709\u53ef\u80fd\u4fee\u6539\u7684\uff0c\u5c31\u8d8a\u9700\u8981\u8bbe\u8ba1\u6a21\u5f0f\u964d\u91cd\uff01 \u4f8b\u5982\u6570\u5b66\u5e38\u6570 PI = 3.1415926535897\uff0c\u8fd9\u8f88\u5b50\u90fd\u4e0d\u53ef\u80fd\u51fa\u73b0\u4fee\u6539\u7684\u9700\u6c42\uff0c\u90a3\u5199\u6b7b\u4e5f\u6ca1\u5173\u7cfb\u3002\u5982\u679c\u8981\u628a PI \u5b9a\u4e49\u6210\u5b8f\uff0c\u53ea\u662f\u51fa\u4e8e\u201c\u8bb0\u4e0d\u4f4f\u201d\u201c\u5199\u8d77\u6765\u592a\u957f\u4e86\u201d\u201c\u590d\u5236\u7c98\u8d34\u9ebb\u70e6\u201d\u3002\u6240\u4ee5\u5bf9\u4e8e PI \u8fd9\u79cd\u4e0d\u4f1a\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u964d\u91cd\u53ea\u662f\u589e\u52a0 \u53ef\u8bfb\u6027 \uff0c\u800c\u4e0d\u662f \u53ef\u4fee\u6539\u6027 \u3002 \u4f46\u662f\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\uff01\u9700\u6c42\u7684\u5343\u53d8\u4e07\u5316\u603b\u662f\u8d85\u51fa\u4f60\u7684\u60f3\u8c61\u3002 \u4f8b\u5982\u4f60\u505a\u4e86\u4e00\u4e2a\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u6e38\u620f\uff0c\u9700\u8981\u7528\u5230\u91cd\u529b\u52a0\u901f\u5ea6 g = 9.8\uff0c\u4f60\u60f3\u5f53\u7136\u8ba4\u4e3a g \u4ee5\u540e\u4e0d\u53ef\u80fd\u4fee\u6539\u3002\u8001\u677f\u4e5f\u4fe1\u8a93\u65e6\u65e6\u5411\u4f60\u4fdd\u8bc1\uff1a\u201c\u6ca1\u4e8b\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u4e0d\u4f1a\u6539\u53d8\u3002\u201d\u4f60\u5c31\u5199\u6b7b\u5728\u4ee3\u7801\u91cc\u4e86\u3002 \u6ca1\u60f3\u5230\uff0c\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u8001\u677f\u7a81\u7136\u8981\u6c42\u4f60\u52a0\u5165\u201c\u6708\u7403\u7ae0\u201d\u5173\u5361\uff0c\u5728\u8fd9\u4e9b\u5173\u5361\u4e2d\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u662f g = 1.6\u3002 \u5982\u679c\u4f60\u4e00\u5f00\u59cb\u5c31\u5df2\u7ecf\u628a g \u63d0\u53d6\u51fa\u6765\uff0c\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a struct Level { const double g = 9.8; void physics_sim() { bird.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f pig.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f } }; \u90a3\u4e48\u8981\u652f\u6301\u6708\u7403\u5173\u5361\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5c31\u53ef\u4ee5\u4e86\u3002 struct Level { double g; Level(Chapter chapter) { if (chapter == ChapterMoon) { g = 1.6; } else { g = 9.8; } } void physics_sim() { bird.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g pig.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g } }; \u5c0f\u5f6d\u8001\u5e08\u4e4b\u524d\u505a zeno \u65f6\uff0c\u8be2\u95ee\u8981\u4e0d\u8981\u628a\u6e32\u67d3\u7ba1\u7ebf\u8282\u70b9\u5316\uff0c\u65b9\u4fbf\u7528\u6237\u52a8\u6001\u7f16\u7a0b\uff1f\u5f20\u7329\u7329\u5c31\u662f\u4fe1\u8a93\u65e6\u65e6\u9053\uff1a\u201c\u6e32\u67d3\u662f\u4e00\u4e2a\u9ad8\u5ea6\u6210\u719f\u9886\u57df\uff0c\u4e0d\u4f1a\u6709\u591a\u5c11\u4fee\u6539\u9700\u6c42\u7684\u3002\u201d\u5c0f\u5f6d\u8001\u5e08\u9042\u5199\u6b7b\u4e86\u6e32\u67d3\u7ba1\u7ebf\uff0c\u4e13\u4e3a\u6027\u80fd\u6781\u5ea6\u4f18\u5316\uff0c\u51e0\u4e2a\u6708\u540e\uff0c\u5f20\u7329\u7329\u7f9e\u7b54\u7b54\u627e\u5230\u5c0f\u5f6d\u8001\u5e08\uff1a\u201c\u5c0f\u5f6d\u8001\u5e08\uff0c\u90a3\u4e2a\uff0c\u6e32\u67d3\uff0c\u80fd\u4e0d\u80fd\u6539\u6210\u8282\u70b9\u554a\u2026\u2026\u201d\u3002\u8fd9\u4e2a\u6545\u4e8b\u544a\u8bc9\u6211\u4eec\uff0c\u7532\u65b9\u7684\u4fe1\u8a93\u65e6\u65e6\u653e\u7684\u4e00\u4e2a\u5c41\u90fd\u4e0d\u80fd\u4fe1\u3002 \u7528\u51fd\u6570\u5c01\u88c5 \u51fd\u6570\u5c31\u662f\u6765\u5e2e\u4f60\u89e3\u51b3\u4ee3\u7801\u91cd\u590d\u95ee\u9898\u7684\uff01\u8981\u9886\uff1a \u628a\u5171\u540c\u7684\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002 void sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } fmt::println(\"sum of v = {}\", s); } int main() { std::vector a = {1, 2, 3, 4}; sum(a); std::vector b = {5, 6, 7, 8}; sum(b); return 0; } \u8fd9\u6837 main \u51fd\u6570\u91cc\u5c31\u53ef\u4ee5\u53ea\u5173\u5fc3\u8981\u6c42\u548c\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u7528\u5173\u5fc3\u6c42\u548c\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u4e86\u3002\u4e8b\u540e\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u628a sum \u7684\u5185\u5bb9\u5077\u5077\u6362\u6389\uff0c\u6362\u6210\u5e76\u884c\u7684\u7b97\u6cd5\uff0cmain \u4e5f\u4e0d\u7528\u77e5\u9053\u3002\u8fd9\u5c31\u662f \u5c01\u88c5 \uff0c\u53ef\u4ee5\u628a\u91cd\u590d\u7684\u516c\u5171\u90e8\u5206\u62bd\u53d6\u51fa\u6765\uff0c\u65b9\u4fbf\u4ee5\u540e\u4fee\u6539\u4ee3\u7801\u3002 sum \u51fd\u6570\u76f8\u5f53\u4e8e\uff0c\u5f53\u9700\u8981\u5439\u7a7a\u8c03\u65f6\uff0c\u63d2\u4e0a\u7a7a\u8c03\u63d2\u5ea7\u3002\u5f53\u9700\u8981\u7ed9\u624b\u673a\u5145\u7535\u65f6\uff0c\u63d2\u4e0a\u624b\u673a\u5145\u7535\u5668\u3002\u4f60\u4e0d\u9700\u8981\u5173\u5fc3\u63d2\u5ea7\u91cc\u7684\u7535\u54ea\u91cc\u6765\uff0c\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4f1a\u66ff\u4f60\u60f3\u529e\u6cd5\u89e3\u51b3\uff0c\u60f3\u529e\u6cd5\u4f18\u5316\uff0c\u60f3\u529e\u6cd5\u5347\u7ea7\u5230\u7eff\u8272\u80fd\u6e90\u3002\u4f60\u53ea\u9700\u8981\u5439\u7740\u7a7a\u8c03\u7ed9\u4f60\u6b63\u5728\u5f00\u53d1\u7684\u624b\u673a App \u4f18\u5316\u5c31\u884c\u4e86\uff0c\u5927\u5927\u51cf\u8f7b\u7a0b\u5e8f\u5458\u5fc3\u667a\u8d1f\u62c5\u3002 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u4f46\u662f\uff01\u8fd9\u6bb5\u4ee3\u7801\u4ecd\u7136\u6709\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u628a sum \u6c42\u548c\u7684\u7ed3\u679c\uff0c\u76f4\u63a5\u5728 sum \u91cc\u6253\u5370\u4e86\u51fa\u6765\u3002sum \u91cc\u5199\u6b7b\u4e86\uff0c\u6c42\u5b8c\u548c\u4e4b\u540e\u53ea\u80fd\u76f4\u63a5\u6253\u5370\uff0c\u8c03\u7528\u8005 main \u6839\u672c\u65e0\u6cd5\u63a7\u5236\u3002 \u8fd9\u662f\u4e00\u79cd\u9519\u8bef\u7684\u5c01\u88c5\uff0c\u6216\u8005\u8bf4\uff0c\u5c01\u88c5\u8fc7\u5934\u4e86\u3002 \u4f60\u628a\u624b\u673a\u5145\u7535\u5668 (fmt::println) \u710a\u6b7b\u5728\u4e86\u63d2\u5ea7 (sum) \u4e0a\uff0c\u73b0\u5728\u8fd9\u4e2a\u63d2\u5ea7\u53ea\u80fd\u7ed9\u624b\u673a\u5145\u7535 (\u7528\u4e8e\u76f4\u63a5\u6253\u5370) \u4e86\uff0c\u4e0d\u80fd\u7ed9\u7b14\u8bb0\u672c\u7535\u8111\u5145\u7535 (\u6c42\u548c\u7ed3\u679c\u4e0d\u76f4\u63a5\u7528\u4e8e\u6253\u5370) \u4e86\uff01\u5c3d\u7ba1\u901a\u8fc7\u66f4\u6362\u5145\u7535\u7ebf (\u53c2\u6570 v)\uff0c\u8fd8\u53ef\u4ee5\u652f\u6301\u652f\u6301\u5b89\u5353 (a) \u548c\u82f9\u679c (b) \u4e24\u79cd\u624b\u673a\u7684\u5145\u7535\uff0c\u4f46\u8fd9\u6837\u710a\u6b7b\u7684\u63d2\u5ea7\u5df2\u7ecf\u548c\u7b14\u8bb0\u672c\u7535\u8111\u65e0\u7f18\u4e86\u3002 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u5f88\u660e\u663e\uff0c\u201c\u6253\u5370\u201d\u548c\u201c\u6c42\u548c\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u64cd\u4f5c\uff0c\u4e0d\u5e94\u8be5\u710a\u6b7b\u5728\u4e00\u5757\u3002 sum \u51fd\u6570\u7684\u672c\u804c\u5de5\u4f5c\u662f\u201c\u6570\u7ec4\u6c42\u548c\u201d\uff0c\u4e0d\u5e94\u8be5\u9644\u8d60\u6253\u5370\u529f\u80fd\u3002 sum \u8ba1\u7b97\u51fa\u6c42\u548c\u7ed3\u679c\u540e\uff0c\u76f4\u63a5 return \u5373\u53ef\u3002 \u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u7ed3\u679c\uff0c\u662f\u8c03\u7528\u8005 main \u7684\u4e8b\uff0c\u6b63\u5982\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4e0d\u4f1a\u7ba1\u4f60\u7528\u4ed6\u63d0\u4f9b\u7684\u7535\u6765\u5439\u7a7a\u8c03\u8fd8\u662f\u73a9\u6e38\u620f\u4e00\u6837\uff0c\u53ea\u8981\u4e0d\u59a8\u788d\u5230\u5176\u4ed6\u5c45\u6c11\u7684\u6b63\u5e38\u7528\u7535\u3002 int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum of a = {}\", sum(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"sum of b = {}\", sum(b)); return 0; } \u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8bf4\u7684 \u804c\u8d23\u5355\u4e00\u539f\u5219 \u3002 \u4e8c\u6b21\u5c01\u88c5 \u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e2a\u51fd\u6570 average\uff0c\u4ed6\u53ef\u4ee5\u57fa\u4e8e sum \u5b9e\u73b0\uff1a int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } double average(std::vector const &v) { return (double)sum(v) / v.size(); } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"average of a = {}\", average(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"average of b = {}\", average(b)); return 0; } \u8fdb\u4e00\u6b65\u5c01\u88c5\u4e00\u4e2a\u6253\u5370\u6570\u7ec4\u6240\u6709\u7edf\u8ba1\u5b66\u4fe1\u606f\u7684\u51fd\u6570\uff1a void print_statistics(std::vector const &v) { if (v.empty()) { fmt::println(\"this is empty...\"); } else { fmt::println(\"sum: {}\", sum(v)); fmt::println(\"average: {}\", average(v)); fmt::println(\"min: {}\", min(v)); fmt::println(\"max: {}\", max(v)); } } int main() { std::vector a = {1, 2, 3, 4}; print_statistics(a); std::vector b = {5, 6, 7, 8}; print_statistics(b); return 0; } \u66b4\u9732 API \u65f6\uff0c\u8981\u540c\u65f6\u63d0\u4f9b\u5e95\u5c42\u7684 API \u548c\u9ad8\u5c42\u5c01\u88c5\u7684 API\u3002\u7528\u6237\u5982\u679c\u60f3\u8981\u63a7\u5236\u66f4\u591a\u7ec6\u8282\u53ef\u4ee5\u8c03\u7528\u5e95\u5c42 API\uff0c\u60f3\u8981\u7701\u4e8b\u7684\u7528\u6237\u53ef\u4ee5\u8c03\u7528\u9ad8\u5c42\u5c01\u88c5\u597d\u7684 API\u3002 \u9ad8\u5c42\u5c01\u88c5 API \u5e94\u5f53\u53ef\u4ee5\u5b8c\u5168\u901a\u8fc7\u8c03\u7528\u5e95\u5c42 API \u5b9e\u73b0\uff0c\u63d0\u4f9b\u9ad8\u5c42 API \u53ea\u662f\u65b9\u4fbf\u521d\u7ea7\u7528\u6237\u4f7f\u7528\u548c\u7406\u89e3\u3002 \u4f8b\u5982 libcurl \u5c31\u63d0\u4f9b\u4e86 curl_easy \u548c curl_multi \u4e24\u5957 API\u3002 - `curl_multi` \u63d0\u4f9b\u4e86\u8d85\u8be6\u7ec6\u7684\u53c2\u6570\uff0c\u628a\u6bcf\u4e2a\u64cd\u4f5c\u5206\u62c6\u6210\u591a\u6b65\uff0c\u65b9\u4fbf\u7528\u6237\u63d2\u624b\u7ec6\u8282\uff0c\u6ee1\u8db3\u9ad8\u7ea7\u7528\u6237\u7684\u5b9a\u5236\u5316\u9700\u6c42\uff0c\u4f46\u592a\u8fc7\u590d\u6742\uff0c\u96be\u4ee5\u5b66\u4e60\u3002 - `curl_easy` \u662f\u5bf9 `curl_multi` \u7684\u518d\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684 API\uff0c\u4f46\u662f\u5bf9\u5177\u4f53\u7ec6\u8282\u5c31\u96be\u4ee5\u64cd\u63a7\u4e86\uff0c\u9002\u5408\u521d\u5b66\u8005\u4e0a\u624b\u3002 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c Linux \u5185\u6838\u4e3a\u4ec0\u4e48\u575a\u6301\u4f7f\u7528 TAB=8 \u4e3a\u4ee3\u7801\u98ce\u683c\uff1f TODO\uff1a\u8fd8\u5728\u5199 \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u4f60\u4ea7\u751f\u4e86\u4e24\u4e2a\u9700\u6c42\uff0c\u5206\u522b\u5c01\u88c5\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a sum \u6c42\u6240\u6709\u5143\u7d20\u7684\u548c product \u6c42\u6240\u6709\u5143\u7d20\u7684\u79ef int sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret += v[i]; } return ret; } int product(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret *= v[i]; } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", sum(a)); fmt::println(\"product: {}\", product(a)); return 0; } \u6ce8\u610f\u5230 sum \u548c product \u7684\u5185\u5bb9\u51e0\u4e4e\u5982\u51fa\u4e00\u8f99\uff0c\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff1a sum \u7684\u5faa\u73af\u4f53\u4e3a += \uff1b product \u7684\u5faa\u73af\u4f53\u4e3a *= \u3002 \u8fd9\u79cd\u51fd\u6570\u4f53\u5185\u6709\u90e8\u5206\u4ee3\u7801\u91cd\u590d\uff0c\u4f46\u53c8\u6709\u7279\u5b9a\u90e8\u5206\u4e0d\u540c\uff0c\u96be\u4ee5\u62bd\u79bb\u3002 \u8be5\u600e\u4e48\u590d\u7528\u8fd9\u91cd\u590d\u7684\u90e8\u5206\u4ee3\u7801\u5462\uff1f \u6211\u4eec\u8981\u628a sum \u548c product \u5408\u5e76\u6210\u4e00\u4e2a\u51fd\u6570 generic_sum \u3002\u7136\u540e\u901a\u8fc7\u51fd\u6570\u53c2\u6570\uff0c\u628a\u5dee\u5f02\u90e8\u5206\uff080\u3001 += \uff09\u201c\u6ce8\u5165\u201d\u5230\u4e24\u4e2a\u51fd\u6570\u539f\u672c\u4e0d\u540c\u5730\u65b9\u3002 \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u5982\u4f55\u8868\u793a\u6211\u8fd9\u4e2a\u51fd\u6570\u662f\u8981\u505a\u6c42\u548c += \u8fd8\u662f\u6c42\u79ef *= \uff1f \u8ba9\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\uff1a enum Mode { ADD, // \u6c42\u548c\u64cd\u4f5c MUL, // \u6c42\u79ef\u64cd\u4f5c }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { // \u51fd\u6570\u5185\u5224\u65ad\u679a\u4e3e\uff0c\u51b3\u5b9a\u8981\u505a\u4ec0\u4e48\u64cd\u4f5c ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", generic_sum(a, ADD)); // \u7528\u6237\u6307\u5b9a\u4ed6\u60f3\u8981\u7684\u64cd\u4f5c fmt::println(\"product: {}\", generic_sum(a, MUL)); return 0; } \u7136\u800c\uff0c\u5982\u679c\u7528\u6237\u73b0\u5728\u60f3\u8981\u6c42\u6570\u7ec4\u7684 \u6700\u5927\u503c \u5462\uff1f \u679a\u4e3e\u4e2d\u8fd8\u6ca1\u6709\u5b9e\u73b0\u6700\u5927\u503c\u7684\u64cd\u4f5c\u2026\u2026\u8981\u652f\u6301\uff0c\u5c31\u5f97\u624b\u5fd9\u811a\u4e71\u5730\u53bb\u4fee\u6539 generic_sum \u51fd\u6570\u548c Mode \u679a\u4e3e\u539f\u672c\u7684\u5b9a\u4e49\uff0c\u771f\u9ebb\u70e6\uff01 enum Mode { ADD, MUL, MAX, // ***\u6539*** }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } else if (mode == MAX) { // ***\u6539*** ret = std::max(ret, v[i]); // ***\u6539*** } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, MAX); // ***\u6539*** return 0; } \u6211\u7528 // ***\u6539*** \u6307\u793a\u4e86\u6240\u6709\u9700\u8981\u6539\u52a8\u7684\u5730\u65b9\u3002 \u4e3a\u4e86\u589e\u52a0\u4e00\u4e2a\u6c42\u6700\u5927\u503c\u7684\u64cd\u4f5c\uff0c\u5c31\u9700\u8981\u4e09\u5904\u5206\u6563\u5728\u5404\u5730\u7684\u6539\u52a8\uff01 \u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u5bb9\u6613\u6284\u6f0f\uff0c\u6284\u9519\uff0c\u6bd4\u5982 MAX \u4e0d\u5c0f\u5fc3\u6253\u9519\u6210 MUL \u4e86\uff0c\u81ea\u5df1\u5374\u6ca1\u53d1\u73b0\uff0c\u7559\u4e0b BUG \u9690\u60a3\u3002 \u8fd9\u6837\u5199\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u5fc3\u667a\u8d1f\u62c5\u6781\u5927\uff0c\u6574\u5929\u5c31\u63d0\u5fc3\u540a\u80c6\u7740\u4e1c\u4e00\u5757\uff0c\u897f\u4e00\u5757\u7684\u6563\u88c5\u4ee3\u7801\uff0c\u62c5\u5fc3\u7740\u6709\u6ca1\u6709\u54ea\u4e2a\u5730\u65b9\u5199\u9519\u5199\u6f0f\uff0c\u4e25\u91cd\u59a8\u788d\u4e86\u5f00\u53d1\u6548\u7387\u3002 \u5e76\u4e14\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e0d\u80fd\u9002\u5e94\u9700\u6c42\u7684\u53d8\u5316\uff1a\u5047\u5982\u6211\u9700\u8981\u652f\u6301 MIN \u5462\uff1f\u53c8\u5f97\u6539\u4e09\u4e2a\u5730\u65b9\uff01\u8fd9\u8fdd\u80cc\u4e86\u8bbe\u8ba1\u6a21\u5f0f\u7684 \u5f00\u95ed\u539f\u5219 \u3002 \u5f00\u95ed\u539f\u5219: \u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\u3002\u6307\u7684\u662f\u8f6f\u4ef6\u5728\u9002\u5e94\u9700\u6c42\u53d8\u5316\u65f6\uff0c\u5e94\u5c3d\u91cf\u901a\u8fc7 \u6269\u5c55\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 \u4fee\u6539\u5df2\u6709\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\u3002 \u4f7f\u7528\u679a\u4e3e\u548c if-else \u5b9e\u73b0\u591a\u6001\uff0c\u96be\u4ee5\u6269\u5c55\uff0c\u8fd8\u8981\u4e00\u76f4\u53bb\u4fee\u6539\u539f\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u5c31\u8fdd\u80cc\u4e86 \u5f00\u95ed\u539f\u5219 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u5982\u679c\u6211\u4eec\u53ef\u4ee5\u201c\u6ce8\u5165\u201d\u4ee3\u7801\u5c31\u597d\u4e86\uff01\u80fd\u5426\u628a\u4e00\u6bb5\u201c\u4ee3\u7801\u201d\u4f5c\u4e3a generic_sum \u51fd\u6570\u7684\u53c2\u6570\u5462\uff1f \u4ee3\u7801\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u51fd\u6570\uff0c\u6ce8\u5165\u4ee3\u7801\u5c31\u662f\u6ce8\u5165\u51fd\u6570\u3002\u6211\u4eec\u5148\u5b9a\u4e49\u51fa\u4e09\u4e2a\u4e0d\u540c\u64cd\u4f5c\u5bf9\u5e94\u7684\u51fd\u6570\uff1a int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } \u7136\u540e\uff0c\u628a\u8fd9\u4e09\u4e2a\u5c0f\u51fd\u6570\uff0c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5927\u51fd\u6570 generic_sum \u7684\u53c2\u6570\u5c31\u884c\uff01 int generic_sum(std::vector const &v, auto op) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { // \u51fd\u6570\u4f5c\u8005\u65e0\u9700\u4e86\u89e3\u7528\u6237\u6307\u5b9a\u7684\u201c\u64cd\u4f5c\u201d\u5177\u4f53\u662f\u4ec0\u4e48 // \u53ea\u9700\u8981\u8c03\u7528\u8fd9\u4e00\u201c\u64cd\u4f5c\u201d\uff0c\u5f97\u5230\u7ed3\u679c\u5c31\u884c ret = op(ret, v[i]); } return ret; } int main() { std::vector a = {1, 2, 3, 4}; // \u7528\u6237\u65e0\u9700\u5173\u5fc3\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u4ec0\u4e48 // \u53ea\u9700\u968f\u5fc3\u6240\u6b32\u6307\u5b9a\u4ed6\u7684\u201c\u64cd\u4f5c\u201d\u4f5c\u4e3a\u53c2\u6570 generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u8d23\u4efb\u660e\u786e\u4e86\uff0c\u6211\u4eec\u6210\u529f\u628a\u4e00\u90e8\u5206\u7ec6\u8282\u4ece generic_sum \u4e2d\u8fdb\u4e00\u6b65\u62bd\u79bb\u3002 \u5e93\u4f5c\u8005 generic_sum \u4e0d\u5fc5\u4e86\u89e3 main \u7684\u64cd\u4f5c\u5177\u4f53\u662f\u4ec0\u4e48\uff0c\u4ed6\u53ea\u8d1f\u8d23\u5229\u7528\u8fd9\u4e2a\u64cd\u4f5c\u6c42\u201c\u548c\u201d\u3002 \u5e93\u7528\u6237 main \u4e0d\u5fc5\u4e86\u89e3 generic_sum \u5982\u4f55\u5b9e\u73b0\u64cd\u4f5c\u7d2f\u52a0\uff0c\u4ed6\u53ea\u7ba1\u6ce8\u5165\u201c\u5982\u4f55\u64cd\u4f5c\u201d\u7684\u4ee3\u7801\uff0c\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u3002 \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 int generic_sum(std::vector const &v, auto op) P } \u8fd9\u91cc\u7684\u53c2\u6570 op \u7c7b\u578b\u58f0\u660e\u4e3a auto\uff0c\u6548\u679c\u5c31\u662f\uff0cop \u8fd9\u4e2a\u53c2\u6570\u73b0\u5728\u80fd\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u4e86\uff08\u5305\u62ec\u51fd\u6570\uff01\uff09 int generic_sum(std::vector const &v, auto op) { ... } \u51c6\u786e\u7684\u8bf4\uff0c auto op \u53c2\u6570\u7684\u6548\u679c\u662f\u4f7f generic_sum \u53d8\u4e3a\u4e00\u4e2a \u6a21\u677f\u51fd\u6570 \uff0c\u5176\u4e2d op \u53c2\u6570\u53d8\u6210\u4e86\u6a21\u677f\u53c2\u6570\uff0c\u80fd\u591f\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u4e86\u3002\u800c\u5199\u660e\u7c7b\u578b\u7684\u53c2\u6570 std::vector const &v \u5c31\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u6548\u679c\uff0c\u5c31\u53ea\u80fd\u63a5\u53d7 vector \u800c\u5df2\u3002 \u5982\u679c\u4f60\u4e0d\u652f\u6301 C++20 \u7684\u8bdd\uff0c\u9700\u8981\u663e\u5f0f\u5199\u51fa template \uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff1a template int generic_sum(std::vector const &v, Op op) { ... } C++11\uff1aauto \u53ea\u80fd\u7528\u4e8e\u5b9a\u4e49\u53d8\u91cf\uff1bC++14\uff1a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u662f auto\uff1bC++17\uff1a\u6a21\u677f\u53c2\u6570\u4e5f\u53ef\u4ee5 auto\uff1bC++20\uff1a\u51fd\u6570\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f auto \u4e86\uff1b\uff08\u72c2\u60f3\uff09C++47\uff1aauto \u73b0\u5728\u662f C++47 \u7684\u552f\u4e00\u5173\u952e\u5b57\uff0c\u7528\u6237\u53ea\u9700\u4e0d\u65ad\u8f93\u5165 auto-auto-auto\uff0c\u7f16\u8bd1\u5668\u5185\u5efa\u4eba\u5de5\u667a\u80fd\u81ea\u52a8\u8bc6\u522b\u4f60\u7684\u610f\u56fe\u751f\u6210\u673a\u5668\u7801\u3002 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 \u5728\u8fc7\u53bb\u7684 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f \u4e2d\uff0c\u51fd\u6570\uff08\u4ee3\u7801\uff09\u548c\u5bf9\u8c61\uff08\u6570\u636e\uff09\u88ab \u5272\u88c2 \u5f00\u6765\uff0c\u4ed6\u4eec\u611a\u6627\u5730\u8ba4\u4e3a \u51fd\u6570\u4e0d\u662f\u5bf9\u8c61 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f \u5219\u8ba4\u4e3a\uff1a \u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u53d8\u91cf\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\uff01 Function lives matter! \u9762\u5411\u5bf9\u8c61\u5c31\u597d\u6bd4\u8ba1\u7b97\u673a\u7684\u201c\u54c8\u4f5b\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u548c\u6570\u636e\u5272\u88c2\uff0c\u4ee3\u7801\u53ea\u80fd\u5355\u65b9\u9762\u64cd\u4f5c\u6570\u636e\u3002\u51fd\u6570\u5f0f\u5c31\u597d\u6bd4\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u4e5f\u662f\u6570\u636e\u3002\u770b\u4f3c\u4f1a\u5bfc\u81f4\u4f4e\u6548\uff0c\u5b9e\u5219\u5927\u5927\u65b9\u4fbf\u4e86\u52a8\u6001\u52a0\u8f7d\u65b0\u7a0b\u5e8f\uff0c\u56e0\u800c\u73b0\u5728\u7684\u8ba1\u7b97\u673a\u57fa\u672c\u90fd\u91c7\u7528\u4e86\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\u3002 \u603b\u4e4b\uff0c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u88ab\u4eb2\u5207\u5730\u5c0a\u79f0\u4e3a \u51fd\u6570\u5bf9\u8c61 \u3002 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 C++98 \u65f6\u4ee3\uff0c\u4eba\u4eec\u8fd8\u9700\u8981\u5355\u72ec\u8dd1\u5230 main \u5916\u9762\uff0c\u4e13\u95e8\u5b9a\u4e49 add \u3001 mul \u3001 max \u51fd\u6570\u3002\u5f04\u5f97\u6574\u4e2a\u4ee3\u7801\u4e71\u54c4\u54c4\u7684\uff0c\u975e\u5e38\u9ebb\u70e6\u3002 int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } C++11 \u5f15\u5165\u4e86 Lambda \u8868\u8fbe\u5f0f \u8bed\u6cd5\uff0c\u5141\u8bb8\u4f60\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002 int main() { std::vector a = {1, 2, 3, 4}; auto add = [](int a, int b) { return a + b; }; auto mul = [](int a, int b) { return a * b; }; auto max = [](int a, int b) { return std::max(a, b); }; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u4e0d\u7528\u5f80 main \u5916\u9762\u585e\u5783\u573e\u4e86\uff0c\u4e00\u6e05\u723d\u3002 \u66f4\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u751a\u81f3\u4e0d\u7528\u5b9a\u4e49\u53d8\u91cf\uff0c\u76f4\u63a5\u628a Lambda \u8868\u8fbe\u5f0f\u5199\u5728 generic_sum \u7684\u53c2\u6570\u91cc\u5c31\u884c\u4e86\uff01 int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); // ***\u6539*** return 0; } \u4ee5\u4e0a\u5199\u6cd5\u90fd\u662f\u7b49\u4ef7\u7684\u3002 \u8981\u652f\u6301\u4e00\u4e2a\u65b0\u64cd\u4f5c\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5730\u65b9\uff1a\u5728\u8c03\u7528 generic_sum \u65f6\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002\u968f\u53eb\u968f\u5230\uff0c\u4e0d\u7528\u7ea0\u7ed3\u4e8e\u201c\u8d77\u540d\u5f3a\u8feb\u75c7\u201d\uff0c\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u5462\uff1f \u51c6\u786e\u7684\u8bf4\uff0cLambda \u521b\u5efa\u7684\u662f\u51fd\u6570\u5bf9\u8c61 (function object) \u6216\u79f0\u4eff\u51fd\u6570 (functor) \u800c\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u3002 \u5176\u5b9e C++98 \u65f6\u4ee3\u4eba\u4eec\u5c31\u5df2\u7ecf\u5927\u91cf\u5728\u7528 operator()() \u6a21\u62df\u51fd\u6570\u5bf9\u8c61\u4e86\uff0c\u8457\u540d\u7684\u7b2c\u4e09\u65b9\u5e93 Boost \u4e5f\u5c01\u88c5\u4e86\u5404\u79cd\u51fd\u6570\u5f0f\u5e38\u7528\u7684\u5bb9\u5668\u548c\u5de5\u5177\u3002C++11 \u624d\u7ec8\u4e8e\u628a \u51fd\u6570\u5bf9\u8c61 \u8fd9\u4e2a\u6982\u5ff5\u8f6c\u6b63\uff0c\u5e76\u5f15\u5165\u4e86\u66f4\u65b9\u4fbf\u7684 Lambda \u8bed\u6cd5\u7cd6\u3002 \u5373\u4f7f\u662f\u9762\u5411\u5bf9\u8c61\u7684\u5934\u53f7\u5b5d\u5b50 Java\uff0c\u4e5f\u5df2\u7ecf\u5f00\u59cb\u5f15\u5165\u51fd\u6570\u5f0f\u7684 Lambda \u8bed\u6cd5\u7cd6\uff0cC# \u7684 LINQ \u66f4\u662f\u660e\u76ee\u5f20\u80c6\u7684\u81f4\u656c map-reduce \u5168\u5bb6\u6876\uff0c\u751a\u81f3 C \u8bed\u8a00\u7528\u6237\u4e5f\u5f00\u59cb\u73a9\u5404\u79cd\u51fd\u6570\u6307\u9488\u56de\u8c03\u2026\u2026\u6ca1\u529e\u6cd5\uff0c\u51fd\u6570\u5f0f\u786e\u5b9e\u65b9\u4fbf\u5440\uff01 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u51fd\u6570\u5bf9\u8c61 op \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8ba9 generic_sum \u5185\u90e8\u53bb\u8c03\u7528\uff0c\u5c31\u50cf\u5f80 generic_sum \u4f53\u5185\u201c\u6ce8\u5165\u201d\u4e86\u4e00\u6bb5\u81ea\u5b9a\u4e49\u4ee3\u7801\u4e00\u6837\u3002 \u8fd9\u53ef\u4ee5\u8ba9 generic_sum \u5728\u4e0d\u4fee\u6539\u672c\u4f53\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u201c\u6ce8\u5165\u201d\u90e8\u5206\uff0c\u8f7b\u677e\u6269\u5c55\uff0c\u6ee1\u8db3 \u5f00\u95ed\u539f\u5219 \u3002 \u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u8fd9\u4f53\u73b0\u7684\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8981\u6c42\u7684 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u3002 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219: \u4e00\u4e2a\u5c01\u88c5\u597d\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u56db\u5927\u7f16\u7a0b\u8303\u5f0f\u90fd\u5404\u81ea\u53d1\u5c55\u51fa\u4e86 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u7684\u89e3\u51b3\u65b9\u6848\uff1a \u9762\u5411\u8fc7\u7a0b\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u6307\u9488 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u865a\u51fd\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u5bf9\u8c61 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u6a21\u677f\u5143\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u6a21\u677f\u53c2\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u540c\u6837\u662f\u628a\u62bd\u8c61\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u6837\u89e3\u51b3\u53ef\u6269\u5c55\u95ee\u9898\u3002 \u51fd\u6570\u6307\u9488\u8d34\u8fd1\u5e95\u5c42\u786c\u4ef6\uff0c\u865a\u51fd\u6570\u65b9\u4fbf\u6574\u5408\u591a\u4e2a\u63a5\u53e3\uff0c\u51fd\u6570\u5bf9\u8c61\u8f7b\u91cf\u7ea7\u3001\u968f\u5730\u53d6\u7528\uff0c\u6a21\u677f\u5143\u6709\u52a9\u9ad8\u6027\u80fd\u4f18\u5316\uff0c\u4e0d\u540c\u7684\u7f16\u7a0b\u8303\u5f0f\u6b8a\u9014\u540c\u5f52\u3002 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4f9d\u8d56\u6ce8\u5165\u539f\u5219\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u8026\u5408\u5ea6: \u6307\u7684\u662f\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u548c\u5176\u4ed6\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u8026\u5408\u5ea6\u8d8a\u4f4e\uff0c\u8d8a\u5bb9\u6613\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3001\u91cd\u6784\u3001\u590d\u7528\u548c\u6269\u5c55\u3002 \u9ad8\u8026\u5408\u5ea6\u7684\u5178\u578b\u662f\u201c\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab\u201d\u3002\u4f4e\u8026\u5408\u7684\u5178\u8303\u662f\u86af\u8693\uff0c\u56e0\u4e3a\u86af\u8693\u53ef\u4ee5\u5728\u4efb\u610f\u65ad\u9762\u5207\u5f00\uff0c\u8fd8\u80fd\u6d3b\u4e0b\u6765\uff0c\u770b\u6765\u86af\u8693\u7684\u8eab\u4f53\u8bbe\u8ba1\u975e\u5e38\u201c\u6a21\u5757\u5316\u201d\u5462\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u8f6f\u4ef6\u5e94\u5f53\u8ffd\u6c42\u4f4e\u8026\u5408\u5ea6\uff0c\u9002\u5ea6\u89e3\u8026\u7684\u8f6f\u4ef6\u80fd\u66f4\u5feb\u9002\u5e94\u9700\u6c42\u53d8\u5316\u3002\u4f46\u8fc7\u5ea6\u7684\u4f4e\u8026\u5408\u4e5f\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fc7\u4e8e\u5206\u6563\uff0c\u4e0d\u6613\u9605\u8bfb\u548c\u4fee\u6539\uff0c\u751a\u81f3\u53ef\u80fd\u8d77\u5230\u53cd\u6548\u679c\u3002 \u82e5\u4f60\u89e3\u8026\u540e\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u5316\u8981\u6539\u52a8\u7684\u5730\u65b9\u53d8\u5c11\u4e86\uff0c\u90a3\u5c31\u662f\u5408\u7406\u7684\u89e3\u8026\u3002\u82e5\u4f60\u8fc7\u5206\u89e3\u8026\uff0c\u4ee3\u7801\u4e1c\u4e00\u5757\u897f\u4e00\u5757\uff0c\u4ee5\u81f3\u4e8e\u9700\u6c42\u53d8\u5316\u65f6\u9700\u8981\u5230\u5904\u6539\uff0c\u6bd4\u4e0d\u89e3\u8026\u65f6\u6d6a\u8d39\u7684\u65f6\u95f4\u8fd8\u8981\u591a\uff0c\u90a3\u5c31\u662f\u89e3\u8026\u8fc7\u5ea6\u3002 \u5b8c\u5168\u96f6\u8026\u5408\u7684\u7a0b\u5e8f\u6bcf\u4e2a\u51fd\u6570\u4e92\u4e0d\u8054\u7cfb\uff0c\u5c31\u50cf\u628a\u86af\u8693\u62c6\u6563\u6210\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u7ec6\u80de\u4e00\u6837\u3002\u8fde\u521d\u59cb\u9700\u6c42\u201c\u6d3b\u7740\u201d\u90fd\u5b9e\u73b0\u4e0d\u4e86\uff0c\u8c08\u4f55\u9002\u5e94\u9700\u6c42\u53d8\u5316\uff1f\u6240\u4ee5\u89e3\u8026\u4e5f\u5207\u52ff\u77eb\u6789\u8fc7\u6b63\u3002 \u4e3a\u4e86\u907f\u514d\u89e3\u8026\u77eb\u6789\u8fc7\u6b63\uff0c\u4eba\u4eec\u53c8\u63d0\u51fa\u4e86\u5185\u805a\u7684\u6982\u5ff5\uff0c\u5e76\u89c4\u5b9a\u89e3\u8026\u7684\u524d\u63d0\u662f\uff1a\u4e0d\u803d\u8bef\u5185\u805a\u3002\u803d\u8bef\u5230\u5185\u805a\u7684\u89e3\u8026\uff0c\u5c31\u53ea\u4f1a\u8d77\u5230\u964d\u4f4e\u53ef\u7ef4\u62a4\u6027\u7684\u53cd\u6548\u679c\u4e86\u3002 \u5185\u805a: \u6307\u7684\u662f\u540c\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u5185\u90e8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u5185\u805a\u5ea6\u8d8a\u9ad8\uff0c\u529f\u80fd\u8d8a\u72ec\u7acb\uff0c\u8d8a\u65b9\u4fbf\u96c6\u4e2d\u7ef4\u62a4\u3002 \u4f8b\u5982\uff0c\u4eba\u7684\u5fc3\u810f\u4e13\u95e8\u8d1f\u8d23\u6cf5\u8840\uff0c\u809d\u810f\u53ea\u8d1f\u8d23\u89e3\u6bd2\uff0c\u8fd9\u5c31\u662f\u9ad8\u5185\u805a\u7684\u4eba\u4f53\u5668\u5b98\u3002\u82e5\u4eba\u7684\u5fc3\u810f\u8fd8\u8981\u517c\u804c\u89e3\u6bd2\uff0c\u809d\u810f\u8fd8\u517c\u804c\u6cf5\u8840\uff0c\u770b\u4f3c\u597d\u50cf\u662f\u589e\u52a0\u4e86\u201c\u4e07\u4e00\u5fc3\u810f\u574f\u6389\u201d\u7684\u5197\u4f59\u6027\uff0c\u5b9e\u9645\u4e0a\u628a\u201c\u6cf5\u8840\u201d\u8fd9\u4e00\u529f\u80fd\u62c6\u6563\u5230\u5404\u5730\uff0c\u65e0\u6cd5\u201c\u96c6\u4e2d\u529b\u91cf\u6cf5\u5927\u8840\u201d\u4e86\u3002 \u4eba\u7c7b\u7684\u5927\u8111\u548c CPU \u4e00\u6837\uff0c\u4e5f\u6709\u201c\u7f13\u5b58\u5c40\u57df\u6027 (cache-locality)\u201d\u7684\u9650\u5236\uff1a\u4e0d\u80fd\u540c\u65f6\u5728\u5f88\u591a\u4e2a\u4e3b\u9898\u4e4b\u95f4\u5feb\u901f\u5207\u6362\uff0c\u65e0\u8bba\u662f\u65f6\u95f4\u4e0a\u7684\u8fd8\u662f\u7a7a\u95f4\u4e0a\u7684\u5272\u88c2 (cache-miss)\uff0c\u90fd\u4f1a\u5e72\u6270\u7a0b\u5e8f\u5458\u601d\u7ef4\u7684\u8fde\u8d2f\u6027\uff0c\u4ece\u800c\u589e\u5927\u5fc3\u667a\u8d1f\u62c5\u3002 \u597d\u7684\u8f6f\u4ef6\u8981\u4fdd\u6301\u4f4e\u8026\u5408\uff0c\u540c\u65f6\u9ad8\u5185\u805a\u3002 \u5c31\u50cf\u201c\u6c11\u4e3b\u96c6\u4e2d\u5236\u201d\u4e00\u6837\uff0c\u65e2\u8981\u76d1\u7763\u9632\u6b62\u5927\u6743\u72ec\u63fd\uff0c\u53c8\u8981\u96c6\u4e2d\u529b\u91cf\u529e\u4e00\u4e2a\u4eba\u529e\u4e0d\u6210\u7684\u5927\u4e8b\u3002 \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u4f20\u7edf\u7684\u9762\u5411\u5bf9\u8c61\u540c\u6837\u53ef\u4ee5\u7528 \u865a\u51fd\u6570\u63a5\u53e3\u7c7b \u6a21\u62df \u51fd\u6570\u5bf9\u8c61 \u4e00\u6837\u7684\u529f\u80fd\uff0c\u53ea\u4e0d\u8fc7\u6ca1\u6709 lambda \u548c\u95ed\u5305\u7684\u8bed\u6cd5\u52a0\u6301\uff0c\u5199\u8d77\u6765\u975e\u5e38\u7e41\u7410\uff0c\u5c31\u548c\u5728 C \u8bed\u8a00\u91cc\u201c\u6a21\u62df\u201d\u9762\u5411\u5bf9\u8c61\u4e00\u6837\u3002 \u4e3a\u4e86\u8fd9\u4e48\u5c0f\u7684\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u5355\u72ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5c31\u50cf\u5988\u5988\u5f00\u4e00\u67b6\u201c\u7a7a\u4e2d\u6218\u8f66\u201d A380 \u53ea\u662f\u4e3a\u4e86\u63a5\u4f60\u653e\u5b66\u4e00\u6837\uff0c\u7b49\u4f60\u503c\u597d\u673a\u7684\u65f6\u95f4\u6211\u81ea\u5df1\u8d70\u90fd\u8d70\u5230\u4e86\u3002\u800c\u51fd\u6570\u5f0f\u4e2d\uff0c\u7528 lambda \u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e\u968f\u5730\u6293\u6765\u4e00\u53f0\u5171\u4eab\u5355\u8f66\u5f00\u8d70\u3002 struct OpBase { // \u9762\u5411\u5bf9\u8c61\uff1a\u9047\u4e8b\u4e0d\u51b3\u5148\u5b9a\u4e49\u63a5\u53e3\u2026\u2026 virtual int compute(int a, int b) = 0; virtual ~OpBase() = default; }; struct OpAdd : OpBase { int compute(int a, int b) override { return a + b; } }; struct OpMul : OpBase { int compute(int a, int b) override { return a * b; } }; struct OpMax : OpBase { int compute(int a, int b) override { return std::max(a, b); } }; int generic_sum(std::vector const &v, OpBase *op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op->compute(ret, v[i]); // \u5199\u8d77\u6765\u4e5f\u9ebb\u70e6\uff0c\u9700\u8981\u8c03\u7528\u4ed6\u7684\u6210\u5458\u51fd\u6570\uff0c\u6210\u5458\u51fd\u6570\u53c8\u8981\u8d77\u540d\u2026\u2026 } delete op; return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, new OpAdd()); generic_sum(a, new OpMul()); generic_sum(a, new OpMax()); return 0; } \u4e0d\u4ec5\u9700\u8981\u5b9a\u4e49\u4e00\u5806\u7c7b\uff0c\u63a5\u53e3\u7c7b\uff0c\u5b9e\u73b0\u7c7b\uff0c\u7ee7\u627f\u6765\u7ee7\u627f\u53bb\uff0c\u8fd8\u9700\u8981\u7ba1\u7406\u8ba8\u538c\u7684\u6307\u9488\uff0c\u4ee3\u7801\u91cf\u7ffb\u500d\uff0c\u6ca1\u4ec0\u4e48\u53ef\u8bfb\u6027\uff0c\u53c8\u5f71\u54cd\u8fd0\u884c\u6548\u7387\u3002 3 \u5e74 2 \u73ed\u5c0f\u5f6d\u540c\u5b66\uff0c\u4f60\u7684\u5988\u5988\u5f00\u7740 A380 \u6765\u63a5\u4f60\u4e86\u3002 \u800c\u73b0\u4ee3 C++ \u53ea\u9700 Lambda \u8bed\u6cd5\u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u723d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u521a\u521a\uff0c\u6211\u4eec\u7684\u5b9e\u73b0\u7528\u4e86 auto op \u505a\u53c2\u6570\uff0c\u8fd9\u7b49\u4ef7\u4e8e\u8ba9 generic_sum \u53d8\u6210\u4e00\u4e2a\u6a21\u677f\u51fd\u6570\u3002 int generic_sum(std::vector const &v, auto op); // \u4e0d\u652f\u6301 C++20 \u65f6\u7684\u66ff\u4ee3\u5199\u6cd5\uff1a template int generic_sum(std::vector const &v, Op op); \u8fd9\u610f\u5473\u7740\u6bcf\u5f53\u7528\u6237\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\uff08lambda\uff09\u65f6\uff0c generic_sum \u90fd\u4f1a\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u904d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u7f16\u8bd1\u540e\uff0c\u4f1a\u53d8\u6210\u7c7b\u4f3c\u4e8e\u8fd9\u6837\uff1a generic_sum(a); generic_sum(a); generic_sum(a); \u4f1a\u751f\u6210\u4e09\u4efd\u51fd\u6570\uff0c\u6bcf\u4e2a\u90fd\u662f\u72ec\u7acb\u7f16\u8bd1\u7684\uff1a int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = add(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = mul(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = max(ret, v[i]); } return ret; } \u8fd9\u5141\u8bb8\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e2a\u7248\u672c\u7684 generic_sum \u5355\u72ec\u505a\u4f18\u5316\uff0c\u91cf\u8eab\u5b9a\u5236\u6700\u4f18\u7684\u4ee3\u7801\u3002 \u4f8b\u5982 add \u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u56e0\u4e3a\u53ea\u5728 generic_sum \u4e2d\u4f7f\u7528\u4e86\uff0c\u4f1a\u88ab\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u5185\u8054\uff0c\u4e0d\u4f1a\u4ea7\u751f\u51fd\u6570\u8c03\u7528\u548c\u8df3\u8f6c\u7684\u6307\u4ee4\uff0c\u5404\u81ea\u4f18\u5316\u6210\u5355\u72ec\u4e00\u6761\u52a0\u6cd5 / \u4e58\u6cd5 / \u6700\u5927\u503c\u6307\u4ee4\u7b49\u3002 \u6bd4\u5982\uff0c\u7f16\u8bd1\u5668\u4f1a\u68c0\u6d4b\u5230 += \u53ef\u4ee5\u77e2\u91cf\u5316\uff0c\u4e8e\u662f\u7528 _mm_add_epi32 \u66ff\u4ee3\u4e86\u3002\u540c\u7406\uff0cmul \u5219\u7528 _mm_mullo_epi32 \u66ff\u4ee3\uff0cmax \u5219\u7528 _mm_max_epi32 \u66ff\u4ee3\u7b49\uff0c\u5404\u81ea\u5206\u522b\u751f\u6210\u4e86\u5404\u81ea\u7248\u672c\u6700\u4f18\u7684\u4ee3\u7801\u3002\u800c\u5982\u679c\u662f\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u4e0d\u4f1a\u751f\u6210\u4e09\u4efd\u91cf\u8eab\u5b9a\u505a\u7684\u5b9e\u4f8b\uff0c\u65e0\u6cd5\u77e2\u91cf\u5316\uff08\u6709\u4e00\u79cd\u4f8b\u5916\uff0c\u5c31\u662f\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u4e86 generic_sum \u4f3c\u4e4e\u53ea\u6709\u8fd9\u4e09\u79cd\u53ef\u80fd\u53c2\u6570\uff0c\u7136\u540e\u505a\u4e86 IPO \u4f18\u5316\uff0c\u4f46\u5e76\u4e0d\u5982\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u6837\u7a33\u5b9a\u5f3a\u5236\uff09\u3002 \u4e3a\u4e09\u79cd\u4e0d\u540c\u7684 op \u53c2\u6570\u5206\u522b\u5b9a\u505a\u4e09\u4efd\u3002\u867d\u7136\u589e\u52a0\u4e86\u7f16\u8bd1\u65f6\u95f4\uff0c\u81a8\u80c0\u4e86\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u4f53\u79ef\uff1b\u4f46\u751f\u6210\u7684\u673a\u5668\u7801\u662f\u5206\u522b\u9488\u5bf9\u6bcf\u79cd\u7279\u4f8b\u4e00\u5bf9\u4e00\u6df1\u5ea6\u4f18\u5316\u7684\uff0c\u66f4\u9ad8\u6548\u3002 \u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\uff08gemm\uff09\u7684\u6700\u4f18\u7b97\u6cd5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u77e9\u9635\u5927\u5c0f\u548c\u5f62\u72b6\u662f\u4e0d\u540c\u7684\u3002\u8457\u540d\u7684\u7ebf\u6027\u4ee3\u6570\u5e93 CUBLAS \u548c MKL \u4e2d\uff0c\u4f1a\u81ea\u52a8\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\u5f62\u72b6\uff0c\u9009\u53d6\u6700\u4f18\u7684\u7b97\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cCUBLAS \u5e93\u91cc\u5176\u5b9e\u5b58\u7740\u9002\u5408\u5404\u79cd\u77e9\u9635\u5927\u5c0f\u6392\u5217\u7ec4\u5408\u7684\u7b97\u6cd5\u4ee3\u7801\uff08\u4ee5 fatbin \u683c\u5f0f\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u4e2d\uff09\u3002\u5f53\u8c03\u7528\u77e9\u9635\u4e58\u6cd5\u65f6\uff0c\u81ea\u52a8\u67e5\u5230\u6700\u9002\u5408\u7684\u4e00\u7248\u6765\u8c03\u7528\u7ed9\u4f60\u3002\u7c7b\u4f3c gemm\uff0c\u8fd8\u6709 gemv\u3001spmv\u2026\u2026\u6240\u6709\u7684\u77e9\u9635\u8fd0\u7b97 API \u90fd\u7ecf\u5386\u4e86\u8fd9\u6837\u7684\u201c\u7f16\u8bd1\u671f\u201d\u66b4\u529b\u6392\u5217\u7ec4\u5408\uff0c\u53ea\u4e3a\u201c\u8fd0\u884c\u65f6\u201d\u91ca\u653e\u6700\u5927\u6027\u80fd\uff01\u8fd9\u4e5f\u5bfc\u81f4\u7f16\u8bd1\u597d\u7684 cublas.dll \u6587\u4ef6\u6765\u5230\u4e86\u6050\u6016\u7684 20 MB \u5de6\u53f3\uff0c\u800c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9ad8\u6548\u3002 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 Lambda \u51fd\u6570\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u533f\u540d\u7684\uff0c\u6bcf\u4e2a Lambda \u8868\u8fbe\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97 generic_sum \u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684 Lambda \u90fd\u4f1a\u5b9e\u4f8b\u5316\u4e00\u904d\u3002\u867d\u7136\u6709\u5229\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u4f46\u4e5f\u5f71\u54cd\u4e86\u7f16\u8bd1\u901f\u5ea6\u548c\u7075\u6d3b\u6027\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7 decltype(add) \u83b7\u53d6 add \u8fd9\u4e2a Lambda \u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4e5f\u53ea\u80fd\u901a\u8fc7 auto \u6765\u6355\u83b7 Lambda \u5bf9\u8c61\u4e3a\u53d8\u91cf\u3002 \u4e3a\u6b64\uff0c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::function \u5bb9\u5668\uff0c\u4ed6\u80fd\u5bb9\u7eb3\u4efb\u4f55\u51fd\u6570\u5bf9\u8c61\uff01\u65e0\u8bba\u662f\u533f\u540d\u7684 Lambda \u51fd\u6570\u5bf9\u8c61\uff0c\u8fd8\u662f\u666e\u666e\u901a\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u90fd\u80fd\u7eb3\u5165 std::function \u7684\u4f53\u5185\u3002 \u552f\u4e00\u7684\u4ee3\u4ef7\u662f\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u51fa\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002 \u4f8b\u5982\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e24\u4e2a int \uff0c std::function auto add_lambda = [](int a, int b) { // Lambda \u51fd\u6570\u5bf9\u8c61 return a + b; }; struct AddClass { int operator()(int a, int b) { // \u81ea\u5b9a\u4e49\u7c7b\u6a21\u62df\u51fd\u6570\u5bf9\u8c61 return a + b; } }; AddClass add_object; int add_regular_func(int a, int b) { // \u666e\u901a\u51fd\u6570 return a + b; } std::function add; // \u6240\u6709\u5e7f\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u7edf\u7edf\u63a5\u7eb3 add = add_lambda; // OK add = add_object; // OK add = add_regular_func; // OK int generic_sum(std::vector const &v, std::function op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op(ret, v[i]); // \u5199\u8d77\u6765\u548c\u6a21\u677f\u4f20\u53c2\u65f6\u4e00\u6837\u65e0\u611f } // \u65e0\u9700\u6307\u9488\uff0c\u65e0\u9700 delete\uff0cfunction \u80fd\u81ea\u52a8\u7ba1\u7406\u51fd\u6570\u5bf9\u8c61\u751f\u547d\u5468\u671f return ret; } \u5982\u679c\u8fd8\u60f3\u652f\u6301\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u770b std::function \u3002\u8fd9\u91cc std::any \u662f\u4e2a\u8d85\u7ea7\u4e07\u80fd\u5bb9\u5668\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u5bf9\u8c61\uff0c\u4ed6\u548c std::function \u4e00\u6837\u90fd\u91c7\u7528\u4e86\u201c\u7c7b\u578b\u64e6\u9664 (type-erasure)\u201d\u6280\u672f\uff0c\u7f3a\u70b9\u662f\u5fc5\u987b\u914d\u5408 std::any_cast \u624d\u80fd\u53d6\u51fa\u4f7f\u7528\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u8fdb\u9636\u4e13\u9898\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u4ed6\u4eec\u7684\u539f\u7406\uff0c\u5e76\u5e26\u4f60\u81ea\u5df1\u505a\u4e00\u4e2a\u64e6\u52a0\u6cd5\u7684\u7c7b\u578b\u64e6\u9664\u5bb9\u5668\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u80fd\u5728\u9759\u6001\u4e0e\u52a8\u6001\u4e4b\u95f4\u8f7b\u677e\u5207\u6362\uff0c \u9ad8\u6027\u80fd \u4e0e \u7075\u6d3b\u6027 \u4efb\u541b\u9009\u62e9\u3002 \u5728\u9700\u8981\u6027\u80fd\u7684 \u74f6\u9888\u4ee3\u7801 \u4e2d\u7528\u6a21\u677f\u4f20\u53c2\uff0c\u7f16\u8bd1\u671f\u9759\u6001\u5206\u53d1\uff0c\u591a\u6b21\u91cf\u8eab\u5b9a\u505a\uff0c\u63d0\u9ad8\u8fd0\u884c\u65f6\u6027\u80fd\u3002 \u74f6\u9888\u4ee3\u7801: \u5f80\u5f80\u4e00\u4e2a\u7a0b\u5e8f 80% \u7684\u65f6\u95f4\u82b1\u5728 20% \u7684\u4ee3\u7801\u4e0a\u3002\u8fd9 20% \u662f\u5728\u7a0b\u5e8f\u4e2d\u9891\u7e41\u6267\u884c\u7684\u3001\u8ba1\u7b97\u91cf\u5927\u7684\u3001\u6216\u8005\u8c03\u7528\u7279\u522b\u8017\u65f6\u7684\u51fd\u6570\u3002\u9488\u5bf9\u8fd9\u90e8\u5206\u74f6\u9888\u4ee3\u7801\u4f18\u5316\u5373\u53ef\uff0c\u800c\u5269\u4f59\u7684 80% \u6253\u9171\u6cb9\u4ee3\u7801\uff0c\u5927\u53ef\u4ee5\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u5199\u3002 \u5728\u6027\u80fd\u65e0\u5173\u7d27\u8981\u7684\u9876\u5c42\u4e1a\u52a1\u903b\u8f91\u4e2d\u7528 function \u5bb9\u5668\u4f20\u53c2\uff0c\u8fd0\u884c\u65f6\u52a8\u6001\u5206\u53d1\uff0c\u8282\u7701\u7f16\u8bd1\u4f53\u79ef\uff0c\u65b9\u4fbf\u6301\u4e45\u5b58\u50a8\uff0c\u7075\u6d3b\u6613\u7528\u3002 \u4f8b\u5982\u4e0a\u9762\u7684 generic_sum \u51fd\u6570\uff0c\u5982\u679c\u6211\u4eec\u7a81\u7136\u60f3\u8981\u9ad8\u6027\u80fd\u4e86\uff0c\u53ea\u9700\u628a std::function op \u8f7b\u8f7b\u6539\u4e3a auto op \u5c31\u8f7b\u677e\u5207\u6362\u5230\u9759\u6001\u5206\u53d1\u6a21\u5f0f\u4e86\u3002 \u800c\u865a\u51fd\u6570\u4e00\u65e6\u7528\u4e86\uff0c\u57fa\u672c\u5c31\u53ea\u80fd\u52a8\u6001\u5206\u53d1\u4e86\uff0c\u5373\u4f7f\u80fd\u88ab IPO \u4f18\u5316\u6389\uff0c\u865a\u8868\u6307\u9488\u4e5f\u6c38\u8fdc\u5360\u636e\u7740\u4e00\u4e2a 8 \u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u4e14\u6c38\u8fdc\u53ea\u80fd\u4ee5\u6307\u9488\u5f62\u5f0f\u4f20\u6765\u4f20\u53bb\u3002 \u4e00\u79cd\u9759\u6001\u5206\u53d1\u7248\u7684\u865a\u51fd\u6570\u66ff\u4ee3\u54c1\u662f CRTP\uff0c\u4ed6\u57fa\u4e8e\u6a21\u677f\u5143\u7f16\u7a0b\uff0c\u4f46\u4e0e\u865a\u51fd\u6570\u4e4b\u95f4\u5207\u6362\u56f0\u96be\uff0c\u4e0d\u50cf\u51fd\u6570\u5bf9\u8c61\u90a3\u4e48\u65e0\u611f\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u4e13\u9898\u8bfe\u4e2d\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u3002 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 mt_queue> task_queue; void thread1() { task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a11\"); }); task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a12\"); }); } void thread2() { while (true) { auto task = task_queue.pop(); task(); } } mt_queue \u662f\u5c0f\u5f6d\u8001\u5e08\u5c01\u88c5\u7684\u591a\u7ebf\u7a0b\u5b89\u5168\u7684\u6d88\u606f\u961f\u5217\uff0c\u5b9e\u73b0\u539f\u7406\u4f1a\u5728\u7a0d\u540e\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4e2d\u8be6\u7ec6\u8bb2\u89e3\u3002 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570 int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello(2, 3) \u7ed3\u679c\uff1a{}\", hello(2, 3)); fmt::println(\"main \u8c03\u7528 hello(2, 4) \u7ed3\u679c\uff1a{}\", hello(2, 4)); fmt::println(\"main \u8c03\u7528 hello(2, 5) \u7ed3\u679c\uff1a{}\", hello(2, 5)); return 0; } int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello2(3) \u7ed3\u679c\uff1a{}\", hello2(3)); fmt::println(\"main \u8c03\u7528 hello2(4) \u7ed3\u679c\uff1a{}\", hello2(4)); fmt::println(\"main \u8c03\u7528 hello2(5) \u7ed3\u679c\uff1a{}\", hello2(5)); return 0; }","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b"},{"location":"functional/#_1","text":"\u51fd\u6570\u5f0f\u7f16\u7a0b \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u7528\u51fd\u6570\u5c01\u88c5 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u4e8c\u6b21\u5c01\u88c5 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b"},{"location":"functional/#_2","text":"int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum = {}\", s); return 0; } \u8fd9\u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u7ec4\u6c42\u548c\u7684\u7b80\u5355\u7a0b\u5e8f\u3002 \u4f46\u662f\uff0c\u4ed6\u53ea\u80fd\u8ba1\u7b97\u6570\u7ec4 a \u7684\u6c42\u548c\uff0c\u65e0\u6cd5\u590d\u7528\u3002 \u5982\u679c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u6570\u7ec4 b \u4e5f\u9700\u8981\u6c42\u548c\u7684\u8bdd\uff0c\u5c31\u5f97\u628a\u6574\u4e2a\u6c42\u548c\u7684 for \u5faa\u73af\u91cd\u65b0\u5199\u4e00\u904d\uff1a int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum of a = {}\", s); std::vector b = {5, 6, 7, 8}; s = 0; for (int i = 0; i < a.size(); i++) { s += b[i]; } fmt::println(\"sum of b = {}\", s); return 0; } \u8fd9\u5c31\u51fa\u73b0\u4e86\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5927\u5fcc\uff1a\u4ee3\u7801\u91cd\u590d\u3002 \u4f8b\u5982\uff0c\u4f60\u6709\u5439\u7a7a\u8c03\u7684\u9700\u6c42\uff0c\u548c\u5145\u624b\u673a\u7684\u9700\u6c42\u3002\u4f60\u4e3a\u4e86\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u9700\u6c42\uff0c\u8d2d\u4e70\u4e86\u4e24\u53f0\u53d1\u7535\u673a\uff0c\u5206\u522b\u4e3a\u7a7a\u8c03\u548c\u624b\u673a\u4f9b\u7535\u3002\u7b2c\u4e8c\u5929\uff0c\u4f60\u53c8\u4ea7\u751f\u4e86\u73a9\u7535\u8111\u9700\u6c42\uff0c\u4e8e\u662f\u4f60\u53c8\u8d2d\u4e70\u4e00\u53f0\u53d1\u7535\u673a\uff0c\u4e13\u4e3a\u7535\u8111\u4f9b\u7535\u2026\u2026\u771f\u662f\u6d6a\u8d39\uff01 \u91cd\u590d\u7684\u4ee3\u7801\u4e0d\u4ec5\u5f71\u54cd\u4ee3\u7801\u7684 \u53ef\u8bfb\u6027 \uff0c\u4e5f\u589e\u52a0\u4e86 \u7ef4\u62a4 \u4ee3\u7801\u7684\u6210\u672c\u3002 \u770b\u8d77\u6765\u4e71\u7cdf\u7cdf\u7684\uff0c\u4fe1\u606f\u5bc6\u5ea6\u4f4e\uff0c\u8ba9\u4eba\u4e00\u773c\u770b\u4e0d\u51fa\u4ee3\u7801\u5728\u5e72\u4ec0\u4e48\u7684\u529f\u80fd \u5f88\u5bb9\u6613\u5199\u9519\uff0c\u770b\u8d70\u773c\uff0c\u96be\u8c03\u8bd5 \u590d\u5236\u7c98\u8d34\u8fc7\u7a0b\u4e2d\uff0c\u5bb9\u6613\u6f0f\u6539\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684 s += b[i] \u53ef\u80fd\u5199\u6210 s += a[i] \u800c\u81ea\u5df1\u4e0d\u53d1\u73b0 \u6539\u8d77\u6765\u4e0d\u65b9\u4fbf\uff0c\u5f53\u6211\u4eec\u7684\u9700\u6c42\u53d8\u66f4\u65f6\uff0c\u9700\u8981\u591a\u5904\u4fee\u6539\uff0c\u6bd4\u5982\u5f53\u6211\u9700\u8981\u6539\u4e3a\u8ba1\u7b97\u4e58\u79ef\u65f6\uff0c\u9700\u8981\u628a\u4e24\u4e2a\u5730\u65b9\u90fd\u6539\u6210 s *= \u6539\u4e86\u4ee5\u540e\u53ef\u80fd\u6f0f\u6539\u4e00\u90e8\u5206\uff0c\u7559\u4e0b Bug \u9690\u60a3 \u654f\u6377\u5f00\u53d1\u9700\u8981\u53cd\u590d\u4fee\u6539\u4ee3\u7801\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u8c03\u8bd5 += \u548c -= \u7684\u533a\u522b\uff0c\u770b\u7ed3\u679c\u53d8\u5316\uff0c\u5982\u679c\u4e00\u6b21\u5207\u6362\u9700\u8981\u6539\u591a\u5904\uff0c\u5c31\u5f71\u54cd\u4e86\u8c03\u8bd5\u901f\u5ea6","title":"\u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f"},{"location":"functional/#_3","text":"\u5982\u679c\u4f60\u8fd8\u662f\u559c\u6b22\u201c\u4e00\u672c\u9053\u201d\u5199\u6cd5\u7684\u8bdd\uff0c\u4e0d\u59a8\u60f3\u60f3\u770b\uff0c\u5b8c\u5168\u4e0d\u7528\u4efb\u4f55\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u628a fmt::println \u548c std::vector \u8fd9\u4e9b\u51fd\u6570\u5168\u90e8\u62c6\u89e3\u6210\u4e00\u4e2a\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u90a3\u8fd9\u6574\u4e2a\u7a0b\u5e8f\u4f1a\u6709\u591a\u96be\u5199\uff1f int main() { #ifdef _WIN32 int *a = (int *)VirtualAlloc(NULL, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else int *a = (int *)mmap(NULL, 4 * sizeof(int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; int s = 0; for (int i = 0; i < 4; i++) { s += a[i]; } char buffer[64]; buffer[0] = 's'; buffer[1] = 'u'; buffer[2] = 'm'; buffer[3] = ' '; buffer[4] = '='; buffer[5] = ' '; // \u4f8b\u5982\uff0c\u5982\u679c\u8981\u4fee\u6539\u6b64\u5904\u7684\u63d0\u793a\u6587\u672c\uff0c\u751a\u81f3\u9700\u8981\u4fee\u6539\u540e\u9762\u7684 len \u53d8\u91cf... int len = 6; int x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif int *b = (int *)a; b[0] = 4; b[1] = 5; b[2] = 6; b[3] = 7; int s = 0; for (int i = 0; i < 4; i++) { s += b[i]; } len = 6; x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif #ifdef _WIN32 VirtualFree(a, 0, MEM_RELEASE); #else munmap(a); #endif return 0; } \u4e0d\u4ec5\u5b8c\u5168\u6ca1\u6709\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\uff0c\u751a\u81f3\u90fd\u6ca1\u6709\u53ef\u79fb\u690d\u6027\u3002 \u9664\u975e\u4f60\u53ea\u5199\u5e94\u4ed8\u5bfc\u5e08\u7684\u201c\u4e00\u6b21\u6027\u201d\u7a0b\u5e8f\uff0c\u4e00\u65e6\u8981\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u53ef\u907f\u514d\u7684\u8981\u81ea\u5df1\u5c01\u88c5\u51fd\u6570\u6216\u7c7b\u3002\u7f51\u4e0a\u6240\u6709\u9f13\u5439\u201c\u4e0d\u5c01\u88c5\u201d\u201c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u9762\u5b50\u5de5\u7a0b\u201d\u7684\u53cd\u667a\u8a00\u8bba\uff0c\u90fd\u662f\u6ca1\u6709\u505a\u8fc7\u5927\u578b\u9879\u76ee\u7684\u3002","title":"\u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f"},{"location":"functional/#_4","text":"\u5f88\u591a\u8bbe\u8ba1\u6a21\u5f0f\u6559\u6750\u7247\u9762\u5f3a\u8c03 \u53ef\u8bfb\u6027 \uff0c\u4eff\u4f5b\u8bbe\u8ba1\u6a21\u5f0f\u5c31\u662f\u4e3a\u4e86\u201c\u4f18\u96c5\u201d\u201c\u9ad8\u5927\u4e0a\u201d\u201c\u7f8e\u5b66\u201d\uff1f\u4f7f\u5f97\u5f88\u591a\u4eba\u8ba4\u4e3a\uff0c\u201c\u6211\u8fd9\u4e2a\u662f\u81ea\u5df1\u7684\u9879\u76ee\uff0c\u4e0d\u7528\u7f8e\u5316\u7ed9\u9886\u5bfc\u770b\u201d\u800c\u62d2\u7edd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b9e\u9645\u4e0a\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4e3b\u8981\u4ef7\u503c\u5728\u4e8e \u65b9\u4fbf\u540e\u7eed\u4fee\u6539 *\uff01 \u4f8b\u5982 B \u7ad9\u4ee5\u524d\u53ea\u652f\u6301\u4e0a\u4f20\u666e\u901a\u89c6\u9891\uff0c\u73b0\u5728\u53d4\u53d4\u7a81\u7136\u63d0\u51fa\uff1a\u8981\u652f\u6301\u4e92\u52a8\u89c6\u9891\uff0c\u5145\u7535\u89c6\u9891\uff0c\u89c6\u9891\u5408\u96c6\uff0c\u8fd8\u5e9f\u9664\u4e86\u89c6\u9891\u5206 p\uff0c\u8fd8\u8981\u652f\u6301\u4e0a\u4f20\u77ed\u89c6\u9891\uff0c\u7ad6\u5c4f\u5f00\u5173\u7b49\u2026\u2026\u6bcf\u4e00\u4e2a\u53d4\u53d4\u7684\u8981\u6c42\uff0c\u90fd\u9700\u8981\u5927\u91cf\u7a0b\u5e8f\u5458\u4fee\u6539\u4ee3\u7801\uff0c\u65e0\u8bba\u6d89\u53ca\u524d\u7aef\u8fd8\u662f\u540e\u7aef\u3002 \u4e0e\u5efa\u7b51\u3001\u7ed8\u753b\u7b49\u9886\u57df\u4e0d\u540c\uff0c\u4e00\u6b21\u4ea4\u4ed8\u5b8c\u6bd5\u5c31\u53ef\u4ee5\u51e0\u4e4e\u6c38\u4e45\u4f7f\u7528\u3002\u800c\u8f6f\u4ef6\u5f00\u53d1\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u66f4\uff0c\u90fd\u5bfc\u81f4\u4ee3\u7801\u9700\u8981\u4fee\u6539\u3002\u5f00\u53d1\u4eba\u5458\u51e0\u4e4e\u9700\u8981\u4e00\u76f4\u56f4\u7ed5\u7740\u8f6f\u4ef6\u4ee3\u7801\uff0c\u4e0d\u65ad\u7684\u4fee\u6539\u3002\u8c03\u67e5\u8868\u660e\uff0c\u7a0b\u5e8f\u5458 90% \u7684\u65f6\u95f4\u82b1\u5728 \u6539\u4ee3\u7801 \u4e0a\uff0c \u5199\u4ee3\u7801 \u53ea\u5360 10%\u3002 \u8f6f\u4ef6\u5c31\u50cf\u751f\u7269\uff0c\u8981\u4e0d\u65ad\u8fdb\u5316\uff0c\u8f6f\u4ef6\u4e0d\u66f4\u65b0\u4e0d\u7ef4\u62a4\u4e86\u7b49\u4e8e\u6b7b\u3002\u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6\u9010\u6e10\u53d8\u5f97\u81c3\u80bf\u96be\u4ee5\u4fee\u6539\uff0c\u65e0\u6cd5\u9002\u5e94\u65b0\u9700\u6c42\uff0c\u90a3\u4ed6\u5c31\u50cf\u5df2\u7ecf\u5931\u53bb\u8fdb\u5316\u80fd\u529b\u7684\u751f\u7269\u79cd\u7fa4\uff0c\u5982\u300a\u4e09\u4f53\u300b\u4e16\u754c\u89c2\u4e2d\u201c\u5b89\u987f\u201d\u5230\u6fb3\u5927\u5229\u4e9a\u4fdd\u7559\u533a\u91cc\u201c\u7edd\u80b2\u201d\u7684\u4eba\u7c7b\uff0c\u88ab\u6dd8\u6c70\u53ea\u662f\u65f6\u95f4\u95ee\u9898\u3002 \u5982\u679c\u6211\u4eec\u80fd\u5728 \u5199\u4ee3\u7801 \u9636\u6bb5\uff0c\u5c31\u628a\u7a0b\u5e8f\u51c6\u5907\u5f97 \u6613\u4e8e\u540e\u7eed\u4fee\u6539 \uff0c\u90a3\u5c31\u53ef\u4ee5\u5728\u540e\u7eed 90% \u7684 \u6539\u4ee3\u7801 \u9636\u6bb5\u7701\u4e0b\u65e0\u6570\u65f6\u95f4\u3002 \u5982\u4f55\u8ba9\u4ee3\u7801\u6613\u4e8e\u4fee\u6539\uff1f\u524d\u4eba\u603b\u7ed3\u51fa\u4e00\u7cfb\u5217\u5e38\u7528\u7684\u5199\u6cd5\uff0c\u8fd9\u7c7b\u5199\u6cd5\u6709\u52a9\u4e8e\u8ba9\u540e\u7eed\u4fee\u6539\u66f4\u5bb9\u6613\uff0c\u5404\u81ea\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u5408\uff0c\u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u3002 \u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u6700\u57fa\u7840\u7684\u4e00\u70b9\uff0c\u5c31\u662f\u907f\u514d\u91cd\u590d\uff01 \u5f53\u4f60\u6709\u5f88\u591a\u5730\u65b9\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\u65f6\uff0c\u4e00\u65e6\u9700\u8981\u6d89\u53ca\u4fee\u6539\u8fd9\u90e8\u5206\u903b\u8f91\u65f6\uff0c\u5c31\u9700\u8981\u5230\u6bcf\u4e00\u4e2a\u51fa\u73b0\u4e86\u8fd9\u4e2a\u903b\u8f91\u7684\u4ee3\u7801\u4e2d\uff0c\u53bb\u9010\u4e00\u4fee\u6539\u3002 \u4f8b\u5982\u4f60\u7684\u540d\u5b57\uff0c\u5728\u51fa\u751f\u8bc1\uff0c\u8eab\u4efd\u8bc1\uff0c\u5b66\u751f\u8bc1\uff0c\u6bd5\u4e1a\u8bc1\uff0c\u623f\u4ea7\u8bc1\uff0c\u9a7e\u9a76\u8bc1\uff0c\u5404\u79cd\u5730\u65b9\u90fd\u51fa\u73b0\u4e86\u3002\u90a3\u4e48\u4f60\u8981\u6539\u540d\u7684\u8bdd\uff0c\u6240\u6709\u8fd9\u4e9b\u8bc1\u4ef6\u90fd\u9700\u8981\u91cd\u65b0\u5370\u5237\uff01\u5982\u679c\u80fd\u628a\u4ed6\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u201c\u7edf\u4e00\u8bc1\u201d\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u4fee\u6539\u201c\u7edf\u4e00\u8bc1\u201d\u4e0a\u7684\u540d\u5b57\u5c31\u884c\u4e86\u3002 \u4e0d\u8fc7\uff0c\u73b0\u5b9e\u4e2d\u5e76\u6ca1\u6709\u9891\u7e41\u6539\u540d\u5b57\u7684\u9700\u6c42\uff0c\u8fd9\u8bf4\u660e\uff1a \u5bf9\u4e8e\u4e0d\u5e38\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u53ef\u4ee5\u5bb9\u5fcd\u4e00\u5b9a\u7684\u91cd\u590d\u3002 \u8d8a\u662f\u672a\u6765\u6709\u53ef\u80fd\u4fee\u6539\u7684\uff0c\u5c31\u8d8a\u9700\u8981\u8bbe\u8ba1\u6a21\u5f0f\u964d\u91cd\uff01 \u4f8b\u5982\u6570\u5b66\u5e38\u6570 PI = 3.1415926535897\uff0c\u8fd9\u8f88\u5b50\u90fd\u4e0d\u53ef\u80fd\u51fa\u73b0\u4fee\u6539\u7684\u9700\u6c42\uff0c\u90a3\u5199\u6b7b\u4e5f\u6ca1\u5173\u7cfb\u3002\u5982\u679c\u8981\u628a PI \u5b9a\u4e49\u6210\u5b8f\uff0c\u53ea\u662f\u51fa\u4e8e\u201c\u8bb0\u4e0d\u4f4f\u201d\u201c\u5199\u8d77\u6765\u592a\u957f\u4e86\u201d\u201c\u590d\u5236\u7c98\u8d34\u9ebb\u70e6\u201d\u3002\u6240\u4ee5\u5bf9\u4e8e PI \u8fd9\u79cd\u4e0d\u4f1a\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u964d\u91cd\u53ea\u662f\u589e\u52a0 \u53ef\u8bfb\u6027 \uff0c\u800c\u4e0d\u662f \u53ef\u4fee\u6539\u6027 \u3002 \u4f46\u662f\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\uff01\u9700\u6c42\u7684\u5343\u53d8\u4e07\u5316\u603b\u662f\u8d85\u51fa\u4f60\u7684\u60f3\u8c61\u3002 \u4f8b\u5982\u4f60\u505a\u4e86\u4e00\u4e2a\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u6e38\u620f\uff0c\u9700\u8981\u7528\u5230\u91cd\u529b\u52a0\u901f\u5ea6 g = 9.8\uff0c\u4f60\u60f3\u5f53\u7136\u8ba4\u4e3a g \u4ee5\u540e\u4e0d\u53ef\u80fd\u4fee\u6539\u3002\u8001\u677f\u4e5f\u4fe1\u8a93\u65e6\u65e6\u5411\u4f60\u4fdd\u8bc1\uff1a\u201c\u6ca1\u4e8b\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u4e0d\u4f1a\u6539\u53d8\u3002\u201d\u4f60\u5c31\u5199\u6b7b\u5728\u4ee3\u7801\u91cc\u4e86\u3002 \u6ca1\u60f3\u5230\uff0c\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u8001\u677f\u7a81\u7136\u8981\u6c42\u4f60\u52a0\u5165\u201c\u6708\u7403\u7ae0\u201d\u5173\u5361\uff0c\u5728\u8fd9\u4e9b\u5173\u5361\u4e2d\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u662f g = 1.6\u3002 \u5982\u679c\u4f60\u4e00\u5f00\u59cb\u5c31\u5df2\u7ecf\u628a g \u63d0\u53d6\u51fa\u6765\uff0c\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a struct Level { const double g = 9.8; void physics_sim() { bird.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f pig.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f } }; \u90a3\u4e48\u8981\u652f\u6301\u6708\u7403\u5173\u5361\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5c31\u53ef\u4ee5\u4e86\u3002 struct Level { double g; Level(Chapter chapter) { if (chapter == ChapterMoon) { g = 1.6; } else { g = 9.8; } } void physics_sim() { bird.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g pig.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g } }; \u5c0f\u5f6d\u8001\u5e08\u4e4b\u524d\u505a zeno \u65f6\uff0c\u8be2\u95ee\u8981\u4e0d\u8981\u628a\u6e32\u67d3\u7ba1\u7ebf\u8282\u70b9\u5316\uff0c\u65b9\u4fbf\u7528\u6237\u52a8\u6001\u7f16\u7a0b\uff1f\u5f20\u7329\u7329\u5c31\u662f\u4fe1\u8a93\u65e6\u65e6\u9053\uff1a\u201c\u6e32\u67d3\u662f\u4e00\u4e2a\u9ad8\u5ea6\u6210\u719f\u9886\u57df\uff0c\u4e0d\u4f1a\u6709\u591a\u5c11\u4fee\u6539\u9700\u6c42\u7684\u3002\u201d\u5c0f\u5f6d\u8001\u5e08\u9042\u5199\u6b7b\u4e86\u6e32\u67d3\u7ba1\u7ebf\uff0c\u4e13\u4e3a\u6027\u80fd\u6781\u5ea6\u4f18\u5316\uff0c\u51e0\u4e2a\u6708\u540e\uff0c\u5f20\u7329\u7329\u7f9e\u7b54\u7b54\u627e\u5230\u5c0f\u5f6d\u8001\u5e08\uff1a\u201c\u5c0f\u5f6d\u8001\u5e08\uff0c\u90a3\u4e2a\uff0c\u6e32\u67d3\uff0c\u80fd\u4e0d\u80fd\u6539\u6210\u8282\u70b9\u554a\u2026\u2026\u201d\u3002\u8fd9\u4e2a\u6545\u4e8b\u544a\u8bc9\u6211\u4eec\uff0c\u7532\u65b9\u7684\u4fe1\u8a93\u65e6\u65e6\u653e\u7684\u4e00\u4e2a\u5c41\u90fd\u4e0d\u80fd\u4fe1\u3002","title":"\u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01"},{"location":"functional/#_5","text":"\u51fd\u6570\u5c31\u662f\u6765\u5e2e\u4f60\u89e3\u51b3\u4ee3\u7801\u91cd\u590d\u95ee\u9898\u7684\uff01\u8981\u9886\uff1a \u628a\u5171\u540c\u7684\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002 void sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } fmt::println(\"sum of v = {}\", s); } int main() { std::vector a = {1, 2, 3, 4}; sum(a); std::vector b = {5, 6, 7, 8}; sum(b); return 0; } \u8fd9\u6837 main \u51fd\u6570\u91cc\u5c31\u53ef\u4ee5\u53ea\u5173\u5fc3\u8981\u6c42\u548c\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u7528\u5173\u5fc3\u6c42\u548c\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u4e86\u3002\u4e8b\u540e\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u628a sum \u7684\u5185\u5bb9\u5077\u5077\u6362\u6389\uff0c\u6362\u6210\u5e76\u884c\u7684\u7b97\u6cd5\uff0cmain \u4e5f\u4e0d\u7528\u77e5\u9053\u3002\u8fd9\u5c31\u662f \u5c01\u88c5 \uff0c\u53ef\u4ee5\u628a\u91cd\u590d\u7684\u516c\u5171\u90e8\u5206\u62bd\u53d6\u51fa\u6765\uff0c\u65b9\u4fbf\u4ee5\u540e\u4fee\u6539\u4ee3\u7801\u3002 sum \u51fd\u6570\u76f8\u5f53\u4e8e\uff0c\u5f53\u9700\u8981\u5439\u7a7a\u8c03\u65f6\uff0c\u63d2\u4e0a\u7a7a\u8c03\u63d2\u5ea7\u3002\u5f53\u9700\u8981\u7ed9\u624b\u673a\u5145\u7535\u65f6\uff0c\u63d2\u4e0a\u624b\u673a\u5145\u7535\u5668\u3002\u4f60\u4e0d\u9700\u8981\u5173\u5fc3\u63d2\u5ea7\u91cc\u7684\u7535\u54ea\u91cc\u6765\uff0c\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4f1a\u66ff\u4f60\u60f3\u529e\u6cd5\u89e3\u51b3\uff0c\u60f3\u529e\u6cd5\u4f18\u5316\uff0c\u60f3\u529e\u6cd5\u5347\u7ea7\u5230\u7eff\u8272\u80fd\u6e90\u3002\u4f60\u53ea\u9700\u8981\u5439\u7740\u7a7a\u8c03\u7ed9\u4f60\u6b63\u5728\u5f00\u53d1\u7684\u624b\u673a App \u4f18\u5316\u5c31\u884c\u4e86\uff0c\u5927\u5927\u51cf\u8f7b\u7a0b\u5e8f\u5458\u5fc3\u667a\u8d1f\u62c5\u3002","title":"\u7528\u51fd\u6570\u5c01\u88c5"},{"location":"functional/#_6","text":"\u4f46\u662f\uff01\u8fd9\u6bb5\u4ee3\u7801\u4ecd\u7136\u6709\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u628a sum \u6c42\u548c\u7684\u7ed3\u679c\uff0c\u76f4\u63a5\u5728 sum \u91cc\u6253\u5370\u4e86\u51fa\u6765\u3002sum \u91cc\u5199\u6b7b\u4e86\uff0c\u6c42\u5b8c\u548c\u4e4b\u540e\u53ea\u80fd\u76f4\u63a5\u6253\u5370\uff0c\u8c03\u7528\u8005 main \u6839\u672c\u65e0\u6cd5\u63a7\u5236\u3002 \u8fd9\u662f\u4e00\u79cd\u9519\u8bef\u7684\u5c01\u88c5\uff0c\u6216\u8005\u8bf4\uff0c\u5c01\u88c5\u8fc7\u5934\u4e86\u3002 \u4f60\u628a\u624b\u673a\u5145\u7535\u5668 (fmt::println) \u710a\u6b7b\u5728\u4e86\u63d2\u5ea7 (sum) \u4e0a\uff0c\u73b0\u5728\u8fd9\u4e2a\u63d2\u5ea7\u53ea\u80fd\u7ed9\u624b\u673a\u5145\u7535 (\u7528\u4e8e\u76f4\u63a5\u6253\u5370) \u4e86\uff0c\u4e0d\u80fd\u7ed9\u7b14\u8bb0\u672c\u7535\u8111\u5145\u7535 (\u6c42\u548c\u7ed3\u679c\u4e0d\u76f4\u63a5\u7528\u4e8e\u6253\u5370) \u4e86\uff01\u5c3d\u7ba1\u901a\u8fc7\u66f4\u6362\u5145\u7535\u7ebf (\u53c2\u6570 v)\uff0c\u8fd8\u53ef\u4ee5\u652f\u6301\u652f\u6301\u5b89\u5353 (a) \u548c\u82f9\u679c (b) \u4e24\u79cd\u624b\u673a\u7684\u5145\u7535\uff0c\u4f46\u8fd9\u6837\u710a\u6b7b\u7684\u63d2\u5ea7\u5df2\u7ecf\u548c\u7b14\u8bb0\u672c\u7535\u8111\u65e0\u7f18\u4e86\u3002","title":"\u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408"},{"location":"functional/#_7","text":"\u5f88\u660e\u663e\uff0c\u201c\u6253\u5370\u201d\u548c\u201c\u6c42\u548c\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u64cd\u4f5c\uff0c\u4e0d\u5e94\u8be5\u710a\u6b7b\u5728\u4e00\u5757\u3002 sum \u51fd\u6570\u7684\u672c\u804c\u5de5\u4f5c\u662f\u201c\u6570\u7ec4\u6c42\u548c\u201d\uff0c\u4e0d\u5e94\u8be5\u9644\u8d60\u6253\u5370\u529f\u80fd\u3002 sum \u8ba1\u7b97\u51fa\u6c42\u548c\u7ed3\u679c\u540e\uff0c\u76f4\u63a5 return \u5373\u53ef\u3002 \u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u7ed3\u679c\uff0c\u662f\u8c03\u7528\u8005 main \u7684\u4e8b\uff0c\u6b63\u5982\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4e0d\u4f1a\u7ba1\u4f60\u7528\u4ed6\u63d0\u4f9b\u7684\u7535\u6765\u5439\u7a7a\u8c03\u8fd8\u662f\u73a9\u6e38\u620f\u4e00\u6837\uff0c\u53ea\u8981\u4e0d\u59a8\u788d\u5230\u5176\u4ed6\u5c45\u6c11\u7684\u6b63\u5e38\u7528\u7535\u3002 int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum of a = {}\", sum(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"sum of b = {}\", sum(b)); return 0; } \u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8bf4\u7684 \u804c\u8d23\u5355\u4e00\u539f\u5219 \u3002","title":"\u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528"},{"location":"functional/#_8","text":"\u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e2a\u51fd\u6570 average\uff0c\u4ed6\u53ef\u4ee5\u57fa\u4e8e sum \u5b9e\u73b0\uff1a int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } double average(std::vector const &v) { return (double)sum(v) / v.size(); } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"average of a = {}\", average(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"average of b = {}\", average(b)); return 0; } \u8fdb\u4e00\u6b65\u5c01\u88c5\u4e00\u4e2a\u6253\u5370\u6570\u7ec4\u6240\u6709\u7edf\u8ba1\u5b66\u4fe1\u606f\u7684\u51fd\u6570\uff1a void print_statistics(std::vector const &v) { if (v.empty()) { fmt::println(\"this is empty...\"); } else { fmt::println(\"sum: {}\", sum(v)); fmt::println(\"average: {}\", average(v)); fmt::println(\"min: {}\", min(v)); fmt::println(\"max: {}\", max(v)); } } int main() { std::vector a = {1, 2, 3, 4}; print_statistics(a); std::vector b = {5, 6, 7, 8}; print_statistics(b); return 0; } \u66b4\u9732 API \u65f6\uff0c\u8981\u540c\u65f6\u63d0\u4f9b\u5e95\u5c42\u7684 API \u548c\u9ad8\u5c42\u5c01\u88c5\u7684 API\u3002\u7528\u6237\u5982\u679c\u60f3\u8981\u63a7\u5236\u66f4\u591a\u7ec6\u8282\u53ef\u4ee5\u8c03\u7528\u5e95\u5c42 API\uff0c\u60f3\u8981\u7701\u4e8b\u7684\u7528\u6237\u53ef\u4ee5\u8c03\u7528\u9ad8\u5c42\u5c01\u88c5\u597d\u7684 API\u3002 \u9ad8\u5c42\u5c01\u88c5 API \u5e94\u5f53\u53ef\u4ee5\u5b8c\u5168\u901a\u8fc7\u8c03\u7528\u5e95\u5c42 API \u5b9e\u73b0\uff0c\u63d0\u4f9b\u9ad8\u5c42 API \u53ea\u662f\u65b9\u4fbf\u521d\u7ea7\u7528\u6237\u4f7f\u7528\u548c\u7406\u89e3\u3002 \u4f8b\u5982 libcurl \u5c31\u63d0\u4f9b\u4e86 curl_easy \u548c curl_multi \u4e24\u5957 API\u3002 - `curl_multi` \u63d0\u4f9b\u4e86\u8d85\u8be6\u7ec6\u7684\u53c2\u6570\uff0c\u628a\u6bcf\u4e2a\u64cd\u4f5c\u5206\u62c6\u6210\u591a\u6b65\uff0c\u65b9\u4fbf\u7528\u6237\u63d2\u624b\u7ec6\u8282\uff0c\u6ee1\u8db3\u9ad8\u7ea7\u7528\u6237\u7684\u5b9a\u5236\u5316\u9700\u6c42\uff0c\u4f46\u592a\u8fc7\u590d\u6742\uff0c\u96be\u4ee5\u5b66\u4e60\u3002 - `curl_easy` \u662f\u5bf9 `curl_multi` \u7684\u518d\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684 API\uff0c\u4f46\u662f\u5bf9\u5177\u4f53\u7ec6\u8282\u5c31\u96be\u4ee5\u64cd\u63a7\u4e86\uff0c\u9002\u5408\u521d\u5b66\u8005\u4e0a\u624b\u3002","title":"\u4e8c\u6b21\u5c01\u88c5"},{"location":"functional/#linus-3-24","text":"Linux \u5185\u6838\u4e3a\u4ec0\u4e48\u575a\u6301\u4f7f\u7528 TAB=8 \u4e3a\u4ee3\u7801\u98ce\u683c\uff1f TODO\uff1a\u8fd8\u5728\u5199","title":"Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c"},{"location":"functional/#_9","text":"\u4f60\u4ea7\u751f\u4e86\u4e24\u4e2a\u9700\u6c42\uff0c\u5206\u522b\u5c01\u88c5\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a sum \u6c42\u6240\u6709\u5143\u7d20\u7684\u548c product \u6c42\u6240\u6709\u5143\u7d20\u7684\u79ef int sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret += v[i]; } return ret; } int product(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret *= v[i]; } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", sum(a)); fmt::println(\"product: {}\", product(a)); return 0; } \u6ce8\u610f\u5230 sum \u548c product \u7684\u5185\u5bb9\u51e0\u4e4e\u5982\u51fa\u4e00\u8f99\uff0c\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff1a sum \u7684\u5faa\u73af\u4f53\u4e3a += \uff1b product \u7684\u5faa\u73af\u4f53\u4e3a *= \u3002 \u8fd9\u79cd\u51fd\u6570\u4f53\u5185\u6709\u90e8\u5206\u4ee3\u7801\u91cd\u590d\uff0c\u4f46\u53c8\u6709\u7279\u5b9a\u90e8\u5206\u4e0d\u540c\uff0c\u96be\u4ee5\u62bd\u79bb\u3002 \u8be5\u600e\u4e48\u590d\u7528\u8fd9\u91cd\u590d\u7684\u90e8\u5206\u4ee3\u7801\u5462\uff1f \u6211\u4eec\u8981\u628a sum \u548c product \u5408\u5e76\u6210\u4e00\u4e2a\u51fd\u6570 generic_sum \u3002\u7136\u540e\u901a\u8fc7\u51fd\u6570\u53c2\u6570\uff0c\u628a\u5dee\u5f02\u90e8\u5206\uff080\u3001 += \uff09\u201c\u6ce8\u5165\u201d\u5230\u4e24\u4e2a\u51fd\u6570\u539f\u672c\u4e0d\u540c\u5730\u65b9\u3002","title":"\u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f"},{"location":"functional/#_10","text":"\u5982\u4f55\u8868\u793a\u6211\u8fd9\u4e2a\u51fd\u6570\u662f\u8981\u505a\u6c42\u548c += \u8fd8\u662f\u6c42\u79ef *= \uff1f \u8ba9\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\uff1a enum Mode { ADD, // \u6c42\u548c\u64cd\u4f5c MUL, // \u6c42\u79ef\u64cd\u4f5c }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { // \u51fd\u6570\u5185\u5224\u65ad\u679a\u4e3e\uff0c\u51b3\u5b9a\u8981\u505a\u4ec0\u4e48\u64cd\u4f5c ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", generic_sum(a, ADD)); // \u7528\u6237\u6307\u5b9a\u4ed6\u60f3\u8981\u7684\u64cd\u4f5c fmt::println(\"product: {}\", generic_sum(a, MUL)); return 0; } \u7136\u800c\uff0c\u5982\u679c\u7528\u6237\u73b0\u5728\u60f3\u8981\u6c42\u6570\u7ec4\u7684 \u6700\u5927\u503c \u5462\uff1f \u679a\u4e3e\u4e2d\u8fd8\u6ca1\u6709\u5b9e\u73b0\u6700\u5927\u503c\u7684\u64cd\u4f5c\u2026\u2026\u8981\u652f\u6301\uff0c\u5c31\u5f97\u624b\u5fd9\u811a\u4e71\u5730\u53bb\u4fee\u6539 generic_sum \u51fd\u6570\u548c Mode \u679a\u4e3e\u539f\u672c\u7684\u5b9a\u4e49\uff0c\u771f\u9ebb\u70e6\uff01 enum Mode { ADD, MUL, MAX, // ***\u6539*** }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } else if (mode == MAX) { // ***\u6539*** ret = std::max(ret, v[i]); // ***\u6539*** } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, MAX); // ***\u6539*** return 0; } \u6211\u7528 // ***\u6539*** \u6307\u793a\u4e86\u6240\u6709\u9700\u8981\u6539\u52a8\u7684\u5730\u65b9\u3002 \u4e3a\u4e86\u589e\u52a0\u4e00\u4e2a\u6c42\u6700\u5927\u503c\u7684\u64cd\u4f5c\uff0c\u5c31\u9700\u8981\u4e09\u5904\u5206\u6563\u5728\u5404\u5730\u7684\u6539\u52a8\uff01 \u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u5bb9\u6613\u6284\u6f0f\uff0c\u6284\u9519\uff0c\u6bd4\u5982 MAX \u4e0d\u5c0f\u5fc3\u6253\u9519\u6210 MUL \u4e86\uff0c\u81ea\u5df1\u5374\u6ca1\u53d1\u73b0\uff0c\u7559\u4e0b BUG \u9690\u60a3\u3002 \u8fd9\u6837\u5199\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u5fc3\u667a\u8d1f\u62c5\u6781\u5927\uff0c\u6574\u5929\u5c31\u63d0\u5fc3\u540a\u80c6\u7740\u4e1c\u4e00\u5757\uff0c\u897f\u4e00\u5757\u7684\u6563\u88c5\u4ee3\u7801\uff0c\u62c5\u5fc3\u7740\u6709\u6ca1\u6709\u54ea\u4e2a\u5730\u65b9\u5199\u9519\u5199\u6f0f\uff0c\u4e25\u91cd\u59a8\u788d\u4e86\u5f00\u53d1\u6548\u7387\u3002 \u5e76\u4e14\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e0d\u80fd\u9002\u5e94\u9700\u6c42\u7684\u53d8\u5316\uff1a\u5047\u5982\u6211\u9700\u8981\u652f\u6301 MIN \u5462\uff1f\u53c8\u5f97\u6539\u4e09\u4e2a\u5730\u65b9\uff01\u8fd9\u8fdd\u80cc\u4e86\u8bbe\u8ba1\u6a21\u5f0f\u7684 \u5f00\u95ed\u539f\u5219 \u3002 \u5f00\u95ed\u539f\u5219: \u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\u3002\u6307\u7684\u662f\u8f6f\u4ef6\u5728\u9002\u5e94\u9700\u6c42\u53d8\u5316\u65f6\uff0c\u5e94\u5c3d\u91cf\u901a\u8fc7 \u6269\u5c55\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 \u4fee\u6539\u5df2\u6709\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\u3002 \u4f7f\u7528\u679a\u4e3e\u548c if-else \u5b9e\u73b0\u591a\u6001\uff0c\u96be\u4ee5\u6269\u5c55\uff0c\u8fd8\u8981\u4e00\u76f4\u53bb\u4fee\u6539\u539f\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u5c31\u8fdd\u80cc\u4e86 \u5f00\u95ed\u539f\u5219 \u3002","title":"\u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5"},{"location":"functional/#_11","text":"\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u201c\u6ce8\u5165\u201d\u4ee3\u7801\u5c31\u597d\u4e86\uff01\u80fd\u5426\u628a\u4e00\u6bb5\u201c\u4ee3\u7801\u201d\u4f5c\u4e3a generic_sum \u51fd\u6570\u7684\u53c2\u6570\u5462\uff1f \u4ee3\u7801\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u51fd\u6570\uff0c\u6ce8\u5165\u4ee3\u7801\u5c31\u662f\u6ce8\u5165\u51fd\u6570\u3002\u6211\u4eec\u5148\u5b9a\u4e49\u51fa\u4e09\u4e2a\u4e0d\u540c\u64cd\u4f5c\u5bf9\u5e94\u7684\u51fd\u6570\uff1a int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } \u7136\u540e\uff0c\u628a\u8fd9\u4e09\u4e2a\u5c0f\u51fd\u6570\uff0c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5927\u51fd\u6570 generic_sum \u7684\u53c2\u6570\u5c31\u884c\uff01 int generic_sum(std::vector const &v, auto op) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { // \u51fd\u6570\u4f5c\u8005\u65e0\u9700\u4e86\u89e3\u7528\u6237\u6307\u5b9a\u7684\u201c\u64cd\u4f5c\u201d\u5177\u4f53\u662f\u4ec0\u4e48 // \u53ea\u9700\u8981\u8c03\u7528\u8fd9\u4e00\u201c\u64cd\u4f5c\u201d\uff0c\u5f97\u5230\u7ed3\u679c\u5c31\u884c ret = op(ret, v[i]); } return ret; } int main() { std::vector a = {1, 2, 3, 4}; // \u7528\u6237\u65e0\u9700\u5173\u5fc3\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u4ec0\u4e48 // \u53ea\u9700\u968f\u5fc3\u6240\u6b32\u6307\u5b9a\u4ed6\u7684\u201c\u64cd\u4f5c\u201d\u4f5c\u4e3a\u53c2\u6570 generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u8d23\u4efb\u660e\u786e\u4e86\uff0c\u6211\u4eec\u6210\u529f\u628a\u4e00\u90e8\u5206\u7ec6\u8282\u4ece generic_sum \u4e2d\u8fdb\u4e00\u6b65\u62bd\u79bb\u3002 \u5e93\u4f5c\u8005 generic_sum \u4e0d\u5fc5\u4e86\u89e3 main \u7684\u64cd\u4f5c\u5177\u4f53\u662f\u4ec0\u4e48\uff0c\u4ed6\u53ea\u8d1f\u8d23\u5229\u7528\u8fd9\u4e2a\u64cd\u4f5c\u6c42\u201c\u548c\u201d\u3002 \u5e93\u7528\u6237 main \u4e0d\u5fc5\u4e86\u89e3 generic_sum \u5982\u4f55\u5b9e\u73b0\u64cd\u4f5c\u7d2f\u52a0\uff0c\u4ed6\u53ea\u7ba1\u6ce8\u5165\u201c\u5982\u4f55\u64cd\u4f5c\u201d\u7684\u4ee3\u7801\uff0c\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u3002","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a"},{"location":"functional/#c20-auto","text":"int generic_sum(std::vector const &v, auto op) P } \u8fd9\u91cc\u7684\u53c2\u6570 op \u7c7b\u578b\u58f0\u660e\u4e3a auto\uff0c\u6548\u679c\u5c31\u662f\uff0cop \u8fd9\u4e2a\u53c2\u6570\u73b0\u5728\u80fd\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u4e86\uff08\u5305\u62ec\u51fd\u6570\uff01\uff09 int generic_sum(std::vector const &v, auto op) { ... } \u51c6\u786e\u7684\u8bf4\uff0c auto op \u53c2\u6570\u7684\u6548\u679c\u662f\u4f7f generic_sum \u53d8\u4e3a\u4e00\u4e2a \u6a21\u677f\u51fd\u6570 \uff0c\u5176\u4e2d op \u53c2\u6570\u53d8\u6210\u4e86\u6a21\u677f\u53c2\u6570\uff0c\u80fd\u591f\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u4e86\u3002\u800c\u5199\u660e\u7c7b\u578b\u7684\u53c2\u6570 std::vector const &v \u5c31\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u6548\u679c\uff0c\u5c31\u53ea\u80fd\u63a5\u53d7 vector \u800c\u5df2\u3002 \u5982\u679c\u4f60\u4e0d\u652f\u6301 C++20 \u7684\u8bdd\uff0c\u9700\u8981\u663e\u5f0f\u5199\u51fa template \uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff1a template int generic_sum(std::vector const &v, Op op) { ... } C++11\uff1aauto \u53ea\u80fd\u7528\u4e8e\u5b9a\u4e49\u53d8\u91cf\uff1bC++14\uff1a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u662f auto\uff1bC++17\uff1a\u6a21\u677f\u53c2\u6570\u4e5f\u53ef\u4ee5 auto\uff1bC++20\uff1a\u51fd\u6570\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f auto \u4e86\uff1b\uff08\u72c2\u60f3\uff09C++47\uff1aauto \u73b0\u5728\u662f C++47 \u7684\u552f\u4e00\u5173\u952e\u5b57\uff0c\u7528\u6237\u53ea\u9700\u4e0d\u65ad\u8f93\u5165 auto-auto-auto\uff0c\u7f16\u8bd1\u5668\u5185\u5efa\u4eba\u5de5\u667a\u80fd\u81ea\u52a8\u8bc6\u522b\u4f60\u7684\u610f\u56fe\u751f\u6210\u673a\u5668\u7801\u3002","title":"\u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6"},{"location":"functional/#_12","text":"\u5728\u8fc7\u53bb\u7684 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f \u4e2d\uff0c\u51fd\u6570\uff08\u4ee3\u7801\uff09\u548c\u5bf9\u8c61\uff08\u6570\u636e\uff09\u88ab \u5272\u88c2 \u5f00\u6765\uff0c\u4ed6\u4eec\u611a\u6627\u5730\u8ba4\u4e3a \u51fd\u6570\u4e0d\u662f\u5bf9\u8c61 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f \u5219\u8ba4\u4e3a\uff1a \u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u53d8\u91cf\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\uff01 Function lives matter! \u9762\u5411\u5bf9\u8c61\u5c31\u597d\u6bd4\u8ba1\u7b97\u673a\u7684\u201c\u54c8\u4f5b\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u548c\u6570\u636e\u5272\u88c2\uff0c\u4ee3\u7801\u53ea\u80fd\u5355\u65b9\u9762\u64cd\u4f5c\u6570\u636e\u3002\u51fd\u6570\u5f0f\u5c31\u597d\u6bd4\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u4e5f\u662f\u6570\u636e\u3002\u770b\u4f3c\u4f1a\u5bfc\u81f4\u4f4e\u6548\uff0c\u5b9e\u5219\u5927\u5927\u65b9\u4fbf\u4e86\u52a8\u6001\u52a0\u8f7d\u65b0\u7a0b\u5e8f\uff0c\u56e0\u800c\u73b0\u5728\u7684\u8ba1\u7b97\u673a\u57fa\u672c\u90fd\u91c7\u7528\u4e86\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\u3002 \u603b\u4e4b\uff0c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u88ab\u4eb2\u5207\u5730\u5c0a\u79f0\u4e3a \u51fd\u6570\u5bf9\u8c61 \u3002","title":"\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01"},{"location":"functional/#c11-lambda","text":"C++98 \u65f6\u4ee3\uff0c\u4eba\u4eec\u8fd8\u9700\u8981\u5355\u72ec\u8dd1\u5230 main \u5916\u9762\uff0c\u4e13\u95e8\u5b9a\u4e49 add \u3001 mul \u3001 max \u51fd\u6570\u3002\u5f04\u5f97\u6574\u4e2a\u4ee3\u7801\u4e71\u54c4\u54c4\u7684\uff0c\u975e\u5e38\u9ebb\u70e6\u3002 int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } C++11 \u5f15\u5165\u4e86 Lambda \u8868\u8fbe\u5f0f \u8bed\u6cd5\uff0c\u5141\u8bb8\u4f60\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002 int main() { std::vector a = {1, 2, 3, 4}; auto add = [](int a, int b) { return a + b; }; auto mul = [](int a, int b) { return a * b; }; auto max = [](int a, int b) { return std::max(a, b); }; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u4e0d\u7528\u5f80 main \u5916\u9762\u585e\u5783\u573e\u4e86\uff0c\u4e00\u6e05\u723d\u3002 \u66f4\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u751a\u81f3\u4e0d\u7528\u5b9a\u4e49\u53d8\u91cf\uff0c\u76f4\u63a5\u628a Lambda \u8868\u8fbe\u5f0f\u5199\u5728 generic_sum \u7684\u53c2\u6570\u91cc\u5c31\u884c\u4e86\uff01 int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); // ***\u6539*** return 0; } \u4ee5\u4e0a\u5199\u6cd5\u90fd\u662f\u7b49\u4ef7\u7684\u3002 \u8981\u652f\u6301\u4e00\u4e2a\u65b0\u64cd\u4f5c\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5730\u65b9\uff1a\u5728\u8c03\u7528 generic_sum \u65f6\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002\u968f\u53eb\u968f\u5230\uff0c\u4e0d\u7528\u7ea0\u7ed3\u4e8e\u201c\u8d77\u540d\u5f3a\u8feb\u75c7\u201d\uff0c\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u5462\uff1f \u51c6\u786e\u7684\u8bf4\uff0cLambda \u521b\u5efa\u7684\u662f\u51fd\u6570\u5bf9\u8c61 (function object) \u6216\u79f0\u4eff\u51fd\u6570 (functor) \u800c\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u3002 \u5176\u5b9e C++98 \u65f6\u4ee3\u4eba\u4eec\u5c31\u5df2\u7ecf\u5927\u91cf\u5728\u7528 operator()() \u6a21\u62df\u51fd\u6570\u5bf9\u8c61\u4e86\uff0c\u8457\u540d\u7684\u7b2c\u4e09\u65b9\u5e93 Boost \u4e5f\u5c01\u88c5\u4e86\u5404\u79cd\u51fd\u6570\u5f0f\u5e38\u7528\u7684\u5bb9\u5668\u548c\u5de5\u5177\u3002C++11 \u624d\u7ec8\u4e8e\u628a \u51fd\u6570\u5bf9\u8c61 \u8fd9\u4e2a\u6982\u5ff5\u8f6c\u6b63\uff0c\u5e76\u5f15\u5165\u4e86\u66f4\u65b9\u4fbf\u7684 Lambda \u8bed\u6cd5\u7cd6\u3002 \u5373\u4f7f\u662f\u9762\u5411\u5bf9\u8c61\u7684\u5934\u53f7\u5b5d\u5b50 Java\uff0c\u4e5f\u5df2\u7ecf\u5f00\u59cb\u5f15\u5165\u51fd\u6570\u5f0f\u7684 Lambda \u8bed\u6cd5\u7cd6\uff0cC# \u7684 LINQ \u66f4\u662f\u660e\u76ee\u5f20\u80c6\u7684\u81f4\u656c map-reduce \u5168\u5bb6\u6876\uff0c\u751a\u81f3 C \u8bed\u8a00\u7528\u6237\u4e5f\u5f00\u59cb\u73a9\u5404\u79cd\u51fd\u6570\u6307\u9488\u56de\u8c03\u2026\u2026\u6ca1\u529e\u6cd5\uff0c\u51fd\u6570\u5f0f\u786e\u5b9e\u65b9\u4fbf\u5440\uff01","title":"C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6"},{"location":"functional/#_13","text":"\u51fd\u6570\u5bf9\u8c61 op \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8ba9 generic_sum \u5185\u90e8\u53bb\u8c03\u7528\uff0c\u5c31\u50cf\u5f80 generic_sum \u4f53\u5185\u201c\u6ce8\u5165\u201d\u4e86\u4e00\u6bb5\u81ea\u5b9a\u4e49\u4ee3\u7801\u4e00\u6837\u3002 \u8fd9\u53ef\u4ee5\u8ba9 generic_sum \u5728\u4e0d\u4fee\u6539\u672c\u4f53\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u201c\u6ce8\u5165\u201d\u90e8\u5206\uff0c\u8f7b\u677e\u6269\u5c55\uff0c\u6ee1\u8db3 \u5f00\u95ed\u539f\u5219 \u3002 \u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u8fd9\u4f53\u73b0\u7684\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8981\u6c42\u7684 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u3002 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219: \u4e00\u4e2a\u5c01\u88c5\u597d\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u56db\u5927\u7f16\u7a0b\u8303\u5f0f\u90fd\u5404\u81ea\u53d1\u5c55\u51fa\u4e86 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u7684\u89e3\u51b3\u65b9\u6848\uff1a \u9762\u5411\u8fc7\u7a0b\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u6307\u9488 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u865a\u51fd\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u5bf9\u8c61 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u6a21\u677f\u5143\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u6a21\u677f\u53c2\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u540c\u6837\u662f\u628a\u62bd\u8c61\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u6837\u89e3\u51b3\u53ef\u6269\u5c55\u95ee\u9898\u3002 \u51fd\u6570\u6307\u9488\u8d34\u8fd1\u5e95\u5c42\u786c\u4ef6\uff0c\u865a\u51fd\u6570\u65b9\u4fbf\u6574\u5408\u591a\u4e2a\u63a5\u53e3\uff0c\u51fd\u6570\u5bf9\u8c61\u8f7b\u91cf\u7ea7\u3001\u968f\u5730\u53d6\u7528\uff0c\u6a21\u677f\u5143\u6709\u52a9\u9ad8\u6027\u80fd\u4f18\u5316\uff0c\u4e0d\u540c\u7684\u7f16\u7a0b\u8303\u5f0f\u6b8a\u9014\u540c\u5f52\u3002","title":"\u4f9d\u8d56\u6ce8\u5165\u539f\u5219"},{"location":"functional/#_14","text":"\u4f9d\u8d56\u6ce8\u5165\u539f\u5219\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u8026\u5408\u5ea6: \u6307\u7684\u662f\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u548c\u5176\u4ed6\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u8026\u5408\u5ea6\u8d8a\u4f4e\uff0c\u8d8a\u5bb9\u6613\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3001\u91cd\u6784\u3001\u590d\u7528\u548c\u6269\u5c55\u3002 \u9ad8\u8026\u5408\u5ea6\u7684\u5178\u578b\u662f\u201c\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab\u201d\u3002\u4f4e\u8026\u5408\u7684\u5178\u8303\u662f\u86af\u8693\uff0c\u56e0\u4e3a\u86af\u8693\u53ef\u4ee5\u5728\u4efb\u610f\u65ad\u9762\u5207\u5f00\uff0c\u8fd8\u80fd\u6d3b\u4e0b\u6765\uff0c\u770b\u6765\u86af\u8693\u7684\u8eab\u4f53\u8bbe\u8ba1\u975e\u5e38\u201c\u6a21\u5757\u5316\u201d\u5462\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u8f6f\u4ef6\u5e94\u5f53\u8ffd\u6c42\u4f4e\u8026\u5408\u5ea6\uff0c\u9002\u5ea6\u89e3\u8026\u7684\u8f6f\u4ef6\u80fd\u66f4\u5feb\u9002\u5e94\u9700\u6c42\u53d8\u5316\u3002\u4f46\u8fc7\u5ea6\u7684\u4f4e\u8026\u5408\u4e5f\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fc7\u4e8e\u5206\u6563\uff0c\u4e0d\u6613\u9605\u8bfb\u548c\u4fee\u6539\uff0c\u751a\u81f3\u53ef\u80fd\u8d77\u5230\u53cd\u6548\u679c\u3002 \u82e5\u4f60\u89e3\u8026\u540e\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u5316\u8981\u6539\u52a8\u7684\u5730\u65b9\u53d8\u5c11\u4e86\uff0c\u90a3\u5c31\u662f\u5408\u7406\u7684\u89e3\u8026\u3002\u82e5\u4f60\u8fc7\u5206\u89e3\u8026\uff0c\u4ee3\u7801\u4e1c\u4e00\u5757\u897f\u4e00\u5757\uff0c\u4ee5\u81f3\u4e8e\u9700\u6c42\u53d8\u5316\u65f6\u9700\u8981\u5230\u5904\u6539\uff0c\u6bd4\u4e0d\u89e3\u8026\u65f6\u6d6a\u8d39\u7684\u65f6\u95f4\u8fd8\u8981\u591a\uff0c\u90a3\u5c31\u662f\u89e3\u8026\u8fc7\u5ea6\u3002 \u5b8c\u5168\u96f6\u8026\u5408\u7684\u7a0b\u5e8f\u6bcf\u4e2a\u51fd\u6570\u4e92\u4e0d\u8054\u7cfb\uff0c\u5c31\u50cf\u628a\u86af\u8693\u62c6\u6563\u6210\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u7ec6\u80de\u4e00\u6837\u3002\u8fde\u521d\u59cb\u9700\u6c42\u201c\u6d3b\u7740\u201d\u90fd\u5b9e\u73b0\u4e0d\u4e86\uff0c\u8c08\u4f55\u9002\u5e94\u9700\u6c42\u53d8\u5316\uff1f\u6240\u4ee5\u89e3\u8026\u4e5f\u5207\u52ff\u77eb\u6789\u8fc7\u6b63\u3002 \u4e3a\u4e86\u907f\u514d\u89e3\u8026\u77eb\u6789\u8fc7\u6b63\uff0c\u4eba\u4eec\u53c8\u63d0\u51fa\u4e86\u5185\u805a\u7684\u6982\u5ff5\uff0c\u5e76\u89c4\u5b9a\u89e3\u8026\u7684\u524d\u63d0\u662f\uff1a\u4e0d\u803d\u8bef\u5185\u805a\u3002\u803d\u8bef\u5230\u5185\u805a\u7684\u89e3\u8026\uff0c\u5c31\u53ea\u4f1a\u8d77\u5230\u964d\u4f4e\u53ef\u7ef4\u62a4\u6027\u7684\u53cd\u6548\u679c\u4e86\u3002 \u5185\u805a: \u6307\u7684\u662f\u540c\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u5185\u90e8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u5185\u805a\u5ea6\u8d8a\u9ad8\uff0c\u529f\u80fd\u8d8a\u72ec\u7acb\uff0c\u8d8a\u65b9\u4fbf\u96c6\u4e2d\u7ef4\u62a4\u3002 \u4f8b\u5982\uff0c\u4eba\u7684\u5fc3\u810f\u4e13\u95e8\u8d1f\u8d23\u6cf5\u8840\uff0c\u809d\u810f\u53ea\u8d1f\u8d23\u89e3\u6bd2\uff0c\u8fd9\u5c31\u662f\u9ad8\u5185\u805a\u7684\u4eba\u4f53\u5668\u5b98\u3002\u82e5\u4eba\u7684\u5fc3\u810f\u8fd8\u8981\u517c\u804c\u89e3\u6bd2\uff0c\u809d\u810f\u8fd8\u517c\u804c\u6cf5\u8840\uff0c\u770b\u4f3c\u597d\u50cf\u662f\u589e\u52a0\u4e86\u201c\u4e07\u4e00\u5fc3\u810f\u574f\u6389\u201d\u7684\u5197\u4f59\u6027\uff0c\u5b9e\u9645\u4e0a\u628a\u201c\u6cf5\u8840\u201d\u8fd9\u4e00\u529f\u80fd\u62c6\u6563\u5230\u5404\u5730\uff0c\u65e0\u6cd5\u201c\u96c6\u4e2d\u529b\u91cf\u6cf5\u5927\u8840\u201d\u4e86\u3002 \u4eba\u7c7b\u7684\u5927\u8111\u548c CPU \u4e00\u6837\uff0c\u4e5f\u6709\u201c\u7f13\u5b58\u5c40\u57df\u6027 (cache-locality)\u201d\u7684\u9650\u5236\uff1a\u4e0d\u80fd\u540c\u65f6\u5728\u5f88\u591a\u4e2a\u4e3b\u9898\u4e4b\u95f4\u5feb\u901f\u5207\u6362\uff0c\u65e0\u8bba\u662f\u65f6\u95f4\u4e0a\u7684\u8fd8\u662f\u7a7a\u95f4\u4e0a\u7684\u5272\u88c2 (cache-miss)\uff0c\u90fd\u4f1a\u5e72\u6270\u7a0b\u5e8f\u5458\u601d\u7ef4\u7684\u8fde\u8d2f\u6027\uff0c\u4ece\u800c\u589e\u5927\u5fc3\u667a\u8d1f\u62c5\u3002 \u597d\u7684\u8f6f\u4ef6\u8981\u4fdd\u6301\u4f4e\u8026\u5408\uff0c\u540c\u65f6\u9ad8\u5185\u805a\u3002 \u5c31\u50cf\u201c\u6c11\u4e3b\u96c6\u4e2d\u5236\u201d\u4e00\u6837\uff0c\u65e2\u8981\u76d1\u7763\u9632\u6b62\u5927\u6743\u72ec\u63fd\uff0c\u53c8\u8981\u96c6\u4e2d\u529b\u91cf\u529e\u4e00\u4e2a\u4eba\u529e\u4e0d\u6210\u7684\u5927\u4e8b\u3002","title":"\u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a"},{"location":"functional/#_15","text":"\u4f20\u7edf\u7684\u9762\u5411\u5bf9\u8c61\u540c\u6837\u53ef\u4ee5\u7528 \u865a\u51fd\u6570\u63a5\u53e3\u7c7b \u6a21\u62df \u51fd\u6570\u5bf9\u8c61 \u4e00\u6837\u7684\u529f\u80fd\uff0c\u53ea\u4e0d\u8fc7\u6ca1\u6709 lambda \u548c\u95ed\u5305\u7684\u8bed\u6cd5\u52a0\u6301\uff0c\u5199\u8d77\u6765\u975e\u5e38\u7e41\u7410\uff0c\u5c31\u548c\u5728 C \u8bed\u8a00\u91cc\u201c\u6a21\u62df\u201d\u9762\u5411\u5bf9\u8c61\u4e00\u6837\u3002 \u4e3a\u4e86\u8fd9\u4e48\u5c0f\u7684\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u5355\u72ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5c31\u50cf\u5988\u5988\u5f00\u4e00\u67b6\u201c\u7a7a\u4e2d\u6218\u8f66\u201d A380 \u53ea\u662f\u4e3a\u4e86\u63a5\u4f60\u653e\u5b66\u4e00\u6837\uff0c\u7b49\u4f60\u503c\u597d\u673a\u7684\u65f6\u95f4\u6211\u81ea\u5df1\u8d70\u90fd\u8d70\u5230\u4e86\u3002\u800c\u51fd\u6570\u5f0f\u4e2d\uff0c\u7528 lambda \u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e\u968f\u5730\u6293\u6765\u4e00\u53f0\u5171\u4eab\u5355\u8f66\u5f00\u8d70\u3002 struct OpBase { // \u9762\u5411\u5bf9\u8c61\uff1a\u9047\u4e8b\u4e0d\u51b3\u5148\u5b9a\u4e49\u63a5\u53e3\u2026\u2026 virtual int compute(int a, int b) = 0; virtual ~OpBase() = default; }; struct OpAdd : OpBase { int compute(int a, int b) override { return a + b; } }; struct OpMul : OpBase { int compute(int a, int b) override { return a * b; } }; struct OpMax : OpBase { int compute(int a, int b) override { return std::max(a, b); } }; int generic_sum(std::vector const &v, OpBase *op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op->compute(ret, v[i]); // \u5199\u8d77\u6765\u4e5f\u9ebb\u70e6\uff0c\u9700\u8981\u8c03\u7528\u4ed6\u7684\u6210\u5458\u51fd\u6570\uff0c\u6210\u5458\u51fd\u6570\u53c8\u8981\u8d77\u540d\u2026\u2026 } delete op; return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, new OpAdd()); generic_sum(a, new OpMul()); generic_sum(a, new OpMax()); return 0; } \u4e0d\u4ec5\u9700\u8981\u5b9a\u4e49\u4e00\u5806\u7c7b\uff0c\u63a5\u53e3\u7c7b\uff0c\u5b9e\u73b0\u7c7b\uff0c\u7ee7\u627f\u6765\u7ee7\u627f\u53bb\uff0c\u8fd8\u9700\u8981\u7ba1\u7406\u8ba8\u538c\u7684\u6307\u9488\uff0c\u4ee3\u7801\u91cf\u7ffb\u500d\uff0c\u6ca1\u4ec0\u4e48\u53ef\u8bfb\u6027\uff0c\u53c8\u5f71\u54cd\u8fd0\u884c\u6548\u7387\u3002 3 \u5e74 2 \u73ed\u5c0f\u5f6d\u540c\u5b66\uff0c\u4f60\u7684\u5988\u5988\u5f00\u7740 A380 \u6765\u63a5\u4f60\u4e86\u3002 \u800c\u73b0\u4ee3 C++ \u53ea\u9700 Lambda \u8bed\u6cd5\u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u723d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); });","title":"\u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4"},{"location":"functional/#_16","text":"\u521a\u521a\uff0c\u6211\u4eec\u7684\u5b9e\u73b0\u7528\u4e86 auto op \u505a\u53c2\u6570\uff0c\u8fd9\u7b49\u4ef7\u4e8e\u8ba9 generic_sum \u53d8\u6210\u4e00\u4e2a\u6a21\u677f\u51fd\u6570\u3002 int generic_sum(std::vector const &v, auto op); // \u4e0d\u652f\u6301 C++20 \u65f6\u7684\u66ff\u4ee3\u5199\u6cd5\uff1a template int generic_sum(std::vector const &v, Op op); \u8fd9\u610f\u5473\u7740\u6bcf\u5f53\u7528\u6237\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\uff08lambda\uff09\u65f6\uff0c generic_sum \u90fd\u4f1a\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u904d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u7f16\u8bd1\u540e\uff0c\u4f1a\u53d8\u6210\u7c7b\u4f3c\u4e8e\u8fd9\u6837\uff1a generic_sum(a); generic_sum(a); generic_sum(a); \u4f1a\u751f\u6210\u4e09\u4efd\u51fd\u6570\uff0c\u6bcf\u4e2a\u90fd\u662f\u72ec\u7acb\u7f16\u8bd1\u7684\uff1a int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = add(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = mul(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = max(ret, v[i]); } return ret; } \u8fd9\u5141\u8bb8\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e2a\u7248\u672c\u7684 generic_sum \u5355\u72ec\u505a\u4f18\u5316\uff0c\u91cf\u8eab\u5b9a\u5236\u6700\u4f18\u7684\u4ee3\u7801\u3002 \u4f8b\u5982 add \u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u56e0\u4e3a\u53ea\u5728 generic_sum \u4e2d\u4f7f\u7528\u4e86\uff0c\u4f1a\u88ab\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u5185\u8054\uff0c\u4e0d\u4f1a\u4ea7\u751f\u51fd\u6570\u8c03\u7528\u548c\u8df3\u8f6c\u7684\u6307\u4ee4\uff0c\u5404\u81ea\u4f18\u5316\u6210\u5355\u72ec\u4e00\u6761\u52a0\u6cd5 / \u4e58\u6cd5 / \u6700\u5927\u503c\u6307\u4ee4\u7b49\u3002 \u6bd4\u5982\uff0c\u7f16\u8bd1\u5668\u4f1a\u68c0\u6d4b\u5230 += \u53ef\u4ee5\u77e2\u91cf\u5316\uff0c\u4e8e\u662f\u7528 _mm_add_epi32 \u66ff\u4ee3\u4e86\u3002\u540c\u7406\uff0cmul \u5219\u7528 _mm_mullo_epi32 \u66ff\u4ee3\uff0cmax \u5219\u7528 _mm_max_epi32 \u66ff\u4ee3\u7b49\uff0c\u5404\u81ea\u5206\u522b\u751f\u6210\u4e86\u5404\u81ea\u7248\u672c\u6700\u4f18\u7684\u4ee3\u7801\u3002\u800c\u5982\u679c\u662f\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u4e0d\u4f1a\u751f\u6210\u4e09\u4efd\u91cf\u8eab\u5b9a\u505a\u7684\u5b9e\u4f8b\uff0c\u65e0\u6cd5\u77e2\u91cf\u5316\uff08\u6709\u4e00\u79cd\u4f8b\u5916\uff0c\u5c31\u662f\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u4e86 generic_sum \u4f3c\u4e4e\u53ea\u6709\u8fd9\u4e09\u79cd\u53ef\u80fd\u53c2\u6570\uff0c\u7136\u540e\u505a\u4e86 IPO \u4f18\u5316\uff0c\u4f46\u5e76\u4e0d\u5982\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u6837\u7a33\u5b9a\u5f3a\u5236\uff09\u3002 \u4e3a\u4e09\u79cd\u4e0d\u540c\u7684 op \u53c2\u6570\u5206\u522b\u5b9a\u505a\u4e09\u4efd\u3002\u867d\u7136\u589e\u52a0\u4e86\u7f16\u8bd1\u65f6\u95f4\uff0c\u81a8\u80c0\u4e86\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u4f53\u79ef\uff1b\u4f46\u751f\u6210\u7684\u673a\u5668\u7801\u662f\u5206\u522b\u9488\u5bf9\u6bcf\u79cd\u7279\u4f8b\u4e00\u5bf9\u4e00\u6df1\u5ea6\u4f18\u5316\u7684\uff0c\u66f4\u9ad8\u6548\u3002 \u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\uff08gemm\uff09\u7684\u6700\u4f18\u7b97\u6cd5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u77e9\u9635\u5927\u5c0f\u548c\u5f62\u72b6\u662f\u4e0d\u540c\u7684\u3002\u8457\u540d\u7684\u7ebf\u6027\u4ee3\u6570\u5e93 CUBLAS \u548c MKL \u4e2d\uff0c\u4f1a\u81ea\u52a8\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\u5f62\u72b6\uff0c\u9009\u53d6\u6700\u4f18\u7684\u7b97\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cCUBLAS \u5e93\u91cc\u5176\u5b9e\u5b58\u7740\u9002\u5408\u5404\u79cd\u77e9\u9635\u5927\u5c0f\u6392\u5217\u7ec4\u5408\u7684\u7b97\u6cd5\u4ee3\u7801\uff08\u4ee5 fatbin \u683c\u5f0f\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u4e2d\uff09\u3002\u5f53\u8c03\u7528\u77e9\u9635\u4e58\u6cd5\u65f6\uff0c\u81ea\u52a8\u67e5\u5230\u6700\u9002\u5408\u7684\u4e00\u7248\u6765\u8c03\u7528\u7ed9\u4f60\u3002\u7c7b\u4f3c gemm\uff0c\u8fd8\u6709 gemv\u3001spmv\u2026\u2026\u6240\u6709\u7684\u77e9\u9635\u8fd0\u7b97 API \u90fd\u7ecf\u5386\u4e86\u8fd9\u6837\u7684\u201c\u7f16\u8bd1\u671f\u201d\u66b4\u529b\u6392\u5217\u7ec4\u5408\uff0c\u53ea\u4e3a\u201c\u8fd0\u884c\u65f6\u201d\u91ca\u653e\u6700\u5927\u6027\u80fd\uff01\u8fd9\u4e5f\u5bfc\u81f4\u7f16\u8bd1\u597d\u7684 cublas.dll \u6587\u4ef6\u6765\u5230\u4e86\u6050\u6016\u7684 20 MB \u5de6\u53f3\uff0c\u800c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9ad8\u6548\u3002","title":"\u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1"},{"location":"functional/#function","text":"Lambda \u51fd\u6570\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u533f\u540d\u7684\uff0c\u6bcf\u4e2a Lambda \u8868\u8fbe\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97 generic_sum \u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684 Lambda \u90fd\u4f1a\u5b9e\u4f8b\u5316\u4e00\u904d\u3002\u867d\u7136\u6709\u5229\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u4f46\u4e5f\u5f71\u54cd\u4e86\u7f16\u8bd1\u901f\u5ea6\u548c\u7075\u6d3b\u6027\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7 decltype(add) \u83b7\u53d6 add \u8fd9\u4e2a Lambda \u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4e5f\u53ea\u80fd\u901a\u8fc7 auto \u6765\u6355\u83b7 Lambda \u5bf9\u8c61\u4e3a\u53d8\u91cf\u3002 \u4e3a\u6b64\uff0c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::function \u5bb9\u5668\uff0c\u4ed6\u80fd\u5bb9\u7eb3\u4efb\u4f55\u51fd\u6570\u5bf9\u8c61\uff01\u65e0\u8bba\u662f\u533f\u540d\u7684 Lambda \u51fd\u6570\u5bf9\u8c61\uff0c\u8fd8\u662f\u666e\u666e\u901a\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u90fd\u80fd\u7eb3\u5165 std::function \u7684\u4f53\u5185\u3002 \u552f\u4e00\u7684\u4ee3\u4ef7\u662f\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u51fa\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002 \u4f8b\u5982\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e24\u4e2a int \uff0c std::function auto add_lambda = [](int a, int b) { // Lambda \u51fd\u6570\u5bf9\u8c61 return a + b; }; struct AddClass { int operator()(int a, int b) { // \u81ea\u5b9a\u4e49\u7c7b\u6a21\u62df\u51fd\u6570\u5bf9\u8c61 return a + b; } }; AddClass add_object; int add_regular_func(int a, int b) { // \u666e\u901a\u51fd\u6570 return a + b; } std::function add; // \u6240\u6709\u5e7f\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u7edf\u7edf\u63a5\u7eb3 add = add_lambda; // OK add = add_object; // OK add = add_regular_func; // OK int generic_sum(std::vector const &v, std::function op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op(ret, v[i]); // \u5199\u8d77\u6765\u548c\u6a21\u677f\u4f20\u53c2\u65f6\u4e00\u6837\u65e0\u611f } // \u65e0\u9700\u6307\u9488\uff0c\u65e0\u9700 delete\uff0cfunction \u80fd\u81ea\u52a8\u7ba1\u7406\u51fd\u6570\u5bf9\u8c61\u751f\u547d\u5468\u671f return ret; } \u5982\u679c\u8fd8\u60f3\u652f\u6301\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u770b std::function \u3002\u8fd9\u91cc std::any \u662f\u4e2a\u8d85\u7ea7\u4e07\u80fd\u5bb9\u5668\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u5bf9\u8c61\uff0c\u4ed6\u548c std::function \u4e00\u6837\u90fd\u91c7\u7528\u4e86\u201c\u7c7b\u578b\u64e6\u9664 (type-erasure)\u201d\u6280\u672f\uff0c\u7f3a\u70b9\u662f\u5fc5\u987b\u914d\u5408 std::any_cast \u624d\u80fd\u53d6\u51fa\u4f7f\u7528\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u8fdb\u9636\u4e13\u9898\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u4ed6\u4eec\u7684\u539f\u7406\uff0c\u5e76\u5e26\u4f60\u81ea\u5df1\u505a\u4e00\u4e2a\u64e6\u52a0\u6cd5\u7684\u7c7b\u578b\u64e6\u9664\u5bb9\u5668\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u80fd\u5728\u9759\u6001\u4e0e\u52a8\u6001\u4e4b\u95f4\u8f7b\u677e\u5207\u6362\uff0c \u9ad8\u6027\u80fd \u4e0e \u7075\u6d3b\u6027 \u4efb\u541b\u9009\u62e9\u3002 \u5728\u9700\u8981\u6027\u80fd\u7684 \u74f6\u9888\u4ee3\u7801 \u4e2d\u7528\u6a21\u677f\u4f20\u53c2\uff0c\u7f16\u8bd1\u671f\u9759\u6001\u5206\u53d1\uff0c\u591a\u6b21\u91cf\u8eab\u5b9a\u505a\uff0c\u63d0\u9ad8\u8fd0\u884c\u65f6\u6027\u80fd\u3002 \u74f6\u9888\u4ee3\u7801: \u5f80\u5f80\u4e00\u4e2a\u7a0b\u5e8f 80% \u7684\u65f6\u95f4\u82b1\u5728 20% \u7684\u4ee3\u7801\u4e0a\u3002\u8fd9 20% \u662f\u5728\u7a0b\u5e8f\u4e2d\u9891\u7e41\u6267\u884c\u7684\u3001\u8ba1\u7b97\u91cf\u5927\u7684\u3001\u6216\u8005\u8c03\u7528\u7279\u522b\u8017\u65f6\u7684\u51fd\u6570\u3002\u9488\u5bf9\u8fd9\u90e8\u5206\u74f6\u9888\u4ee3\u7801\u4f18\u5316\u5373\u53ef\uff0c\u800c\u5269\u4f59\u7684 80% \u6253\u9171\u6cb9\u4ee3\u7801\uff0c\u5927\u53ef\u4ee5\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u5199\u3002 \u5728\u6027\u80fd\u65e0\u5173\u7d27\u8981\u7684\u9876\u5c42\u4e1a\u52a1\u903b\u8f91\u4e2d\u7528 function \u5bb9\u5668\u4f20\u53c2\uff0c\u8fd0\u884c\u65f6\u52a8\u6001\u5206\u53d1\uff0c\u8282\u7701\u7f16\u8bd1\u4f53\u79ef\uff0c\u65b9\u4fbf\u6301\u4e45\u5b58\u50a8\uff0c\u7075\u6d3b\u6613\u7528\u3002 \u4f8b\u5982\u4e0a\u9762\u7684 generic_sum \u51fd\u6570\uff0c\u5982\u679c\u6211\u4eec\u7a81\u7136\u60f3\u8981\u9ad8\u6027\u80fd\u4e86\uff0c\u53ea\u9700\u628a std::function op \u8f7b\u8f7b\u6539\u4e3a auto op \u5c31\u8f7b\u677e\u5207\u6362\u5230\u9759\u6001\u5206\u53d1\u6a21\u5f0f\u4e86\u3002 \u800c\u865a\u51fd\u6570\u4e00\u65e6\u7528\u4e86\uff0c\u57fa\u672c\u5c31\u53ea\u80fd\u52a8\u6001\u5206\u53d1\u4e86\uff0c\u5373\u4f7f\u80fd\u88ab IPO \u4f18\u5316\u6389\uff0c\u865a\u8868\u6307\u9488\u4e5f\u6c38\u8fdc\u5360\u636e\u7740\u4e00\u4e2a 8 \u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u4e14\u6c38\u8fdc\u53ea\u80fd\u4ee5\u6307\u9488\u5f62\u5f0f\u4f20\u6765\u4f20\u53bb\u3002 \u4e00\u79cd\u9759\u6001\u5206\u53d1\u7248\u7684\u865a\u51fd\u6570\u66ff\u4ee3\u54c1\u662f CRTP\uff0c\u4ed6\u57fa\u4e8e\u6a21\u677f\u5143\u7f16\u7a0b\uff0c\u4f46\u4e0e\u865a\u51fd\u6570\u4e4b\u95f4\u5207\u6362\u56f0\u96be\uff0c\u4e0d\u50cf\u51fd\u6570\u5bf9\u8c61\u90a3\u4e48\u65e0\u611f\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u4e13\u9898\u8bfe\u4e2d\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1"},{"location":"functional/#_17","text":"mt_queue> task_queue; void thread1() { task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a11\"); }); task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a12\"); }); } void thread2() { while (true) { auto task = task_queue.pop(); task(); } } mt_queue \u662f\u5c0f\u5f6d\u8001\u5e08\u5c01\u88c5\u7684\u591a\u7ebf\u7a0b\u5b89\u5168\u7684\u6d88\u606f\u961f\u5217\uff0c\u5b9e\u73b0\u539f\u7406\u4f1a\u5728\u7a0d\u540e\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4e2d\u8be6\u7ec6\u8bb2\u89e3\u3002","title":"\u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217"},{"location":"functional/#_18","text":"","title":"\u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305"},{"location":"functional/#c","text":"","title":"\u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389"},{"location":"functional/#bind","text":"int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello(2, 3) \u7ed3\u679c\uff1a{}\", hello(2, 3)); fmt::println(\"main \u8c03\u7528 hello(2, 4) \u7ed3\u679c\uff1a{}\", hello(2, 4)); fmt::println(\"main \u8c03\u7528 hello(2, 5) \u7ed3\u679c\uff1a{}\", hello(2, 5)); return 0; } int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello2(3) \u7ed3\u679c\uff1a{}\", hello2(3)); fmt::println(\"main \u8c03\u7528 hello2(4) \u7ed3\u679c\uff1a{}\", hello2(4)); fmt::println(\"main \u8c03\u7528 hello2(5) \u7ed3\u679c\uff1a{}\", hello2(5)); return 0; }","title":"bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570"},{"location":"functions/","text":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u81ea\u5b9a\u4e49\u51fd\u6570 \u81ea\u5b9a\u4e49\u51fd\u6570 \u51fd\u6570\u53ef\u4ee5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u7c7b\u578b\u5199 void \u5373\u53ef\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7684\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u4ed6\u7684\u526f\u4f5c\u7528\uff08\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8f93\u51fa\u6587\u672c\u5230\u63a7\u5236\u53f0\uff0c\u4fee\u6539\u5f15\u7528\u53c2\u6570\u7b49\uff09\u3002 void compute() { return; } \u5bf9\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a void \uff09\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u7565 return \u4e0d\u5199\u3002 \u5bf9\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u5199 return \u8bed\u53e5\uff0c\u5426\u5219\u7a0b\u5e8f\u51fa\u9519\u3002 TODO\uff1a\u66f4\u591a\u4ecb\u7ecd\u51fd\u6570","title":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5)"},{"location":"functions/#_1","text":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u81ea\u5b9a\u4e49\u51fd\u6570","title":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5)"},{"location":"functions/#_2","text":"\u51fd\u6570\u53ef\u4ee5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u7c7b\u578b\u5199 void \u5373\u53ef\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7684\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u4ed6\u7684\u526f\u4f5c\u7528\uff08\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8f93\u51fa\u6587\u672c\u5230\u63a7\u5236\u53f0\uff0c\u4fee\u6539\u5f15\u7528\u53c2\u6570\u7b49\uff09\u3002 void compute() { return; } \u5bf9\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a void \uff09\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u7565 return \u4e0d\u5199\u3002 \u5bf9\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u5199 return \u8bed\u53e5\uff0c\u5426\u5219\u7a0b\u5e8f\u51fa\u9519\u3002 TODO\uff1a\u66f4\u591a\u4ecb\u7ecd\u51fd\u6570","title":"\u81ea\u5b9a\u4e49\u51fd\u6570"},{"location":"helloworld/","text":"\u4f60\u597d\uff0c\u4e16\u754c \u4f60\u597d\uff0c\u4e16\u754c \u4ec0\u4e48\u662f\u51fd\u6570 \u4ece main \u51fd\u6570\u8bf4\u8d77 main \u51fd\u6570\u7684\u8fd4\u56de\u503c \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f \u6253\u5370\u4e00\u4e9b\u4fe1\u606f \u6ce8\u91ca \u4ec0\u4e48\u662f\u51fd\u6570 \u51fd\u6570: \u4e00\u6bb5\u7528 {} \u5305\u88f9\u7684\u4ee3\u7801\u5757\uff0c\u6709\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u540d\u5b57\u505a\u6807\u8bc6\u3002\u51fd\u6570\u53ef\u4ee5\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002\u51fd\u6570\u7684 {} \u4ee3\u7801\u5757\u5185\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u6bcf\u6b21\u8be5\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u6267\u884c\u3002 int compute() { return 42; } \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c compute \u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c int \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u6574\u6570\u3002 \u4e43\u53d6\u6574\u6570\u4e4b\u82f1\u6587\u201cinteger\u201d\u7684\u201cint\u201d\u800c\u5f97\u540d\uff08\u6a21\u4eff\u4faf\u6377\u8001\u5e08\u8bf4\u8bdd\uff09 \u800c {} \u5305\u88f9\u7684\u662f\u51fd\u6570\u4f53\uff0c\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u3002 \u6b64\u5904 return 42 \u5c31\u662f\u51fd\u6570\u4f53\u5185\u7684\u552f\u4e00\u4e00\u6761\u8bed\u53e5\uff0c\u8868\u793a\u51fd\u6570\u7acb\u5373\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd4\u56de 42\u3002 \u8fd4\u56de\u503c: \u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4f1a\u5411\u8c03\u7528\u8be5\u51fd\u6570\u7684\u8c03\u7528\u8005\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f return \u540e\u9762\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6b64\u5904 compute \u7684\u8fd4\u56de\u7c7b\u578b\u662f int \uff0c\u4e5f\u5c31\u662f\u8bf4 compute \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002 \u5173\u4e8e\u51fd\u6570\u7684\u53c2\u6570\uff0c\u6211\u4eec\u7a0d\u540e\u518d\u505a\u8bf4\u660e\u3002 \u4ece main \u51fd\u6570\u8bf4\u8d77 C++ \u7a0b\u5e8f\u901a\u5e38\u7531\u4e00\u7cfb\u5217\u51fd\u6570\u7ec4\u6210\uff0c\u5176\u4e2d\u5fc5\u987b\u6709\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002 main \u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a int main() { } \u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u8c03\u7528 main \u51fd\u6570\u3002 \u4e25\u683c\u6765\u8bf4\uff0c\u662f C++ \u8fd0\u884c\u65f6\u8c03\u7528\u4e86 main \u51fd\u6570\uff0c\u4f46\u76ee\u524d\u5148\u7406\u89e3\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\u4e86 main \u51fd\u6570\u201d\u4e5f\u65e0\u59a8\u3002 \u8981\u628a\u7a0b\u5e8f\u53d1\u5c55\u58ee\u5927\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9 main \u51fd\u6570\u7ee7\u7eed\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u6574\u4e2a\u7a0b\u5e8f\u7684\u903b\u8f91\uff08\u4e0d\u63a8\u8350\uff09\u3002 \u56e0\u6b64\uff0c main \u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u201c\u5b87\u5b99\u5927\u7206\u70b8\u201d\u3002 main \u51fd\u6570\u7684\u8fd4\u56de\u503c int main() { return 0; } return \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\uff0cmain \u51fd\u6570\u8fd4\u56de\uff0c\u5373\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u7ed3\u675f\u3002 main \u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u6574\u6570 ( int \u7c7b\u578b)\uff0c\u7528\u8fd9\u4e2a\u6574\u6570\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u793a\u7a0b\u5e8f\u9000\u51fa\u7684\u539f\u56e0\u3002 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\u5b8c\u6bd5\uff0c\u6b63\u5e38\u7ed3\u675f\u9000\u51fa\uff0c\u90a3\u5c31\u8bf7\u8fd4\u56de 0\u3002 \u8fd4\u56de\u4e00\u4e2a\u4e0d\u4e3a 0 \u7684\u6574\u6570\u53ef\u4ee5\u8868\u793a\u7a0b\u5e8f\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u662f\u56e0\u4e3a\u51fa\u9519\u4e86\u624d\u9000\u51fa\u7684\uff0c\u503c\u7684\u591a\u5c11\u53ef\u4ee5\u7528\u4e8e\u8868\u660e\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\u3002 \u64cd\u4f5c\u7cfb\u7edf\uff1a\u6211\u8c03\u7528\u4e86\u4f60\u8fd9\u4e2a\u7a0b\u5e8f\u7684 main \u51fd\u6570\uff0c\u6211\u597d\u5947\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u4e86\uff1f\u8ba9\u6211\u4eec\u7ea6\u5b9a\u597d\uff1a\u5982\u679c\u4f60\u8fd0\u8f6c\u6b63\u5e38\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de0\u8868\u793a\u6210\u529f\u54e6\uff01\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u4ee3\u7801\uff0c\u6bd4\u5982\u8fd4\u56de1\u8868\u793a\u65e0\u6743\u9650\uff0c2\u8868\u793a\u627e\u4e0d\u5230\u6587\u4ef6\u2026\u2026\u4e4b\u7c7b\u7684\u3002\u5f53\u7136\uff0c\u9519\u8bef\u4ee3\u7801\u90fd\u662f\u4e0d\u4e3a0\u7684\u3002 \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f TODO: \u4ecb\u7ecd\u63a7\u5236\u53f0 \u6253\u5370\u4e00\u4e9b\u4fe1\u606f int main() { std::println(\"Hello, World!\"); } \u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa Hello, World! \u3002 \u6ce8\u91ca int main() { // \u5c0f\u5f6d\u8001\u5e08\uff0c\u8bf7\u4f60\u5728\u8fd9\u91cc\u63d2\u5165\u7a0b\u5e8f\u7684\u903b\u8f91\u54e6\uff01 } \u8fd9\u91cc\u7684 // \u662f\u6ce8\u91ca\uff0c\u6ce8\u91ca\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u690d\u5165\u63cf\u8ff0\u6027\u7684\u6587\u672c\u3002\u6709\u65f6\u4e5f\u4f1a\u7528\u4e8e\u591a\u4eba\u534f\u4f5c\u9879\u76ee\u4e2d\u7a0b\u5e8f\u5458\u4e4b\u95f4\u4e92\u76f8\u6c9f\u901a\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u8bf6\u563f\u4f60\u770b\u4e0d\u89c1\u6211 } \u5728\u7f16\u8bd1\u5668\u770b\u6765\u5c31\u53ea\u662f\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); } (* \u7f16\u8bd1\u5668\u8138\u7ea2\u4e2d* ) C++ \u652f\u6301\u884c\u6ce8\u91ca // xx \u548c\u5757\u6ce8\u91ca /* xx */ \u4e24\u79cd\u8bed\u6cd5\u3002 int main() { // \u6211\u662f\u884c\u6ce8\u91ca /* \u6211\u662f\u5757\u6ce8\u91ca */ /* \u5757\u6ce8\u91ca \u53ef\u4ee5 \u6709 \u5f88\u591a\u884c */ std::println(/* \u5757\u6ce8\u91ca\u4e5f\u53ef\u4ee5\u5939\u5728\u4ee3\u7801\u4e2d\u95f4 */\"\u4f60\u597d\"); std::println(\"\u4e16\u754c\"); // \u884c\u6ce8\u91ca\u53ea\u80fd\u8ffd\u52a0\u5728\u4e00\u884c\u7684\u672b\u5c3e std::println(\"\u65e9\u5b89\"); } \u5728\u6211\u4eec\u4ee5\u540e\u7684\u6848\u4f8b\u4ee3\u7801\u4e2d\uff0c\u90fd\u4f1a\u50cf\u8fd9\u6837\u6ce8\u91ca\u8bf4\u660e\uff0c\u5145\u5f53 \u5c31\u5730\u8bb2\u89e3\u5458 \u7684\u6548\u679c\u3002\u53bb\u9664\u8fd9\u4e9b\u6ce8\u91ca\u5e76\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6dfb\u52a0\u6587\u5b57\u6ce8\u91ca\u53ea\u662f\u5c0f\u5f6d\u8001\u5e08\u4e3a\u4e86\u63d0\u9192\u4f60\u6bcf\u4e00\u884c\u7684\u4ee3\u7801\u4f5c\u7528\u3002","title":"\u4f60\u597d\uff0c\u4e16\u754c"},{"location":"helloworld/#_1","text":"\u4f60\u597d\uff0c\u4e16\u754c \u4ec0\u4e48\u662f\u51fd\u6570 \u4ece main \u51fd\u6570\u8bf4\u8d77 main \u51fd\u6570\u7684\u8fd4\u56de\u503c \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f \u6253\u5370\u4e00\u4e9b\u4fe1\u606f \u6ce8\u91ca","title":"\u4f60\u597d\uff0c\u4e16\u754c"},{"location":"helloworld/#_2","text":"\u51fd\u6570: \u4e00\u6bb5\u7528 {} \u5305\u88f9\u7684\u4ee3\u7801\u5757\uff0c\u6709\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u540d\u5b57\u505a\u6807\u8bc6\u3002\u51fd\u6570\u53ef\u4ee5\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002\u51fd\u6570\u7684 {} \u4ee3\u7801\u5757\u5185\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u6bcf\u6b21\u8be5\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u6267\u884c\u3002 int compute() { return 42; } \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c compute \u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c int \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u6574\u6570\u3002 \u4e43\u53d6\u6574\u6570\u4e4b\u82f1\u6587\u201cinteger\u201d\u7684\u201cint\u201d\u800c\u5f97\u540d\uff08\u6a21\u4eff\u4faf\u6377\u8001\u5e08\u8bf4\u8bdd\uff09 \u800c {} \u5305\u88f9\u7684\u662f\u51fd\u6570\u4f53\uff0c\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u3002 \u6b64\u5904 return 42 \u5c31\u662f\u51fd\u6570\u4f53\u5185\u7684\u552f\u4e00\u4e00\u6761\u8bed\u53e5\uff0c\u8868\u793a\u51fd\u6570\u7acb\u5373\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd4\u56de 42\u3002 \u8fd4\u56de\u503c: \u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4f1a\u5411\u8c03\u7528\u8be5\u51fd\u6570\u7684\u8c03\u7528\u8005\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f return \u540e\u9762\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6b64\u5904 compute \u7684\u8fd4\u56de\u7c7b\u578b\u662f int \uff0c\u4e5f\u5c31\u662f\u8bf4 compute \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002 \u5173\u4e8e\u51fd\u6570\u7684\u53c2\u6570\uff0c\u6211\u4eec\u7a0d\u540e\u518d\u505a\u8bf4\u660e\u3002","title":"\u4ec0\u4e48\u662f\u51fd\u6570"},{"location":"helloworld/#main","text":"C++ \u7a0b\u5e8f\u901a\u5e38\u7531\u4e00\u7cfb\u5217\u51fd\u6570\u7ec4\u6210\uff0c\u5176\u4e2d\u5fc5\u987b\u6709\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002 main \u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a int main() { } \u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u8c03\u7528 main \u51fd\u6570\u3002 \u4e25\u683c\u6765\u8bf4\uff0c\u662f C++ \u8fd0\u884c\u65f6\u8c03\u7528\u4e86 main \u51fd\u6570\uff0c\u4f46\u76ee\u524d\u5148\u7406\u89e3\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\u4e86 main \u51fd\u6570\u201d\u4e5f\u65e0\u59a8\u3002 \u8981\u628a\u7a0b\u5e8f\u53d1\u5c55\u58ee\u5927\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9 main \u51fd\u6570\u7ee7\u7eed\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u6574\u4e2a\u7a0b\u5e8f\u7684\u903b\u8f91\uff08\u4e0d\u63a8\u8350\uff09\u3002 \u56e0\u6b64\uff0c main \u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u201c\u5b87\u5b99\u5927\u7206\u70b8\u201d\u3002","title":"\u4ece main \u51fd\u6570\u8bf4\u8d77"},{"location":"helloworld/#main_1","text":"int main() { return 0; } return \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\uff0cmain \u51fd\u6570\u8fd4\u56de\uff0c\u5373\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u7ed3\u675f\u3002 main \u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u6574\u6570 ( int \u7c7b\u578b)\uff0c\u7528\u8fd9\u4e2a\u6574\u6570\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u793a\u7a0b\u5e8f\u9000\u51fa\u7684\u539f\u56e0\u3002 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\u5b8c\u6bd5\uff0c\u6b63\u5e38\u7ed3\u675f\u9000\u51fa\uff0c\u90a3\u5c31\u8bf7\u8fd4\u56de 0\u3002 \u8fd4\u56de\u4e00\u4e2a\u4e0d\u4e3a 0 \u7684\u6574\u6570\u53ef\u4ee5\u8868\u793a\u7a0b\u5e8f\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u662f\u56e0\u4e3a\u51fa\u9519\u4e86\u624d\u9000\u51fa\u7684\uff0c\u503c\u7684\u591a\u5c11\u53ef\u4ee5\u7528\u4e8e\u8868\u660e\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\u3002 \u64cd\u4f5c\u7cfb\u7edf\uff1a\u6211\u8c03\u7528\u4e86\u4f60\u8fd9\u4e2a\u7a0b\u5e8f\u7684 main \u51fd\u6570\uff0c\u6211\u597d\u5947\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u4e86\uff1f\u8ba9\u6211\u4eec\u7ea6\u5b9a\u597d\uff1a\u5982\u679c\u4f60\u8fd0\u8f6c\u6b63\u5e38\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de0\u8868\u793a\u6210\u529f\u54e6\uff01\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u4ee3\u7801\uff0c\u6bd4\u5982\u8fd4\u56de1\u8868\u793a\u65e0\u6743\u9650\uff0c2\u8868\u793a\u627e\u4e0d\u5230\u6587\u4ef6\u2026\u2026\u4e4b\u7c7b\u7684\u3002\u5f53\u7136\uff0c\u9519\u8bef\u4ee3\u7801\u90fd\u662f\u4e0d\u4e3a0\u7684\u3002","title":"main \u51fd\u6570\u7684\u8fd4\u56de\u503c"},{"location":"helloworld/#_3","text":"TODO: \u4ecb\u7ecd\u63a7\u5236\u53f0","title":"\u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f"},{"location":"helloworld/#_4","text":"int main() { std::println(\"Hello, World!\"); } \u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa Hello, World! \u3002","title":"\u6253\u5370\u4e00\u4e9b\u4fe1\u606f"},{"location":"helloworld/#_5","text":"int main() { // \u5c0f\u5f6d\u8001\u5e08\uff0c\u8bf7\u4f60\u5728\u8fd9\u91cc\u63d2\u5165\u7a0b\u5e8f\u7684\u903b\u8f91\u54e6\uff01 } \u8fd9\u91cc\u7684 // \u662f\u6ce8\u91ca\uff0c\u6ce8\u91ca\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u690d\u5165\u63cf\u8ff0\u6027\u7684\u6587\u672c\u3002\u6709\u65f6\u4e5f\u4f1a\u7528\u4e8e\u591a\u4eba\u534f\u4f5c\u9879\u76ee\u4e2d\u7a0b\u5e8f\u5458\u4e4b\u95f4\u4e92\u76f8\u6c9f\u901a\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u8bf6\u563f\u4f60\u770b\u4e0d\u89c1\u6211 } \u5728\u7f16\u8bd1\u5668\u770b\u6765\u5c31\u53ea\u662f\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); } (* \u7f16\u8bd1\u5668\u8138\u7ea2\u4e2d* ) C++ \u652f\u6301\u884c\u6ce8\u91ca // xx \u548c\u5757\u6ce8\u91ca /* xx */ \u4e24\u79cd\u8bed\u6cd5\u3002 int main() { // \u6211\u662f\u884c\u6ce8\u91ca /* \u6211\u662f\u5757\u6ce8\u91ca */ /* \u5757\u6ce8\u91ca \u53ef\u4ee5 \u6709 \u5f88\u591a\u884c */ std::println(/* \u5757\u6ce8\u91ca\u4e5f\u53ef\u4ee5\u5939\u5728\u4ee3\u7801\u4e2d\u95f4 */\"\u4f60\u597d\"); std::println(\"\u4e16\u754c\"); // \u884c\u6ce8\u91ca\u53ea\u80fd\u8ffd\u52a0\u5728\u4e00\u884c\u7684\u672b\u5c3e std::println(\"\u65e9\u5b89\"); } \u5728\u6211\u4eec\u4ee5\u540e\u7684\u6848\u4f8b\u4ee3\u7801\u4e2d\uff0c\u90fd\u4f1a\u50cf\u8fd9\u6837\u6ce8\u91ca\u8bf4\u660e\uff0c\u5145\u5f53 \u5c31\u5730\u8bb2\u89e3\u5458 \u7684\u6548\u679c\u3002\u53bb\u9664\u8fd9\u4e9b\u6ce8\u91ca\u5e76\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6dfb\u52a0\u6587\u5b57\u6ce8\u91ca\u53ea\u662f\u5c0f\u5f6d\u8001\u5e08\u4e3a\u4e86\u63d0\u9192\u4f60\u6bcf\u4e00\u884c\u7684\u4ee3\u7801\u4f5c\u7528\u3002","title":"\u6ce8\u91ca"},{"location":"platform/","text":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 \u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC GCC Clang \u7f16\u8bd1\u5668\u9009\u9879 C++ \u6807\u51c6 \u4f18\u5316\u7b49\u7ea7 \u8c03\u8bd5\u4fe1\u606f \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 TODO \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u662f\u5c06\u6e90\u4ee3\u7801 ( .cpp ) \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7a0b\u5e8f ( .exe ) \u7684\u5de5\u5177\u3002 C++ \u662f \u7f16\u8bd1\u578b\u8bed\u8a00 \uff0c\u6e90\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u6267\u884c\u54e6\uff01\u521a\u5f00\u59cb\u5b66\u7f16\u7a0b\u7684\u5c0f\u5f6d\u8001\u5e08\u66fe\u7ecf\u628a\u7f51\u4e0a\u7684 \u201cHello, World\u201d \u4ee3\u7801\u62f7\u8d1d\u5230 .c \u6e90\u7801\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u628a\u540e\u7f00\u540d\u6539\u6210 .exe \uff0c\u53d1\u73b0\u8fd9\u6837\u6839\u672c\u6267\u884c\u4e0d\u4e86\u2026\u2026\u540e\u6765\u624d\u77e5\u9053\u9700\u8981\u901a\u8fc7\u4e00\u79cd\u53eb\u505a \u7f16\u8bd1\u5668 \u7f16\u8bd1 .c \u6587\u4ef6\uff0c\u624d\u80fd\u5f97\u5230\u8ba1\u7b97\u673a\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u7684 .exe \u6587\u4ef6\u3002 C++ \u6e90\u7801 .cpp \u662f\u5199\u7ed9\u4eba\u7c7b\u770b\u7684\uff01\u8ba1\u7b97\u673a\u5e76\u4e0d\u8ba4\u8bc6\uff0c\u8ba1\u7b97\u673a\u53ea\u8ba4\u8bc6\u4e8c\u8fdb\u5236\u7684\u673a\u5668\u7801\u3002\u8981\u628a C++ \u6e90\u7801\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u53ef\u4ee5\u6267\u884c\u7684\u673a\u5668\u7801\u3002 \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u6700\u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u6709\uff1aGCC\u3001Clang\u3001MSVC \u4fd7\u79f0\u201c\u5fa1\u4e09\u5bb6\u201d\u3002 \u8fd9\u4e9b\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u4e86\u5927\u90e8\u5206 C++20 \u6807\u51c6\u548c\u5c0f\u90e8\u5206 C++23 \u6807\u51c6\uff0c\u800c C++17 \u6807\u51c6\u90fd\u662f\u5b8c\u5168\u652f\u6301\u7684\u3002 \u6709\u4eba\u8bf4\u8fc7\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f GCC\u3002\u201d GCC \u4e3b\u8981\u53ea\u5728 Linux \u548c MacOS \u7b49 Unix \u7c7b\u7cfb\u7edf\u53ef\u7528\uff0c\u4e0d\u652f\u6301 Windows \u7cfb\u7edf\u3002\u4f46\u662f GCC \u6709\u7740\u5927\u91cf\u597d\u7528\u7684\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u5927\u540d\u9f0e\u9f0e\u7684 pbds \uff08\u57fa\u4e8e\u7b56\u7565\u7684\u6570\u636e\u7ed3\u6784\uff09\uff0c\u8fd8\u6709\u5404\u79cd __attribute__ \uff0c\u5404\u79cd __builtin_ \u7cfb\u5217\u51fd\u6570\u3002\u4e0d\u8fc7\u968f\u7740\u65b0\u6807\u51c6\u7684\u51fa\u53f0\uff0c\u5f88\u591a\u539f\u672c\u5c5e\u4e8e GCC \u7684\u529f\u80fd\u90fd\u6210\u4e86\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982 __attribute__((warn_unused)) \u53d8\u6210\u4e86\u6807\u51c6\u7684 [[nodiscard]] \uff0c __builtin_clz \u53d8\u6210\u4e86\u6807\u51c6\u7684 std::countl_zero \uff0c __VA_OPT__ \u540d\u5b57\u90fd\u6ca1\u53d8\u5c31\u8fdb\u4e86 C++20 \u6807\u51c6\u3002 PBDS \u53c8\u79f0 \u201c\u5e73\u677f\u7535\u89c6\u201d \u4e5f\u6709 MinGW \u8fd9\u6837\u7684\u9b54\u6539\u7248 GCC \u7f16\u8bd1\u5668\uff0c\u628a GCC \u79fb\u690d\u5230\u4e86 Windows \u7cfb\u7edf\u4e0a\uff0c\u540c\u65f6\u4e5f\u80fd\u7528 GCC \u7684\u4e00\u4e9b\u7279\u6027\u3002\u4e0d\u8fc7 MinGW \u6700\u8fd1\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u6700\u65b0\u7684 GCC Windows \u79fb\u690d\u7248\u7531 MinGW-w64 \u7ee7\u7eed\u7ef4\u62a4\u3002 Clang \u662f\u8de8\u5e73\u53f0\u7684\u7f16\u8bd1\u5668\uff0c\u652f\u6301\u5927\u591a\u6570\u4e3b\u6d41\u5e73\u53f0\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u754c\u7684\u5fa1\u4e09\u5bb6\uff1aLinux\u3001MacOS\u3001Windows\u3002Clang \u652f\u6301\u4e86\u5f88\u5927\u4e00\u90e8\u5206 GCC \u7279\u6027\u548c\u90e8\u5206 MSVC \u7279\u6027\u3002\u5176\u6240\u5c5e\u7684 LLVM \u9879\u76ee\u66f4\u662f\u7f16\u8bd1\u5668\u9886\u57df\u7684\u4e2d\u6d41\u7825\u67f1\uff0c\u4e0d\u4ec5\u652f\u6301 C\u3001C++\u3001Objective-C\u3001Fortran \u7b49\uff0cRust \u548c Swift \u7b49\u8bed\u8a00\u4e5f\u662f\u57fa\u4e8e LLVM \u540e\u7aef\u7f16\u8bd1\u7684\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709\u5f88\u591a\u663e\u5361\u5382\u5546\u7684 OpenGL \u9a71\u52a8\u4e5f\u662f\u57fa\u4e8e LLVM \u5b9e\u73b0\u7f16\u8bd1\u7684\u3002\u5e76\u4e14 Clang \u8eab\u517c\u6570\u804c\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u8bd1\uff0c\u8fd8\u652f\u6301\u9759\u6001\u5206\u6790\u3002\u8bb8\u591a IDE \u5e38\u89c1\u7684\u8bed\u8a00\u670d\u52a1\u534f\u8bae (LSP) \u5c31\u662f\u57fa\u4e8e Clang \u7684\u670d\u52a1\u7248\u2014\u2014\u2014\u2014Clangd \u5b9e\u73b0\u7684 (\u4f8b\u5982\u4f60\u53ef\u4ee5\u6309 Ctrl \u70b9\u51fb\uff0c\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u8fd9\u6837\u7684\u529f\u80fd\u5c31\u662f IDE \u901a\u8fc7\u8c03\u7528 Clangd \u7684 LSP \u63a5\u53e3\u5b9e\u73b0\uff09\u3002\u4e0d\u8fc7 Clang \u7684\u6027\u80fd\u4f18\u5316\u6bd4\u8f83\u6fc0\u8fdb\uff0c\u867d\u7136\u6709\u52a9\u4e8e\u6027\u80fd\u63d0\u5347\uff0c\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u72af\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u53ef\u80fd\u4f18\u5316\u51fa\u532a\u5937\u6240\u601d\u7684\u7ed3\u679c\uff0c\u5982\u679c\u4f60\u8981\u5b9e\u9a8c\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u662f\u6700\u64c5\u957f\u590d\u73b0\u7684\u3002\u4e14 Clang \u5bf9\u4e00\u4e9b C++ \u65b0\u6807\u51c6\u7279\u6027\u652f\u6301\u76f8\u5bf9\u8f83\u6162\uff0c\u6ca1\u6709 GCC \u548c MSVC \u90a3\u4e48\u4e0a\u5fc3\u3002 \u4f8b\u5982 C++20 \u65e9\u5df2\u5141\u8bb8 lambda \u8868\u8fbe\u5f0f\u6355\u83b7 structural-binding \u53d8\u91cf\uff0c\u800c Clang \u81f3\u4eca\u8fd8\u6ca1\u6709\u652f\u6301\uff0c\u5c3d\u7ba1 Clang \u5df2\u7ecf\u652f\u6301\u4e86\u5f88\u591a\u5176\u4ed6 C++20 \u7279\u6027\u3002 Apple Clang \u662f\u82f9\u679c\u516c\u53f8\u81ea\u5df1\u9b54\u6539\u7684 Clang \u7248\u672c\uff0c\u53ea\u5728 MacOS \u7cfb\u7edf\u4e0a\u53ef\u7528\uff0c\u652f\u6301 Objective-C \u548c Swift \u8bed\u8a00\u3002\u4f46\u662f\u7248\u672c\u8f83\u5b98\u65b9 Clang \u843d\u540e\u4e00\u4e9b\uff0c\u5f88\u591a\u65b0\u7279\u6027\u90fd\u6ca1\u6709\u8ddf\u8fdb\uff0c\u57fa\u672c\u4e0a\u53ea\u6709\u4e13\u95e8\u4f3a\u5019\u82f9\u679c\u7684\u5f00\u53d1\u8005\u4f1a\u7528\u3002 GCC \u548c Clang \u4e5f\u652f\u6301 Objective-C\u3002 MSVC \u662f Windows \u9650\u5b9a\u7684\u7f16\u8bd1\u5668\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a MSVC \u7279\u6709\u7684\u6269\u5c55\u3002\u4e5f\u6709\u4eba\u5728 Clang \u4e0a\u9b54\u6539\u51fa\u4e86 MSVC \u517c\u5bb9\u6a21\u5f0f\uff0c\u517c\u987e Clang \u7279\u6027\u7684\u540c\u65f6\uff0c\u652f\u6301\u4e86 MSVC \u7684\u4e00\u4e9b\u7279\u6027\uff08\u4f8b\u5982 __declspec \uff09\uff0c\u53ef\u4ee5\u7f16\u8bd1\u7528\u4e86 MSVC \u7279\u6027\u7684\u4ee3\u7801\uff0c\u5373 clang-cl \uff0c\u5728\u6700\u65b0\u7684 VS2022 IDE \u4e2d\u4e5f\u96c6\u6210\u4e86 clang-cl \u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cMSVC \u7684\u4f18\u5316\u80fd\u529b\u662f\u6bd4\u8f83\u5dee\u7684\uff0c\u6bd4 GCC \u548c Clang \u90fd\u5dee\uff0c\u4f8b\u5982 MSVC \u51e0\u4e4e\u603b\u662f\u5047\u5b9a\u6240\u6709\u6307\u9488 aliasing\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u9047\u5230\u5f88\u591a\u6307\u9488\u64cd\u4f5c\u7684\u5faa\u73af\u65f6\uff0c\u51e0\u4e4e\u6ca1\u6cd5\u505a\u5faa\u73af\u77e2\u91cf\u5316\u3002\u4f46\u662f\u4e5f\u4f7f\u5f97\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f Bug\uff0c\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u4e00\u4e9b\u53ea\u7528 MSVC \u7684\u4eba\u4e0d\u77e5\u9053\u67d0\u4e9b\u5199\u6cd5\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 Intel C++ compiler \u662f\u82f1\u7279\u5c14\u5f00\u53d1\u7684 C++ \u7f16\u8bd1\u5668\uff0c\u7531\u4e8e\u662f\u786c\u4ef6\u5382\u5546\u5f00\u53d1\u7684\uff0c\u7279\u522b\u64c5\u957f\u505a\u6027\u80fd\u4f18\u5316\u3002\u4f46\u7531\u4e8e\u66f4\u65b0\u8f83\u6162\uff0c\u57fa\u672c\u6ca1\u6709\u66f4\u4e0a\u65b0\u7279\u6027\uff0c\u4e5f\u6ca1\u4ec0\u4e48\u4eba\u5728\u7528\u4e86\u3002 \u6700\u8fd1\u4ed6\u4eec\u53c8\u51fa\u4e86\u4e2a Intel DPC++ compiler\uff0c\u652f\u6301\u6700\u65b0\u7684\u5e76\u884c\u7f16\u7a0b\u9886\u57df\u7279\u5b9a\u8bed\u8a00 SyCL\u3002 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC cl.exe /c main.cpp \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main.exe \u4e86\u3002 GCC g++ -c main.cpp -o main \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main \u4e86\u3002 Linux \u7cfb\u7edf\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u6ca1\u6709\u540e\u7f00\u540d\uff0c\u6240\u4ee5\u6ca1\u6709 .exe \u540e\u7f00\u3002 Clang Windows \u4e0a\uff1a clang++.exe -c main.cpp -o main.exe Linux / MacOS \u4e0a\uff1a clang++ -c main.cpp -o main \u7f16\u8bd1\u5668\u9009\u9879 \u7f16\u8bd1\u5668\u9009\u9879\u662f\u7528\u6765\u63a7\u5236\u7f16\u8bd1\u5668\u7684\u884c\u4e3a\u7684\u3002\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6709\u4e0d\u540c\u7684\u9009\u9879\uff0c\u8bed\u6cd5\u6709\u5fae\u5999\u7684\u4e0d\u540c\uff0c\u4f46\u5927\u81f4\u529f\u6548\u76f8\u540c\u3002 \u4f8b\u5982\u5f53\u6211\u4eec\u8bf4\u201c\u7f16\u8bd1\u8fd9\u4e2a\u6e90\u7801\u65f6\uff0c\u6211\u7528\u4e86 GCC \u7f16\u8bd1\u5668\uff0c -O3 \u548c -std=c++20 \u9009\u9879\u201d\uff0c\u8bf4\u7684\u5c31\u662f\u628a\u8fd9\u4e9b\u9009\u9879\u52a0\u5230\u4e86 g++ \u7684\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\uff1a g++ -O3 -std=c++20 -c main.cpp -o main \u5176\u4e2d Clang \u548c GCC \u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\u5f88\u5927\u4ea4\u96c6\u3002\u800c MSVC \u57fa\u672c\u81ea\u6210\u4e00\u6d3e\u3002 Clang \u548c GCC \u7684\u9009\u9879\u90fd\u662f -xxx \u7684\u5f62\u5f0f\uff0cMSVC \u7684\u9009\u9879\u662f /xxx \u7684\u5f62\u5f0f\u3002 \u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\uff1a C++ \u6807\u51c6 \u6307\u5b9a\u8981\u9009\u7528\u7684 C++ \u6807\u51c6\u3002 Clang \u548c GCC\uff1a -std=c++98 \u3001 -std=c++03 \u3001 -std=c++11 \u3001 -std=c++14 \u3001 -std=c++17 \u3001 -std=c++20 \u3001 -std=c++23 MSVC\uff1a /std:c++98 \u3001 /std:c++11 \u3001 /std:c++14 \u3001 /std:c++17 \u3001 /std:c++20 \u3001 /std:c++latest \u4f8b\u5982\u8981\u7f16\u8bd1\u4e00\u4e2a C++20 \u6e90\u7801\u6587\u4ef6\uff0c\u5206\u522b\u7528 GCC\u3001Clang\u3001MSVC\uff1a GCC\uff08Linux\uff09\uff1a g++ -std=c++20 -c main.cpp -o main Clang\uff08Linux\uff09\uff1a clang++ -std=c++20 -c main.cpp -o main MSVC\uff08Windows\uff09\uff1a cl.exe /std:c++20 /c main.cpp \u4f18\u5316\u7b49\u7ea7 Clang \u548c GCC\uff1a -O0 \u3001 -O1 \u3001 -O2 \u3001 -O3 \u3001 -Ofast \u3001 -Os \u3001 -Oz \u3001 -Og -O0 \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u7f16\u8bd1\u901f\u5ea6\u6700\u5feb\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u5f00\u53d1\u4eba\u5458\u5185\u90e8\u8c03\u8bd5\u9636\u6bb5\u3002 -O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\uff08\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u7684\u4e0d\u53ef\u62b5\u8fbe\u4ee3\u7801\uff09\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u90e8\u5206\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\uff0c\u7f16\u8bd1\u901f\u5ea6\u8f83\u5feb\uff0c\u6267\u884c\u901f\u5ea6\u4e5f\u6bd4 -O0 \u5feb\u3002\u4f46\u662f\u4f1a\u4e22\u5931\u51fd\u6570\u7684\u884c\u53f7\u4fe1\u606f\uff0c\u5f71\u54cd\u8bf8\u5982 gdb \u7b49\u8c03\u8bd5\uff0c\u5982\u9700\u5feb\u901f\u8c03\u8bd5\u53ef\u4ee5\u7528 -Og \u9009\u9879\u3002 -O2 \uff1a\u6bd4 -O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002 -O3 \uff1a\u6bd4 -O2 \u66f4\u6fc0\u8fdb\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u590d\u6742\u7684\u5faa\u73af\u7528 SIMD \u77e2\u91cf\u6307\u4ee4\u4f18\u5316\u52a0\u901f\uff0c\u628a\u4e00\u4e9b\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u6027\u80fd\u63d0\u5347\u5f88\u5927\uff0c\u4f46\u662f\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b Bug\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\u5219\u7edd\u4e0d\u4f1a\u6709\u95ee\u9898\uff0c\u5bf9\u81ea\u5df1\u7684\u4ee3\u7801\u8d28\u91cf\u6709\u81ea\u4fe1\u5c31\u53ef\u4ee5\u653e\u5fc3\u5f00\uff0c\u7f16\u8bd1\u901f\u5ea6\u4e5f\u4f1a\u5f88\u6162\uff0c\u4e00\u822c\u7528\u4e8e\u7a0b\u5e8f\u6700\u7ec8\u6210\u54c1\u53d1\u5e03\u9636\u6bb5\u3002 -Ofast \uff1a\u5728 -O3 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5bf9\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97\u8fdb\u884c\u66f4\u6df1\u5c42\u6b21\u7684\u4f18\u5316\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u6d6e\u70b9\u6570\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e0d\u6d89\u53ca\u5230 NaN \u548c Inf \u7684\u5904\u7406\uff0c\u90a3\u4e48 -Ofast \u4e0d\u4f1a\u6709\u592a\u5927\u7684\u95ee\u9898\uff0c\u4e00\u822c\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\u7684\u7ec8\u6781\u6027\u80fd\u4f18\u5316\u3002 -Os \uff1a\u5728 -O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u6bd4 -O0 \u3001 -O1 \u3001 -O2 \u90fd\u8981\u5c0f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Oz \uff1a\u5728 -Os \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u628a\u4ee3\u7801\u538b\u7f29\uff0c\u53ef\u80fd\u628a\u672c\u53ef\u4ee5\u4e00\u6761\u5927\u6307\u4ee4\u5b8c\u6210\u7684\u4efb\u52a1\u4e5f\u62c6\u6210\u591a\u6761\u5c0f\u6307\u4ee4\uff0c\u4e3a\u4e86\u5c3a\u5bf8\u5b8c\u5168\u6027\u80fd\uff0c\u5927\u5e45\u51cf\u5c11\u4e86\u51fd\u6570\u5185\u8054\u7684\u673a\u4f1a\uff0c\u6709\u65f6\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Og \uff1a\u5728 -O0 \u7684\u57fa\u7840\u4e0a\uff0c\u5c3d\u53ef\u80fd\u4fdd\u7559\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\uff0c\u4e0d\u505a\u7834\u574f\u51fd\u6570\u884c\u53f7\u7b49\u4fe1\u606f\u7684\u4f18\u5316\uff0c\u5efa\u8bae\u914d\u5408\u4ea7\u751f\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\u7684 -g \u9009\u9879\u4f7f\u7528\u3002\u4f46\u8fd8\u662f\u4f1a\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u4f18\u5316\uff0c\u6bd4 -O0 \u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002\u4f46 -Og \u7684\u6240\u6709\u4f18\u5316\u90fd\u4e0d\u4f1a\u6d89\u53ca\u5230\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u8c03\u8bd5\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u4f46\u662f\u7531\u4e8e\u63d2\u5165\u4e86\u8c03\u8bd5\u4fe1\u606f\uff0c\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u53d8\u5f97\u5f88\u5927\uff0c\u4e00\u822c\u5728\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002 MSVC\uff1a /Od \u3001 /O1 \u3001 /O2 \u3001 /Ox \u3001 /Ob1 \u3001 /Ob2 \u3001 /Os /Od \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\u9636\u6bb5\u3002 /O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\u3002 /O2 \uff1a\u6bd4 /O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u4f1a\u5c1d\u8bd5\u628a\u4e00\u4e9b\u5faa\u73af\u77e2\u91cf\u5316\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ox \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u4f46\u662f\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ob1 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\u3002 /Ob2 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\uff0c\u4f46\u662f\u4f1a\u6269\u5927\u5185\u8054\u8303\u56f4\uff0c\u4e00\u822c\u6bd4 /Ob1 \u66f4\u5feb\uff0c\u4f46\u662f\u4e5f\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u3002 /Os \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 \u8c03\u8bd5\u4fe1\u606f Clang \u548c GCC\uff1a -g \u3001 -g0 \u3001 -g1 \u3001 -g2 \u3001 -g3 MSVC\uff1a /Z7 \u3001 /Zi \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f Clang \u548c GCC\uff1a -Dmacro=value MSVC\uff1a /Dmacro=value \u4f8b\u5982\uff1a \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 libstdc++ \u662f GCC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e GCC \u662f Linux \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libstdc++ \u4e5f\u662f Linux \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u770b\u5230\u4ed6\u7684\u6e90\u7801\uff1ahttps://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3 libc++ \u662f Clang \u5b98\u65b9\u7f16\u5199\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e Clang \u662f MacOS \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libc++ \u4e5f\u662f MacOS \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002libc++ \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u65e9\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\u3002\u9879\u76ee\u7684\u5f00\u6e90\u5730\u5740\u662f\uff1ahttps://github.com/llvm/llvm-project/tree/main/libcxx MSVC STL \u662f MSVC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e MSVC \u662f Windows \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 MSVC STL \u4e5f\u662f Windows \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002MSVC STL \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u665a\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\uff0c\u4f46\u662f\u73b0\u5728\u4ed6\u5df2\u7ecf\u5b8c\u5168\u652f\u6301 C++20\uff0c\u5e76\u4e14\u4e5f\u5b8c\u5168\u5f00\u6e90\u4e86\uff1ahttps://github.com/microsoft/STL \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6807\u51c6\u5e93\u548c\u7f16\u8bd1\u5668\u5e76\u4e0d\u662f\u7ed1\u5b9a\u7684\uff0c\u4f8b\u5982 Clang \u53ef\u4ee5\u7528 libstdc++ \u6216 MSVC STL\uff0cGCC \u4e5f\u53ef\u4ee5\u88ab\u914d\u7f6e\u4f7f\u7528 libc++\u3002 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0cClang \u9ed8\u8ba4\u7528\u7684\u5c31\u662f libstdc++\u3002\u9700\u8981\u4e3a Clang \u6307\u5b9a -stdlib=libc++ \u9009\u9879\uff0c\u624d\u80fd\u4f7f\u7528\u3002 \u725b\u5934\u4eba\u7b11\u8bdd\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f libstdc++\u3002\u56e0\u4e3a\u5373\u4f7f\u4ed6\u7684\u7f16\u8bd1\u5668\u662f Clang\uff0c\u4ed6\u7528\u7684\u5927\u6982\u7387\u4f9d\u7136\u662f libstdc++\u3002\u201d \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO","title":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9"},{"location":"platform/#_1","text":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC GCC Clang \u7f16\u8bd1\u5668\u9009\u9879 C++ \u6807\u51c6 \u4f18\u5316\u7b49\u7ea7 \u8c03\u8bd5\u4fe1\u606f \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO","title":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9"},{"location":"platform/#ide","text":"TODO","title":"IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01"},{"location":"platform/#_2","text":"\u7f16\u8bd1\u5668\u662f\u5c06\u6e90\u4ee3\u7801 ( .cpp ) \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7a0b\u5e8f ( .exe ) \u7684\u5de5\u5177\u3002 C++ \u662f \u7f16\u8bd1\u578b\u8bed\u8a00 \uff0c\u6e90\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u6267\u884c\u54e6\uff01\u521a\u5f00\u59cb\u5b66\u7f16\u7a0b\u7684\u5c0f\u5f6d\u8001\u5e08\u66fe\u7ecf\u628a\u7f51\u4e0a\u7684 \u201cHello, World\u201d \u4ee3\u7801\u62f7\u8d1d\u5230 .c \u6e90\u7801\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u628a\u540e\u7f00\u540d\u6539\u6210 .exe \uff0c\u53d1\u73b0\u8fd9\u6837\u6839\u672c\u6267\u884c\u4e0d\u4e86\u2026\u2026\u540e\u6765\u624d\u77e5\u9053\u9700\u8981\u901a\u8fc7\u4e00\u79cd\u53eb\u505a \u7f16\u8bd1\u5668 \u7f16\u8bd1 .c \u6587\u4ef6\uff0c\u624d\u80fd\u5f97\u5230\u8ba1\u7b97\u673a\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u7684 .exe \u6587\u4ef6\u3002 C++ \u6e90\u7801 .cpp \u662f\u5199\u7ed9\u4eba\u7c7b\u770b\u7684\uff01\u8ba1\u7b97\u673a\u5e76\u4e0d\u8ba4\u8bc6\uff0c\u8ba1\u7b97\u673a\u53ea\u8ba4\u8bc6\u4e8c\u8fdb\u5236\u7684\u673a\u5668\u7801\u3002\u8981\u628a C++ \u6e90\u7801\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u53ef\u4ee5\u6267\u884c\u7684\u673a\u5668\u7801\u3002","title":"\u7f16\u8bd1\u5668\u662f\uff1f"},{"location":"platform/#_3","text":"\u6700\u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u6709\uff1aGCC\u3001Clang\u3001MSVC \u4fd7\u79f0\u201c\u5fa1\u4e09\u5bb6\u201d\u3002 \u8fd9\u4e9b\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u4e86\u5927\u90e8\u5206 C++20 \u6807\u51c6\u548c\u5c0f\u90e8\u5206 C++23 \u6807\u51c6\uff0c\u800c C++17 \u6807\u51c6\u90fd\u662f\u5b8c\u5168\u652f\u6301\u7684\u3002 \u6709\u4eba\u8bf4\u8fc7\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f GCC\u3002\u201d GCC \u4e3b\u8981\u53ea\u5728 Linux \u548c MacOS \u7b49 Unix \u7c7b\u7cfb\u7edf\u53ef\u7528\uff0c\u4e0d\u652f\u6301 Windows \u7cfb\u7edf\u3002\u4f46\u662f GCC \u6709\u7740\u5927\u91cf\u597d\u7528\u7684\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u5927\u540d\u9f0e\u9f0e\u7684 pbds \uff08\u57fa\u4e8e\u7b56\u7565\u7684\u6570\u636e\u7ed3\u6784\uff09\uff0c\u8fd8\u6709\u5404\u79cd __attribute__ \uff0c\u5404\u79cd __builtin_ \u7cfb\u5217\u51fd\u6570\u3002\u4e0d\u8fc7\u968f\u7740\u65b0\u6807\u51c6\u7684\u51fa\u53f0\uff0c\u5f88\u591a\u539f\u672c\u5c5e\u4e8e GCC \u7684\u529f\u80fd\u90fd\u6210\u4e86\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982 __attribute__((warn_unused)) \u53d8\u6210\u4e86\u6807\u51c6\u7684 [[nodiscard]] \uff0c __builtin_clz \u53d8\u6210\u4e86\u6807\u51c6\u7684 std::countl_zero \uff0c __VA_OPT__ \u540d\u5b57\u90fd\u6ca1\u53d8\u5c31\u8fdb\u4e86 C++20 \u6807\u51c6\u3002 PBDS \u53c8\u79f0 \u201c\u5e73\u677f\u7535\u89c6\u201d \u4e5f\u6709 MinGW \u8fd9\u6837\u7684\u9b54\u6539\u7248 GCC \u7f16\u8bd1\u5668\uff0c\u628a GCC \u79fb\u690d\u5230\u4e86 Windows \u7cfb\u7edf\u4e0a\uff0c\u540c\u65f6\u4e5f\u80fd\u7528 GCC \u7684\u4e00\u4e9b\u7279\u6027\u3002\u4e0d\u8fc7 MinGW \u6700\u8fd1\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u6700\u65b0\u7684 GCC Windows \u79fb\u690d\u7248\u7531 MinGW-w64 \u7ee7\u7eed\u7ef4\u62a4\u3002 Clang \u662f\u8de8\u5e73\u53f0\u7684\u7f16\u8bd1\u5668\uff0c\u652f\u6301\u5927\u591a\u6570\u4e3b\u6d41\u5e73\u53f0\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u754c\u7684\u5fa1\u4e09\u5bb6\uff1aLinux\u3001MacOS\u3001Windows\u3002Clang \u652f\u6301\u4e86\u5f88\u5927\u4e00\u90e8\u5206 GCC \u7279\u6027\u548c\u90e8\u5206 MSVC \u7279\u6027\u3002\u5176\u6240\u5c5e\u7684 LLVM \u9879\u76ee\u66f4\u662f\u7f16\u8bd1\u5668\u9886\u57df\u7684\u4e2d\u6d41\u7825\u67f1\uff0c\u4e0d\u4ec5\u652f\u6301 C\u3001C++\u3001Objective-C\u3001Fortran \u7b49\uff0cRust \u548c Swift \u7b49\u8bed\u8a00\u4e5f\u662f\u57fa\u4e8e LLVM \u540e\u7aef\u7f16\u8bd1\u7684\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709\u5f88\u591a\u663e\u5361\u5382\u5546\u7684 OpenGL \u9a71\u52a8\u4e5f\u662f\u57fa\u4e8e LLVM \u5b9e\u73b0\u7f16\u8bd1\u7684\u3002\u5e76\u4e14 Clang \u8eab\u517c\u6570\u804c\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u8bd1\uff0c\u8fd8\u652f\u6301\u9759\u6001\u5206\u6790\u3002\u8bb8\u591a IDE \u5e38\u89c1\u7684\u8bed\u8a00\u670d\u52a1\u534f\u8bae (LSP) \u5c31\u662f\u57fa\u4e8e Clang \u7684\u670d\u52a1\u7248\u2014\u2014\u2014\u2014Clangd \u5b9e\u73b0\u7684 (\u4f8b\u5982\u4f60\u53ef\u4ee5\u6309 Ctrl \u70b9\u51fb\uff0c\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u8fd9\u6837\u7684\u529f\u80fd\u5c31\u662f IDE \u901a\u8fc7\u8c03\u7528 Clangd \u7684 LSP \u63a5\u53e3\u5b9e\u73b0\uff09\u3002\u4e0d\u8fc7 Clang \u7684\u6027\u80fd\u4f18\u5316\u6bd4\u8f83\u6fc0\u8fdb\uff0c\u867d\u7136\u6709\u52a9\u4e8e\u6027\u80fd\u63d0\u5347\uff0c\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u72af\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u53ef\u80fd\u4f18\u5316\u51fa\u532a\u5937\u6240\u601d\u7684\u7ed3\u679c\uff0c\u5982\u679c\u4f60\u8981\u5b9e\u9a8c\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u662f\u6700\u64c5\u957f\u590d\u73b0\u7684\u3002\u4e14 Clang \u5bf9\u4e00\u4e9b C++ \u65b0\u6807\u51c6\u7279\u6027\u652f\u6301\u76f8\u5bf9\u8f83\u6162\uff0c\u6ca1\u6709 GCC \u548c MSVC \u90a3\u4e48\u4e0a\u5fc3\u3002 \u4f8b\u5982 C++20 \u65e9\u5df2\u5141\u8bb8 lambda \u8868\u8fbe\u5f0f\u6355\u83b7 structural-binding \u53d8\u91cf\uff0c\u800c Clang \u81f3\u4eca\u8fd8\u6ca1\u6709\u652f\u6301\uff0c\u5c3d\u7ba1 Clang \u5df2\u7ecf\u652f\u6301\u4e86\u5f88\u591a\u5176\u4ed6 C++20 \u7279\u6027\u3002 Apple Clang \u662f\u82f9\u679c\u516c\u53f8\u81ea\u5df1\u9b54\u6539\u7684 Clang \u7248\u672c\uff0c\u53ea\u5728 MacOS \u7cfb\u7edf\u4e0a\u53ef\u7528\uff0c\u652f\u6301 Objective-C \u548c Swift \u8bed\u8a00\u3002\u4f46\u662f\u7248\u672c\u8f83\u5b98\u65b9 Clang \u843d\u540e\u4e00\u4e9b\uff0c\u5f88\u591a\u65b0\u7279\u6027\u90fd\u6ca1\u6709\u8ddf\u8fdb\uff0c\u57fa\u672c\u4e0a\u53ea\u6709\u4e13\u95e8\u4f3a\u5019\u82f9\u679c\u7684\u5f00\u53d1\u8005\u4f1a\u7528\u3002 GCC \u548c Clang \u4e5f\u652f\u6301 Objective-C\u3002 MSVC \u662f Windows \u9650\u5b9a\u7684\u7f16\u8bd1\u5668\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a MSVC \u7279\u6709\u7684\u6269\u5c55\u3002\u4e5f\u6709\u4eba\u5728 Clang \u4e0a\u9b54\u6539\u51fa\u4e86 MSVC \u517c\u5bb9\u6a21\u5f0f\uff0c\u517c\u987e Clang \u7279\u6027\u7684\u540c\u65f6\uff0c\u652f\u6301\u4e86 MSVC \u7684\u4e00\u4e9b\u7279\u6027\uff08\u4f8b\u5982 __declspec \uff09\uff0c\u53ef\u4ee5\u7f16\u8bd1\u7528\u4e86 MSVC \u7279\u6027\u7684\u4ee3\u7801\uff0c\u5373 clang-cl \uff0c\u5728\u6700\u65b0\u7684 VS2022 IDE \u4e2d\u4e5f\u96c6\u6210\u4e86 clang-cl \u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cMSVC \u7684\u4f18\u5316\u80fd\u529b\u662f\u6bd4\u8f83\u5dee\u7684\uff0c\u6bd4 GCC \u548c Clang \u90fd\u5dee\uff0c\u4f8b\u5982 MSVC \u51e0\u4e4e\u603b\u662f\u5047\u5b9a\u6240\u6709\u6307\u9488 aliasing\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u9047\u5230\u5f88\u591a\u6307\u9488\u64cd\u4f5c\u7684\u5faa\u73af\u65f6\uff0c\u51e0\u4e4e\u6ca1\u6cd5\u505a\u5faa\u73af\u77e2\u91cf\u5316\u3002\u4f46\u662f\u4e5f\u4f7f\u5f97\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f Bug\uff0c\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u4e00\u4e9b\u53ea\u7528 MSVC \u7684\u4eba\u4e0d\u77e5\u9053\u67d0\u4e9b\u5199\u6cd5\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 Intel C++ compiler \u662f\u82f1\u7279\u5c14\u5f00\u53d1\u7684 C++ \u7f16\u8bd1\u5668\uff0c\u7531\u4e8e\u662f\u786c\u4ef6\u5382\u5546\u5f00\u53d1\u7684\uff0c\u7279\u522b\u64c5\u957f\u505a\u6027\u80fd\u4f18\u5316\u3002\u4f46\u7531\u4e8e\u66f4\u65b0\u8f83\u6162\uff0c\u57fa\u672c\u6ca1\u6709\u66f4\u4e0a\u65b0\u7279\u6027\uff0c\u4e5f\u6ca1\u4ec0\u4e48\u4eba\u5728\u7528\u4e86\u3002 \u6700\u8fd1\u4ed6\u4eec\u53c8\u51fa\u4e86\u4e2a Intel DPC++ compiler\uff0c\u652f\u6301\u6700\u65b0\u7684\u5e76\u884c\u7f16\u7a0b\u9886\u57df\u7279\u5b9a\u8bed\u8a00 SyCL\u3002","title":"\u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6"},{"location":"platform/#_4","text":"","title":"\u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801"},{"location":"platform/#msvc","text":"cl.exe /c main.cpp \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main.exe \u4e86\u3002","title":"MSVC"},{"location":"platform/#gcc","text":"g++ -c main.cpp -o main \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main \u4e86\u3002 Linux \u7cfb\u7edf\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u6ca1\u6709\u540e\u7f00\u540d\uff0c\u6240\u4ee5\u6ca1\u6709 .exe \u540e\u7f00\u3002","title":"GCC"},{"location":"platform/#clang","text":"Windows \u4e0a\uff1a clang++.exe -c main.cpp -o main.exe Linux / MacOS \u4e0a\uff1a clang++ -c main.cpp -o main","title":"Clang"},{"location":"platform/#_5","text":"\u7f16\u8bd1\u5668\u9009\u9879\u662f\u7528\u6765\u63a7\u5236\u7f16\u8bd1\u5668\u7684\u884c\u4e3a\u7684\u3002\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6709\u4e0d\u540c\u7684\u9009\u9879\uff0c\u8bed\u6cd5\u6709\u5fae\u5999\u7684\u4e0d\u540c\uff0c\u4f46\u5927\u81f4\u529f\u6548\u76f8\u540c\u3002 \u4f8b\u5982\u5f53\u6211\u4eec\u8bf4\u201c\u7f16\u8bd1\u8fd9\u4e2a\u6e90\u7801\u65f6\uff0c\u6211\u7528\u4e86 GCC \u7f16\u8bd1\u5668\uff0c -O3 \u548c -std=c++20 \u9009\u9879\u201d\uff0c\u8bf4\u7684\u5c31\u662f\u628a\u8fd9\u4e9b\u9009\u9879\u52a0\u5230\u4e86 g++ \u7684\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\uff1a g++ -O3 -std=c++20 -c main.cpp -o main \u5176\u4e2d Clang \u548c GCC \u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\u5f88\u5927\u4ea4\u96c6\u3002\u800c MSVC \u57fa\u672c\u81ea\u6210\u4e00\u6d3e\u3002 Clang \u548c GCC \u7684\u9009\u9879\u90fd\u662f -xxx \u7684\u5f62\u5f0f\uff0cMSVC \u7684\u9009\u9879\u662f /xxx \u7684\u5f62\u5f0f\u3002 \u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\uff1a","title":"\u7f16\u8bd1\u5668\u9009\u9879"},{"location":"platform/#c","text":"\u6307\u5b9a\u8981\u9009\u7528\u7684 C++ \u6807\u51c6\u3002 Clang \u548c GCC\uff1a -std=c++98 \u3001 -std=c++03 \u3001 -std=c++11 \u3001 -std=c++14 \u3001 -std=c++17 \u3001 -std=c++20 \u3001 -std=c++23 MSVC\uff1a /std:c++98 \u3001 /std:c++11 \u3001 /std:c++14 \u3001 /std:c++17 \u3001 /std:c++20 \u3001 /std:c++latest \u4f8b\u5982\u8981\u7f16\u8bd1\u4e00\u4e2a C++20 \u6e90\u7801\u6587\u4ef6\uff0c\u5206\u522b\u7528 GCC\u3001Clang\u3001MSVC\uff1a GCC\uff08Linux\uff09\uff1a g++ -std=c++20 -c main.cpp -o main Clang\uff08Linux\uff09\uff1a clang++ -std=c++20 -c main.cpp -o main MSVC\uff08Windows\uff09\uff1a cl.exe /std:c++20 /c main.cpp","title":"C++ \u6807\u51c6"},{"location":"platform/#_6","text":"Clang \u548c GCC\uff1a -O0 \u3001 -O1 \u3001 -O2 \u3001 -O3 \u3001 -Ofast \u3001 -Os \u3001 -Oz \u3001 -Og -O0 \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u7f16\u8bd1\u901f\u5ea6\u6700\u5feb\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u5f00\u53d1\u4eba\u5458\u5185\u90e8\u8c03\u8bd5\u9636\u6bb5\u3002 -O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\uff08\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u7684\u4e0d\u53ef\u62b5\u8fbe\u4ee3\u7801\uff09\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u90e8\u5206\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\uff0c\u7f16\u8bd1\u901f\u5ea6\u8f83\u5feb\uff0c\u6267\u884c\u901f\u5ea6\u4e5f\u6bd4 -O0 \u5feb\u3002\u4f46\u662f\u4f1a\u4e22\u5931\u51fd\u6570\u7684\u884c\u53f7\u4fe1\u606f\uff0c\u5f71\u54cd\u8bf8\u5982 gdb \u7b49\u8c03\u8bd5\uff0c\u5982\u9700\u5feb\u901f\u8c03\u8bd5\u53ef\u4ee5\u7528 -Og \u9009\u9879\u3002 -O2 \uff1a\u6bd4 -O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002 -O3 \uff1a\u6bd4 -O2 \u66f4\u6fc0\u8fdb\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u590d\u6742\u7684\u5faa\u73af\u7528 SIMD \u77e2\u91cf\u6307\u4ee4\u4f18\u5316\u52a0\u901f\uff0c\u628a\u4e00\u4e9b\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u6027\u80fd\u63d0\u5347\u5f88\u5927\uff0c\u4f46\u662f\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b Bug\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\u5219\u7edd\u4e0d\u4f1a\u6709\u95ee\u9898\uff0c\u5bf9\u81ea\u5df1\u7684\u4ee3\u7801\u8d28\u91cf\u6709\u81ea\u4fe1\u5c31\u53ef\u4ee5\u653e\u5fc3\u5f00\uff0c\u7f16\u8bd1\u901f\u5ea6\u4e5f\u4f1a\u5f88\u6162\uff0c\u4e00\u822c\u7528\u4e8e\u7a0b\u5e8f\u6700\u7ec8\u6210\u54c1\u53d1\u5e03\u9636\u6bb5\u3002 -Ofast \uff1a\u5728 -O3 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5bf9\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97\u8fdb\u884c\u66f4\u6df1\u5c42\u6b21\u7684\u4f18\u5316\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u6d6e\u70b9\u6570\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e0d\u6d89\u53ca\u5230 NaN \u548c Inf \u7684\u5904\u7406\uff0c\u90a3\u4e48 -Ofast \u4e0d\u4f1a\u6709\u592a\u5927\u7684\u95ee\u9898\uff0c\u4e00\u822c\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\u7684\u7ec8\u6781\u6027\u80fd\u4f18\u5316\u3002 -Os \uff1a\u5728 -O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u6bd4 -O0 \u3001 -O1 \u3001 -O2 \u90fd\u8981\u5c0f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Oz \uff1a\u5728 -Os \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u628a\u4ee3\u7801\u538b\u7f29\uff0c\u53ef\u80fd\u628a\u672c\u53ef\u4ee5\u4e00\u6761\u5927\u6307\u4ee4\u5b8c\u6210\u7684\u4efb\u52a1\u4e5f\u62c6\u6210\u591a\u6761\u5c0f\u6307\u4ee4\uff0c\u4e3a\u4e86\u5c3a\u5bf8\u5b8c\u5168\u6027\u80fd\uff0c\u5927\u5e45\u51cf\u5c11\u4e86\u51fd\u6570\u5185\u8054\u7684\u673a\u4f1a\uff0c\u6709\u65f6\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Og \uff1a\u5728 -O0 \u7684\u57fa\u7840\u4e0a\uff0c\u5c3d\u53ef\u80fd\u4fdd\u7559\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\uff0c\u4e0d\u505a\u7834\u574f\u51fd\u6570\u884c\u53f7\u7b49\u4fe1\u606f\u7684\u4f18\u5316\uff0c\u5efa\u8bae\u914d\u5408\u4ea7\u751f\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\u7684 -g \u9009\u9879\u4f7f\u7528\u3002\u4f46\u8fd8\u662f\u4f1a\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u4f18\u5316\uff0c\u6bd4 -O0 \u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002\u4f46 -Og \u7684\u6240\u6709\u4f18\u5316\u90fd\u4e0d\u4f1a\u6d89\u53ca\u5230\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u8c03\u8bd5\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u4f46\u662f\u7531\u4e8e\u63d2\u5165\u4e86\u8c03\u8bd5\u4fe1\u606f\uff0c\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u53d8\u5f97\u5f88\u5927\uff0c\u4e00\u822c\u5728\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002 MSVC\uff1a /Od \u3001 /O1 \u3001 /O2 \u3001 /Ox \u3001 /Ob1 \u3001 /Ob2 \u3001 /Os /Od \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\u9636\u6bb5\u3002 /O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\u3002 /O2 \uff1a\u6bd4 /O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u4f1a\u5c1d\u8bd5\u628a\u4e00\u4e9b\u5faa\u73af\u77e2\u91cf\u5316\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ox \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u4f46\u662f\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ob1 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\u3002 /Ob2 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\uff0c\u4f46\u662f\u4f1a\u6269\u5927\u5185\u8054\u8303\u56f4\uff0c\u4e00\u822c\u6bd4 /Ob1 \u66f4\u5feb\uff0c\u4f46\u662f\u4e5f\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u3002 /Os \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002","title":"\u4f18\u5316\u7b49\u7ea7"},{"location":"platform/#_7","text":"Clang \u548c GCC\uff1a -g \u3001 -g0 \u3001 -g1 \u3001 -g2 \u3001 -g3 MSVC\uff1a /Z7 \u3001 /Zi","title":"\u8c03\u8bd5\u4fe1\u606f"},{"location":"platform/#_8","text":"","title":"\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84"},{"location":"platform/#_9","text":"","title":"\u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93"},{"location":"platform/#_10","text":"","title":"\u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84"},{"location":"platform/#_11","text":"Clang \u548c GCC\uff1a -Dmacro=value MSVC\uff1a /Dmacro=value \u4f8b\u5982\uff1a","title":"\u5b9a\u4e49\u5b8f"},{"location":"platform/#_12","text":"","title":"\u8b66\u544a\u5f00\u5173"},{"location":"platform/#_13","text":"libstdc++ \u662f GCC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e GCC \u662f Linux \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libstdc++ \u4e5f\u662f Linux \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u770b\u5230\u4ed6\u7684\u6e90\u7801\uff1ahttps://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3 libc++ \u662f Clang \u5b98\u65b9\u7f16\u5199\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e Clang \u662f MacOS \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libc++ \u4e5f\u662f MacOS \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002libc++ \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u65e9\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\u3002\u9879\u76ee\u7684\u5f00\u6e90\u5730\u5740\u662f\uff1ahttps://github.com/llvm/llvm-project/tree/main/libcxx MSVC STL \u662f MSVC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e MSVC \u662f Windows \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 MSVC STL \u4e5f\u662f Windows \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002MSVC STL \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u665a\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\uff0c\u4f46\u662f\u73b0\u5728\u4ed6\u5df2\u7ecf\u5b8c\u5168\u652f\u6301 C++20\uff0c\u5e76\u4e14\u4e5f\u5b8c\u5168\u5f00\u6e90\u4e86\uff1ahttps://github.com/microsoft/STL \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6807\u51c6\u5e93\u548c\u7f16\u8bd1\u5668\u5e76\u4e0d\u662f\u7ed1\u5b9a\u7684\uff0c\u4f8b\u5982 Clang \u53ef\u4ee5\u7528 libstdc++ \u6216 MSVC STL\uff0cGCC \u4e5f\u53ef\u4ee5\u88ab\u914d\u7f6e\u4f7f\u7528 libc++\u3002 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0cClang \u9ed8\u8ba4\u7528\u7684\u5c31\u662f libstdc++\u3002\u9700\u8981\u4e3a Clang \u6307\u5b9a -stdlib=libc++ \u9009\u9879\uff0c\u624d\u80fd\u4f7f\u7528\u3002 \u725b\u5934\u4eba\u7b11\u8bdd\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f libstdc++\u3002\u56e0\u4e3a\u5373\u4f7f\u4ed6\u7684\u7f16\u8bd1\u5668\u662f Clang\uff0c\u4ed6\u7528\u7684\u5927\u6982\u7387\u4f9d\u7136\u662f libstdc++\u3002\u201d","title":"\u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6"},{"location":"platform/#_14","text":"TODO","title":"\u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f"},{"location":"unicode/","text":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u96c6 ASCII Latin-1 Unicode \u603b\u7ed3 \u5b57\u7b26\u7f16\u7801 UTF-32 UTF-8 \u517c\u5bb9 ASCII \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d UTF-16 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 BOM \u6807\u8bb0 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 UTF-8 \u9635\u8425 UTF-16 \u9635\u8425 UTF-32 \u9635\u8425 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale UTF \u4e4b\u95f4\u4e92\u8f6c GBK \u548c UTF \u4e92\u8f6c UTF \u548c ANSI \u4e92\u8f6c \u5927\u603b\u7ed3 GBK \u548c Shift-JIS \u4e92\u8f6c \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u66f4\u591a\u529f\u80fd\uff1f\uff01 Windows \u7528\u6237\uff1aMultiByteToWideChar MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b Linux \u7528\u6237\uff1aiconv iconv \u547d\u4ee4\u884c\u5de5\u5177 \u672c\u5730\u5316 (locale) \u533a\u5206\u5b57\u7b26\u7c7b\u578b \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u5bbd\u5b57\u7b26\u6d41 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring wchar_t \u7cfb\u5217\u51fd\u6570 std::wcout \u7684\u4f7f\u7528 std::wfstream \u7684\u4f7f\u7528 C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 Windows \u4e13\u9898 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 TCHAR \u6d41\u6d3e UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 Qt QString QTextCodec from/toLocal8Bits/Utf8/Latin1/Ascii \u5b57\u7b26\u4e32\u5e38\u91cf QTextStream Python 3 str Rust &str \u548c String Java String COW \u5b57\u7b26\u4e32 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme \u6b63\u89c4\u5316 \u96f6\u5bbd\u7a7a\u683c \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u7684\u5999\u7528 Base64 \u9632\u4e71\u7801 \u5b57\u7b26\u96c6 \u8ba1\u7b97\u673a\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5b57\u7b26\uff0c\u800c\u662f\u7528\u6570\u5b57\u6765\u4ee3\u66ff\uff0c\u8fd9\u5c31\u662f\u5b57\u7b26\u96c6\uff0c\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\u3002 ASCII ASCII \u4e3a\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u7ec4\u3001\u6807\u70b9\u7b26\u53f7\u7b49 128 \u4e2a\u5b57\u7b26\uff0c\u6bcf\u4e2a\u90fd\u7528\u4e00\u4e2a 0 \u5230 127 \u8303\u56f4\u5185\u7684\u6570\u5b57\u5bf9\u5e94\u3002 \u5982\u679c\u4f60\u60f3\u8981\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c31\u5728\u8fd9\u4e2a\u8868\u91cc\u5bfb\u627e\u5230\u76f8\u5e94\u7684\u6570\u5b57\u7f16\u53f7\uff0c\u7136\u540e\u5b58\u8fd9\u4e2a\u7f16\u53f7\u5373\u53ef\u3002 \u4f8b\u5982\u4e0b\u9762\u7684\u4e00\u4e32\u6570\u5b57\uff1a 80 101 110 103 \u5728 ASCII \u8868\u4e2d\u67e5\u627e\uff0c\u53d1\u73b0\u8fd9\u4e9b\u6570\u5b57\u5206\u522b\u5bf9\u5e94 P \u3001 e \u3001 n \u3001 g \u56db\u4e2a\u5b57\u6bcd\uff0c\u8fde\u8d77\u6765\u5c31\u8fd8\u539f\u5f97\u5230\u4e86\u539f\u672c\u7684\u5b57\u7b26\u4e32\u201cPeng\u201d\u3002 Latin-1 Latin-1 \u6269\u5145\u4e86 ASCII \u5b57\u7b26\u96c6\uff0c\u4fdd\u6301 ASCII \u539f\u6709 0 \u5230 127 \u7684\u90e8\u5206\u6620\u5c04\u4e0d\u53d8\uff0c\u989d\u5916\u8ffd\u52a0\u4e86 128 \u5230 255 \u7684\u6620\u5c04\u5173\u7cfb\u3002\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a EASCII\uff08\u6269\u5c55 ASCII\uff09\u3002 Unicode Unicode \u5b57\u7b26\u96c6\u4e3a\u5168\u4e16\u754c\u7684\u6240\u6709\u5b57\u7b26\u90fd\u5bf9\u5e94\u4e86\u4e00\u4e2a\u6574\u6570\u3002 \u5b57\u7b26 \u7f16\u53f7 \u6211 25105 \u6212 25106 \u6213 25107 \u6214 25108 \u6215 25109 \u6216 25110 \u6217 25111 \u6218 25112 \u6219 25113 \u621a 25114 \u51fa\u4e8e\u5386\u53f2\u517c\u5bb9\u6027\u8003\u8651\uff0cUnicode \u5728 0 \u5230 256 \u533a\u95f4\u5185\u7684\u6620\u5c04\u548c ASCII\u3001Latin-1 \u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002 \u5b57\u7b26 \u7f16\u53f7 P 80 e 101 n 110 g 103 Unicode \u7ecf\u8fc7\u4e86\u8bb8\u591a\u7248\u672c\u7684\u53d1\u5c55\uff0c\u65e9\u671f\u7684 Unicode \u53ea\u6536\u5f55\u4e86 65536 (0x10000) \u4e2a\u5b57\u7b26\uff0c\u540e\u6765\u6269\u5145\u5230\u4e86 1114112 (0x110000) \u4e2a\u5b57\u7b26\u3002 \u603b\u4e4b\uff0c\u73b0\u5728 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u867d\u7136\u5360\u7528\u4e86 1114112 \u8fd9\u591a\u683c\u7801\u70b9\u7a7a\u95f4\uff0c\u4e0d\u8fc7\u5176\u4e2d\u5f88\u591a\u90fd\u662f\u7a7a\u53f7\uff0c\u7559\u5f85\u672a\u6765\u6269\u5145\u4f7f\u7528\u3002 Unicode \u5b57\u7b26\u6620\u5c04\u8868\u53ef\u4ee5\u5728\u7f51\u4e0a\u627e\u5230\uff1a https://symbl.cc/en/unicode-table/ https://www.compart.com/en/unicode/ \u603b\u7ed3 \u5b57\u7b26\u96c6: \u4ece\u5b57\u7b26\u5230\u6574\u6570\u7684\u4e00\u4e00\u6620\u5c04\u3002 ASCII: \u53ea\u6536\u5f55\u4e86\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 Latin-1: \u5728 ASCII \u57fa\u7840\u4e0a\u8ffd\u52a0\u4e86\u6ce8\u97f3\u5b57\u6bcd\uff0c\u6ee1\u8db3\u6b27\u6d32\u7528\u6237\u9700\u8981\u3002 Unicode: \u6536\u5f55\u4e86\u5168\u4e16\u754c\u6240\u6709\u6587\u5b57\u548c\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 \u8ba1\u7b97\u673a\u5b58\u50a8\u5b57\u7b26\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5b58\u50a8\u4e86\u90a3\u4e2a\u5bf9\u5e94\u7684\u6574\u6570\u3002 \u8fd9\u4e9b\u6574\u6570\u5c31\u88ab\u79f0\u4e3a \u7801\u70b9 (code point) \uff0c\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002 \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u5458\u901a\u5e38\u559c\u6b22\u7528\u5341\u516d\u8fdb\u5236\u4e66\u5199\u6570\u5b57\uff1a \u5b57\u7b26 \u7f16\u53f7 \u6211 0x6211 \u6212 0x6212 \u6213 0x6213 \u6214 0x6214 \u6215 0x6215 \u6216 0x6216 \u6217 0x6217 \u6218 0x6218 \u6219 0x6219 \u621a 0x621A \u4f8b\u5982\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\uff0c\u5728 Unicode \u8868\u4e2d\u7f16\u53f7\u4e3a 0x6211\u3002\u4e8e\u662f\u5f53\u8ba1\u7b97\u673a\u9700\u8981\u8868\u793a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\u65f6\uff0c\u5c31\u7528 0x6211 \u8fd9\u4e2a\u6574\u6570\u4ee3\u66ff\u3002 \u5982\u679c\u8981\u8868\u793a\u591a\u4e2a\u5b57\u7b26\uff0c\u90a3\u5c31\u7528\u4e00\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u5427\uff01 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5904\u7406\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a 0x6211 0x7231 0x30EDE 0x30EDE 0x9762 0x21 \u8fd9\u4e00\u4e32\u6570\u5b57\u4ee3\u66ff\u3002 \u5b57\u7b26\u7f16\u7801 Unicode \u53ea\u662f\u6307\u5b9a\u4e86\u6574\u6570\uff0c\u6ca1\u6709\u89c4\u5b9a\u6574\u6570\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5b58\u5728\u3002 \u5b57\u7b26\u7f16\u7801: \u5c06\u5b57\u7b26\u7684\u6574\u6570\u7f16\u53f7\u5e8f\u5217\u5316\u4e3a\u8ba1\u7b97\u673a\u53ef\u76f4\u63a5\u5b58\u50a8\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u5b9e\u9645\u5b58\u5728\u7684\u6574\u6570\u7c7b\u578b\u3002 Unicode \u5b57\u7b26\u53ef\u4ee5\u9009\u7528\u4ee5\u4e0b\u8fd9\u4e9b\u5b57\u7b26\u7f16\u7801\u6765\u5e8f\u5217\u5316\uff1a UTF-32: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u4e2a uint32_t \u6574\u6570\u5b58\u50a8\u3002 UTF-16: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 2 \u4e2a uint16_t \u6574\u6570\u5b58\u50a8\u3002 UTF-8: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u5b58\u50a8\u3002 \u7ffb\u8bd1\u51fa\u6765\u7684\u8fd9\u4e9b\u5c0f\u6574\u6570\u53eb \u7801\u4f4d (code unit) \u3002\u4f8b\u5982\u5bf9\u4e8e UTF-8 \u800c\u8a00\uff0c\u6bcf\u4e2a uint8_t \u5c31\u662f\u4ed6\u7684\u7801\u4f4d\u3002 UTF-32 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u6700\u5927\u503c 0x10FFFF \u6709 21 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0cC \u8bed\u8a00\u4e2d uint32_t \u80fd\u5bb9\u7eb3 32 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0c\u6240\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u7528 uint32_t \u6570\u7ec4\u6765\u4e00\u4e2a\u4e2a\u5bb9\u7eb3 Unicode \u5b57\u7b26\u7801\u70b9\u3002\u867d\u7136\u6d6a\u8d39\u4e86 11 \u4f4d\uff0c\u4f46\u81f3\u5c11\u6240\u6709 Unicode \u5b57\u7b26\u90fd\u80fd\u5b89\u5168\u5bb9\u7eb3\u3002 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a std::vector s = { 0x00006211, // \u6211 0x00007231, // \u7231 0x00030EDE, // \ud883\udede 0x00030EDE, // \ud883\udede 0x00009762, // \u9762 0x00000021, // ! }; \u8fd9\u4e2a\u6570\u7ec4\u8868\u793a\u3002 UTF-32 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u56fa\u5b9a\u5bf9\u5e94\u4e00\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-32 \u662f \u5b9a\u957f\u7f16\u7801 \u3002\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5c31\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002 \u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u64cd\u4f5c\u3002 \u65e0\u8bba\u5bf9\u6570\u7ec4\u5982\u4f55\u5207\u7247\uff0c\u90fd\u4e0d\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u7834\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u53cd\u8f6c\uff0c\u4e0d\u4f1a\u4ea7\u751f\u7834\u574f\u5b57\u7b26\u7684\u95ee\u9898\u3002 \u7f3a\u70b9\u662f\uff1a \u6d6a\u8d39\u5b58\u50a8\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\uff0c\u59cb\u7ec8\u91c7\u7528 UTF-32 \u5f62\u5f0f\u5904\u7406\u6587\u5b57\u3002 UTF-32 \u4e5f\u88ab\u79f0\u4e3a UCS-4\uff0c\u4ed6\u4fe9\u662f\u540c\u4e49\u8bcd\u3002 UTF-8 UTF-32 \u867d\u7136\u65b9\u4fbf\u4e86\u6587\u5b57\u5904\u7406\uff0c\u7136\u800c\uff0c\u5374\u6d6a\u8d39\u4e86\u5927\u91cf\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e0d\u5229\u4e8e\u6587\u5b57\u5b58\u50a8\uff01\u4e00\u4e2a\u5b57\u7b26\uff0c\u65e0\u8bba\u4ed6\u662f\u5e38\u7528\u8fd8\u662f\u4e0d\u5e38\u7528\uff0c\u90fd\u8981\u9738\u5360 4 \u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\u3002 Unicode \u7f16\u7801\u5b57\u7b26\u65f6\uff0c\u7279\u610f\u628a\u5e38\u7528\u7684\u5b57\u7b26\u9760\u524d\u6392\u5217\u4e86\u3002 \u4e16\u754c\u4e0a\u5e38\u7528\u8bed\u8a00\u6587\u5b57\u90fd\u88ab\u523b\u610f\u7f16\u7801\u5728\u4e86 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u8d85\u8fc7 0x10000 \u7684\u57fa\u672c\u90fd\u662f\u4e0d\u5e38\u7528\u7684\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\uff0c\u5f88\u591a\u90fd\u662f\u5df2\u7ecf\u65e0\u4eba\u4f7f\u7528\u7684\u53e4\u4ee3\u6587\u5b57\u548c\u751f\u50fb\u5b57\uff0c\u4f8b\u5982\u201c\ud883\udede\u201d\u3002\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8fd9\u4e9b\u5076\u5c14\u4f7f\u7528\u7684\u7f55\u89c1\u6587\u5b57\uff0c\u5c31\u8981\u6c42\u6240\u6709\u6587\u5b57\u90fd\u7528\u540c\u6837\u7684 4 \u5b57\u8282\u5bbd\u5ea6\u5b58\u50a8\uff0c\u5b9e\u5728\u662f\u6709\u70b9\u6d6a\u8d39\u3002 \u5728 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u540c\u6837\u6709\u6309\u7167\u5e38\u7528\u5ea6\u6392\u5e8f\uff1a 0 \u5230 0x7F \u662f\uff08\u6b27\u7f8e\u7528\u6237\uff09\u6700\u5e38\u7528\u7684\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u534a\u89d2\u6807\u70b9\u3002 0x80 \u5230 0x7FF \u662f\u8868\u97f3\u6587\u5b57\u533a\uff0c\u5e38\u7528\u7684\u6ce8\u97f3\u5b57\u6bcd\u3001\u62c9\u4e01\u5b57\u6bcd\u3001\u5e0c\u814a\u5b57\u6bcd\u3001\u897f\u91cc\u5c14\u5b57\u6bcd\u3001\u5e0c\u4f2f\u6765\u5b57\u6bcd\u7b49\u3002 0x800 \u5230 0xFFFF \u662f\u8868\u610f\u6587\u5b57\uff0c\u7b80\u7e41\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u6cf0\u6587\u3001\u9a6c\u6765\u6587\u3001\u963f\u62c9\u4f2f\u6587\u7b49\u3002 0x10000 \u5230 0x10FFFF \u662f\u4e0d\u5e38\u7528\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\u3002 UTF-8 \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u538b\u7f29\u95ee\u9898\u800c\u8bde\u751f\u7684\u3002 UTF-8 \u628a\u4e00\u4e2a\u7801\u70b9\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u7801\u4f4d\uff0c\u4e00\u4e2a\u7801\u4f4d\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u8868\u793a\u3002 0 \u5230 0x7F \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 1 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 3 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002 \u5e8f\u5217\u5316\u89c4\u5219\u5982\u4e0b\uff1a \u517c\u5bb9 ASCII \u5bf9\u4e8e 0 \u5230 0x7F \u7684\u5b57\u7b26\uff0c\u8fd9\u4e2a\u8303\u56f4\u7684\u5b57\u7b26\u9700\u8981 7 \u4f4d\u5b58\u50a8\u3002 \u6211\u4eec\u9009\u62e9\u76f4\u63a5\u5b58\u50a8\u5176\u503c\u3002 \u4f8b\u5982 \u2018P\u2019 \u4f1a\u88ab\u76f4\u63a5\u5b58\u50a8\u5176 Unicode \u503c\u7684 80\uff080x50\uff09\uff1a 01010000 \u7531\u4e8e Unicode \u5728 0 \u5230 0x7F \u8303\u56f4\u5185\u4e0e ASCII \u8868\u76f8\u540c\uff0c\u800c UTF-8 \u53c8\u628a 0 \u5230 0x7F \u7684\u503c\u76f4\u63a5\u5b58\u50a8\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u517c\u5bb9 ASCII\u3002\u8fd9\u4f7f\u5f97\u539f\u672c\u8bbe\u8ba1\u4e8e\u5904\u7406 ASCII \u7684 C \u8bed\u8a00\u51fd\u6570\uff0c\u4f8b\u5982 strlen\u3001strcat\u3001sprintf \u7b49\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u65e0\u7f1d\u5207\u6362\u5230 UTF-8\u3002\u53cd\u4e4b\u4ea6\u7136\uff0c\u4efb\u4f55\u8bbe\u8ba1\u7528\u4e8e UTF-8 \u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5b8c\u5168\u63a5\u53d7 ASCII \u683c\u5f0f\u7684\u8f93\u5165\u6587\u672c\u3002 \u4f46\u90e8\u5206\u6d89\u53ca\u5b57\u7b26\u957f\u5ea6\u7684\u51fd\u6570\u4f1a\u6709\u4e9b\u8bb8\u4e0d\u517c\u5bb9\uff0c\u4f8b\u5982 strlen \u6c42\u51fa\u7684\u957f\u5ea6\u4f1a\u53d8\u6210\u5b57\u8282\u7684\u6570\u91cf\u800c\u4e0d\u662f\u5b57\u7b26\u7684\u6570\u91cf\u4e86\uff0c\u4f8b\u5982 strlen(\"\u6211\u4eec\") \u4f1a\u5f97\u5230 6 \u800c\u4e0d\u662f 2\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6784\u9020\u5c31\u50cf\u4e00\u5217\u5c0f\u706b\u8f66\u4e00\u6837\uff0c\u4e0d\u540c\u8303\u56f4\u5185\u7684\u7801\u4f4d\u4f1a\u88ab\u7f16\u7801\u6210\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8f66\uff0c\u4f46\u4ed6\u4eec\u90fd\u6709\u4e00\u4e2a\u8f66\u5934\u3002 \u6839\u636e\u706b\u8f66\u5934\u7684\u201c\u7b49\u7ea7\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u65ad\u51fa\u540e\u9762\u62c9\u7740\u51e0\u8282\u8f66\u53a2\u3002 \u706b\u8f66\u5934\u662f\u4ec0\u4e48\u7b49\u7ea7\u7531\u4ed6\u7684\u4e8c\u8fdb\u5236\u524d\u7f00\u51b3\u5b9a\uff1a \u5982\u679c\u662f 0 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u662f\u5355\u72ec\u4e00\u53f0\u706b\u8f66\u5934\uff0c\u540e\u9762\u6ca1\u6709\u8f66\u53a2\u4e86\uff0c\u8fd9\u8868\u793a\u8f66\u5934\u91cc\u9762\u76f4\u63a5\u88c5\u7740 0 \u5230 0x7F \u8303\u56f4\u7684\u666e\u901a ASCII \u5b57\u7b26\u3002 \u5982\u679c\u662f 110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e00\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u6b27\u6d32\u5b57\u7b26\u3002 \u5982\u679c\u662f 1110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e24\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u4e16\u754c\u5e38\u7528\u5b57\u7b26\u3002 \u5982\u679c\u662f 11110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e09\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u751f\u50fb\u5b57\u7b26\u3002 \u5982\u679c\u662f 10 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u8fd9\u662f\u4e00\u8282\u8f66\u53a2\uff0c\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u8282\u5355\u72ec\u7684\u8f66\u53a2\u5728\u524d\u9762\u65e0\u5934\u9a7e\u9a76\uff0c\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\u3002 \u5c0f\u670b\u53cb\u7528\u5c0f\u53f7\u5217\u8f66\u88c5\uff0c\u5927\u670b\u53cb\u7528\u5927\u53f7\u5217\u8f66\u88c5\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u4e32\u4e8c\u8fdb\u5236\uff1a 11100110 10000010 10000001 \u9996\u5148\uff0c\u770b\u5230\u7b2c\u4e00\u4e2a\u5b57\u8282\uff0c\u662f 1110 \u5f00\u5934\u7684\u4e09\u7ea7\u8f66\u5934\uff01\u8bf4\u660e\u540e\u9762\u8fd8\u6709\u4e24\u8282\u8f66\u53a2\u662f\u5c5e\u4e8e\u4ed6\u7684\u3002\u706b\u8f66\u5934\u4e2d 4 \u4f4d\u7528\u4e8e\u8868\u793a\u8f66\u5934\u7b49\u7ea7\u4e86\uff0c\u5269\u4e0b\u8fd8\u6709 4 \u4f4d\u7528\u4e8e\u88c5\u4e58\u5ba2\u3002 \u8f66\u53a2\u4e5f\u6709\u56fa\u5b9a\u7684\u524d\u7f00\uff0c\u6240\u6709\u7684\u8f66\u53a2\u90fd\u5fc5\u987b\u662f 10 \u5f00\u5934\u7684\u3002\u53bb\u9664\u8fd9\u5f00\u5934\u7684 2 \u4f4d\uff0c\u5269\u4e0b\u7684 6 \u4f4d\u5c31\u662f\u4e58\u5ba2\u3002 \u5bf9\u4e8e\u8fd9\u79cd\u4e09\u7ea7\u5217\u8f66\uff0c4 + 6 + 6 \u603b\u5171 16 \u4f4d\u4e8c\u8fdb\u5236\uff0c\u521a\u597d\u53ef\u4ee5\u88c5\u5f97\u4e0b 0xFFFF \u5185\u7684\u4e58\u5ba2\u3002 0110 000010 000001 \u7f16\u7801\u65f6\u5219\u662f\u53cd\u8fc7\u6765\u3002 \u4e58\u5ba2\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e09\u7247\uff0c\u4f8b\u5982\u5bf9\u4e8e\u201c\u6211\u201d\u8fd9\u4e2a\u4e58\u5ba2\uff0c\u201c\u6211\u201d\u7684\u7801\u70b9\u662f 0x6211\uff0c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u662f\uff1a 110010000010001 \u628a\u4e58\u5ba2\u5207\u5206\u6210\u9ad8 4 \u4f4d\u3001\u4e2d 6 \u4f4d\u548c\u4f4e 6 \u4f4d\uff1a 1100 100000 10001 \u52a0\u4e0a 1110 \u3001 10 \u548c 10 \u524d\u7f00\u540e\uff0c\u5f62\u6210\u4e00\u5217\u706b\u8f66\uff1a 11100110 10000010 10000001 \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u628a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\uff0c\u7f16\u7801\u6210\u4e86\u4e09\u8282\u5217\u8f66\uff0c\u585e\u8fdb\u5b57\u8282\u6d41\u7684\u7f51\u7edc\u96a7\u9053\u91cc\u4e86\u3002 \u603b\u7ed3\uff1a \u524d\u7f00\u662f 0 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 7 \u540d\u3002 \u524d\u7f00\u662f 10 \u7684\u662f\u8f66\u53a2\uff1a\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002 \u524d\u7f00\u662f 110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 5 \u540d + 1 \u8282\u8f66\u53a2\u8f7d\u5ba2 6 \u540d = \u5171 11 \u540d\u3002 \u524d\u7f00\u662f 1110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 4 \u540d + 2 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 16 \u540d\u3002 \u524d\u7f00\u662f 11110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 3 \u540d + 3 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 21 \u540d\u3002 \u9ad8\u7ea7\u8f66\u5934\u88c5\u4e86\u9632\u5f39\u94a2\u677f\uff0c\u8f7d\u5ba2\u7a7a\u95f4\u53d8\u5c11\uff0c\u53ea\u597d\u5300\u5230\u540e\u9762\u7684\u8f66\u53a2\u3002 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u5982\u679c\u53d1\u73b0 10 \u5f00\u5934\u7684\u72ec\u7acb\u8f66\u53a2\uff0c\u5c31\u8bf4\u660e\u51fa\u95ee\u9898\u4e86\uff0c\u53ef\u80fd\u662f\u706b\u8f66\u88ab\u9519\u8bef\u62e6\u8170\u622a\u65ad\uff0c\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u88ab\u9519\u8bef\u5730\u53cd\u8f6c\u3002\u56e0\u4e3a 10 \u53ea\u53ef\u80fd\u662f\u706b\u8f66\u8f66\u53a2\uff0c\u4e0d\u53ef\u80fd\u51fa\u73b0\u5728\u706b\u8f66\u5934\u90e8\u3002\u6b64\u65f6\u89e3\u7801\u5668\u5e94\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u66ff\u6362\u3002 10000010 10000001 \u5728\u7f51\u7edc\u6536\u53d1\u5305\u65f6\uff0c\u5982\u679c\u4f60\u4e0d\u59a5\u5584\u5904\u7406 TCP \u7c98\u5305\u95ee\u9898\uff0c\u5c31\u53ef\u80fd\u706b\u8f66\u5934\u8fdb\u53bb\u4e86\uff0c\u706b\u8f66\u5c3e\u5df4\u8fd8\u9732\u5728\u96a7\u9053\u5916\u9762\uff0c\u4e00\u6bb5\u5b8c\u6574\u7684\u5217\u8f66\u88ab\u5207\u65ad\uff0c\u5bfc\u81f4 UTF-8 \u89e3\u8bfb\u7684\u65f6\u5019\u51fa\u9519\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u8bbe\u7acb\u4e00\u4e2a\u72b6\u6001\u673a\u6765\u89e3\u7801 UTF-8\u3002C \u8bed\u8a00\u7684 mbstate_t \u5c31\u662f\u8fd9\u79cd\u72b6\u6001\u673a\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\uff0c\u5374\u53d1\u73b0\u91cc\u9762\u88c5\u7740 0x394 (\u201c\u0394\u201d)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u5c31\u80fd\u88c5\u4e0b\u7684\u6b27\u6d32\u5b57\u7b26\uff0c\u5374\u7528\u4e86\u4e09\u7ea7\u706b\u8f66\u5934\u88c5\uff0c\u8bf4\u660e\u88c5\u7bb1\u90a3\u8fb9\u7684\u4eba\u5077\u61d2\u6ee5\u7528\u8d44\u6e90\u4e86\uff01\u8fd9\u79cd\u60c5\u51b5\u4e0b UTF-8 \u89e3\u7801\u5668\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u8981\u4fdd\u8bc1\u7f16\u7801\u7684\u552f\u4e00\u6027\uff0c0x394 \u662f 0x7F \u5230 0x7FF \u8303\u56f4\u7684\uff0c\u5c31\u5e94\u8be5\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u88c5\u3002 \u4ee5\u53ca\uff0c\u5982\u679c\u53d1\u73b0 11111 \u5f00\u5934\u7684\u4e94\u7ea7\u706b\u8f66\u5934\uff0c\u4e5f\u8981\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u6700\u591a\u53ea\u652f\u6301\u56db\u7ea7\u706b\u8f66\u5934\u3002 \u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u56db\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u7684\u5b57\u7b26\u8303\u56f4\u8d85\u8fc7\u4e86 0x10FFFF\uff0c\u8fd9\u8d85\u51fa\u4e86 Unicode \u7684\u8303\u56f4\uff0c\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\u3002\u5982\u679c\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u53d1\u73b0\u5b57\u7b26\u8303\u56f4\u5904\u5728\u4fdd\u7559\u533a 0xD800 \u5230 0xDFFF \u5185\uff0c\u8fd9\u662f Unicode \u627f\u8bfa\u6c38\u4e0d\u52a0\u5165\u5b57\u7b26\u7684\u533a\u95f4\uff08\u7a0d\u540e\u8bb2\u89e3 UTF-16 \u65f6\u4f1a\u89e3\u91ca\u4e3a\u4ec0\u4e48\uff09\uff0c\u4e5f\u8981\u62a5\u9519\u3002\u603b\u4e4b Unicode \u7801\u70b9\u7684\u5408\u6cd5\u8303\u56f4\u662f 0x0 \u5230 0xD7FF\uff0c0xE000 \u5230 0x10FFFF\u3002 \u603b\u4e4b\uff0cUTF-8 \u5177\u6709\u4e00\u5b9a\u7684\u5197\u4f59\u548c\u81ea\u7ea0\u9519\u80fd\u529b\uff0c\u5982\u679c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5dee\u9519\uff0c\u53ef\u80fd\u4f1a\u7206\u51fa\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u3002\u8fd9\u4e2a\u7279\u6b8a\u5b57\u7b26\u662f Unicode \u5b98\u65b9\u89c4\u5b9a\u7684\uff0c\u7801\u70b9\u4e3a 0xFFFD\uff0c\u51fa\u73b0\u4ed6\u5c31\u610f\u5473\u7740 UTF-8 \u89e3\u7801\u5931\u8d25\u4e86\u3002 \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u4ee5 UTF-8 \u683c\u5f0f\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff1a std::vector s = { 0xE6, 0x88, 0x91, // \u6211\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xE7, 0x88, 0xB1, // \u7231\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xE9, 0x9D, 0xA2, // \u9762\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0x21, // !\uff0c\u8fd9\u662f\u4e2a ASCII \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u76f4\u63a5\u7528\u5355\u4e2a\u706b\u8f66\u5934\u88c5 }; UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u662f\u4e00\u79cd \u53d8\u957f\u7f16\u7801 \u3002\u53d8\u957f\u7f16\u7801\u7684\u7f3a\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e0d\u4e00\u5b9a\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u56e0\u6b64\uff0c\u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\u3002 \u6570\u7ec4\u7684\u5355\u4e2a\u5143\u7d20\u7d22\u5f15\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u53d6\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u3002 \u5bf9\u6570\u7ec4\u7684\u5207\u7247\uff0c\u53ef\u80fd\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u5207\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u4e0d\u4e00\u5b9a\u80fd\u628a\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\uff0c\u56e0\u4e3a\u53ef\u80fd\u4e0d\u614e\u628a\u4e00\u4e2a\u5b57\u7b26\u7684\u591a\u4e2a\u7801\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u5b57\u7b26\u7834\u574f\u3002 \u4f18\u70b9\u662f\uff1a \u8282\u7ea6\u5b58\u50a8\u7a7a\u95f4\u3002 \u6211\u4eec\u63a8\u8350\u53ea\u5728\u7f51\u7edc\u901a\u4fe1\u3001\u786c\u76d8\u5b58\u50a8\u65f6\uff0c\u91c7\u7528 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u6587\u5b57\u3002 \u603b\u4e4b\uff0cUTF-8 \u9002\u5408\u5b58\u50a8\uff0cUTF-32 \u9002\u5408\u5904\u7406\u3002 \u6211\u4eec\u5efa\u8bae\u8ba1\u7b97\u673a\u4ece\u786c\u76d8\u6216\u7f51\u7edc\u4e2d\u8bfb\u51fa UTF-8 \u5b57\u7b26\u4e32\u540e\uff0c\u7acb\u5373\u5c06\u5176\u8f6c\u6362\u4e3a UTF-32\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u6587\u5b57\u5904\u7406\u3002\u5f53\u9700\u8981\u5199\u5165\u786c\u76d8\u6216\u7f51\u7edc\u65f6\uff0c\u518d\u8f6c\u6362\u56de UTF-8\uff0c\u907f\u514d\u786c\u76d8\u5bb9\u91cf\u548c\u7f51\u7edc\u5e26\u5bbd\u7684\u6d6a\u8d39\u3002 \u8ba1\u7b97\u673a\u9700\u8981\u5916\u7801\u548c\u5185\u7801\u4e24\u79cd\uff1a \u5916\u7801=\u786c\u76d8\u4e2d\u7684\u6587\u672c=UTF-32 \u5185\u7801=\u5185\u5b58\u4e2d\u7684\u6587\u672c=UTF-8 UTF-16 UTF-16 \u7684\u7b56\u7565\u662f\uff1a\u65e2\u7136\u5927\u591a\u6570\u5e38\u7528\u5b57\u7b26\u7684\u7801\u70b9\u90fd\u5728 0x0 \u5230 0xFFFF \u5185\uff0c\u7528 uint32_t \u6765\u5b58\u50a8\u4e5f\u592a\u6d6a\u8d39\u4e86\u3002\u4ed6\u7684\u65b9\u6848\u5982\u4e0b\uff1a \u5bf9\u4e8e 0x0 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5c31\u7528\u4e00\u4e2a uint16_t \u76f4\u63a5\u5b58\u3002 \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u53cd\u6b63\u4e0d\u5e38\u89c1\uff0c\u5c31\u62c6\u6210\u4e24\u4e2a uint16_t \u5b58\u3002\u8fd9\u4e2a\u62c6\u7684\u65b9\u6848\u5f88\u6709\u8bb2\u7a76\uff0c\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u62c6\uff0c\u7531\u4e8e\u89e3\u7801\u65f6\u6536\u5230\u7684\u662f\u4e2a\u6ca1\u5934\u6ca1\u5c3e\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u65e0\u6cd5\u5206\u8fa8\u8fd9\u5230\u5e95\u662f\u4e24\u4e2a uint16_t \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u8fd8\u662f\u4e00\u4e2a uint16_t \u7684\u666e\u901a\u5b57\u7b26\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u628a\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u201c\ud883\udede\u201d\uff0c0x30EDE\u3002\u62c6\u6210\u4e24\u4e2a uint16_t \uff0c\u5f97\u5230 0x3 \u548c 0x0EDE\u3002\u5982\u679c\u76f4\u63a5\u5b58\u50a8\u8fd9\u4e24\u4e2a uint16_t \uff1a 0x0003 0x0EDE \u4e4b\u540e\u89e3\u7801\u65f6\uff0c\u5148\u8bfb\u5230 0x0003\uff0c\u8fd8\u4f1a\u4ee5\u4e3a\u4ed6\u662f\u5355\u72ec\u7684\u4e00\u4e2a uint16_t \uff0c\u8868\u793a 3 \u53f7\u5b57\u7b26\u201c\u201d\u3002\u540e\u9762\u7684 0x0EDE \u5c31\u53d8\u6210\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 0x0EDE\uff0c\u53d8\u6210\u4e86 0x0EDE \u53f7\u5b57\u7b26 \u201c\u0ede\u201d\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u201c\ud883\udede\u201d\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u6beb\u4e0d\u76f8\u5e72\u7684\u5b57\u7b26\uff0c\u201c\u0ede\u201d\u4e86\u3002 \u4e3a\u4e86\u907f\u514d\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u4e24\u4e2a uint16_t \u9700\u8981\u91c7\u7528\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\u4ee5\u793a\u533a\u5206\u3002\u8ba9\u89e3\u7801\u5668\u4e00\u770b\u5230\uff0c\u5c31\u80fd\u786e\u5b9a\u8fd9\u4e24\u4e2a uint16_t \u9700\u8981\u7ec4\u88c5\u6210\u540c\u4e00\u4e2a\u5b57\u7b26\u3002 \u8fd9\u5c31\u7528\u5230\u4e86\u4e00\u4e2a\u201c\u6f0f\u6d1e\u201d\uff1aUnicode \u5e76\u6ca1\u6709\u628a\u7801\u70b9\u5206\u914d\u7684\u6ee1\u6ee1\u5f53\u5f53\uff0c\u6216\u8bb8\u662f\u51fa\u4e8e\u5148\u89c1\u4e4b\u660e\uff0c\u5728 0xD800 \u5230 0xDFFF \u4e4b\u95f4\u9884\u7559\u4e86\u4e00\u5927\u6bb5\u7a7a\u53f7\uff1a UTF-16 \u5c31\u662f\u5229\u7528\u4e86\u8fd9\u4e00\u6bb5\u7a7a\u95f4\uff0c\u4ed6\u89c4\u5b9a\uff1a0xD800 \u5230 0xDFFF \u4e4b\u95f4\u7684\u7801\u70b9\u5c06\u6c38\u8fdc\u4e0d\u7528\u6765\u8868\u793a\u5b57\u7b26\uff0c\u800c\u662f\u4f5c\u4e3a \u4ee3\u7406\u5bf9 (surrogate-pair) \u3002\u5176\u4e2d 0xD800 \u5230 0xDBFF \u662f \u9ad8\u4f4d\u4ee3\u7406 (high surrogate) \uff0c0xDC00 \u5230 0xDFFF \u662f \u4f4e\u4f4d\u4ee3\u7406 (low surrogate) \u3002\u9ad8\u4ee3\u7406\u5728\u524d\uff0c\u4f4e\u4ee3\u7406\u5728\u540e\u3002 \u4e00\u4e2a\u8d85\u8fc7 0xFFFF \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f1a\u88ab\u62c6\u6210\u4e24\u6bb5\uff0c\u4e00\u6bb5\u653e\u5728\u9ad8\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u6bb5\u653e\u5728\u4f4e\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u524d\u4e00\u540e\u653e\u5165 uint16_t \u5e8f\u5217\u4e2d\u3002 \u642d\u8f7d\u8d85\u5bbd\u8d85\u9650\u8d27\u7269\u7684\u8f66\u8f86\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e24\u6bb5\u518d\u8fdb\u5165\u96a7\u9053\u3002 \u5177\u4f53\u62c6\u5206\u65b9\u6cd5\u5982\u4e0b\uff1a \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7801\u70b9\uff0c\u9996\u5148\u5c06\u5176\u503c\u51cf\u53bb 0x10000\uff0c\u53d8\u6210\u4e00\u4e2a\u8303\u56f4 0x0 \u5230 0xFFFFF \u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u8fd9\u80fd\u4fdd\u8bc1\u4ed6\u4eec\u53ea\u9700 20 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u53ef\u8868\u793a\u3002 \u4f8b\u5982\u201c\ud883\udede\u201d\u5bf9\u5e94\u7684\u7801\u70b9 0x30EDE\uff0c\u51cf\u53bb\u540e\u5c31\u53d8\u6210 0x20EDE\u3002 \u7136\u540e\uff0c\u5199\u51fa 0x20EDE \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a 00100000111011011110 \u603b\u5171 20 \u4f4d\uff0c\u6211\u4eec\u5c06\u5176\u62c6\u6210\u9ad8\u4f4e\u5404 10 \u4f4d\uff1a 0010000011 1011011110 \u5404\u81ea\u5199\u51fa\u76f8\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6570\uff1a 0x083 0x2DE \u56e0\u4e3a\u6700\u591a\u53ea\u6709 10 \u4f4d\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4f1a\u5728 0 \u5230 0x3FF \u7684\u8303\u56f4\u5185\u3002 \u800c 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u9884\u7559\u7684\u7a7a\u95f4\uff0c\u521a\u597d\u53ef\u4ee5\u5206\u522b\u5bb9\u7eb3 0x400 \u4e2a\u6570\uff01 \u6240\u4ee5\uff0c\u6211\u4eec\u5c06\u62c6\u5206\u51fa\u6765\u7684\u4e24\u4e2a 10 \u4f4d\u6570\uff0c\u5206\u522b\u52a0\u4e0a 0xD800 \u548c 0xDC00\uff1a 0xD800+0x083=0xD883 0xDC00+0x2DE=0xDFDE \u8fd9\u4e24\u4e2a\u6570\uff0c\u5fc5\u5b9a\u662f 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u6570\u3002\u800c\u8fd9\u4e24\u4e2a\u8303\u56f4\u90fd\u662f Unicode \u59d4\u5458\u4f1a\u9884\u7559\u7684\u4ee3\u7406\u5bf9\u533a\u95f4\uff0c\u7edd\u5bf9\u6ca1\u6709\u666e\u901a\u5b57\u7b26\u3002\u6240\u4ee5\uff0c\u751f\u6210\u7684\u4e24\u4e2a\u4ee3\u7406\u5bf9\u4e0d\u4f1a\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u53ef\u4ee5\u653e\u5fc3\u653e\u8fdb uint16_t \u6570\u7ec4\uff0c\u89e3\u7801\u5668\u5982\u679c\u68c0\u6d4b\u5230\u4ee3\u7406\u5bf9\uff0c\u5c31\u8bf4\u660e\u662f\u4e24\u8282\u8f66\u53a2\uff0c\u53ef\u4ee5\u653e\u5fc3\u8fde\u7eed\u8bfb\u53d6\u4e24\u4e2a uint16_t \u3002 \u6240\u4ee5\uff0c 0xD883 0xDFDE \u5c31\u662f\u201c\ud883\udede\u201d\u7528 UTF-16 \u7f16\u7801\u540e\u7684\u7ed3\u679c\u3002 \u4ee3\u7406\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\uff0c\u5f53\u89e3\u7801\u5668\u68c0\u6d4b\u5230\u4e00\u4e2a 0xD800 \u5230 0xDBFF \u8303\u56f4\u5185\u7684\u9ad8\u4ee3\u7406\u65f6\uff0c\u5c31\u9884\u793a\u7740\u8fd8\u9700\u8981\u518d\u8bfb\u53d6\u4e00\u4e2a\u4f4e\u4ee3\u7406\uff0c\u624d\u80fd\u62fc\u63a5\u6210\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u3002 \u5982\u679c\u63a5\u4e0b\u6765\u8bfb\u5230\u7684\u4e0d\u662f 0xDC00 \u5230 0xDFFF \u8303\u56f4\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u800c\u662f\u666e\u901a\u5b57\u7b26\u7684\u8bdd\uff0c\u90a3\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u53ef\u80fd\u662f\u4e2d\u95f4\u88ab\u4eba\u4e22\u5305\u4e86\uff0c\u9700\u8981\u62a5\u9519\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u9876\u66ff\u3002 \u53e6\u5916\uff0c\u5982\u679c\u8bfb\u5230\u4e86\u4e00\u4e2a\u5355\u72ec\u5b58\u5728\u7684 0xD800 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u90a3\u4e5f\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u56e0\u4e3a\u4ee3\u7406\u5b57\u7b26\u53ea\u6709\u6210\u5bf9\u51fa\u73b0\u624d\u6709\u610f\u4e49\uff0c\u4f4e\u4ee3\u7406\u5b57\u7b26\u4e0d\u53ef\u80fd\u5355\u72ec\u5728\u5f00\u5934\u51fa\u73b0\u3002 \u53ef\u89c1\uff0cUTF-16 \u548c UTF-8 \u4e00\u6837\uff0c\u90fd\u662f\u201c\u5c0f\u706b\u8f66\u201d\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0cUTF-16 \u540c\u6837\u4e5f\u6709\u7740\u7c7b\u4f3c\u4e8e UTF-8 \u7684\u6297\u5e72\u6270\u673a\u5236\u3002 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u591a\u5b57\u8282\u7684\u6574\u6570\u7c7b\u578b\uff08\u5982 uint16_t \u548c uint32_t \uff09\u9700\u8981\u88ab\u62c6\u6210\u591a\u4e2a\u5b57\u8282\u6765\u5b58\u50a8\u3002\u62c6\u5f00\u540e\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u6309\u4ec0\u4e48\u987a\u5e8f\u5b58\u5165\u5185\u5b58\uff1f\u4e0d\u540c\u7684\u786c\u4ef6\u67b6\u6784\u4ea7\u751f\u4e86\u4e89\u6267\uff1a \u5927\u7aef\u6d3e (bit endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u4e5f\u5c31\u662f\u5927\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5c0f\u7aef\u6d3e (little endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u4e5f\u5c31\u662f\u5c0f\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u8ba1\u7b97\u673a\u7535\u8def\u7684\u8ba1\u7b97\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x78 0x56 0x34 0x12 \u4f8b\u5982\uff0cIntel \u7684 x86 \u67b6\u6784\u548c ARM \u516c\u53f8\u7684 ARM \u67b6\u6784\u90fd\u662f\u5c0f\u7aef\u6d3e\uff0c\u800c Motorola \u516c\u53f8\u7684 68k \u67b6\u6784\u548c Sun \u516c\u53f8\u7684 SPARC \u67b6\u6784\u90fd\u662f\u5927\u7aef\u6d3e\u3002 \u8fd9\u5176\u5b9e\u662f\u5f88\u65e0\u804a\u7684\u4e89\u6267\uff0c\u4e3a\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u6539\u53d8\u8ba1\u7b97\u673a\u7684\u8bbe\u8ba1\u6beb\u65e0\u9053\u7406\uff0c\u6bd5\u7adf\u4e16\u754c\u4e0a\u4e5f\u6709\u4ece\u53f3\u5f80\u5de6\u4e66\u5199\u7684\u6587\u5b57\u548c\u4ece\u4e0a\u5f80\u4e0b\u4e66\u5199\u7684\u6587\u5b57\uff0c\u751a\u81f3\u6709\u5de6\u53f3\u6765\u56de\u4e66\u5199\u7684\u6587\u5b57\u2026\u2026\u5982\u679c\u8981\u4f3a\u5019\u4eba\u7c7b\uff0c\u4f60\u600e\u4e48\u4e0d\u6539\u6210\u5341\u8fdb\u5236\u5462\uff1f\u603b\u4e4b\uff0c\u6211\u8ba4\u4e3a\u5c0f\u7aef\u624d\u662f\u6700\u9002\u5408\u8ba1\u7b97\u673a\u7684\uff0c\u5e02\u9762\u4e0a\u5927\u591a\u6570\u4e3b\u6d41\u786c\u4ef6\u90fd\u662f\u5c0f\u7aef\u67b6\u6784\u3002 \u5728\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u53d1\u6d88\u606f\u548c\u6536\u6d88\u606f\u7684\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u67b6\u6784\uff0c\u5982\u679c\u53d1\u6d88\u606f\u7684\u662f\u5c0f\u7aef\u67b6\u6784\uff0c\u6536\u6d88\u606f\u7684\u662f\u5927\u7aef\u67b6\u6784\uff0c\u90a3\u4e48\u53d1\u51fa\u53bb\u7684\u662f 0x12345678\uff0c\u6536\u5230\u7684\u5c31\u4f1a\u53d8\u6210 0x78563421 \u4e86\u3002 \u56e0\u6b64\u4e92\u8054\u7f51\u4e00\u822c\u89c4\u5b9a\uff0c\u6240\u6709\u591a\u5b57\u8282\u7684\u6570\u636e\u5728\u7f51\u7edc\u5305\u4e2d\u7edf\u4e00\u91c7\u7528\u5927\u7aef\u3002\u5bf9\u4e8e\u5927\u7aef\u67b6\u6784\uff0c\u4ed6\u4eec\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u5bf9\u4e8e\u5c0f\u7aef\u67b6\u6784\uff0c\u5728\u53d1\u5305\u524d\u9700\u8981\u628a\u81ea\u5df1\u7684\u5c0f\u7aef\u6570\u636e\u505a\u5b57\u8282\u5e8f\u53cd\u8f6c\uff0c\u53d8\u6210\u5927\u7aef\u7684\u4ee5\u540e\uff0c\u518d\u53d1\u9001\u3002\u4e4b\u540e\u7684\u7f51\u7edc\u4e13\u9898\u8bfe\u4e2d\u6211\u4eec\u4f1a\u8be6\u89e3\u8fd9\u4e00\u5757\u3002 \u57fa\u4e8e\u5b57\u8282\u7801\u7684\u865a\u62df\u673a\u8bed\u8a00\u901a\u5e38\u4f1a\u89c4\u5b9a\u4e00\u4e2a\u5b57\u8282\u5e8f\uff1a\u50cf Java \u8fd9\u79cd\u9762\u5411\u4e92\u8054\u7f51\u8bed\u8a00\uff0c\u7d22\u6027\u4e5f\u89c4\u5b9a\u4e86\u7edf\u4e00\u91c7\u7528\u5927\u7aef\uff0c\u65e0\u8bba JVM \u8fd0\u884c\u5728\u5927\u7aef\u673a\u5668\u8fd8\u662f\u5c0f\u7aef\u673a\u5668\u4e0a\u3002\u8fd9\u4f7f\u5f97\u4ed6\u4e0e\u4e92\u8054\u7f51\u901a\u4fe1\u6bd4\u8f83\u65b9\u4fbf\uff0c\u800c\u5728 x86 \u548c ARM \u67b6\u6784\u4e0a\uff0c\u4e0e\u672c\u5730\u53ea\u63a5\u53d7\u5c0f\u7aef\u6570\u636e\u7684 API\uff0c\u4f8b\u5982 OpenGL\uff0c\u6c9f\u901a\u8f83\u4e3a\u56f0\u96be\uff0c\u9700\u8981\u505a\u989d\u5916\u7684\u5b57\u8282\u5e8f\u8f6c\u6362\u3002\u800c C# \u4e3b\u6253\u6e38\u620f\u4e1a\u52a1\uff08\u4f8b\u5982 Unity\uff09\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\uff0c\u6240\u4ee5\u89c4\u5b9a\u5168\u90e8\u91c7\u7528\u5c0f\u7aef\u3002\u4f5c\u4e3a\u5e95\u5c42\u7f16\u7a0b\u8bed\u8a00\u7684 C++ \u5219\u662f\u5165\u4e61\u968f\u4fd7\uff0c\u4f60\u7684\u786c\u4ef6\u662f\u4ec0\u4e48\u7aef\uff0c\u4ed6\u5c31\u662f\u4ec0\u4e48\u7aef\uff0c\u4e0d\u4e3b\u52a8\u505a\u4efb\u4f55\u989d\u5916\u7684\u8f6c\u6362\u3002 UTF-16 \u548c UTF-32 \u7684\u7801\u4f4d\u90fd\u662f\u591a\u5b57\u8282\u7684\uff0c\u4e5f\u4f1a\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u3002\u4f8b\u5982\uff0cUTF-16 \u4e2d\u7684 uint16_t \u5e8f\u5217\uff1a 0x1234 0x5678 \u5728\u5927\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5728\u5c0f\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x34 0x12 0x78 0x56 \u8fd9\u6837\u4e00\u6765\uff0cUTF-16 \u548c UTF-32 \u7684\u5b57\u8282\u6d41\uff0c\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u987a\u5e8f\u3002\u8fd9\u7ed9\u8de8\u5e73\u53f0\u7684\u6587\u672c\u5904\u7406\u5e26\u6765\u4e86\u9ebb\u70e6\u3002 \u6240\u4ee5\u5f53\u4f60\u9700\u8981\u628a UTF-16 \u5b58\u5165\u786c\u76d8\u548c\u5728\u7f51\u7edc\u53d1\u9001\u65f6\uff0c\u8fd8\u9700\u8981\u989d\u5916\u6307\u660e\u4f60\u7528\u7684\u662f\u5927\u7aef\u7684 UTF-16 \u8fd8\u662f\u5c0f\u7aef\u7684 UTF-16\u3002 \u56e0\u6b64 UTF-16 \u548c UTF-32 \u8fdb\u4e00\u6b65\u5206\u88c2\u4e3a\uff1a UTF-16LE\uff1a\u5c0f\u7aef\u7684 UTF-16 UTF-16BE\uff1a\u5927\u7aef\u7684 UTF-16 UTF-32LE\uff1a\u5c0f\u7aef\u7684 UTF-32 UTF-32BE\uff1a\u5927\u7aef\u7684 UTF-32 \u5982\u679c\u53ea\u5728\u5185\u5b58\u7684 wchar_t \u4e2d\u4f7f\u7528\u5c31\u4e0d\u7528\u533a\u5206\uff0c\u9ed8\u8ba4\u8ddf\u968f\u5f53\u524d\u673a\u5668\u7684\u5927\u5c0f\u7aef\u3002\u6240\u4ee5 UTF-16 \u548c UTF-32 \u901a\u5e38\u53ea\u4f1a\u51fa\u73b0\u5728\u5185\u5b58\u4e2d\u7528\u4e8e\u5feb\u901f\u5904\u7406\u548c\u8ba1\u7b97\uff0c\u5f88\u5c11\u7528\u5728\u5b58\u50a8\u548c\u901a\u4fe1\u4e2d\u3002 UTF-8 \u662f\u57fa\u4e8e\u5355\u5b57\u8282\u7684\u7801\u4f4d\uff0c\u706b\u8f66\u5934\u7684\u987a\u5e8f\u4e5f\u6709\u4e25\u683c\u89c4\u5b9a\uff0c\u706b\u8f66\u5934\u603b\u662f\u5728\u6700\u524d\uff0c\u6839\u672c\u4e0d\u53d7\u5b57\u8282\u5e8f\u5927\u5c0f\u7aef\u5f71\u54cd\uff0c\u4e5f\u5c31\u6ca1\u6709\u5f71\u54cd\u3002 \u7531\u4e8e\u538b\u7f29\u7387\u4f4e\uff0c\u53c8\u5b58\u5728\u5927\u5c0f\u7aef\u5b57\u8282\u5e8f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u800c\u4e92\u8054\u7f51\u6570\u636e\u9700\u8981\u4fdd\u8bc1\u76f8\u540c\u7684\u5927\u5c0f\u7aef\uff0c\u5728\u6536\u53d1\u5305\u65f6\u9700\u8981\u989d\u5916\u8f6c\u6362\uff0c\u56e0\u800c\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u7f51\u7edc\u3002\u800c UTF-8 \u7684\u5b58\u50a8\u5355\u4f4d\u662f\u5b57\u8282\uff0c\u5929\u751f\u6ca1\u6709\u5927\u5c0f\u7aef\u56f0\u6270\u3002\u66f4\u5999\u7684\u662f\uff0c\u4ed6\u4e14\u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u800c\u4e92\u8054\u7f51\u53c8\u662f\u53e4\u8463\u4e2d\u95f4\u4ef6\u6700\u591a\u7684\u5730\u65b9\u2026\u2026 \u603b\u4e4b\uff0c\u5b8c\u5168\u57fa\u4e8e\u5b57\u8282\u7684 UTF-8 \u662f\u6700\u9002\u5408\u7f51\u7edc\u901a\u4fe1\u548c\u786c\u76d8\u5b58\u50a8\u7684\u6587\u672c\u7f16\u7801\u683c\u5f0f\uff0c\u800c UTF-32 \u662f\u6700\u9002\u5408\u5728\u5185\u5b58\u4e2d\u5904\u7406\u7684\u683c\u5f0f\u3002 BOM \u6807\u8bb0 0xFEFF \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u4e0d\u53ef\u89c1\u5b57\u7b26\u201c\ufeff\u201d\uff0c\u8fd9\u662f\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002 \u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u7b26\u52a0\u5728\u6587\u672c\u6587\u4ef6\u7684\u5934\u90e8\uff0c\u544a\u8bc9\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u7528\u4ec0\u4e48\u7f16\u7801\u7684\u3002 \u5982\u679c\u662f UTF-16 \u548c UTF-32\uff0c\u56e0\u4e3a 0xFEFF \u4e0d\u5bf9\u79f0\uff0c\u4ed6\u8fd8\u80fd\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\u3002\u56e0\u6b64 0xFEFF \u88ab\u79f0\u4e3a\u5b57\u8282\u5e8f\u6807\u5fd7\uff08Byte-order-mark\uff0cBOM\uff09\u3002 \u5982\u679c\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u90a3\u4e48\u4ed6\u7167\u5e38\u8bfb\u51fa 0xFEFF\uff0c\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u5728\u6587\u672c\u4e2d\u4e0d\u663e\u793a\uff0c\u4e0d\u5f71\u54cd\u89c6\u89c9\u7ed3\u679c\u3002 \u4e00\u4e9b\u8001\u7684\u7f16\u8bd1\u5668\uff08\u8fdc\u53e4 MinGW\uff0c\u73b0\u5728\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u4f1a\u628a\u5e26\u6709 BOM \u7684 UTF-8 \u7684 .cpp \u6e90\u7801\u6587\u4ef6\uff0c\u5f53\u4f5c\u5934\u90e8\u5e26\u6709\u9519\u8bef\u5b57\u7b26\u7684\u4e71\u7801\u6587\u4ef6\uff0c\u4ece\u800c\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u7684\u8bb0\u4e8b\u672c\u4fdd\u5b58\u4e3a UTF-8 \u65f6\uff0c\u603b\u662f\u4f1a\u52a0\u4e0a BOM\u3002\u5982\u679c\u8bb0\u4e8b\u672c\u53d1\u73b0\u4e00\u4e2a\u6587\u4ef6\u6ca1\u6709 BOM\uff0c\u4f1a\u5f53\u4f5c ANSI\uff08GBK\uff09\u6765\u8bfb\u53d6\u3002 0xFEFF \u5728\u4e0d\u540c\u7684\u7f16\u7801\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1a UTF-8\uff1a 0xEF 0xBB 0xBF \uff0c\u4ed6\u4f1a\u5360\u7528 3 \u5b57\u8282\uff0c\u800c\u4e14\u4e0d\u4f1a\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\uff0c\u56e0\u4e3a UTF-8 \u662f\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u7684\u3002 UTF-16\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE \u3002 UTF-32\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0x00 0x00 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE 0x00 0x00 \u3002 \u56e0\u6b64\uff0c\u5728\u6587\u672c\u5934\u90e8\u52a0\u4e0a BOM \u6709\u52a9\u4e8e\u8f6f\u4ef6\u63a8\u6d4b\u8be5\u6587\u4ef6\u662f\u4ec0\u4e48\u7f16\u7801\u7684\uff08\u5982\u679c\u90a3\u8f6f\u4ef6\u652f\u6301\u89e3\u6790 BOM \u7684\u8bdd\uff09\u3002 \u4f8b\u5982 Windows \u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\u90fd\u88ab\u9ed8\u8ba4\u5047\u5b9a\u4e3a ANSI\uff08GBK\uff09\u7f16\u7801\uff0c\u5982\u679c\u4f60\u8981\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u4e3a UTF-8 \u7f16\u7801\uff0c\u5c31\u9700\u8981\u52a0\u4e0a BOM \u6807\u5fd7\u3002\u5f53 MSVC \u8bfb\u53d6\u65f6\uff0c\u770b\u5230\u5f00\u5934\u662f 0xEF 0xBB 0xBF \uff0c\u5c31\u660e\u767d\u8fd9\u662f\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u6587\u4ef6\u3002\u8fd9\u6837\uff0cMSVC \u5c31\u80fd\u6b63\u786e\u5730\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4e86\u3002\u5982\u679c MSVC \u6ca1\u770b\u5230 BOM\uff0c\u4f1a\u9ed8\u8ba4\u4ee5\u4e3a\u662f ANSI\uff08GBK\uff09\u7f16\u7801\u7684\uff0c\u4ece\u800c\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4f1a\u4e71\u7801\u3002\u5f00\u542f /utf-8 \u9009\u9879\u4e5f\u80fd\u8ba9 MSVC \u628a\u6ca1\u6709 BOM \u7684\u6e90\u7801\u6587\u4ef6\u5f53\u4f5c UTF-8 \u6765\u89e3\u6790\uff0c\u9002\u5408\u8de8\u5e73\u53f0\u5b9d\u5b9d\u4f53\u8d28\u3002 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u7c7b\u578b \u5927\u5c0f \u7f16\u7801 \u5b57\u9762\u91cf Linux char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e $LC_ALL \u201chello\u201d Windows char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e \u201chello\u201d Linux wchar_t 4 \u5b57\u8282 UTF-32 L\u201dhello\u201d Windows wchar_t 2 \u5b57\u8282 UTF-16 L\u201dhello\u201d char8_t 1 \u5b57\u8282 UTF-8 u8\u201dhello\u201d char16_t 2 \u5b57\u8282 UTF-16 u\u201dhello\u201d char32_t 4 \u5b57\u8282 UTF-32 U\u201dhello\u201d \u7531\u6b64\u53ef\u89c1\uff0c char \u548c wchar_t \u662f\u4e0d\u8de8\u5e73\u53f0\u7684\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f GBK\u3002\u5bf9\u4e8e\u7f8e\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\u3002 \u5bf9\u4e8e Linux \u7528\u6237\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e13\u95e8\u4fee\u6539\u8fc7\uff0c $LC_ALL \u9ed8\u8ba4\u662f en_US.UTF-8 \u6216 C.UTF-8 \u3002 \u8fd9\u5e26\u6765\u4e86\u5de8\u5927\u7684\u6df7\u6dc6\uff01\u5f88\u591a\u7f8e\u56fd\u7a0b\u5e8f\u5458\u6f5c\u610f\u8bc6\u91cc\u4f1a\u60f3\u5f53\u7136\u5730\u628a char \u5f53\u4f5c UTF-8 \u6765\u7528\u3002\u5f88\u591a\u5f00\u6e90\u9879\u76ee\uff0c\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u5f88\u591a\u56fd\u4eba\u505a\u7684\u9879\u76ee\uff0c\u90fd\u88ab\u8fd9\u79cd\u201c\u60f3\u5f53\u7136\u201d\u4f20\u67d3\u4e86\u3002 \u597d\u6d88\u606f\u662f\u65e0\u8bba\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u662f\u4ec0\u4e48\uff0c\u80af\u5b9a\u517c\u5bb9 ASCII\u3002\u4f8b\u5982 GBK \u548c UTF-8 \u90fd\u517c\u5bb9 ASCII\uff0c\u5426\u5219\u5c31\u548c\u6240\u6709\u7684 C \u8bed\u8a00\u7ecf\u5178\u51fd\u6570\u5982 strlen \uff0c\u6362\u884c\u7b26 '\\n' \uff0c\u8def\u5f84\u5206\u9694\u7b26 '/' \u548c '\\\\' \u51b2\u7a81\u4e86\u3002 wchar_t \u5c31\u597d\u4e00\u4e9b\uff0c\u867d\u7136\u5728 Windows \u7cfb\u7edf\u4e0a\u662f\u7cdf\u7cd5\u7684 UTF-16\uff0c\u4f46\u81f3\u5c11\u7a33\u5b9a\u4e86\uff0c\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u800c\u968f\u610f\u6539\u53d8\uff0c\u53ea\u8981\u4f60\u4e0d\u6253\u7b97\u8de8\u5e73\u53f0\uff0c wchar_t \u5c31\u662f Windows \u7a0b\u5e8f\u7684\u6807\u914d\u3002 \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u7684\u706b\u8f66\u5934\u548c\u8f66\u53a2\uff0c\u90fd\u662f 1 \u5f00\u5934\u7684\uff0c\u800c ASCII \u7684\u5355\u4f53\u706b\u8f66\u5934\u6c38\u8fdc\u662f 0 \u5f00\u5934\u3002\u8fd9\u5f88\u91cd\u8981\uff0c\u4e0d\u4ec5\u706b\u8f66\u5934\u9700\u8981\u548c ASCII \u533a\u5206\u5f00\u6765\uff0c\u8f66\u53a2\u4e5f\u9700\u8981\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff1a std::u32string path = \"\u4e00\u4e2a\u8001\u4f2f.txt\"; \u201c\u4e00\u4e2a\u8001\u4f2f\u201d \u8f6c\u6362\u4e3a Unicode \u7801\u70b9\u5206\u522b\u662f\uff1a 0x4E00 0x4E2A 0x8001 0x4F2F \u5982\u679c\u8ba9\u4ed6\u4eec\u539f\u5c01\u4e0d\u52a8\u76f4\u63a5\u5b58\u50a8\u8fdb char \u6570\u7ec4\u91cc\uff1a 0x4E 0x00 0x4E 0x2A 0x80 0x01 0x4F 0x2F \u5c31\u51fa\u95ee\u9898\u4e86\uff01\u9996\u5148\uff0c\u8fd9\u91cc 0x4E00 \u7684 0x00 \u90e8\u5206\uff0c\u4f1a\u88ab C \u8bed\u8a00\u5f53\u4f5c\u662f\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u62ff\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u53bb\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684 open \u51fd\u6570\uff0c\u4ed6\u4f1a\u4ee5\u4e3a\u4f60\u5728\u6253\u5f00 0x4E \u5355\u4e2a\u5b57\u7b26\u7684\u6587\u4ef6\u540d\uff0c\u4e5f\u5c31\u662f \"N\" \u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c0x2F \u5bf9\u5e94\u7684 ASCII \u5b57\u7b26\u662f '/' \uff0c\u662f\u8def\u5f84\u5206\u9694\u7b26\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4ee5\u4e3a\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u5b50\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6 \"N\\x00N*\\x80\\x01O/.txt\" \uff0c\u6587\u4ef6\u5939\u540d\u5b57\u53eb \"N\\x00N*\\x80\\x01O\" \u800c\u6587\u4ef6\u53eb \".txt\" \u3002 \u4e3a\u4e86\u80fd\u8ba9\u9488\u5bf9 ASCII \u8bbe\u8ba1\u7684\u64cd\u4f5c\u7cfb\u7edf API \u652f\u6301\u4e2d\u6587\u6587\u4ef6\u540d\uff0c\u5c31\u53ea\u80fd\u7ed5\u5f00\u6240\u6709 0x7F \u4ee5\u4e0b\u7684\u503c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 UTF-8 \u5bf9\u8f66\u53a2\u4e5f\u5168\u90e8\u62ac\u9ad8\u5230 0x80 \u4ee5\u4e0a\uff0c\u907f\u514d\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u614e\u628a\u8f66\u53a2\u5f53\u4f5c\u662f '/' \u6216 '\\0' \u3002 UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u7531\u4e8e\u5de8\u5927\u7684\u60ef\u6027\uff0c\u5f88\u591a\u4eba\u90fd\u60f3\u5f53\u7136\u7684\u628a std::string \u5f53\u4f5c UTF-8 \u6765\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6253\u5370\uff0c\u5e38\u89c4\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u662f\u6ca1\u95ee\u9898\u7684\u3002 \u5b57\u7b26\u4e32\u64cd\u4f5c\u6709\u4e0b\u9762\u8fd9\u51e0\u79cd\uff0c\u5f97\u76ca\u4e8e UTF-8 \u4f18\u79c0\u7684\u5e8f\u5217\u5316\u6d89\u53ca\u548c\u5197\u4f59\u6297\u5e72\u6270\u673a\u5236\uff0c\u7edd\u5927\u591a\u6570 ASCII \u652f\u6301\u7684\u64cd\u4f5c\uff0cUTF-8 \u5b57\u7b26\u4e32\u90fd\u80fd\u8f7b\u677e\u80dc\u4efb\uff0c\u552f\u72ec\u5176\u4e2d \u6d89\u53ca\u201c\u7d22\u5f15\u201d\u548c\u201c\u957f\u5ea6\u201d\u7684 \u4e00\u90e8\u5206\u64cd\u4f5c\u4e0d\u884c\u3002\u8fd9\u662f\u7531\u4e8e\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff0c\u5982\u679c\u9700\u8981\u505a\u201c\u7d22\u5f15\u201d\u7c7b\u64cd\u4f5c\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u8f6c\u6362\u6210\u5b9a\u957f\u7684 UTF-32 \u7f16\u7801\u3002 \u64cd\u4f5c UTF-8 UTF-32 GBK \u6c42\u5b57\u7b26\u4e32\u957f\u5ea6 \u00d7 \u221a \u00d7 \u5224\u65ad\u76f8\u7b49 \u221a \u221a \u221a \u5b57\u5178\u5e8f\u7684\u5927\u5c0f\u6bd4\u8f83 \u221a \u221a \u00d7 \u5b57\u7b26\u4e32\u62fc\u63a5 \u221a \u221a \u221a \u641c\u7d22\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u641c\u7d22\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u4e0b\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u904d\u5386\u6240\u6709\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u5b50\u5b57\u7b26\u4e32\u5207\u7247 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u7247 \u00d7 \u221a \u00d7 \u67e5\u627e\u5e76\u66ff\u6362\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u67e5\u627e\u5e76\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u5220\u9664\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u4e3a\u4ec0\u4e48\uff1f\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5b9e\u9a8c\uff1a std::string s = \"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \uff08\u4f7f\u7528 /utf-8 \u7f16\u8bd1\uff09\u8fd0\u884c\u540e\uff0c\u4f1a\u5f97\u5230 6\u3002 \u56e0\u4e3a std::string \u7684 size() \u8fd4\u56de\u7684\u662f char \u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5728 UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u975e ASCII \u7684\u5b57\u7b26\u4f1a\u88ab\u7f16\u7801\u4e3a\u591a\u4e2a char \uff0c\u5bf9\u4e8e\u4e2d\u6587\u800c\u8a00\uff0c\u4e2d\u6587\u90fd\u5728 0x2E80 \u5230 0x9FFF \u8303\u56f4\u5185\uff0c\u5c5e\u4e8e\u4e09\u7ea7\u5217\u8f66\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e2a\u6c49\u5b57\u4f1a\u88ab\u7f16\u7801\u6210 3 \u4e2a char \u3002 char \u662f\u5b57\u8282\uff08\u7801\u4f4d\uff09\u800c\u4e0d\u662f\u771f\u6b63\u7684\u5b57\u7b26\uff08\u7801\u70b9\uff09\u3002\u771f\u6b63\u7684 Unicode \u5b57\u7b26\u5e94\u8be5\u662f char32_t \u7c7b\u578b\u7684\u3002\u8c03\u7528 std::string \u7684 size() \u6216\u8005 strlen \u5f97\u5230\u7684\u53ea\u662f\u201c\u5b57\u8282\u6570\u91cf\u201d\u3002 \u800c UTF-32 \u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\uff08\u7801\u70b9\uff09\u90fd\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684 char32_t \uff08\u7801\u4f4d\uff09\uff0c size() \u5c31\u662f\u771f\u6b63\u7684\u201c\u5b57\u7b26\u6570\u91cf\u201d\uff0c\u8fd9\u5c31\u662f\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 std::u32string s = U\"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \u5982\u679c\u4f60\u7684\u64cd\u4f5c\u53ea\u6d89\u53ca\u5b57\u7b26\u4e32\u67e5\u62fc\u63a5\u4e0e\u67e5\u627e\uff0c\u90a3\u5c31\u53ef\u4ee5\u7528 UTF-8\u3002\u5982\u679c\u5927\u91cf\u6d89\u53ca\u7d22\u5f15\uff0c\u5207\u7247\uff0c\u5355\u4e2a\u5b57\u7b26\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u5fc5\u987b\u7528 UTF-32\uff08\u5426\u5219\u4e00\u9047\u5230\u6c49\u5b57\u5c31\u4f1a\u51fa\u9519\uff09\u3002 std::vector slogan = { \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\", \"\u5168\u4e16\u754c\u7a0b\u5e8f\u5458\u5927\u56e2\u7ed3\u4e07\u5c81\", }; std::string joined; for (auto const &s: slogan) { joined += s; // \u53ea\u662f\u62fc\u63a5\u800c\u5df2\uff0cUTF-8 \u6ca1\u95ee\u9898 } UTF-8 \u6309\u7d22\u5f15\u5207\u7247\u7684\u51fa\u9519\u6848\u4f8b\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-8 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u8282\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\ufffd\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-32 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u7b26\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u53ea\u6709\u5f53\u7d22\u5f15\u6765\u81ea find \u7684\u7ed3\u679c\u65f6\uff0cUTF-8 \u5b57\u7b26\u4e32\u7684\u5207\u7247\u624d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(\"\u516c\"); // pos = 12 fmt::println(\"UTF-8 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u8282\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(U'\u516c'); // pos = 4 fmt::println(\"UTF-32 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u7b26\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u6ce8\u610f\u5230\u8fd9\u91cc UTF-8 \u7684 \"\u516c\" \u9700\u8981\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\u3002 UTF-8 \u65e0\u6cd5\u53d6\u51fa\u5355\u4e2a\u975e ASCII \u5b57\u7b26\uff0c\u5bf9\u4e8e\u5355\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ecd\u7136\u53ea\u80fd\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u8fbe\uff08\u7531\u591a\u4e2a\u5b57\u8282\u7ec4\u6210\uff09\u3002 std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-8 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u8282\uff1a{}\", s[0]); // \u53ef\u80fd\u4f1a\u6253\u5370 \u2018\u00e5\u2019 (0xE5)\uff0c\u56e0\u4e3a\u201c\u5c0f\u201d\u7684 UTF-8 \u7f16\u7801\u662f 0xE5 0xB0 0x8F // \u4e5f\u53ef\u80fd\u662f\u4e71\u7801\u201c\ufffd\u201d\uff0c\u53d6\u51b3\u4e8e\u7ec8\u7aef\u7406\u89e3\u7684\u7f16\u7801\u683c\u5f0f std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-32 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1a{}\", s[0]); // \u4f1a\u6253\u5370 \u2018\u5c0f\u2019 UTF-8 \u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u4e5f\u4f1a\u51fa\u95ee\u9898\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u4e71\u7801 std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u628a\u6309\u5b57\u7b26\u6b63\u5e38\u53cd\u8f6c\uff0c\u5f97\u5230 \u201c\u5c81\u4e07\u8bfe\u5f00\u516c\u5e08\u8001\u5f6d\u5c0f\u201d \u603b\u7ed3\uff1aUTF-8 \u53ea\u80fd\u62fc\u63a5\u3001\u67e5\u627e\u3001\u6253\u5370\u3002\u4e0d\u80fd\u7d22\u5f15\u3001\u5207\u7247\u3001\u53cd\u8f6c\u3002 \u6309\u7d22\u5f15\u5207\u7247\u4e0d\u884c\uff0c\u4f46\u5982\u679c\u7d22\u5f15\u662f find \u51fa\u6765\u7684\u5c31\u6ca1\u95ee\u9898\u3002 \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5728 Windows \u5b98\u65b9\u7684\u8bf4\u8f9e\u4e2d\uff0c\u6709\u201cUnicode \u7f16\u7801\u201d\u548c\u201cANSI \u7f16\u7801\u201d\u7684\u8bf4\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528 Windows \u81ea\u5e26\u7684\u8bb0\u4e8b\u672c\u7a0b\u5e8f\uff0c\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u9009\u5355\uff1a \u7ffb\u8bd1\u4e00\u4e0b\uff1a \u201cANSI\u201d\u6307\u7684\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u8bbe\u7f6e\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\u3002 \u6240\u8c13\u201cUnicode\u201d\u5176\u5b9e\u6307\u7684\u662f UTF-16\u3002 \u6240\u8c13\u201cUnicode big endian\u201d\u6307\u7684\u662f\u5927\u7aef UTF-16\u3002 \u201cUTF-8\u201d\u6307\u7684\u662f UTF-8 with BOM \u800c\u4e0d\u662f\u6b63\u5e38\u7684 UTF-8\u3002 \u5b9e\u9645\u4e0a Unicode \u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u53ea\u662f\u628a\u5b57\u7b26\u6620\u5c04\u5230\u6574\u6570\uff0c\u66f4\u6ca1\u6709\u4ec0\u4e48\u5927\u7aef\u5c0f\u7aef\uff0cUTF-16 \u624d\u662f\u7f16\u7801\u683c\u5f0f\u3002 \u800c ANSI \u672c\u6765\u5e94\u8be5\u662f ASCII \u7684\u610f\u601d\uff0c char \u672c\u6765\u5c31\u53ea\u652f\u6301 ASCII\u3002 \u4f46\u7531\u4e8e\u5f53\u65f6\u5404\u56fd\u8feb\u5207\u9700\u8981\u652f\u6301\u81ea\u5df1\u672c\u56fd\u7684\u6587\u5b57\uff0c\u5c31\u5728\u517c\u5bb9 ASCII \u7684\u57fa\u7840\u4e0a\uff0c\u53d1\u5c55\u51fa\u4e86\u81ea\u5df1\u7684\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\u3002\u8fd9\u4e9b\u5f53\u5730\u7279\u4f9b\u7684\u5b57\u7b26\u96c6\u91cc\u53ea\u5305\u542b\u4e86\u672c\u56fd\u6587\u5b57\uff0c\u6240\u6709\u8fd9\u4e9b\u5404\u56fd\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u90fd\u548c UTF-8 \u7c7b\u4f3c\uff0c\u91c7\u7528\u706b\u8f66\u5934\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0c\u5bf9 0 \u5f00\u5934\u7684 ASCII \u90e8\u5206\u4e5f\u90fd\u662f\u517c\u5bb9\u3002\u6240\u4ee5 Windows \u7d22\u6027\u628a ANSI \u5f53\u4f5c\u201c\u5404\u56fd\u672c\u5730\u6587\u5b57\u7f16\u7801\u201d\u7684\u7b80\u79f0\u4e86\u3002\u4f46\u540e\u6765\u4e92\u8054\u7f51\u7684\u51fa\u73b0\uff0c\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4fe1\u606f\u4ea4\u6362\u56f0\u96be\u3002 \u4f8b\u5982\u4f60\u5728\u73a9\u4e00\u4e9b\u65e5\u672c\u7684 galgame \u65f6\uff0c\u4f1a\u53d1\u73b0\u91cc\u9762\u6587\u5b57\u5168\u90e8\u4e71\u7801\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u5728\u5404\u4e2a\u5730\u533a\u53d1\u884c\u7684\u662f\u201c\u7279\u4f9b\u7248\u201d\uff1a\u5728\u4e2d\u56fd\u5927\u9646\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 GBK \u5b57\u7b26\u96c6\uff0c\u5728\u65e5\u672c\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 Shift-JIS \u5b57\u7b26\u96c6\u3002\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u5185\u90e8\u5b58\u50a8\u7684\u662f Shift-JIS \u7684\u90a3\u4e9b\u201c\u6574\u6570\u201d\u3002\u8fd9\u5bfc\u81f4\u65e5\u672c\u7684 galgame \u5728\u4e2d\u56fd\u5927\u9646\u7279\u4f9b\u7684 Windows \u4e2d\uff0c\u628a Shift-JIS \u7684\u201c\u6574\u6570\u201d\u7528 GBK \u7684\u8868\u6765\u89e3\u8bfb\u4e86\uff0c\u4ece\u800c\u4e71\u7801\uff08GBK \u91cc\u7684\u65e5\u6587\u533a\u57df\u5e76\u6ca1\u6709\u548c Shift-JIS \u91cd\u53e0\uff09\u3002\u9700\u8981\u7528 Locale Emulator \u628a Shift-JIS \u7ffb\u8bd1\u6210 Unicode \u8bfb\u7ed9 Windows \u542c\u3002\u5982\u679c\u65e5\u672c\u7a0b\u5e8f\u5458\u4ece\u4e00\u5f00\u59cb\u5c31\u7edf\u4e00\u7528 Unicode \u6765\u5b58\u50a8\uff0c\u4e2d\u56fd\u533a\u73a9\u5bb6\u7684 Windows \u4e5f\u7edf\u4e00\u7528 Unicode \u89e3\u6790\uff0c\u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cUnicode \u7ec4\u7ec7\u51fa\u73b0\u4e86\uff0c\u4ed6\u7684\u4f7f\u547d\u5c31\u662f\u7edf\u4e00\u5168\u4e16\u754c\u7684\u5b57\u7b26\u96c6\uff0c\u4fdd\u8bc1\u5168\u4e16\u754c\u6240\u6709\u7684\u6587\u5b57\u90fd\u80fd\u5728\u5168\u4e16\u754c\u6240\u6709\u7684\u8ba1\u7b97\u673a\u4e0a\u663e\u793a\u51fa\u6765\u3002\u9996\u5148\u521b\u529e\u4e86 Unicode \u5b57\u7b26\u96c6\uff0c\u7136\u540e\u89c4\u5b9a\u4e86 UTF-8\u3001UTF-16\u3001UTF-32 \u4e09\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u6700\u7ec8 UTF-8 \u6210\u4e3a\u5916\u7801\u7684\u4e3b\u6d41\uff0cUTF-32 \u6210\u4e3a\u5185\u7801\u7684\u4e3b\u6d41\u3002 \u63a5\u4e0b\u6765\u4e3a\u4e86\u65b9\u4fbf\u8bb0\u5fc6\uff0c\u6211\u4eec\u7d22\u6027\u5c31\u987a\u7740\u5fae\u8f6f\u7684\u8fd9\u4e2a\u8bf4\u6cd5\uff1a \u7ba1 char \u53eb ANSI\uff1a\u968f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002 \u7ba1 wchar_t \u53eb Unicode\uff1a\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u5728 Linux \u4e0a\u662f UTF-32\u3002 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5fae\u8f6f\u7ba1 UTF-16 \u53eb Unicode \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u95ee\u9898\uff1a \u56e0\u4e3a\u5f53\u5e74 Unicode 5.0 \u7684\u65f6\u5019\u53ea\u6709 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff0c16 \u4f4d\u5c31\u88c5\u5f97\u4e0b\uff0c\u6240\u4ee5\u5f53\u65f6 UTF-16 \u8fd8\u662f\u4e00\u4e2a \u5b9a\u957f\u7f16\u7801 \u3002\u5fae\u8f6f\u4e8e\u662f\u51b3\u5b9a\u628a wchar_t \u5b9a\u4e49\u6210 2 \u5b57\u8282\uff0c\u5e76\u5728 NT \u5185\u6838\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528\u90fd\u5347\u7ea7\u6210\u4e86\u57fa\u4e8e wchar_t \u5b57\u7b26\u4e32\u7684 \u201cW \u7cfb\u201d API\u3002 \u6bd4\u5c14\u76d6\u5b50\u5f53\u65f6\u4ee5\u4e3a\u8fd9\u6837 UTF-16 \u5b9a\u957f\u5185\u7801\u5c31\u4e00\u52b3\u6c38\u9038\u4e86\uff0c\u5e76\u53f7\u53ec\u6240\u6709\u7a0b\u5e8f\u90fd\u6539\u7528 UTF-16 \u505a\u5185\u7801\uff0c\u522b\u7528 \u201cA \u7cfb\u201d API \u4e86\u3002 \u8d77\u521d\uff0c\u6240\u6709\u4eba\u90fd\u4ee5\u4e3a UTF-16 \u5c31\u662f\u6700\u7ec8\u7b54\u6848\u3002 \u6ca1\u60f3\u5230\u540e\u6765 Unicode \u59d4\u5458\u4f1a\u201c\u80cc\u523a\u201d\u4e86\u6bd4\u5c14\u76d6\u5b50\uff01\u5077\u5077\u628a\u8303\u56f4\u66f4\u65b0\u5230\u4e86 0x10FFFF\uff0c\u7a81\u7834\u4e86 16 \u4f4d\u6574\u6570\u7684\u5bb9\u91cf\u3002\u539f\u6765\u7684 UTF-16 \u5df2\u7ecf\u5bb9\u7eb3\u4e0d\u4e0b\uff0c\u53ea\u597d\u5229\u7528\u4e4b\u524d\u9884\u7559\u7684 0xD800 \u5230 0xDFFF \u7a7a\u53f7\u533a\u95f4\u4e11\u964b\u5730\u5b9e\u73b0\u4e86\u53d8\u957f\u7f16\u7801\u3002 \u76f4\u5230 UTF-16 \u4e00\u591c\u4e4b\u95f4\u6210\u4e86\u4e11\u964b\u7684 \u53d8\u957f\u7f16\u7801 \u3002 \u95f9\u4e86\u534a\u5929\uff0cWindows \u8d39\u5fc3\u8d39\u529b\u66ff Unicode \u59d4\u5458\u4f1a\u597d\u4e0d\u5bb9\u6613\u63a8\u5e7f\u7684 wchar_t \uff0c\u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801 \u7684\u597d\u5904\u3002\u53ef \u201cW \u7cfb\u201d API \u5374\u53c8\u710a\u6b7b\u5728\u4e86 NT \u5185\u6838\u6700\u5e95\u5c42\uff0c\u53cd\u590d\u6765\u5751\u7b2c\u4e00\u6b21\u7528 Windows \u7f16\u7a0b\u7684\u521d\u5b66\u8005\u3002 \u6bd4\u5c14\u76d6\u5b50\uff1a\u4f60\u8fd9\u6837\u663e\u5f97\u6211\u5f88\u5c0f\u4e11\u8bf6\uff1f \u9664 Windows \u5916\uff0cJava \u4e5f\u662f\u201cUTF-16 \u80cc\u523a\u201d\u7684\u53d7\u5bb3\u8005\uff0c\u4ed6\u4eec\u60f3\u5f53\u7136\u7684\u628a char \u5b9a\u4e49\u4e3a UTF-16\uff0c\u4ee5\u4e3a\u8fd9\u5c31\u662f\u672a\u6765\u6c38\u4e45\u7684\u5b9a\u957f\u5185\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u2026\u2026 \u76f4\u5230 Unicode \u52a0\u5165\u4e86 0x10FFFF\uff0cJava \u4e0d\u5f97\u4e0d\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e2a Character \u4f5c\u4e3a UTF-32 \u5b57\u7b26\uff0c\u8fd8\u5f04\u4e2a char \u5230 Character \u7684\u8f6c\u6362\uff0c\u597d\u4e0d\u5c34\u5c2c\uff01 Linux \u6210\u7acb\u4e8e 1991 \u5e74\uff0c\u5f53\u65f6 Unicode \u4e5f\u624d\u521a\u521a\u51fa\u73b0\u3002Unicode \u5ba3\u5e03\u52a0\u5165 0x10FFFF \u540e\uff0cLinux \u624d\u5f00\u59cb\u5f15\u5165\u652f\u6301 Unicode\u3002\u5728\u77e5\u9053\u4e86 Unicode \u5305\u542b 0x10FFFF \u540e\uff0c\u4ed6\u4eec\u4e00\u5f00\u59cb\u5c31\u628a wchar_t \u5b9a\u4e49\u6210 4 \u5b57\u8282\uff0c\u9003\u8fc7\u4e86 UTF-16 \u7684\u80cc\u523a\u3002 \u540e\u6765\u65b0\u51fa\u7684\u8bed\u8a00\uff0c\u5982 Python 3\u3001Go\u3001Rust\u3001Swift\u3001Kotlin\uff0c\u628a\u5b57\u7b26\u94a6\u5b9a\u4e3a UTF-32 \u4e86\u3002\u4ed6\u4eec\u53ea\u6709\u5728\u8c03\u7528 Windows API \u65f6\uff0c\u624d\u4f1a\u4e34\u65f6\u8f6c\u6362\u4e3a UTF-16 \u6765\u8c03\u7528\uff0c\u9664\u6b64\u4e4b\u5916\u518d\u65e0 UTF-16 \u51fa\u73b0\u3002 \u8bb8\u591a\u7cdf\u7cd5\u7684\u535a\u5ba2\u58f0\u79f0\uff1a\u662f\u56e0\u4e3a\u201cUTF-16 \u6700\u6709\u5229\u4e8e\u4e2d\u6587\u538b\u7f29\u201d\uff0c\u6240\u4ee5 Java \u548c Windows \u624d\u91c7\u7528\u7684\uff1f\u7136\u800c\u5c31\u6211\u4e86\u89e3\u5230\u7684\u5b9e\u9645\u60c5\u51b5\u662f\u56e0\u4e3a\u4ed6\u4eec\u9519\u8bef\u7684\u4ee5\u4e3a 0xFFFF \u662f Unicode \u7684\u4e0a\u9650\u624d\u9519\u8bef\u91c7\u7528\u4e86\uff0c\u4e0d\u7136\u4e3a\u4ec0\u4e48\u540e\u6765\u7684\u65b0\u8bed\u8a00\u90fd\u91c7\u7528\u4e86 UTF-32 \u5185\u7801 + UTF-8 \u5916\u7801\u7684\u7ec4\u5408\uff1f\u800c\u4e14\u5728\u5916\u7801\u4e2d\u91c7\u7528 UTF-8 \u6216 UTF-16 \u538b\u7f29\u786e\u5b9e\u6ca1\u95ee\u9898\uff0c\u4f46\u662f Java \u548c Windows \u7684\u5931\u8bef\u5728\u4e8e\u628a UTF-16 \u5f53\u4f5c\u5185\u7801\u4e86\uff01\u5185\u7801\u5c31\u7406\u5e94\u662f\u5b9a\u957f\u7f16\u7801\u7684\u624d\u65b9\u4fbf\uff0c\u5982\u679c\u4f60\u6709\u4e0d\u540c\u60f3\u6cd5\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002 \u603b\u4e4b\uff0cUTF-16 \u662f\u7cdf\u7c95\uff0c\u4f46\u4ed6\u662f Windows \u552f\u4e00\u5b8c\u6574\u652f\u6301\u7684 Unicode \u63a5\u53e3\u3002\u4e0d\u5efa\u8bae\u8f6f\u4ef6\u5185\u90e8\u7528 UTF-16 \u5b58\u50a8\u6587\u5b57\uff0c\u4f60\u53ef\u4ee5\u7528\u66f4\u7d27\u51d1\u7684 UTF-8 \u6216\u66f4\u65b9\u4fbf\u5207\u7247\u7684 UTF-32\uff0c\u53ea\u9700\u5728\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u524d\u4e34\u65f6\u8f6c\u6362\u6210 UTF-16 \u5c31\u884c\u3002 \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u5fc5\u987b\u6307\u51fa\uff1a\u5728 std::string \u4e2d\u88c5 UTF-8 \u5e76\u4e0d\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728 std::u8string \u91cc\u540c\u6837\u53ef\u4ee5\u88c5 GBK\u3002\u8fd9\u5c31\u597d\u6bd4\u4e00\u4e2a\u540d\u53eb Age \u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b9e\u9645\u5374\u88c5\u7740\u6027\u522b\u4e00\u6837\u3002 enum Age { // \u9519\u8bef\u793a\u8303 Male, Female, Custom, }; // \u9664\u4e86\u8ff7\u60d1\u540c\u4e8b\u5916\uff0c\u628a\u5e74\u9f84\u548c\u6027\u522b\u7684\u7c7b\u578b\u6df7\u7528\u6ca1\u6709\u597d\u5904 void registerStudent(Age age, Age sex); \u533a\u5206\u7c7b\u578b\u53ea\u662f\u5927\u591a\u6570\u4eba\u8bbe\u8ba1\u63a5\u53e3\u7684\u89c4\u8303\uff0c\u53ea\u662f\u65b9\u4fbf\u4f60\u901a\u8fc7\u770b\u51fd\u6570\u63a5\u53e3\u4e00\u773c\u533a\u5206\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u7684\u662f\u4ec0\u4e48\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u6ca1\u6709\u5f3a\u5236\u6027\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e00\u770b\u5c31\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u9700\u8981\u7684\u662f\u4ec0\u4e48\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 void thisFuncAcceptsANSI(std::string msg); void thisFuncAcceptsUTF8(std::u8string msg); void thisFuncAcceptsUTF16(std::u16string msg); void thisFuncAcceptsUnicode(std::wstring msg); void thisFuncAcceptsUTF32(std::u32string msg); \u6ca1\u6709 char8_t \u4e4b\u524d\uff0c\u7528\u7c7b\u578b\u522b\u540d\u540c\u6837\u53ef\u4ee5\u8d77\u5230\u5dee\u4e0d\u591a\u7684\u8bf4\u660e\u6548\u679c\uff08\u7f3a\u70b9\u662f\u65e0\u6cd5\u91cd\u8f7d\uff09\uff1a using ANSIString = std::string; using UTF8String = std::string; using UTF16String = std::vector; void thisFuncAcceptsANSI(ANSIString msg); void thisFuncAcceptsUTF8(UTF8String msg); void thisFuncAcceptsUTF16(UTF16String msg); \u4e4b\u6240\u4ee5\u6211\u4f1a\u8bf4\uff0c std::string \u5e94\u8be5\u88c5 ANSI \u5b57\u7b26\u4e32\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u6807\u51c6\u5e93\u5b98\u65b9\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5047\u5b9a std::string \u7c7b\u578b\u662f ANSI \u7f16\u7801\u683c\u5f0f\uff08GBK\uff09\u3002\u5e76\u4e0d\u662f\u8bf4\uff0c\u4f60\u4e0d\u80fd\u7528 std::string \u5b58\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u7684\u5185\u5bb9\u3002 \u5982\u679c\u4f60\u5c31\u662f\u60f3\u7528 std::string \u88c5 UTF-8 \u4e5f\u53ef\u4ee5\uff0c\u53ea\u4e0d\u8fc7\u4f60\u8981\u6ce8\u610f\u5728\u4f20\u5165\u6240\u6709\u4f7f\u7528\u4e86\u6587\u4ef6\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5982 fopen \uff0c std::ifstream \u7684\u6784\u9020\u51fd\u6570\u524d\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u8f6c\u6362\uff0c\u8f6c\u6210 GBK \u7684 std::string \u6216 UTF-16 \u7684 std::wstring \u540e\uff0c\u624d\u80fd\u4f7f\u7528\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u5982\u679c\u4f60\u59cb\u7ec8\u7528 std::u8string \u88c5 UTF-8\uff0c\u90a3\u4e48\u5f53\u4f60\u628a\u5b83\u8f93\u5165\u4e00\u4e2a\u63a5\u53d7 ANSI \u7684\u666e\u901a std::string \u53c2\u6570\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef\uff0c\u5f3a\u8feb\u4f60\u91cd\u65b0\u6e05\u9192\uff0c\u6216\u662f\u5f3a\u8feb\u4f60\u4f7f\u7528\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\uff0c\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u8fd9\u4e2a\u8f6c\u6362\u51fd\u6570\u7684\u5199\u6cd5\u3002 \u4f8b\u5982\u5f53\u4f60\u4f7f\u7528 std::cout << u8string \u65f6\u4f1a\u62a5\u9519\uff0c\u8feb\u4f7f\u4f60\u6539\u4e3a std::cout << u8toansi(u8string) \u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff0c\u4ece\u800c\u907f\u514d\u4e86\u628a UTF-8 \u7684\u5b57\u7b26\u4e32\u6253\u5370\u5230\u4e86\u53ea\u652f\u6301 GBK \u7684\u63a7\u5236\u53f0\u4e0a\u3002 \u5176\u4e2d\u8f6c\u6362\u51fd\u6570\u7b7e\u540d\u4e3a std::string u8toansi(std::u8string s) \uff0c\u5f88\u53ef\u60dc\uff0c\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u51fd\u6570\uff0c\u76f4\u5230 C++26 \u524d\uff0c\u6807\u51c6\u5e93\u5bf9\u5b57\u7b26\u7f16\u7801\u652f\u6301\u4e00\u76f4\u5f88\u5dee\uff0c\u4f60\u4e0d\u5f97\u4e0d\u81ea\u5df1\u5b9e\u73b0\u6216\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u3002 \u603b\u4e4b\uff0c char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u5411\u8c03\u7528\u8005\u6697\u793a\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5982\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a thisFuncAcceptUTF8(std::u8string msg); \u5982\u679c\u8c03\u7528\u8005\u559c\u6b22\u7528 std::string \u88c5 UTF-8 \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\uff1a std::string msg; // \u8c03\u7528\u8005\u786e\u4fe1\uff0c\u8fd9\u4e2a msg \u867d\u7136\u662f `std::string`\uff0c\u4f46\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f UTF-8 // \u90a3\u4e48\u4ed6\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e3a u8string\uff0c\u6765\u8bc1\u660e\u81ea\u5df1\u5934\u8111\u6e05\u9192 thisFuncAcceptUTF8(std::u8string((char8_t *)msg.data(), msg.size())); \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 C++ \u5b98\u65b9\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e24\u79cd\u5b57\u7b26\u96c6\u3002\u4e00\u79cd\u662f \u6e90\u7801\u5b57\u7b26\u96c6 (source charset) \uff0c\u4e00\u79cd\u662f \u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u3002 \u8fd9\u771f\u662f\u7cdf\u7cd5\u7684\u672f\u8bed\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u8fd9\u4e2a\u540d\u5b57\u5177\u6709\u8bef\u5bfc\u6027\uff0c\u4ed6\u548c\u8fd0\u884c\u65f6\u6839\u672c\u6ca1\u6709\u5173\u7cfb\uff0c\u660e\u660e\u662f\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\u3002\u6240\u4ee5\u5c0f\u5f6d\u8001\u5e08\u66ff\u4ed6\u6539\u4e2a\u540d\u5b57\uff0c\u5b9e\u9645\u5e94\u8be5\u53eb\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u96c6\u201d\u3002 \u800c\u4e14\u4ed6\u4eec\u53eb\u5b57\u7b26\u96c6\u4e5f\u4e0d\u5408\u7406\uff0c\u5e94\u8be5\u53eb\u5b57\u7b26\u7f16\u7801\u624d\u5bf9\uff0cUTF-8 \u548c UTF-16 \u90fd\u662f Unicode \u5b57\u7b26\u96c6\u7684\u4e24\u79cd\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u4ed6\u4eec\u660e\u663e\u662f\u4e0d\u540c\u7684\u3002 \u7136\u540e\uff0c\u518d\u5f15\u5165\u4e00\u4e2a\u771f\u6b63\u7684\uff0c\u8fd0\u884c\u65f6\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u8f6f\u4ef6\u5ba2\u6237\u7535\u8111\u7684\u533a\u57df\u8bbe\u7f6e\u3002 \u6700\u7ec8\uff0c\u7ecf\u8fc7\u5c0f\u5f6d\u8001\u5e08\u6539\u826f\u7684\u672f\u8bed\u5982\u4e0b\uff1a \u6e90\u7801\u5b57\u7b26\u7f16\u7801: .cpp \u6e90\u7801\u6587\u4ef6\u65f6\u7528\u7684\u5b57\u7b26\u7f16\u7801\u3002\u4f8b\u5982\u7a0b\u5e8f\u5458\u7528\u8bb0\u4e8b\u672c\u4fdd\u5b58 .cpp \u6e90\u7801\u6587\u4ef6\u65f6\uff0c\u9009\u62e9 \u201cUTF-8\u201d \u4fdd\u5b58\u5c31\u662f UTF-8\uff0c\u9009\u62e9 \u201cANSI\u201d \u4fdd\u5b58\u5c31\u662f GBK\u3002 \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f char \u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u5b57\u7b26\u7f16\u7801\u3002\u9ed8\u8ba4\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff08\u5f00\u53d1\u8005\uff09\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f\u6211\u4eec\u7684\u7a0b\u5e8f\u5728\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fd0\u884c\u65f6\uff0c\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf API \u7684 const char * \u671f\u671b\u63a5\u53d7\u600e\u6837\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002\u9ed8\u8ba4\u662f\u5ba2\u6237\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd9\u4e09\u4e2a\u53ef\u4ee5\u5404\u6709\u4e0d\u540c\u3002 \u5176\u4e2d \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801 \u548c \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801 \u7684\u4e0d\u5339\u914d\uff0c\u662f Windows \u8f6f\u4ef6\u51fa\u73b0\u4e71\u7801\u7684\u4e3b\u8981\u539f\u56e0\u3002 \u800c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u53ea\u4e8b\u5173\u4f60\u5982\u4f55\u4fdd\u5b58\u6e90\u7801\uff0c\u53ea\u662f\u8ba9\u7f16\u8bd1\u5668\u80fd\u591f\u6210\u529f\u8bfb\u53d6\u4f60\u7684\u6e90\u7801\uff0c\u5bf9\u8fd0\u884c\u65f6\u7684\u4e71\u7801\u95ee\u9898\u6ca1\u6709\u5f71\u54cd\u3002\u7f16\u8bd1\u5668\u8bfb\u5b8c\u6e90\u7801\u540e\uff0c\u8981\u5728\u5e38\u91cf\u533a\u751f\u6210\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u8fd8\u662f\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u7684\u3002 \u4f8b\u5982\u4e4b\u524d\u8bf4\u7684\u65e5\u672c galgame \u5728\u4e2d\u56fd\u7535\u8111\u4e0a\u6253\u5f00\u7206\u51fa\u4e71\u7801\uff0c\u5c31\u662f\u56e0\u4e3a\u662f\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u4e86 galgame\uff08\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u4e3a Shift-JIS\uff09\uff0c\u5728\u4e2d\u56fd\u5ba2\u6237\u7535\u8111\u4e0a\u6253\u5f00\uff08\u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801\u4e3a GBK\uff09\u5bfc\u81f4\u7684\u3002 \u65e5\u672c\u7a0b\u5e8f\u5458\u4f7f\u7528\u4ec0\u4e48\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u6839\u672c\u65e0\u5173\u7d27\u8981\u2026\u2026\u54ea\u6015\u4ed6\u4eec\u4f7f\u7528\u4e86 UTF-8 \u4fdd\u5b58\u6e90\u7801\uff0cMSVC \u7f16\u8bd1\u65f6\u4ecd\u7136\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a Shift-JIS \u7f16\u7801\u7684\u5b57\u9762\u91cf\u6765\u5b58\u50a8\u5728\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5e38\u91cf\u533a\u4e2d\u3002 \u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a\u8fd9\u4e9b\u82b1\u91cc\u80e1\u54e8\u73a9\u5e94\u7edf\u4e00\u4e3a UTF-8\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u8f6c\u6362\u6765\u8f6c\u6362\u53bb\u4e86\uff1f\u8fd8\u4e0d\u662f\u56e0\u4e3a\u5386\u53f2\u9057\u7559\uff0c\u4e00\u4e9b\u52b3\u4fdd\u7a0b\u5e8f\u5458\u4e0d\u80af\u628a\u4ed6\u4eec GBK \u7f16\u7801\u7684\u6e90\u7801\u6539\u6210 UTF-8 \u4fdd\u5b58\uff0c\u800c\u4e14\uff0cWindows \u4e5f\u5b8c\u5168\u4e0d\u63d0\u4f9b\u57fa\u4e8e UTF-8 \u7684\u8de8\u5e73\u53f0 API\uff08\u53ea\u63d0\u4f9b GBK \u548c UTF-16 \u4e24\u79cd\uff0c\u5c31\u662f\u4e0d\u7ed9 UTF-8 \u7684\uff0c\u975e\u5e38\u6076\u5fc3\u4eba\uff09\u3002\u6240\u4ee5 MSVC \u81f3\u4eca\u4ecd\u7136\u9ed8\u8ba4\u662f GBK \u7f16\u7801\u7684\uff08\u66f4\u51c6\u786e\u7684\u8bf4\u662f ANSI\uff0c\u8ddf\u968f\u4f60\u7cfb\u7edf\u7684\u533a\u57df\u8bbe\u7f6e\u800c\u53d8\uff0c\u5728\u4e2d\u56fd\u5c31 GBK\uff0c\u5728\u7f8e\u56fd\u5c31 UTF-8\uff0c\u5728\u6b27\u6d32\u5c31 Latin-1\uff0c\u975e\u5e38\u7684\u53cc\u6807\uff09\u3002\u5fae\u8f6f\u5404\u79cd\u626f\u76ae\u6548\u7387\u4f4e\u4e0b\uff0cAPI \u5f04\u4e86\u4e00\u5957\u53c8\u4e00\u5957\u4e92\u76f8\u6781\u9650\u62c9\u626f\uff0c\u800c\u6211\u4eec Linux \u548c GCC \u65e9\u5df2\u9ed8\u8ba4\u5c31\u662f UTF-8\u2026\u2026 \u6211\u7406\u89e3\u4f60\u73b0\u5728\u5927\u8111\u5e72\u70e7\u7684\u5fc3\u60c5\u3002\u4f3a\u5019\u8fd9\u4e9b\u5386\u53f2\u7b54\u8fa9\u5f88\u590d\u6742\uff0c\u4e5f\u5f88\u65e0\u804a\uff0c\u6beb\u65e0\u610f\u4e49\uff01\u53ea\u662f\u4e3a\u4e86\u64e6\u53cd Unicode \u52b3\u4fdd\u7684\u5c41\u80a1\u3002 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a \u5bf9\u4e8e\u8de8\u5e73\u53f0\u8f6f\u4ef6\u6765\u8bf4\uff0c\u6211\u63a8\u8350\u5927\u5bb6\u628a\u4e09\u4e2a\u5168\u90e8\u8bbe\u4e3a UTF-8\uff01\uff08\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4e3b\u8981\u662f\u4f3a\u5019 MSVC\uff09 Linux + GCC \u7528\u6237\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u4f60\u4eec\u6240\u6709\u5b57\u7b26\u96c6\u9ed8\u8ba4\u7684\u8bbe\u5b9a\u5c31\u662f UTF-8\u3002 Windows + MSVC \u7528\u6237\u8bf7\u5f00\u542f /utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\uff0c\u73b0\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u90fd\u662f UTF-8 \u4e86\u3002 Windows + MinGW \u7528\u6237\u8bf7\u5f00\u542f -finput-charset=utf-8 \u548c -fexec-charset=utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\u3002 \u6240\u6709\u6e90\u7801\u6587\u4ef6\u7edf\u4e00\u4ee5 UTF-8 \u7f16\u7801\u4fdd\u5b58\uff0c\u4e14\u5c3d\u91cf\u5728\u6700\u524d\u9762\u52a0\u4e0a 0xFEFF \u8fd9\u4e2a BOM \u6807\u8bb0\uff0c\u9632\u6b62 MSVC \u8111\u62bd\u5f53\u4f5c GBK \u6765\u8bfb\u53d6\u3002 \u5728 main \u51fd\u6570\u524d\uff0c\u52a0\u4e24\u884c\uff1a // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 return 0; } \u8fd9\u6837\u4e00\u5957\u6253\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u6587\u672c\u7f16\u8f91\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u7801\uff0c\u4f60\u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u8bfb\u53d6\u6e90\u7801\uff0c\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6b63\u786e\u7684\u628a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8def\u5f84\u8f6c\u4e3a UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u3002 \u5728 CMake \u4e2d\uff0c\u53ea\u5bf9 MSVC \u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a if (MSVC) target_compile_options(\u4f60\u7684\u7a0b\u5e8f PRIVATE /utf-8) else() \u4e5f\u53ef\u4ee5\u5728\u6700\u524d\u9762 add_compile_options \uff0c\u5b9e\u73b0\u5bf9\u6240\u6709\u4e4b\u540e\u5b9a\u4e49\u7684\u7a0b\u5e8f\u5168\u5c40\u542f\u7528\u8be5\u9009\u9879\u3002 \u5728\u6211\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u90fd\u4f1a\u8fd9\u6837\u5f00\u542f\uff0c\u89e3\u51b3 MSVC \u4e0d\u8de8\u5e73\u53f0\u7684\u95ee\u9898\uff1a if (MSVC) add_compile_options(/Zc:preprocessor /utf-8 /DNOMINMAX /D_USE_MATH_DEFINES /EHsc /bigobj) else() if (WIN32) add_compile_options(-finput-charset=utf-8 -fexec-charset=utf-8) endif() add_compile_options(-Wall -Wextra -Werror=return-type) endif() add_executable(\u4f60\u7684\u7a0b\u5e8f \u4f60\u7684\u6587\u4ef6.cpp) # \u81ea\u52a8\u7ee7\u627f\u4e86\u4e0a\u9762\u6240\u6709\u7684\u7f16\u8bd1\u5668\u9009\u9879 .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 Windows \u5b98\u65b9\u63d0\u4f9b\u7684\u771f\u6b63 API \u662f _wfopen \u3002 fopen \u53ea\u662f\u4ed6\u4eec\u63d0\u4f9b\u7684\u201cPOSIX \u517c\u5bb9\u5c42\u201d \u5305\u88c5\uff0c\u5176\u4f1a\u628a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u901a\u8fc7 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u540e\uff0c\u8f6c\u53d1\u7ed9 _wfopen \u3002 \u51fa\u4e8e\u8de8\u5e73\u53f0\u7684\u8981\u6c42\uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 _wfopen \u8fd9\u79cd\u5176\u4ed6\u5e73\u53f0\u6ca1\u6709\u7684\u51fd\u6570\uff0c\u4e5f\u4e0d\u60f3\u7528\u90a3\u8fde 2 \u5b57\u8282 4 \u5b57\u8282\u90fd\u98d8\u5ffd\u4e0d\u5b9a\u7684 wchar_t \uff0c\u66f4\u4e0d\u60f3\u8ba9 std::string \u5b58\u6839\u672c\u4e0d\u80fd\u8de8\u5e73\u53f0\u7684 GBK\u3002 \u53ea\u8981\u8ba9 fopen \u7684 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u51fd\u6570\u66ff\u6362\u6210 \u201cUTF-8 \u5230 UTF-16\u201d \u5c31\u884c\u4e86\u3002\u8fc7\u53bb\uff0c\u6211\u4eec\u65e0\u6cd5\u66ff\u6362\uff0c\u6700\u65b0\u7684 Windows \u5728\u4e00\u6b21\u66f4\u65b0\u4e2d\uff0c\u652f\u6301\u4e86 \".utf-8\" locale \u8fd9\u4e00\u9ed1\u79d1\u6280\uff0c\u4e13\u95e8\u6ee1\u8db3\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u3002 // \u9ed8\u8ba4 locale fopen(\"\u4f60\u597d.txt\") == _wfopen(gbk_to_utf16(\"\u4f60\u597d.txt\")); // \u8bbe\u7f6e\u4e86 utf-8 locale \u540e fopen(\"\u4f60\u597d.txt\") == _wfopen(utf8_to_utf16(\"\u4f60\u597d.txt\")); \u82e5\u4e0d\u8bbe\u7f6e setlocale(LC_ALL, \".utf-8\") \uff0c\u5219 fopen \u548c ifstream \u9ed8\u8ba4\u4f1a\u628a\u4f60\u63d0\u4f9b\u7684 const char * \u6587\u4ef6\u8def\u5f84\uff0c\u5f53\u4f5c GBK \u7f16\u7801\u7684\uff0c\u800c\u6211\u4eec\u8bbe\u7f6e\u4e86 /utf-8 \u6216 -fexec-charset=utf-8 \u540e\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7f16\u7801\u5df2\u7ecf\u662f UTF-8 \u4e86\uff0c\u8fd9\u6837 UTF-8 \u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u8f93\u5165\u8fdb\u671f\u671b const char * \u7684 fopen \u53c2\u6570\uff0c\u5c31\u4f1a\u51fa\u4e71\u7801\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c .utf-8 locale \u53ea\u662f\u5f71\u54cd\u4e86\u6807\u51c6\u5e93\uff01\u5e76\u4e0d\u6539\u53d8\u7cfb\u7edf API\u3002 \u76f4\u63a5\u8c03\u7528\u7cfb\u7edf API \u65f6\uff0cA \u7cfb API \u4ecd\u7136\u6709\u95ee\u9898\u3002 MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u63d0\u793a\", MB_OK); // \u4e0d\u884c\uff0c.utf-8 \u53ea\u662f\u8ba9\u6807\u51c6\u5e93\u53d8\u6210 UTF-8 \u63a5\u53e3\u4e86\uff0cA \u7cfb Windows API \u4ecd\u7136\u662f GBK MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u63d0\u793a\", MB_OK); // \u6ca1\u95ee\u9898\uff01\u7528 UTF-16 \u7684 wchar_t \u5b57\u9762\u91cf\u6765\u8c03\u7528 W \u63a5\u53e3\u603b\u662f\u6ca1\u95ee\u9898\u7684 \u8fd8\u662f\u9700\u8981\u6211\u4eec\u624b\u52a8\u8f6c\u6362 UTF-8 \u5230 UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u2026\u2026\u4f46\u662f\u53cd\u6b63\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u5f88\u5c11\u9700\u8981\u76f4\u63a5\u8c03\u7528 Windows API\uff0c\u90fd\u662f\u901a\u8fc7\u901a\u7528\u7684 C/C++ \u6807\u51c6\u5e93\uff0c\u56e0\u6b64 .utf-8 locale \u53ef\u80fd\u662f\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u60f3\u8fdb\u519b UTF-8 \u7684\u6700\u4f73\u9009\u62e9\u3002 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e \u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\uff0c\u90fd\u662f\u9488\u5bf9 char \u7684\uff0c\u53ea\u6709 char \u88ab\u6545\u610f\u9488\u5bf9\u4e86\uff0c\u5b58\u5728\u5b57\u7b26\u7f16\u7801\u4e0d\u7edf\u4e00\u7684\u95ee\u9898\u3002 \u5982\u679c\u5168\u90e8\u7528 wchar_t \u7684\u8bdd\uff0c\u867d\u7136\u5728 Linux \u4e0a\u662f UTF-32\uff0c\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u4e0d\u7edf\u4e00\u4e86\u3002\u4f46\u81f3\u5c11\u5728\u540c\u4e00\u4e2a Windows \u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u90fd\u662f\u7edf\u4e00\u7684 UTF-16\u3002 \u6240\u4ee5\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u662f\u5168\u9762\u91c7\u7528 wchar_t \u548c std::wstring \uff0c\u8fd9\u6837\u65e0\u8bba\u4f60\u7684\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\u5982\u4f55\uff0c\u90fd\u5bf9 wchar_t \u548c\u57fa\u4e8e const wchar_t * \u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002 C \u8bed\u8a00\u6807\u51c6\u6ca1\u6709 _wfopen \uff0c\u4f46\u662f std::ifstream \u6709\u57fa\u4e8e std::wstring \u7684\u6784\u9020\u51fd\u6570\uff0c\u5c31 C++ \u6807\u51c6\u5e93\u6765\u770b std::wstring \u7684\u652f\u6301\u8fd8\u662f\u5f88\u4e30\u5bcc\u7684\uff0c\u57fa\u672c std::string \u6709\u7684 std::wstring \u90fd\u6709\uff0c\u4f8b\u5982 std::to_string \u548c std::to_wstring \uff0c std::cout \u548c std::wcout \u3002\u672c\u7ae0\u8282\u6700\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5bbd\u5b57\u7b26\u6d41\u7684\u7528\u6cd5\u3002 \u7f3a\u70b9\u662f\uff0c\u9996\u5148\u6bcf\u6b21\u90fd\u9700\u8981\u5199 L\"\u4f60\u597d\" \u8fd9\u4e2a L \u524d\u7f00\u5f88\u9ebb\u70e6\uff0c\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u4e14\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u90fd\u5728\u7528 std::string \uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b std::wstring \u7684 API\u3002 \u4f8b\u5982 openvdb \u7684\u6587\u4ef6\u5199\u5165\u51fd\u6570\uff1a void openvdb::io::File::write(std::string const &filename); \u8fd9\u6837\u5c31\u5f88\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f60\u5185\u90e8\u5168\u662f UTF-16 \u7684 std::wstring \u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u8c03\u7528\u7b2c\u4e09\u65b9\u5e93\u524d\u5c31\u9700\u8981\u8f6c\u6210 GBK \u7684 std::string \u3002\u53ef\u4ee5\u7528 boost::locale::conv::to_utf \u8fd9\u4e2a\u51fd\u6570\u8f6c\u6362\uff0c\u4f46\u4e5f\u5f88\u9ebb\u70e6\uff0c\u800c\u4e14\u5982\u679c std::wstring \u542b\u6709 GBK \u8303\u56f4\u4e4b\u5916\u7684 \u201c\ud883\udede\u201d\uff0cGBK \u65e0\u6cd5\u8868\u793a\uff0c\u53c8\u4f1a\u6709\u7f16\u7801\u5931\u8d25\u7684\u95ee\u9898\u3002 \u8fd8\u6709 stbi_load \u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u662f\u53ea\u63d0\u4f9b\u4e86 const char * \u7684\u63a5\u53e3\uff0c\u591a\u4e86\u53bb\u4e86\u3002 setlocale(LC_ALL, \".utf-8\") \u7684\u597d\u5904\u662f\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5168\u81ea\u52a8\u90fd\u4ece GBK \u65e0\u7f1d\u5207\u6362\u5230 UTF-8\uff0c\u800c\u4e0d\u7528\u5bf9\u4ed6\u4eec\u7684\u6e90\u7801\u505a\u4efb\u4f55\u66f4\u6539\u3002\u56e0\u4e3a\u4ed6\u4eec\u5185\u90e8\u90fd\u662f\u8c03\u7528\u7684 fopen \u548c ifstream \u3002 u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u4e2d\u56fd\u533a Windows\uff0cMSVC\uff0c\u7f16\u8bd1\u9009\u9879\uff1a /std:c++17 std::string s = \"\u4f60\u597d\"; hexdump(s); // C4 E3 BA C3 (GBK) std::string s = u8\"\u4f60\u597d\"; hexdump(s); // E4 BD A0 E5 A5 BD (UTF-8) u8 \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u7f16\u7801\u5b58\u50a8\u3002\u65e0\u8bba\u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u662f\u4e0d\u662f UTF-8\u3002 \u7f16\u8bd1\u5668\u4fdd\u8bc1\u4f1a\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u8f6c\u6362\u4e3a UTF-8 \u7f16\u7801\u7684 char \u5b57\u8282\u5e8f\u5217\uff0c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u533a\u3002 \u8fd9\u5bf9\u4e8e\u5df2\u7ecf\u8bbe\u7f6e\u4e86 /utf-8 \u9009\u9879\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u5df2\u7ecf\u4fdd\u8bc1\u662f UTF-8 \u7684\u6211\u4eec\u6765\u8bf4\u6beb\u65e0\u4f5c\u7528\u3002\u53ea\u662f\u5bf9\u4e8e\u4e0d\u7528 /utf-8 \u7684\u540c\u5b66\uff0c\u4ed6\u4eec\u60f3\u8981\u4e34\u65f6\u521b\u5efa\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5c31\u53ef\u4ee5\u7528 u8 \u524d\u7f00\u3002 \u5728 C++17 \u548c\u4e4b\u524d\uff0c u8\"\u4f60\u597d\" \u4ea7\u751f\u7684\u662f const char [] \u7c7b\u578b\u7684\u5e38\u91cf\u3002 \u5728 C++20 \u4e2d\uff0c\u5f15\u5165\u4e86 char8_t \u3002\u7136\u540e\uff0c\u4ed6\u4eec\u89c4\u5b9a\uff0c u8\"\u4f60\u597d\" \u73b0\u5728\u4ea7\u751f\u7684\u662f const char8_t [] \u7c7b\u578b\u7684\u5e38\u91cf\u4e86\u3002 \u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u4ee5\u524d\u4f60\u5199\u7684\uff1a std::string s = u8\"\u4f60\u597d\"; \u73b0\u5728\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u56e0\u4e3a const char8_t [] \u65e0\u6cd5\u7528\u4e8e\u6784\u9020\u53ea\u652f\u6301 const char [] \u7684 std::string \u3002 \u597d\u5728 C++23 \u53c8\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u5141\u8bb8 const char8_t [] \u9690\u5f0f\u8f6c\u6362\u4e3a const char [] \uff0cC++17 \u4e4b\u524d\u7684\u8fd9\u79cd\u4ee3\u7801\u53c8\u80fd\u6b63\u5e38\u901a\u8fc7\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u60f3\u5feb\u4e50\u5730\u7528 u8 \u5b57\u9762\u91cf\uff0c\u8981\u4e48 C++17\uff0c\u8981\u4e48 C++23\uff0c\u8df3\u8fc7 C++20 \u6bd4\u8f83\u597d\u3002 \u4f60\u53ef\u4ee5\u770b\u5230\uff0cC++ \u7248\u672c\u7684\u66f4\u65b0\u5e76\u4e0d\u662f 100% \u5b8c\u5168\u5411\u524d\u517c\u5bb9\u7684\uff0c\u6709\u65f6\u4e5f\u4f1a\u6709\u7834\u574f\u6027\u7684\u53d8\u66f4\uff0c\u4f46\u6bd4\u8f83\u5c11\uff0c\u5e73\u65f6\u611f\u89c9\u4e0d\u5230\u3002\u6bd4\u5982 C++11 \u4e4b\u524d auto \u5c31\u6709\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u540e\u6765\u51b3\u5b9a\u8fd9\u4e2a\u529f\u80fd\u6ca1\u4ec0\u4e48\u7528\uff0c\u5c31\u628a auto \u6539\u6210\u53e6\u4e00\u4e2a\u610f\u601d\u4e86\u3002 \u9664\u4e86 u8 \u4ee5\u5916\uff0c\u8fd8\u6709\u8fd9\u4e9b\uff1a \u524d\u7f00 \u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u201c\u4f60\u597d\u201d \u8fd0\u884c\u5b57\u7b26\u96c6 (ANSI) const char [] L\u201d\u4f60\u597d\u201d Windows \u4e0a UTF-16\uff1bLinux \u4e0a UTF-32 const wchar_t [] u8\u201d\u4f60\u597d\u201d UTF-8 const char8_t [] u\u201d\u4f60\u597d\u201d UTF-16 const char16_t [] U\u201d\u4f60\u597d\u201d UTF-32 const char32_t [] \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 \u628a\u5b57\u7b26\u4e32\u5f53\u4f5c\u7eaf\u7cb9\u7684\u201c\u5b57\u8282\u6d41\u201d\uff0c\u65e0\u89c6\u5b57\u7b26\u7f16\u7801\u3002\u6216\u8005\u8bf4\uff0c\u4f60\u4ece\u7cfb\u7edf\u8f93\u5165\u8fdb\u6765\u7684\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u6211\u5c31\u5b58\u50a8\u7684\u4ec0\u4e48\u7f16\u7801\u3002\u5bf9\u4e8e Unicode \u5219\u91c7\u53d6\u5b8c\u5168\u6446\u70c2\u7684\u6001\u5ea6\uff0c\u5b8c\u5168\u65e0\u89c6 Unicode \u7684\u5b58\u5728\u3002 \u9002\u7528\u573a\u666f\uff1a\u901a\u5e38\u4e0e\u6587\u5b57\u5904\u7406\u9886\u57df\u65e0\u5173\u7684\u8f6f\u4ef6\u4f1a\u91c7\u53d6\u8fd9\u79cd\u65b9\u6848\u3002 \u4f18\u70b9\uff1a\u65b9\u4fbf\uff0c\u4e14\u5185\u90e8\u5bf9\u5b57\u7b26\u4e32\u65e0\u4efb\u4f55\u8f6c\u6362\u548c\u5224\u65ad\uff0c\u6548\u7387\u6700\u9ad8\u3002 \u7f3a\u70b9\uff1a\u5728\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u8bfb\u5199\u5e26\u6709\u4e2d\u6587\u7684\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u4f1a\u9971\u53d7\u4e71\u7801\u548c\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u56f0\u6270\u3002 \u65b9\u6cd5\uff1a\u5b8c\u5168\u4f7f\u7528 const char * \u548c std::string \u3002 \u4ee3\u8868\u4f5c\uff1aLinux \u6587\u4ef6\u7cfb\u7edf ext4\u3001Lua \u7f16\u7a0b\u8bed\u8a00\u3001\u73b0\u4ee3 Python \u4e2d\u7684 bytes \u7c7b\u578b\u3001HTTP \u7684 ? \u53c2\u6570\u3001\u65e9\u671f FAT32 \u6587\u4ef6\u7cfb\u7edf\u7b49\u3002 \u8fd9\u7c7b\u8f6f\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u521d\u5b66\u8005\u5199\u6cd5\uff0c\u5982\u679c\u4f60\u4ece\u672a\u60f3\u8fc7\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u4ece\u4e0d\u4e86\u89e3 wchar_t \u3001 char32_t \u4e4b\u95f4\u7684\u6218\u4e89\uff0c\u53ea\u77e5\u9053 char \uff0c\u90a3\u4e48\u4f60\u5df2\u7ecf\u81ea\u52a8\u5728\u6b64\u9635\u8425\u91cc\u3002 \u6709\u4eba\u8bf4 Linux \u6587\u4ef6\u7cfb\u7edf\u662f UTF-8\uff1f\u5e76\u4e0d\u662f\uff01Linux \u6587\u4ef6\u7cfb\u7edf\u6839\u672c\u4e0d\u4f1a\u68c0\u9a8c\u4f60\u7684\u6587\u4ef6\u540d\u662f\u4e0d\u662f\u5408\u6cd5\u7684 UTF-8\uff0c\u53ea\u4e0d\u8fc7\u662f\u56e0\u4e3a\u4f60\u8bbe\u5b9a\u4e86 export LC_ALL=zh_CN.UTF-8 \uff0c\u8fd9\u4f1a\u4f7f\u6240\u6709\u7a0b\u5e8f\uff08\u5305\u62ec\u7ec8\u7aef\u6a21\u62df\u5668\uff09\u5047\u5b9a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5185\u5bb9\u90fd\u6309 UTF-8 \u7f16\u7801\uff0c\u4ece\u800c\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u5404\u7c7b API \u65f6\uff08\u5982 open\u3001write\uff09\u90fd\u4f1a\u4f7f\u7528 UTF-8 \u7f16\u7801\u7684 const char * \u8f93\u5165\uff0c\u5728 Linux \u7cfb\u7edf API \u770b\u6765\uff0c\u6240\u8c13\u201c\u6587\u4ef6\u540d\u201d\u53ea\u662f\u7eaf\u7cb9\u7684\u5b57\u8282\u6d41\uff0c\u53ea\u8981\u4fdd\u8bc1\u4e0d\u5305\u542b '/' \u548c '\\0' \uff0c\u65e0\u8bba\u4f60\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4ed6\u90fd\u4e0d\u5728\u4e4e\u3002\u800c\u6240\u6709\u7684 locale \u90fd\u517c\u5bb9 ASCII\uff0c\u6240\u4ee5\u7edd\u4e0d\u4f1a\u51fa\u73b0\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57\u7f16\u7801\u540e\u4ea7\u751f '/' \u7684\u60c5\u51b5\uff08\u4f8b\u5982 GB2312 \u4f1a\u628a\u4e00\u4e2a\u4e2d\u6587\u7f16\u7801\u6210\u4e24\u4e2a 0x80 \u5230 0xFF \u533a\u95f4\u7684\u5b57\u8282\uff0c\u548c ASCII \u7684\u8303\u56f4\u6ca1\u6709\u91cd\u53e0\uff0c\u66f4\u4e0d\u53ef\u80fd\u51fa\u73b0 '/' \uff09\uff0c\u5373\u4f7f\u6362\u6210 export LC_ALL=zh_CN.GB2312 \uff0cLinux \u6587\u4ef6\u7cfb\u7edf\u4e00\u6837\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u8bfb\u53d6\u4f60\u4e4b\u524d\u4ee5 UTF-8 \u5199\u5165\u7684\u6587\u4ef6\u4f1a\u53d8\u6210\u4e71\u7801\u800c\u5df2\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a\u7684 Windows \u800c\u8a00\uff0c\u4ed6\u7684\u6240\u6709 A \u51fd\u6570\u53ea\u652f\u6301 GBK \u7f16\u7801\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60 Lua \u4e2d\u628a\u5b57\u7b26\u4e32\u201c\u5f53\u4f5c\u201d UTF-8 \u6765\u7528\u3002\u90a3\u4e48\u5f53\u4f60\u5728\u8c03\u7528 Lua \u7684 io.open \u524d\uff0c\u9700\u8981\u5148\u505a\u4e00\u4e2a UTF-8 \u5230 GBK \u7684\u8f6c\u6362\uff0c\u8fd9\u8fd8\u4f1a\u5bfc\u81f4\u4e22\u5931\u90e8\u5206\u4e0d\u5728 GBK \u5185\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u5305\u542b Emoji\uff0c\u90a3\u5c31\u4f1a\u53d8\u6210 ??? \u4e71\u7801\u3002\u800c\u4f7f\u7528 W \u51fd\u6570\u7684 UTF-16 \u5c31\u4e0d\u4f1a\uff0c\u56e0\u4e3a UTF-16 \u80fd\u5bb9\u7eb3\u5b8c\u6574\u7684 Unicode \u6620\u5c04\u3002\u800c\u5b8c\u5168\u6446\u70c2\u7684 Lua\uff0c\u5176 io.open \u53ea\u662f\u4f7f\u7528 C \u8bed\u8a00\u5e93\u51fd\u6570 fopen \uff0c fopen \u53c8\u662f\u57fa\u4e8e Windows \u7684 A \u7cfb\u5217\u51fd\u6570\uff0cLua \u53c8\u6ca1\u6709\u63d0\u4f9b\u5bf9 Windows C \u8fd0\u884c\u65f6\u5e93\u7279\u6709\u7684 _wfopen \u51fd\u6570\u7684\u5c01\u88c5\uff0c\u4ece\u800c\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6253\u5f00\u4e00\u4e2a\u5e26\u6709 Emoji \u7684\u6587\u4ef6\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 ANSI \u9635\u8425\uff0c\u4f60\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0cchar \u6ee1\u5929\u98de\u6446\u70c2\u3002 UTF-8 \u9635\u8425 \u652f\u6301 Unicode\uff0c\u5b57\u7b26\u4e32\u7edf\u4e00\u4ee5 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u3001\u5904\u7406\u548c\u4f20\u8f93\u3002 \u5e94\u7528\u573a\u666f\uff1a\u5e38\u89c1\u4e8e\u6587\u5b57\u5904\u7406\u9700\u6c42\u4e0d\u5927\uff0c\u4f46\u6709\u5f3a\u70c8\u7684\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u7279\u522b\u662f\u4e92\u8054\u7f51\u65b9\u9762\u7684\u8f6f\u4ef6\u3002\u4ed6\u4eec\u901a\u5e38\u53ea\u7528\u5230\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3001\u67e5\u627e\u3001\u5207\u7247\u901a\u5e38\u4e5f\u53ea\u662f\u5728\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u6587\u4ef6\u5206\u9694\u7b26 '/' \uff09\u3002\u4e5f\u975e\u5e38\u9002\u5408\u4e3b\u8981\u9762\u5bf9\u7684\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0cUTF-8 \u662f\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u6700\u9ad8\u7684\uff0c\u6240\u4ee5\u4e5f\u5e7f\u6cdb\u7528\u4e8e\u7f16\u8bd1\u5668\u3001\u6570\u636e\u5e93\u4e4b\u7c7b\u7684\u573a\u666f\u3002\u540c\u65f6\u56e0\u4e3a UTF-8 \u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u4f7f\u5f97\u4ed6\u80fd\u8f7b\u6613\u9002\u914d\u8fdc\u53e4\u7684 C \u8bed\u8a00\u7a0b\u5e8f\u548c\u5e93\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-8 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8de8\u5e73\u53f0\uff0c\u5728\u7f51\u7edc\u4f20\u8f93\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u7801\uff0cUTF-8 \u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u7f16\u7801\u683c\u5f0f\uff0c\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684 UTF-8 \u8f6f\u4ef6\u53ef\u4ee5\u968f\u610f\u5171\u4eab\u6587\u672c\u6570\u636e\u3002\u517c\u5bb9 ASCII\uff0c\u65b9\u4fbf\u590d\u7528\u73b0\u6709\u5e93\u548c\u751f\u6001\u3002\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u9ad8\uff0c\u5bf9\u4e2d\u6587\u6587\u672c\u4e5f\u4e0d\u7b97\u592a\u5dee\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e\u5e95\u5c42 API \u5747\u91c7\u7528 UTF-16 \u7684 Windows \u7cfb\u7edf\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u635f\u5931\u3002\u4e14\u5b57\u7b26\u4e32\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4f1a\u53d8\u6210 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002 \u4ee3\u8868\u4f5c\uff1aRust \u8bed\u8a00\u3001Go \u8bed\u8a00\u3001CMake \u6784\u5efa\u7cfb\u7edf\u3001Julia \u8bed\u8a00\u7b49\u3002 \u5728 C++ \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 u8\"\u4f60\u597d\" \u521b\u5efa\u4e00\u4e2a\u4fdd\u8bc1\u5185\u90e8\u662f UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u7c7b\u578b\u4e3a char8_t [] \u3002 \u5982\u679c\u7528\u65e0\u524d\u7f00\u7684 \"\u4f60\u597d\" \u521b\u5efa\uff0c\u5219 MSVC \u9ed8\u8ba4\u4f1a\u4ee5\u7f16\u8bd1\u8005\u6240\u5728\u7cfb\u7edf\u7684\u201c\u533a\u57df\u8bbe\u7f6e (locale)\u201d \u4f5c\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u7f16\u7801\u683c\u5f0f\uff08\u800c\u4e0d\u662f\u8fd0\u884c\u8005\u7684\u533a\u57df\u8bbe\u7f6e\uff01\uff09\uff0c\u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8ba9 MSVC \u7f16\u8bd1\u5668\u9ed8\u8ba4\u91c7\u7528 UTF-8 \u7f16\u7801\uff0c\u5373\u8ba9 \"\u4f60\u597d\" \u548c u8\"\u4f60\u597d\" \u4e00\u6837\u91c7\u7528 UTF-8\u3002\u800c GCC \u9ed8\u8ba4\u5c31\u662f UTF-8\uff0c\u9664\u975e\u624b\u52a8\u6307\u5b9a -fexec-charset=GBK \u6765\u5207\u6362\u5230 GBK\u3002\u7a0d\u540e\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u7f16\u8bd1\u5668\u7684\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002 \u5047\u8bbe\u4f60\u901a\u8fc7 /utf-8 \u6216 -fexec-charset=utf-8 \u641e\u5b9a\u4e86\u7f16\u8bd1\u671f\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u3002\u63a5\u4e0b\u6765\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6587\u4ef6\u7cfb\u7edf\u3002 Linux \u6587\u4ef6\u7cfb\u7edf\u5185\u90e8\uff0c\u5747\u4f7f\u7528 8 \u4f4d\u7c7b\u578b char \u5b58\u50a8\uff0c\u5c06\u6587\u4ef6\u540d\u5f53\u4f5c\u5e73\u51e1\u7684\u5b57\u8282\u6d41\uff0c\u4e0d\u4f1a\u505a\u4efb\u4f55\u8f6c\u6362\u3002\u56e0\u6b64\u4f60\u7528 UTF-8 \u521b\u5efa\u548c\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5176\u4ed6\u4f7f\u7528 UTF-8 \u533a\u57df\u8bbe\u7f6e\u7684\u8f6f\u4ef6\u90fd\u53ef\u4ee5\u7167\u5e38\u6253\u5f00\uff0c\u4e0d\u4f1a\u6709\u4e71\u7801\u95ee\u9898\u3002 \u5176\u5b9e Windows \u4e0a\u4ee5 GBK \u7f16\u7801\u7684\u538b\u7f29\u6587\u4ef6\u6216\u6587\u672c\u6587\u4ef6\uff0c\u62f7\u8d1d\u5230 Linux \u4e0a\u6253\u5f00\u51fa\u73b0\u4e71\u7801\u95ee\u9898\uff0c\u5c31\u662f\u56e0\u4e3a Linux \u7684\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u90fd\u662f UTF-8 \u7684\u3002\u5b9e\u9645\u4e0a\u5982\u679c\u628a\u4f60\u7684\u6587\u4ef6\u62f7\u7ed9\u4e00\u4e2a\u7f8e\u56fd\u7684 Windows \u7528\u6237\uff0c\u4ed6\u4e5f\u4f1a\u770b\u5230\u4e71\u7801\uff0c\u56e0\u4e3a\u7f8e\u56fd\u5927\u533a\u7684 Windows \u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\uff0c\u800c\u4e2d\u56fd\u5927\u533a\u7684\u662f GBK\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8bb2\u5230\u89e3\u51b3\u65b9\u6848\u3002 \u800c Windows \u7684 NTFS \u6587\u4ef6\u7cfb\u7edf\uff0c\u91c7\u7528 16 \u4f4d\u7684 wchar_t \u5b58\u50a8\uff0cWindows \u7684\u6240\u6709 API\uff0c\u4e5f\u90fd\u662f\u57fa\u4e8e wchar_t \u7684\uff0cWindows \u5185\u6838\u5185\u90e8\u4e5f\u90fd\u7528 wchar_t \u50a8\u5b58\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u53ea\u6709\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6d41\u4f1a\u7528 char \u5b58\u50a8\u3002\u8fd9\u7c7b\u57fa\u4e8e wchar_t \u7684\u7cfb\u7edf API \u90fd\u6709\u4e00\u4e2a W \u540e\u7f00\uff0c\u4f8b\u5982\uff1a MessageBoxW(NULL, L\"\u4f60\u597d\", L\"\u6807\u9898\", MB_OK); \u8fd9\u4e2a MessageBoxW \u51fd\u6570\uff0c\u53ea\u63a5\u53d7 const wchar_t * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002 L\"\u4f60\u597d\" \u662f\u4e00\u4e2a wchar_t [] \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b83\u7684\u5185\u90e8\u7f16\u7801\u7c7b\u578b\u56fa\u5b9a\u662f UTF-16\uff0c\u4e0d\u4f1a\u968f\u7740\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002\u4e4b\u540e\u7684\u4e00\u8282\u4e2d\u4f1a\u8be6\u7ec6\u8bb2\u89e3 W \u548c A \u51fd\u6570\u7684\u95ee\u9898\u3002 \u867d\u7136\u4e5f\u6709\u63d0\u4f9b A \u540e\u7f00\u7684\u7cfb\u5217\u51fd\u6570\uff0c\u4ed6\u4eec\u548c W \u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u662f\u63a5\u53d7 const char * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u95ee\u9898\u5728\u4e8e\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u90fd\u5fc5\u987b\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f GBK \u7f16\u7801\uff01\u800c\u4e14\u65e0\u6cd5\u4fee\u6539\u3002 \u5f53\u8c03\u7528 A \u7cfb\u51fd\u6570\u65f6\uff0c\u4ed6\u4eec\u5185\u90e8\u4f1a\u628a GBK \u7f16\u7801\u8f6c\u6362\u4e3a UTF-16 \u7f16\u7801\uff0c\u7136\u540e\u8c03\u7528 Windows \u5185\u6838\u3002 \u8fd9\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\uff0c\u800c\u6240\u6709\u7684 C/C++ \u6807\u51c6\u5e93\u90fd\u662f\u57fa\u4e8e A \u51fd\u6570\u7684\uff01\u5982\u679c\u4f60\u7528 const char * \u5b57\u7b26\u4e32\u8c03\u7528 C \u6807\u51c6\u5e93\uff0c\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86 A \u51fd\u6570\u3002\u800c A \u51fd\u6570\u53ea\u63a5\u53d7 GBK\uff0c\u4f46\u4f60\u5374\u8f93\u5165\u4e86 UTF-8\uff01\u4ece\u800c UTF-8 \u4e2d\u6240\u6709\u9664 ASCII \u4ee5\u5916\u7684\uff0c\u5404\u79cd\u4e2d\u6587\u5b57\u7b26\u3001Emoji \u90fd\u4f1a\u53d8\u6210\u4e71\u7801\u3002 \u4f8b\u5982 fopen \u51fd\u6570\uff0c\u53ea\u6709 fopen(const char *path, const char *mode) \u8fd9\u4e00\u4e2a\u57fa\u4e8e char \u7684\u7248\u672c\uff0c\u91cc\u9762\u4e5f\u662f\u76f4\u63a5\u8c03\u7528\u7684 A \u51fd\u6570\uff0c\u5b8c\u5168\u4e0d\u7ed9\u6211\u9009\u62e9\u7684\u7a7a\u95f4\u3002\u867d\u7136 Windows \u4e5f\u63d0\u4f9b\u4e86 _wfopen(const wchar_t *path, const wchar_t *mode) \uff0c\u4f46\u90a3\u65e2\u4e0d\u662f POSIX \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u4e0d\u662f C \u8bed\u8a00\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u7528\u8fd9\u6837\u7684\u51fd\u6570\u5c31\u610f\u5473\u7740\u65e0\u6cd5\u8de8\u5e73\u53f0\u3002 Windows \u5b98\u65b9\u8ba4\u4e3a\uff1a W \u51fd\u6570\u624d\u662f\u771f\u6b63\u7684 API\uff0c A \u51fd\u6570\u53ea\u662f\u5e94\u4ed8\u4e0d\u542c\u8bdd\u7684\u5b9d\u5b9d\u3002\u53ef\u4f60\u5c31\u6ca1\u53d1\u73b0\u4f60\u81ea\u5df1\u7684 C/C++ \u6807\u51c6\u5e93\u4e5f\u5168\u90e8\u5728\u8c03\u7528\u7684 A \u51fd\u6570\u4e48\uff1f \u603b\u4e4b\uff0c A \u51fd\u6570\u662f\u6b8b\u5e9f\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u7528 W \u51fd\u6570\uff0c\u5c3d\u7ba1 UTF-16 \u662f\u5386\u53f2\u503a\uff0c\u4f46\u6211\u4eec\u522b\u65e0\u9009\u62e9\uff0c W \u51fd\u6570\u662f\u552f\u4e00\u80fd\u652f\u6301\u5b8c\u6574 Unicode \u5b57\u7b26\u8f93\u5165\u7684\u65b9\u5f0f\u3002 // \u5047\u8bbe\u8fd9\u6bb5 C++ \u4ee3\u7801\u4f7f\u7528 /utf-8 \u9009\u9879\u7f16\u8bd1\uff1a std::ifstream f(\"\u4f60\u597d.txt\"); // \u627e\u4e0d\u5230\u6587\u4ef6\uff0c\u5373\u4f7f\u201c\u4f60\u597d.txt\u201d\u5b58\u5728 std::ofstream f(\"\u4f60\u597d.txt\"); // \u4f1a\u521b\u5efa\u4e00\u4e2a\u4e71\u7801\u6587\u4ef6 \u6b63\u786e\u7684\u505a\u6cd5\u662f\u91c7\u7528 std::filesystem::u8path \u8fd9\u4e2a\u51fd\u6570\u505a UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\uff1a // C++17\uff0c\u9700\u8981\u7528 u8path \u8fd9\u4e2a\u51fd\u6570\u6784\u9020 path \u5bf9\u8c61\uff1a std::ifstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); // C++20 \u5f15\u5165 char8_t\uff0c\u533a\u5206\u4e8e\u666e\u901a char\uff0cpath \u7c7b\u4e5f\u6709\u4e86\u9488\u5bf9 const char8_t * \u7684\u6784\u9020\u51fd\u6570\u91cd\u8f7d\uff1a std::ifstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::filesystem::path \u7c7b\u7684 c_str() \u5728 Windows \u4e0a\u8fd4\u56de const wchar_t * \uff0c\u5728 Linux \u4e0a\u8fd4\u56de const char * \u3002\u8fd9\u5f88\u5408\u7406\uff0c\u56e0\u4e3a Windows \u6587\u4ef6\u7cfb\u7edf\u786e\u5b9e\u4ee5 wchar_t \u5b58\u50a8\u8def\u5f84\u540d\uff0c\u800c Linux \u6587\u4ef6\u7cfb\u7edf\u5b8c\u5168\u7528 char \u3002 \u6bcf\u6b21\u9700\u8981\u52a0 std::filesystem::u8path \u4e5f\u633a\u9ebb\u70e6\u7684\uff0c\u5bb9\u6613\u5fd8\u8bb0\uff0c\u4e00\u5fd8\u8bb0\u5c31\u65e0\u6cd5\u8bbf\u95ee\u4e2d\u6587\u76ee\u5f55\u3002 \u5f88\u591a\u8f6f\u4ef6\u5728 Windows \u4e0a\u65e0\u6cd5\u652f\u6301\u4e2d\u6587\u8def\u5f84\u540d\uff0c\u5c31\u662f\u56e0\u4e3a\u4ed6\u4eec\u4e60\u60ef\u4e86 Linux \u6216 MacOS \u7684\u5168 UTF-8 \u73af\u5883\uff0c\u5bf9\u6587\u4ef6\u8def\u5f84\u6ca1\u6709\u4efb\u4f55\u8f6c\u6362\u3002\u800c Windows \u5e95\u5c42\u5168\u662f UTF-16\uff0c\u6839\u672c\u6ca1\u6709\u63d0\u4f9b UTF-8 \u7684 API\uff0c\u4f60 UTF-8 \u53ea\u80fd\u8f6c\u6362\u6210 UTF-16 \u624d\u80fd\u907f\u514d\u4e2d\u6587\u4e71\u7801\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u6b7b\u6d3b\u4e0d\u80af\u63a5\u53d7\u660e\u6446\u7740\u5df2\u7ecf\u662f\u56fd\u9645\u901a\u7528\u6807\u51c6\u7684 UTF-8\uff0cA \u51fd\u6570\u7684\u7f16\u7801\u8fde\u5f53\u524d\u8fdb\u7a0b\u5207\u6362\u7684\u65b9\u6cd5\u90fd\u4e0d\u7ed9\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u5e94\u8be5\u7531 Windows \u5168\u8d23\u627f\u62c5\u3002 \u597d\u6d88\u606f\u662f\uff0c\u6700\u8fd1 MSVC \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6848\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\u5f00\u5934\uff0c\u52a0\u4e0a setlocale(LC_ALL, \".utf-8\") \u5c31\u53ef\u4ee5\u8ba9 C \u548c C++ \u6807\u51c6\u5e93\u8fdb\u5165 UTF-8 \u6a21\u5f0f\uff1a\u4e0d\u518d\u8c03\u7528 A \u7cfb\u51fd\u6570\u64cd\u4f5c\u6587\u4ef6\uff0c\u800c\u662f\u4f1a\u628a\u6587\u4ef6\u540d\u4ece UTF-8 \u8f6c\u6362\u6210 UTF-16 \u540e\u518d\u8c03\u7528\u771f\u6b63\u7a33\u5b9a\u7684 W \u7cfb\u51fd\u6570\u3002 setlocale(LC_ALL, \".utf-8\"); // \u53ea\u9700\u8981\u8fd9\u4e00\u884c FILE *fp = fopen(u8\"\u4f60\u597d.txt\", \"r\"); // \u53ef\u4ee5\u4e86 std::ifstream fin(u8\"\u4f60\u597d.txt\"); // \u53ef\u4ee5\u4e86 setlocale(LC_ALL, \".utf-8\"); \u53ea\u662f\u628a C \u6807\u51c6\u5e93\u7684 const char * \u53c2\u6570\u53d8\u6210\u4e86\u63a5\u53d7 UTF-8\uff0c\u5e76\u4e0d\u4f1a\u8ba9\u7cfb\u7edf\u7684 A \u51fd\u6570\u4e5f\u53d8\u6210 UTF-8 \u54e6\uff0c\u8c03\u7528\u672c\u5730 API \u65f6\u4ecd\u9700 UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-8 \u9635\u8425\uff0c\u5f00\u542f /utf-8 \uff0c\u7a0b\u5e8f\u5f00\u5934\u5199 setlocale(LC_ALL, \".utf-8\") \u3002Linux \u7528\u6237\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u3002 \u770b\u770b\u5404\u5927\u8f6f\u4ef6\u7ad9\u5728 UTF-8 \u9635\u8425\u7684\u7406\u7531\uff1a CMake\uff1a\u4f5c\u4e3a\u8de8\u5e73\u53f0\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u4e3a\u4e86\u8ba9\u9879\u76ee\u7684 CMakeLists.txt \u80fd\u8de8\u5e73\u53f0\u5171\u7528\u800c\u4e0d\u5fc5\u91cd\u5199\uff0c\u4ed6\u7406\u6240\u5f53\u7136\u5730\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\uff1a\u6240\u6709 CMakeLists.txt \u90fd\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u4e66\u5199\uff0c\u4e14\u7edf\u4e00\u4f7f\u7528\u6b63\u659c\u6760 '/' \u8def\u5f84\u5206\u9694\u7b26\u3002 CMake \u4f1a\u81ea\u52a8\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u5c06 UTF-8 \u5b57\u7b26\u4e32\u8f6c\u6362\u6210 UTF-16 \u540e\uff0c\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u5728 Linux \u7cfb\u7edf\u4e0a\u5219\u4e0d\u505a\u8f6c\u6362\u3002\u5728 Windows \u7cfb\u7edf\u4e0a\u8fd8\u4f1a\u81ea\u52a8\u628a\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u6b63\u659c\u6760 '/' \u8f6c\u6362\u6210 Windows \u4e13\u5c5e\u7684\u53cd\u659c\u6760 '\\\\' \uff0c\u65e0\u9700\u7528\u6237\u64cd\u5fc3\u3002 \u5c0f\u5f6d\u8001\u5e08\u81ea\u4e3b\u7814\u53d1\u7684 Zeno \u8282\u70b9\u4eff\u771f\u8f6f\u4ef6\uff1a\u7531\u4e8e\u4fdd\u5b58\u7684\u9879\u76ee\u5de5\u7a0b\u6587\u4ef6\u9700\u8981\u5728 Linux \u548c Windows \u5e73\u53f0\u4e0a\u4e92\u901a\uff0c\u4e0d\u80fd\u91c7\u7528 Windows \u5404\u81ea\u4e3a\u653f\u7684 GBK \u683c\u5f0f\uff0c\u4e14\u5de5\u7a0b\u6587\u4ef6\u5185\u5bb9\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\u4e2d\u3002 Rust \u548c Go\uff1a\u4e25\u683c\u533a\u5206\u201c\u5b57\u7b26 (32 \u4f4d)\u201d\u548c\u201c\u5b57\u8282 (8 \u4f4d)\u201d\u7684\u6982\u5ff5\u3002\u5728\u5b57\u7b26\u4e32\u7c7b\u578b\u4e2d\u5b58\u50a8\u5b57\u8282\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u4ee5\u5b57\u8282\u65b9\u5f0f\u8bfb\u53d6\u6216\u4ee5\u5b57\u7b26\u65b9\u5f0f\u8bfb\u53d6\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-8 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5904\u7406\u5207\u7247\u548c\u7d22\u5f15\u65f6\u4e0d\u65b9\u4fbf\u3002 \u7f16\u7a0b\u8bed\u8a00 \u5b57\u7b26\u7c7b\u578b (32 \u4f4d) \u5b57\u8282\u7c7b\u578b (8 \u4f4d) Rust char u8 Go rune byte Julia Char UInt8 \u4e3a\u6b64\uff0c\u8fd9\u4e9b\u8bed\u8a00\u90fd\u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e24\u5957 API\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u8282\u7d22\u5f15\u3002\u6309\u5b57\u7b26\u7d22\u5f15\u65f6\uff0c\u4f1a\u4ece\u5934\u5f00\u59cb\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\uff0c\u76f4\u5230\u89e3\u6790\u5230\u60f3\u8981\u7684\u5b57\u7b26\u4e3a\u6b62\uff0c\u590d\u6742\u5ea6 O(N) O(N) \u3002\u6309\u5b57\u8282\u7d22\u5f15\u65f6\uff0c\u76f4\u63a5\u8df3\u5230\u6307\u5b9a\u5b57\u8282\uff0c\u65e0\u9700\u89e3\u6790\uff0c\u590d\u6742\u5ea6 O(1) O(1) \u3002 let s = \"\u4f60\u597d\"; // \u6309\u5b57\u7b26\u904d\u5386 for c in s.chars() { // c: char println!(\"{}\", c); } // \u6309\u5b57\u8282\u904d\u5386 for b in s.bytes() { // b: u8 println!(\"{:02x}\", b); } \u5728 C++ \u4e2d\uff0c\u82e5\u8981\u91c7\u7528\u8fd9\u79cd UTF-8 \u65b9\u6848\uff0c\u53ef\u4ee5\u4f7f\u7528 utfcpp \u8fd9\u4e2a\u5e93\uff1a https://github.com/nemtrif/utfcpp \u7a0d\u540e\u6211\u4eec\u4f1a\u4ee5\u6848\u4f8b\u8be6\u7ec6\u6f14\u793a\u8fd9\u4e2a\u5e93\u7684\u7528\u6cd5\uff0c\u4e5f\u4f1a\u5c1d\u8bd5\u81ea\u5df1\u624b\u6413\u3002 \u65b9\u6cd51\uff1a\u4f7f\u7528 utf8to32 \u4e00\u6b21\u6027\u5b8c\u6210\u8f6c\u6362\uff0c\u7528\u5b8c\u540e\u518d\u8f6c\u56de\u53bb\u3002 std::string s = \"\u4f60\u597d\"; std::u32string u32 = utf8::utf8to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\u574f'; s = utf8::utf32to8(u32); fmt::println(\"{}\", s); // \u4f60\u574f \u65b9\u6cd52\uff1a utfcpp \u4e5f\u5c01\u88c5\u4e86\u4e00\u4e2a utf8::iterator \u8fed\u4ee3\u5668\u9002\u914d\u5668\uff0c\u6548\u679c\u7c7b\u4f3c\u4e8e Rust \u7684 .chars() \uff0c\u53ef\u4ee5\u5b57\u7b26\u800c\u4e0d\u662f\u5b57\u8282\u904d\u5386\u5b57\u7b26\u4e32\u5bb9\u5668\u3002 char s[] = \"\u4f60\u597d\"; utf8::unchecked::iterator bit(s); utf8::unchecked::iterator eit(s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u5b89\u5168\uff08\u5e26\u8fb9\u754c\u68c0\u6d4b\uff09\u7684\u7248\u672c char s[] = \"\u4f60\u597d\"; utf8::iterator bit(s, s, s + strlen(s)); utf8::iterator eit(s + strlen(s), s, s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u57fa\u4e8e std::string \u7684\u7248\u672c std::string s = \"\u4f60\u597d\"; utf8::iterator bit(s.begin(), s.begin(), s.end()); utf8::iterator eit(s.end(), s.begin(), s.end()); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } \u7531\u4e8e\u8fed\u4ee3\u5668\u63a5\u53e3\u590d\u6742\u96be\u61c2\uff0c\u5efa\u8bae\u5148\u5c01\u88c5\u6210\u5e26\u6709 begin() \u548c end() \u7684 range \u5bf9\u8c61\uff0c\u65b9\u4fbf\u4f7f\u7528 C++17 range-based loop \u8bed\u6cd5\u76f4\u89c2\u904d\u5386\uff1a template struct Utf8Range { utf8::iterator bit; utf8::iterator eit; template Utf8Range(T &&t) : bit(std::begin(t), std::begin(t), std::end(t)) , eit(std::end(t), std::begin(t), std::end(t)) {} auto begin() const { return bit; } auto end() const { return eit; } }; template Utf8Range(T &&t) -> Utf8Range; // \u4ee5\u4e0b\u662f\u65b0\u7c7b\u7684\u4f7f\u7528\u65b9\u6cd5 std::string s = \"\u4f60\u597d\"; for (char32_t c : Utf8Range(s)) { fmt::println(\"U+{:04X}\", c); } UTF-16 \u9635\u8425 \u652f\u6301 Unicode \u8fc7\u65e9\uff0c\u8bef\u4ee5\u4e3a 0xFFFF \u5c31\u662f Unicode \u7684\u4e0a\u9650\u3002 \u4e00\u5f00\u59cb\uff0c\u4eba\u4eec\u9519\u8bef\u5730\u628a UTF-16 \u5f53\u6210\u6c38\u8fdc\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e71\u7801\u95ee\u9898\uff0c\u6240\u4ee5\u90a3\u6bb5\u65f6\u671f\u7684\u8f6f\u4ef6\u90fd\u5927\u4e3e\u4f7f\u7528 UTF-16 \u4f5c\u4e3a\u5185\u7801\u3002\u6ca1\u60f3\u5230\u540e\u6765 Unicode \u53c8\u5f15\u5165 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u800c\u73b0\u6709\u7684\u5df2\u7ecf\u91c7\u7528\u4e86 16 \u4f4d\u5185\u7801\u7684\u8f6f\u4ef6\u53c8\u5df2\u7ecf\u65e0\u6cd5\u6839\u9664\uff0c\u53ea\u597d\u4f7f\u7528\u201c\u4ee3\u7406\u5bf9\u201d\u673a\u5236\uff0c\u589e\u91cf\u66f4\u65b0\u4fee\u590d\u4e86\u73b0\u6709\u7684 16 \u4f4d\u5185\u7801\u8f6f\u4ef6\u3002UTF-16 \u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u597d\u5904\uff0c\u7559\u4e0b\u5386\u53f2\u503a\u3002 \u4e8b\u5b9e\u4e0a\uff0cUnicode \u5df2\u7ecf\u65e0\u6cd5\u7ee7\u7eed\u6269\u5bb9\u7a81\u7834 0x10FFFF\uff0c\u5c31\u662f\u56e0\u4e3a\u53cc uint16_t \u7f16\u7801\u7684\u4ee3\u7406\u5bf9\u6700\u591a\u53ea\u80fd\u5bb9\u7eb3\u989d\u5916 0x100000 \u4e2a\u5b57\u7b26\u7684\u7a7a\u95f4\u3002\u672c\u6765 UTF-8 \u4e00\u5f00\u59cb\u7684\u8349\u6848\u662f\u6253\u7b97\u6700\u591a\u652f\u6301 8 \u8282\u5217\u8f66\uff0c\u5b8c\u5168\u5bb9\u7eb3\u9ad8\u8fbe 0x7FFFFFFF \u8303\u56f4\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u8ba9 Windows \u8fd8\u80fd\u7ee7\u7eed\u7528\uff0cUnicode \u624d\u88ab\u8feb\u6b62\u6b65 0x10FFFF\uff0cUTF-8 \u4e5f\u7ec8\u7ed3\u4e8e 4 \u8282\u5217\u8f66\u3002 \u5e94\u7528\u573a\u666f\uff1a\u901a\u5e38\u8ba4\u4e3a\uff0cUTF-16 \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff0c\u65b0\u8f6f\u4ef6\u4e0d\u5e94\u8be5\u518d\u4f7f\u7528 UTF-16\u3002\u53ea\u6709\u5728\u548c\u8fd9\u4e9b\u7cdf\u7c95\u8f6f\u4ef6\u7684 API \u6253\u4ea4\u9053\u65f6\uff0c\u624d\u5fc5\u987b\u8f6c\u6362\u4e3a UTF-16\u3002\u4f46\u4e5f\u6709\u4eba\u6307\u51fa\uff1aUTF-16 \u662f\u7eaf\u4e2d\u6587\u538b\u7f29\u7387\u6700\u9ad8\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6240\u4ee5 UTF-16 \u8fd8\u6bd4\u8f83\u9002\u5408\u7eaf\u4e2d\u6587\u6216\u4ee5\u4e2d\u6587\u5185\u5bb9\u4e3a\u4e3b\u7684\u6587\u672c\u6570\u636e\u538b\u7f29\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-16 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8c03\u7528 Windows \u7cfb\u7edf API \u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u76f4\u63a5\u5c31\u80fd\u8c03\u7528\uff0c\u6700\u9002\u5408 Windows \u672c\u5730\u5f00\u53d1\uff0c\u975e\u8de8\u5e73\u53f0\u3002\u4e14\u5bf9\u7eaf\u4e2d\u6587\u5185\u5bb9\u53ef\u6bd4 UTF-8 \u989d\u5916\u8282\u7701 33% \u7a7a\u95f4\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e Windows \u4ee5\u5916\u7684\u7cfb\u7edf\u5c31\u9700\u8981\u8f6c\u6362\u56de UTF-8\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u5f00\u9500\u3002\u4e14\u5982\u679c\u5b58\u50a8\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u7eaf\u82f1\u6587\uff0c\u5982 XML \u4ee3\u7801\u7b49\uff0c\u5185\u5b58\u5360\u7528\u4f1a\u6bd4 UTF-8 \u7ffb\u500d\u3002\u800c\u4e14 UTF-16 \u4ecd\u7136\u662f\u53d8\u957f\u7f16\u7801\uff0c\u867d\u7136\u51fa\u73b0\u53d8\u957f\u7684\u6982\u7387\u8f83\u4f4e\uff0c\u4f46\u4e0d\u4e3a 0\uff0c\u4ecd\u9700\u8981\u5f00\u53d1\u8005\u505a\u7279\u6b8a\u5904\u7406\u3002\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u4f1a\u5bfc\u81f4\u751f\u50fb\u5b57\u7b26\u51fa\u9519\uff0c\u5b57\u7b26\u4e32\u4ee5\u7801\u70b9\u4e3a\u5355\u4f4d\u7684\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4ecd\u7136 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002\u5e76\u4e14 UTF-16 \u6709\u5927\u5c0f\u7aef\u8f6c\u6362\u7684\u95ee\u9898\u3002 \u4ee3\u8868\u4f5c\uff1aWindows \u7cfb\u7edf API\u3001Java \u8bed\u8a00\u3001Windows \u6587\u4ef6\u7cfb\u7edf (NTFS)\u3001Qt\u3001Word\u3001JSON\uff0c\u4ed6\u4eec\u90fd\u662f UTF-16 \u7684\u53d7\u5bb3\u8005\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-16 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-16 \u4f9d\u7136\u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5904\u7406\u6ca1\u95ee\u9898\uff0c\u751f\u50fb\u5b57\u5c31\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4e14\u56e0\u4e3a\u51fa\u73b0\u6982\u7387\u4f4e\uff0c\u5f88\u5bb9\u6613\u4e0d\u53d1\u73b0\uff0c\u57cb\u4e0b\u9690\u60a3\u3002 Java \u5c31\u662f\u53d7\u5230\u4e86 UTF-16 \u5386\u53f2\u503a\u5f71\u54cd\uff0c char \u662f 16 \u4f4d\u7684\u7801\u4f4d\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\uff0c\u771f\u6b63\u7684\u4e00\u4e2a\u5b57\u7b26\u662f 32 \u4f4d\u7684 Character \u7c7b\u578b\u3002 \u7f16\u7a0b\u8bed\u8a00 \u7801\u70b9\u7c7b\u578b (32 \u4f4d) \u7801\u4f4d\u7c7b\u578b (16 \u4f4d) Java Character char Java \u7684 Character \u7c7b\u578b\u662f\u4e00\u4e2a 32 \u4f4d\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u5305\u542b\u4e86\u4e00\u4e2a Unicode \u7801\u4f4d\u3002 char \u7c7b\u578b\u662f\u4e00\u4e2a 16 \u4f4d\u7684\u503c\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e2a UTF-16 \u7f16\u7801\u7684\u7801\u70b9\u3002 String \u7684 charAt() \u65b9\u6cd5\u8fd4\u56de\u7684\u662f char \u7c7b\u578b\u7684\u7801\u4f4d\uff08\u7c7b\u4f3c\u4e8e\u5b57\u8282\uff09\uff0c\u5982\u679c\u8981\u83b7\u53d6 Character \u7c7b\u578b\u7684\u5b8c\u6574\u5b57\u7b26\uff0c\u5fc5\u987b\u4f7f\u7528 codePointAt() \u65b9\u6cd5\u3002\u8fd9\u662f Java \u8bed\u8a00\u8bbe\u8ba1\u4e0a\u7684\u4e00\u4e2a\u5931\u8bef\uff0c\u5df2\u7ecf\u65e0\u6cd5\u6539\u53d8\u3002 \u800c\u540e\u7eed\u65b0\u51fa\u7684 Kotlin \u662f Java \u7684\u5408\u6cd5\u7ee7\u627f\u8005\uff0c\u4ed6\u679c\u65ad\u653e\u5f03 UTF-16\uff0c\u52a0\u5165\u4e86 UTF-32 \u9635\u8425\u3002\u53ef\u89c1\uff0c\u8001\u8f6f\u4ef6\u575a\u6301\u7528 UTF-32 \u662f\u56e0\u4e3a\u4ed6\u4eec\u79ef\u91cd\u96be\u8fd4\uff0c\u65b0\u8f6f\u4ef6\u518d UTF-16 \u5c31\u662f\u81ea\u4f5c\u5b7d\u4e86\uff01 \u603b\u7ed3\uff1a\u4e0d\u8981\u652f\u6301 UTF-16 \u9635\u8425\uff0c\u9664\u975e\u4f60\u88ab\u8feb\u7ef4\u62a4\u53f2\u5c71\u3002 \u4f8b\u5982\u5c0f\u5f6d\u8001\u5e08\u53d1\u5fae\u4fe1\u670b\u53cb\u5708\u65f6\uff0c\u8f93\u5165 Emoji \u8868\u60c5\u540e\u526a\u5207\uff0c\u518d\u7c98\u8d34\uff0c\u5c31\u548c\u53d1\u73b0\u4e00\u4e2a Emoji \u88ab\u5207\u65ad\u6210\u4e86\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4e71\u7801\u7684\u5f62\u5f0f\u663e\u73b0\u3002\u4f30\u8ba1\u662f\u56e0\u4e3a\u5fae\u4fe1\u57fa\u4e8e Java \u7f16\u5199\uff0c\u75bc\u900a\u7a0b\u5e8f\u5458\u5bf9 UTF-16 \u4ee3\u7406\u5bf9\u5904\u7406\u7684\u4e0d\u5229\u7d22\u3002 Java \u4e2d\u4ee5\u7801\u70b9\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5199\u6cd5\uff1a String s = \"\u4f60\u597d\"; // \u6309\u7801\u70b9\u904d\u5386 for (int i = 0; i < s.length();) { Character c = s.codePointAt(i); System.out.println(String.format(\"U+%04X\", c)); i += Character.charCount(c); } // \u6309\u7801\u4f4d\u904d\u5386 for (char c : s.toCharArray()) { System.out.println(String.format(\"U+%04X\", (int) c)); } \u7531\u4e8e JSON \u662f\u548c Java \u4e00\u5757\u53d1\u660e\u7684\u3002\u5bf9\u4e8e\u8d85\u51fa 0xFFFF \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u91c7\u7528\u7684\u8f6c\u4e49\uff0c\u4e5f\u662f\u57fa\u4e8e UTF-16 \u7f16\u7801\u3002\u5373\u540c\u4e00\u4e2a\u5b57\u4f1a\u53d8\u6210\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4fdd\u8bc1 JSON \u6587\u4ef6\u603b\u662f ASCII \u683c\u5f0f\uff0c\u907f\u514d Windows \u7684 GBK \u7f16\u7801\u4e71\u505a\u989d\u5916\u7684\u5e72\u6270\u3002 // \u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7 {\"name\": \"\ud883\udede\"} {\"name\": \"\\ud883\\udfde\"} \u5728\u521a\u521a\u4ecb\u7ecd\u7684 C++ \u5e93 utfcpp \u4e2d\uff0c\u4e5f\u6709\u9488\u5bf9 UTF-16 \u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5982 utf16to32 \uff1a std::u16string s = u\"\u4f60\u597d\"; std::u32string u32 = utf16::utf16to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\ud883\udede'; s = utf16::utf32to16(u32); fmt::println(\"{}\", s); // \u4f60\ud883\udede fmt::println(\"{}\", u32.size()); // 2 fmt::println(\"{}\", s.size()); // 3 UTF-32 \u9635\u8425 \u652f\u6301 Unicode\uff0c\u6bcf\u4e2a\u7801\u70b9\u90fd\u7528\u4e00\u4e2a uint32_t \u6216 char32_t \u8868\u793a\u3002 \u5e94\u7528\u573a\u666f\uff1a\u9002\u5408\u9700\u8981\u7ecf\u5e38\u5904\u7406\u6587\u5b57\u7684\u9886\u57df\uff0c\u5982\u6587\u672c\u7f16\u8f91\u5668\u3001\u6d4f\u89c8\u5668\u7b49\u3002\u4f46\u4e0d\u9002\u5408\u5b58\u50a8\u548c\u4f20\u8f93\uff0c\u56e0\u4e3a\u6d6a\u8d39\u786c\u76d8\u548c\u7f51\u7edc\u5e26\u5bbd\u3002\u5b57\u7b26\u4e32\u4e00\u822c\u90fd\u957f\u671f\u4ee5 UTF-8 \u5b58\u50a8\uff0c\u53ea\u6709\u5728\u9700\u8981\u9891\u7e41\u7d22\u5f15\u7801\u4f4d\u65f6\uff0c\u624d\u9700\u8981\u8f6c\u6362\u4e3a UTF-32\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-32 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u3001\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u90fd\u662f O(1) O(1) \u7684\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u5f53\u4f5c\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u968f\u610f\u5904\u7406\u3002\u4f8b\u5982\u4f60\u53ef\u4ee5\u8bbe\u60f3\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff0c\u9700\u8981\u652f\u6301\u201c\u9000\u683c\u201d\u64cd\u4f5c\uff0c\u5982\u679c\u662f UTF-8 \u548c UTF-16 \u5c31\u9700\u8981\u7e41\u7410\u7684\u5224\u65ad\u4ee3\u7406\u5bf9\u3001\u5404\u79cd\u8f66\u53a2\uff0c\u800c UTF-32 \u7684\u5b57\u7b26\u4e32\u53ea\u9700\u8981\u4e00\u6b21 pop_back \u5c31\u641e\u5b9a\u4e86\u3002 \u7f3a\u70b9\uff1a\u6d6a\u8d39\u7a7a\u95f4\u5927\uff0c\u901a\u5e38\u5728\u4fdd\u5b58\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8f6c\u6362\u56de UTF-8 \u540e\u518d\u5199\u5165\u6587\u4ef6\uff0c\u6709\u4e00\u5b9a\u6027\u80fd\u5f00\u9500\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-32 \u9635\u8425\uff0c\u8bf7\u5168\u90e8\u4f7f\u7528 char32_t \u548c std::u32string \u3002\u5b57\u9762\u91cf\u5168\u7528 U\"\u4f60\u597d\" \u7684\u5f62\u5f0f\u4e66\u5199\uff0c\u8bfb\u6587\u4ef6\u65f6\u8f6c\u4e3a UTF-32\uff0c\u5199\u6587\u4ef6\u65f6\u8f6c\u56de UTF-8\u3002 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u7531\u4e8e C++26 \u524d\u6807\u51c6\u5e93\u5bf9\u7f16\u7801\u8f6c\u6362\u51e0\u4e4e\u6ca1\u6709\u652f\u6301\uff0c\u5728 C++ \u4e2d\u8f6c\u6362\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u90fd\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\u3002 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1a utfcpp \u5982\u679c\u4f60\u53ea\u662f\u9700\u8981\u4e0d\u540c UTF \u683c\u5f0f\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u6ca1\u6709\u5904\u7406 GBK \u7b49\u7684\u9700\u6c42\uff1a\u90a3\u4e48\u4e4b\u524d\u5df2\u7ecf\u4ecb\u7ecd\u4e86 utfcpp \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5e93\uff0c\u5df2\u7ecf\u591f\u7528\u3002 #include \"utf8/cpp20.h\" std::u8string s8 = u8\"\u4f60\u597d\"; std::u16string s16 = utf8::utf8to16(s8); std::u32string s32 = utf8::utf8to32(s8); std::string s = utf8::utf16to8(s16); s8 = utf8::utf16tou8(s16); \u6700\u540e\u8fd9\u4e24\u4e2a\u533a\u522b\u5728\u4e8e\uff0c utf16to8 \u8fd4\u56de std::string \uff0c utf16tou8 \u8fd4\u56de std::u8string \uff0c\u91cc\u9762\u90fd\u662f UTF-8 \u7f16\u7801\u7684\uff0c\u4e0d\u8fc7\u6709\u7684\u4eba\u559c\u6b22\u7528 std::string \u6765\u5b58\u50a8 UTF-8\uff0c\u4e0d\u559c\u6b22 std::u8string \uff0c\u6216\u8005\u8bf4\u4ed6\u4eec\u6ca1\u6709 C++20\uff0c\u4e0d\u652f\u6301 std::u8string \uff0c\u56e0\u6b64\u8fd9\u4e2a\u5e93\u6ee1\u8db3\u4ed6\u4eec\u7684\u4e0d\u540c\u9700\u8981\u3002\u4f46\u662f std::u8string \u4f5c\u4e3a\u53c2\u6570\u65f6\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u53c2\u6570\u53ef\u4ee5\u81ea\u52a8\u91cd\u8f7d\uff0c\u800c\u8fd4\u56de\u503c\u4e0d\u884c\u3002 \u7f3a\u70b9\u662f\u4ed6\u4e0d\u80fd\u5904\u7406 GBK\u3001Shift-JIS \u7b49\u975e Unicode \u7f16\u7801\uff0c\u4e5f\u4e0d\u80fd\u81ea\u52a8\u68c0\u6d4b\u5f53\u524d\u7684 ANSI \u533a\u57df\u8bbe\u7f6e\u3002 \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1a boost::locale \u5982\u679c\u4f60\u8fd8\u8981\u652f\u6301\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u6bd4\u5982 GBK\u3001Shift-JIS\u3001Latin-1\u3002 \u4e00\u79cd\u662f C \u8bed\u8a00\u7684 iconv \uff0c\u53e6\u4e00\u79cd\u662f\u73b0\u4ee3 C++ \u7684 boost::locale \u3002 \u867d\u7136\u529f\u80fd\u5dee\u4e0d\u591a\uff0c\u5e95\u5c42\u90fd\u662f\u8c03\u7528 icu \u7684\u3002 boost::locale \u7684 API \u66f4\u52a0\u53cb\u597d\uff0c\u800c\u4e14\u662f\u73b0\u4ee3 C++ \u98ce\u683c\u7684\u3002 # Ubuntu \u7528\u6237\u5b89\u88c5 Boost.locale \u65b9\u6cd5\uff1a $ sudo apt-get install libboost-locale-dev # Arch Linux \u7528\u6237\u5b89\u88c5 Boost \u5168\u5bb6\u6876\u65b9\u6cd5\uff1a $ sudo pacman -S boost \u4e0d\u559c\u6b22 Boost \u7684\u4eba\u6709\u96be\u4e86\u3002 UTF \u4e4b\u95f4\u4e92\u8f6c \u4f7f\u7528 boost::locale::conv::utf_to_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::utf_to_utf; int main() { std::string s8 = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c UTF-32\uff1a std::u32string s32 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-16\uff1a std::u16string s16 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-8\uff1a s8 = utf_to_utf(s32); std::cout << s8 << '\\n'; return 0; } \u6a21\u677f\u53c2\u6570\u4e2d\uff0c\u53ea\u9700\u6307\u5b9a\u8f6c\u6362\u5230\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5c31\u884c\uff0c\u6765\u81ea\u4ec0\u4e48\u7c7b\u578b\uff0c\u4ed6\u81ea\u5df1\u4f1a\u91cd\u8f7d\u7684\u3002 \u6bd4\u5982\u4ece char32_t \u8f6c\u5230 char16_t \uff0c\u53ea\u9700\u8981 utf_to_utf \u5c31\u53ef\u4ee5\uff0c\u975e\u5e38\u65b9\u4fbf\u3002 \u7f16\u8bd1\uff1a $ g++ -std=c++17 -lboost_locale main.cpp \u8f93\u51fa\uff1a \u4f60\u597d \u5efa\u8bae\u7528\u540c\u6837\u8de8\u5e73\u53f0\u7684 CMake \u94fe\u63a5 Boost\uff0c\u5426\u5219 Windows \u7528\u6237\u8981\u6709\u96be\u4e86\u2026\u2026 find_package(Boost REQUIRED COMPONENTS locale) target_link_libraries(\u4f60\u7684\u7a0b\u5e8f Boost::locale) \u4e0d\u8fc7 boost::locale \u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u90a3\u5c31\u662f\u4e0d\u652f\u6301 char8_t \u548c std::u8string \u3002 char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff01 \u7531\u4e8e Boost \u8f83\u8001\uff0c\u6ca1\u6709\u53ca\u65f6\u8ddf\u8fdb\uff0c\u6240\u4ee5\u4ed6\u5e76\u6ca1\u6709\u5b9e\u73b0\u9488\u5bf9 char8_t \u7684\u7279\u5316\uff0c\u5982\u679c\u4f7f\u7528\u4e86 utf_to_utf \u4f1a\u62a5 undefined reference \u9519\u8bef\uff0c\u5373\u627e\u4e0d\u5230\u7b26\u53f7\u3002\u6539\u6210 utf_to_utf \u5c31\u6ca1\u95ee\u9898\u4e86\u3002 GBK \u548c UTF \u4e92\u8f6c \u4f7f\u7528 boost::locale::conv::to/from_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::to_utf; using boost::locale::conv::from_utf; int main() { std::string s = \"\u4f60\u597d\"; // \u4ece GBK \u8f6c\u5230 UTF-16 std::wstring ws = to_utf(s, \"GBK\"); std::wcout << ws << '\\n'; // \u4ece UTF-16 \u8f6c\u56de GBK s = from_utf(ws, \"GBK\"); std::wcout << s << '\\n'; return 0; } \u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f GBK \u3001 Shift-JIS \u3001 Latin1 \u7b49\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u5728\u770b\u5230\u3002 \u8fd9\u91cc to_utf \u4f1a\u81ea\u52a8\u5224\u65ad wchar_t \u7684\u5927\u5c0f\u3002\u5982\u679c\u662f 2 \u5b57\u8282\uff08Windows \u5e73\u53f0\u60c5\u51b5\uff09\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-16\uff0c\u5982\u679c\u662f 4 \u5b57\u8282\uff08Linux \u5e73\u53f0\u60c5\u51b5\uff09\uff0c\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-32\u3002 \u800c to_char \u5219\u662f\u65e0\u8bba\u4ec0\u4e48\u5e73\u53f0\uff0c\u90fd\u4f1a\u8f6c\u4e3a UTF-16\u3002 from_utf \u4e0d\u9700\u8981\u6307\u5b9a\u4efb\u4f55\u6a21\u677f\u53c2\u6570\uff0c\u56e0\u4e3a\u4ed6\u603b\u662f\u8fd4\u56de std::string \uff08ANSI \u6216 GBK \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u53c2\u6570\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4f1a\u81ea\u52a8\u901a\u8fc7\u91cd\u8f7d\u5224\u65ad\uff0c\u4f8b\u5982 from_utf(ws, \"GBK\") \u8fd9\u91cc\u7684\u53c2\u6570\u662f wchar_t \uff0c\u90a3\u4e48\u5728 Windows \u4e0a\uff0c\u4ed6\u4f1a\u68c0\u6d4b\u5230 wchar_t \u662f 2 \u5b57\u8282\uff0c\u5c31\u8ba4\u4e3a\u662f UTF-16 \u5230 GBK \u7684\u8f6c\u6362\u3002 UTF \u548c ANSI \u4e92\u8f6c \u6211\u4eec\u7a0b\u5e8f\u7684\u7528\u6237\u4e0d\u4e00\u5b9a\u662f\u4e2d\u56fd\u7528\u6237\uff08GBK\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4fc4\u7f57\u65af\u7528\u6237\uff08CP1251\uff09\u3001\u65e5\u672c\u7528\u6237\uff08Shift-JIS\uff09\u3001\u897f\u73ed\u7259\u7528\u6237\uff08CP1252\uff09\u7b49\u3002 \u5982\u679c\u8981\u91c7\u7528\u7528\u6237\u7684\u533a\u57df\u8bbe\u7f6e\uff0c\u5373\u201cANSI\u201d\uff0c\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u7559\u7a7a\uff08 \"\" \uff09\u3002 \u7a7a\u5b57\u7b26\u4e32\u5c31\u8868\u793a\u5f53\u524d\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e86\uff0c\u5728\u4e2d\u56fd\u5927\u533a\u7b49\u4ef7\u4e8e \"GBK\" \uff0c\u4fc4\u7f57\u65af\u5927\u533a\u7b49\u4ef7\u4e8e \"CP1251\" \u7b49\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::to_utf; int main() { std::string u8s = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c ANSI std::string s = from_utf(u8s, \"\"); // ANSI \u8f6c UTF-8 u8s = to_utf(s, \"\"); return 0; } \u5927\u603b\u7ed3 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-16 utf_to_utf UTF-x UTF-32 utf_to_utf UTF-x Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 UTF-x \u8868\u793a\u53d6\u51b3\u4e8e\u53c2\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u5982\u679c\u53c2\u6570\u662f char16_t \u7684\u5b57\u7b26\u4e32 std::u16string \uff0c\u90a3 x \u5c31\u662f 16\u3002 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-16 to_utf(\"GBK\", string) GBK UTF-32 to_utf(\"GBK\", string) GBK Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-32 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 from_utf(\"GBK\", string) UTF-8 GBK from_utf(\"GBK\", u16string) UTF-16 GBK from_utf(\"GBK\", u32string) UTF-32 GBK from_utf(\"GBK\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 GBK from_utf(\"\", string) UTF-8 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u16string) UTF-16 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u32string) UTF-32 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u533a\u57df\u8bbe\u7f6e GBK \u548c Shift-JIS \u4e92\u8f6c #include #include using boost::locale::conv::between; using boost::locale::conv::from_utf; int main() { // \u521b\u5efa\u4e00\u4e2a Shift-JIS \u5b57\u7b26\u4e32 std::string jis = from_utf(u8\"\u65e5\u672c\u8a9e\", \"Shift-JIS\"); // \u4ece Shift-JIS \u8f6c\u5230 GBK std::string gbk = between(jis, \"GBK\", \"Shift-JIS\"); std::cout << gbk << '\\n'; // \u4ece GBK \u8f6c\u56de Shift-JIS jis = between(gbk, \"Shift-JIS\", \"GBK\"); std::cout << jis << '\\n'; return 0; } \u6ce8\u610f\uff01\u662f\u76ee\u6807\u7f16\u7801\u5728\u524d\uff01\u5982\u679c\u4f60\u8981\u4ece Shift-JIS \u8f6c\u6210 GBK\uff0c\u90a3\u4e48\u9700\u8981 between(jis, \"GBK\", \"Shift-JIS\") \uff0c\u8fd9\u771f\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\u3002\u4e0d\u4ec5 GBK \u548c Shift-JIS \u53ef\u80fd\u4e0d\u5c0f\u5fc3\u5f04\u53cd\u4e86\uff0c\u7f16\u8bd1\u5668\uff0c\u4e00\u70b9\u63d0\u793a\u90fd\u6ca1\u6709\uff0c\u800c\u4e14 jis \u548c \u201cGBK\u201d \u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5f88\u5bb9\u6613\u5927\u8111\u641e\u6df7\u3002\u8ba9\u6211\u6765\u8bbe\u8ba1\u7684\u8bdd\uff0c\u6211\u4f1a\u8fd9\u6837\u63d0\u4f9b API\uff1a decode(jis, Encoding::ShiftJIS).encode(Encoding::GBK) \uff0c\u5176\u4e2d Encoding \u662f\u4e00\u4e2a\u679a\u4e3e\uff0c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e0d\u4ec5\u907f\u514d\u72af\u9519\u7684\u673a\u4f1a\uff0c\u770b\u8d77\u6765\u4e5f\u66f4\u8f7b\u677e\u3002\u4e4b\u540e\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u9898\u8bfe\u4e2d\uff0c\u4f1a\u518d\u8be6\u7ec6\u8bb2\u89e3\u4ec0\u4e48\u662f\u597d\u7684 API \u8bbe\u8ba1\u3002 \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u5982\u679c\u9047\u5230\u65e0\u6cd5\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u8be5\u5982\u4f55\u5904\u7f6e\uff1f \u9ed8\u8ba4\u60c5\u51b5\u4e0b Boost \u4f1a\u5ffd\u89c6\u9519\u8bef\uff0c\u7f16\u7801\u5931\u8d25\u7684\u5b57\u7b26\u4f1a\u88ab\u4e22\u5f03\u3002 #include #include using boost::locale::conv::from_utf; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\"); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 std::cout << gbk << '\\n'; // \u5728 Windows \u7684 GBK \u7ec8\u7aef\u4e0a\uff0c\u53ea\u663e\u793a\u201c\u6211\u7231\u9762\u201d return 0; } \u53ef\u4ee5\u7528 method_type \u8fd9\u4e2a\u679a\u4e3e\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7684\u65b9\u5f0f\u3002 \u9ed8\u8ba4\u662f skip \uff0c\u8df3\u8fc7\u6240\u6709\u89e3\u7801\u51fa\u9519\u7684\u5730\u65b9\uff08\u5bfc\u81f4\u201c\ud883\udede\u201d\u4e22\u5931\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u5207\u6362\u5230 stop \uff0c\u5f53\u9047\u5230\u89e3\u7801\u9519\u8bef\u65f6\uff0c\u4f1a\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\", method_type::stop); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 // from_utf \u4f1a\u629b\u51fa `conversion_error` \u5f02\u5e38 std::cout << gbk << '\\n'; return 0; } \u4e3e\u4f8b\uff1a\u5c1d\u8bd5\u4ee5 GBK \u4fdd\u5b58\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u6539\u4e3a\u5e26\u6709 BOM \u7684 UTF-8\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; using boost::locale::conv::conversion_error; void try_save(std::u32string content, std::wstring path) { std::string binary; try { // \u5c1d\u8bd5\u5c06 UTF-32 \u8f6c\u6210 GBK \u7f16\u7801 binary = from_utf(content, \"GBK\", method_type::stop); } catch (conversion_error const &e) { // \u82e5 GBK \u65e0\u6cd5\u8868\u793a // \u6539\u7528\u524d\u9762\u5e26\u6709 BOM \u7684 UTF-8 \u7f16\u7801 binary = \"\\xEF\\xBB\\xBF\" + utf_to_utf(content); } std::ofstream(path) << binary; } \u4e3e\u4f8b\uff1a\u652f\u6301 UTF-8 \u5b57\u7b26\u4e32\uff08\u800c\u4e0d\u662f ANSI \u5b57\u7b26\u4e32\uff09\u7684\u6253\u5370\u51fd\u6570\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::utf_to_utf; void u8print(std::string msg) { std::cout << from_utf(msg, \"\"); // \u6216\u8005\uff1a // std::wcout << utf_to_utf(msg, \"\"); } \u66f4\u591a\u7ec6\u8282\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1ahttps://www.boost.org/doc/libs/1_81_0/libs/locale/doc/html/group__codepage.html \u66f4\u591a\u529f\u80fd\uff1f\uff01 \u7f16\u7801\u8f6c\u6362\u53ea\u662f boost::locale::conv \u8fd9\u4e2a\u5b50\u6a21\u5757\u4e0b\u7684\u4e00\u4e2a\u5c0f\u529f\u80fd\u800c\u5df2\uff01 boost::locale \u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u5982\u6309\u7167\u5730\u57df\u8bed\u8a00\u89c4\u8303\u683c\u5f0f\u5316\u6570\u5b57\u3001\u8d27\u5e01\u3001\u65e5\u671f\u3001\u65f6\u95f4\u7b49\uff0c\u4e0b\u4e00\u5c0f\u8282\u4e2d\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u3002\u5b8c\u5168\u662f std::locale \u7684\u4e0a\u4f4d\u66ff\u4ee3\u3002 Boost \u54ea\u91cc\u90fd\u597d\uff0c\u4f60\u60f3\u8981\u7684\u529f\u80fd\u5e94\u6709\u5c3d\u6709\u3002\u800c\u4e14\u4e0d\u9700\u8981 C++20\uff0c\u5f88\u4f4e\u7248\u672c\u7684 C++ \u4e5f\u80fd\u7528\u3002\u552f\u4e00\u7f3a\u70b9\u53ef\u80fd\u5c31\u662f\u592a\u80a5\u4e86\uff0c\u7f16\u8bd1\u6162\u3002 Windows \u7528\u6237\uff1aMultiByteToWideChar \u5982\u679c\u4f60\u662f Windows \u7a0b\u5e8f\u5458\uff0c\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u4e14\u9700\u8981\u5728 Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u89c4\u5b9a\u7684 ANSI\uff08\u5728\u4e2d\u56fd\u533a\u662f GBK\uff09\u7f16\u7801\u548c UTF-16 \u4e4b\u95f4\u8f6c\u6362\uff1a \u53ef\u4ee5\u7528 Windows \u5b98\u65b9\u63d0\u4f9b\u7684 MultiByteToWideChar \u548c WideCharToMultiByte \u51fd\u6570\u3002 \u8fd9\u4e24\u4e2a\u51fd\u6570\u56e0\u4e3a C \u8bed\u8a00\u7279\u8272\u7684\u7f18\u6545\uff0c\u53c2\u6570\u6bd4\u8f83\u591a\u800c\u6742\uff0c\u5efa\u8bae\u81ea\u5df1\u52a8\u624b\u5c01\u88c5\u6210\u66f4\u6613\u7528\u7684 C++ \u51fd\u6570\uff1a std::wstring ansi_to_wstring(const std::string &s) { // ACP = ANSI Code Page\uff0c\u544a\u8bc9\u4ed6\u5b57\u7b26\u4e32\u91cc\u7684\u662f\u5f53\u524d\u533a\u57df\u8bbe\u7f6e\u6307\u5b9a\u7684\u7f16\u7801\uff08\u5728\u4e2d\u56fd\u533a\uff0cANSI \u5c31\u662f GBK \u4e86\uff09 int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_ansi(const std::wstring &ws) { int len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } std::wstring utf8_to_wstring(const std::string &s) { int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_utf8(const std::wstring &ws) { int len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } C \u8bed\u8a00\u7279\u8272\uff1a\u6240\u6709\u8981\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff0c\u90fd\u9700\u8981\u8c03\u7528\u4e24\u904d\uff0c\u7b2c\u4e00\u6ce2\u5148\u6c42\u51fa\u957f\u5ea6\uff0c\u7b2c\u4e8c\u6ce2\u624d\u5199\u5165\u3002\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u4e0e\u5185\u5b58\u5206\u914d\u5668\u8026\u5408\uff0c\u6240\u6709\u7684 C \u98ce\u683c API \u90fd\u662f\u8fd9\u6837\u3002 MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b \u590d\u73b0\u6761\u4ef6\uff1a Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e3a\u4e2d\u6587 (GBK)\u3002 \u4f7f\u7528 MSVC \u7684 /utf-8 \u9009\u9879\u7f16\u8bd1\u3002 #include int main() { MessageBoxA(nullptr, \"\u6211\u7231\ud883\udede\ud883\udede\u9762\", \"\u6807\u9898\", MB_OK); // \u4f1a\u53d8\u6210\u4e71\u7801 return 0; } Linux \u7528\u6237\uff1a iconv \u5982\u679c\u4f60\u662f Linux \u7528\u6237\uff0c\u4e14\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u53ef\u4ee5\u4f7f\u7528 C \u8bed\u8a00\u7684 iconv \u5e93\u3002 iconv \u4e5f\u6709 Windows \u7684\u7248\u672c\uff0c\u4f46\u5b89\u88c5\u6bd4\u8f83\u56f0\u96be\u3002\u5982\u679c\u4f60\u8fde iconv \u90fd\u641e\u5f97\u5b9a\uff0c\u6ca1\u7406\u7531 Boost \u641e\u4e0d\u5b9a\u3002 #include #include std::string convert(std::string const &s, char const *from, char const *to) { iconv_t cd = iconv_open(to, from); if (cd == (iconv_t)-1) { throw std::runtime_error(\"iconv_open failed\"); } auto in = s.data(); auto inbytesleft = s.size(); size_t outbytesleft = inbytesleft * 4; std::string buffer(outbytesleft, 0); auto out = buffer.data(); iconv(cd, &in, &inbytesleft, &out, &outbytesleft); iconv_close(cd); buffer.resize(buffer.size() - outbytesleft); return buffer; } // \u4e3e\u4f8b\uff1aUTF-8 \u8f6c GBK std::string utf8_to_gbk(std::string const &s) { return convert(s, \"UTF-8\", \"GBK\"); } // \u4e3e\u4f8b\uff1aGBK \u8f6c UTF-8 std::string gbk_to_utf8(std::string const &s) { return convert(s, \"GBK\", \"UTF-8\"); } iconv \u547d\u4ee4\u884c\u5de5\u5177 iconv \u4e0d\u4ec5\u662f\u4e00\u4e2a\u5e93\uff0c\u4e5f\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5927\u591a Linux \u53d1\u884c\u7248\u90fd\u81ea\u5e26\u4e86\uff09\u3002\u7528\u6cd5\u5982\u4e0b\uff1a iconv -f \u6765\u81ea\u4ec0\u4e48\u7f16\u7801 -t \u5230\u4ec0\u4e48\u7f16\u7801 (\u8f93\u5165\u6587\u4ef6\u540d...) > \u8f93\u51fa\u6587\u4ef6\u540d \u5982\u4e0d\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u9ed8\u8ba4\u4ece\u7ec8\u7aef\u8f93\u5165\u6d41\u8bfb\u53d6\u3002 \u5982\u4e0d\u4f7f\u7528 > \u8f93\u51fa\u6587\u4ef6\u540d \u91cd\u5b9a\u5411\u8f93\u51fa\uff0c\u5219\u9ed8\u8ba4\u8f93\u51fa\u5230\u7ec8\u7aef\u3002 \u53ef\u4ee5\u7528 echo \u914d\u5408\u7ba1\u9053\u6765\u521b\u5efa\u8f93\u5165\u6d41\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 \u6b64\u5904\u663e\u793a\u4e71\u7801\u662f\u56e0\u4e3a\u6211\u7684\u7ec8\u7aef\u662f UTF-8 \u683c\u5f0f\uff0c\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 iconv \u8f93\u51fa\u7684 GBK \u683c\u5f0f\u6570\u636e\u3002 \u628a\u201c\u6211\u7231\u5c0f\u5f6d\u8001\u5e08\u201d\u8f6c\u6362\u4e3a GBK \u683c\u5f0f\u5199\u5165 gbk.txt \uff0c\u7136\u540e\u518d\u91cd\u65b0\u8fd8\u539f\u56de UTF-8 \u683c\u5f0f\u67e5\u770b\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK > gbk.txt $ cat gbk.txt \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 $ iconv -f GBK -t UTF-8 gbk.txt \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 Windows \u53ef\u80fd\u4e5f\u6709\u7c7b\u4f3c\u7684\u5de5\u5177\uff0c\u6bd4\u5982 iconv.exe \uff0c\u4f46\u6211\u6ca1\u627e\u5230\u3002 \u672c\u5730\u5316 (locale) \u672c\u5730\u5316\u662f\u6307\u6839\u636e\u7528\u6237\u7684\u8bed\u8a00\u3001\u5730\u533a\u7b49\u73af\u5883\uff0c\u663e\u793a\u4e0d\u540c\u7684\u754c\u9762\u3002\u6bd4\u5982\u8bf4\uff0c\u540c\u6837\u662f\u6587\u4ef6\u83dc\u5355\uff0c\u4e2d\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201c\u6587\u4ef6\u201d\u3001\u82f1\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201cFile\u201d\u3002 \u533a\u5206\u5b57\u7b26\u7c7b\u578b C \u8bed\u8a00\u63d0\u4f9b\u4e86 \u5934\u6587\u4ef6\uff0c\u91cc\u9762\u5c01\u88c5\u4e86\u5927\u91cf\u5f62\u5982 isspace \u3001 isdigit \u8fd9\u6837\u7684\u5224\u65ad\u5b57\u7b26\u5206\u7c7b\u7684\u51fd\u6570\u3002 #include C++ \u5bf9\u5176\u5b9e\u65bd\u4e86\u518d\u5c01\u88c5\uff0c\u6539\u540d\u4e3a \u3002\u82e5\u4f60\u5bfc\u5165\u7684\u662f\u8be5\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e26\u6709 std \u540d\u5b57\u7a7a\u95f4\u524d\u7f00\u7684\u65b9\u5f0f std::isspace \uff0c std::isdigit \u8bbf\u95ee\u4e86\uff0c\u770b\u8d77\u6765\u66f4\u52a0\u4e13\u4e1a\uff08\u786e\u4fe1\uff09\u3002 #include \u51fd\u6570\u6e05\u5355\uff1a \u51fd\u6570\u540d\u79f0 \u5224\u65ad\u7684\u5b57\u7b26\u7c7b\u578b isascii 0 \u5230 0x7F \u7684\u6240\u6709 ASCII \u5b57\u7b26 isalpha \u5927\u5c0f\u5199\u5b57\u6bcd A-Z a-z isupper \u5927\u5199\u5b57\u6bcd A-Z islower \u5c0f\u5199\u5b57\u6bcd a-z isdigit \u6570\u5b57 0-9 isxdigit \u5341\u516d\u8fdb\u5236\u6570\u5b57 A-F a-f 0-9 isprint \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5305\u62ec\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u6807\u70b9\u7b49 isgraph \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7a7a\u683c iscntrl \u63a7\u5236\u5b57\u7b26\uff0c\u9664\u53ef\u6253\u5370\u5b57\u7b26\u5916\u7684\u5168\u90e8 isspace \u7a7a\u767d\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u6362\u884c\u3001\u56de\u8f66\u3001\u5236\u8868\u7b26\u7b49 ispunct \u6807\u70b9\u7b26\u53f7 isalnum \u5b57\u6bcd\u6216\u6570\u5b57 \u66f4\u8be6\u7ec6\u7684\u8868\u683c\u53ef\u4ee5\u770b\uff1a https://en.cppreference.com/w/cpp/string/byte/isspace \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b TODO \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale TODO \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 TODO \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u72ed\u4e49\u7684\u6c49\u5b57\uff1a0x4E00 \u5230 0x9FA5\uff08\u201c\u4e00\u201d\u5230\u201c\u9fa5\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\uff1a0x2E80 \u5230 0x9FFF\uff08\u201c\u2e80\u201d\u5230\u201c\u9fff\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\u5305\u542b\u4e86\u51e0\u4e4e\u6240\u6709\u4e2d\u65e5\u97e9\u4f7f\u7528\u7684\u6c49\u5b57\u5b57\u7b26\uff0c\u800c\u72ed\u4e49\u7684\u6c49\u5b57\u53ea\u662f\u4e2d\u6587\u91cc\u6700\u5e38\u7528\u7684\u4e00\u90e8\u5206\u3002 TODO \u5bbd\u5b57\u7b26\u6d41 \u4e4b\u6240\u4ee5\u628a\u5bbd\u5b57\u7b26\u6d41\u653e\u5230\u6700\u540e\uff0c\u662f\u56e0\u4e3a\uff0c\u9996\u5148 iostream \u672c\u6765\u5c31\u662f\u4e00\u4e2a\u5931\u8d25\u7684\u8bbe\u8ba1\u3002 \u5c0f\u5f6d\u8001\u5e08\u5728\u672c\u4e66\u5f00\u5934\u5c31\u591a\u6b21\u5f3a\u8c03\u8fc7\u4ed6\u662f format \u5b5d\u5b50\u3002 \u800c\u5bbd\u5b57\u7b26 wchar_t \u672c\u8eab\u5c31\u5145\u65a5\u7740\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff08\u4f8b\u5982 Windows \u88ab UTF-16 \u80cc\u523a\uff09\u3002 \u73b0\u5728 iostream \u4e0e wchar_t \u4e00\u8d77\u51fa\u73b0\u5728\u6211\u9762\u524d\uff0c\u4e0d\u80fd\u8bf4\u662f\u68a6\u5e7b\u8054\u52a8\u5427\uff0c\u81f3\u5c11\u4e5f\u53ef\u4ee5\u8bf4\u662f\u7b54\u8fa9\u8d85\u4eba\u4e86\u3002 \u603b\u4e4b\uff0c\u6211\u4e2a\u4eba\u8fd8\u662f\u63a8\u8350\u7a0b\u5e8f\u5185\u90e8\u4ee5 UTF-8\uff08 char8_t \uff09\u6216 UTF-32\uff08 char32_t \uff09\u7684\u5b57\u7b26\u4e32\u6765\u5904\u7406\u4e07\u7269\u3002 UTF-8 \u6216 UTF-32 \u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u4e2d\u6587\u5904\u7406\u9700\u6c42\u662f\u5426\u65fa\u76db\uff0c\u662f\u5426\u5728\u4e4e\u7a7a\u95f4\uff0c\u662f\u5426\u9700\u8981\u5207\u7247\u548c\u7d22\u5f15\u7b49\u3002 \u5f53\u9700\u8981\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u518d\u7528 boost::locale \u3001 utfcpp \u7b49\u5de5\u5177\u8f6c\u6362\u6210 ANSI\uff08 char \uff09\u6216 UTF-16\uff08 wchar_t \uff09\u3002 \u5bf9\u4e8e Linux \u7528\u6237\uff0c\u4e5f\u53ef\u4ee5\u68c0\u6d4b\u5982\u679c\u662f Linux \u7cfb\u7edf\uff0c\u5219\u4ec0\u4e48\u8f6c\u6362\u90fd\u4e0d\u505a\uff0c\u56e0\u4e3a Linux \u7528\u6237\u51e0\u4e4e\u90fd\u662f UTF-8\uff0c\u90a3\u4e48 const char8_t * \u53ef\u4ee5\u5f3a\u8f6c\u4e3a const char * \u800c\u4e0d\u7528\u4efb\u4f55\u989d\u5916\u5f00\u9500\u3002 std::string to_os_string(std::string const &u8s) { #if _WIN32 // UTF-8 \u5230 ANSI return boost::locale::conv::from_utf(u8s, \"\"); #elif __linux__ // \u4e0d\u8f6c\u6362 return u8s; #else #error \"Unsupported system.\" #endif } \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u5b9e\u5728\u8981\u5b66\u7cdf\u7cd5\u7684\u5bbd\u5b57\u7b26\u6d41\uff0c\u90a3\u6211\u4e5f\u5949\u966a\u5230\u5e95\u3002 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring \u5728\u4ed6\u4eec\u770b\u6765\uff0c std::string \u662f\u5df2\u7ecf\u5e9f\u5f03\u7684\u3002\u4ed6\u4eec\u8ba4\u4e3a std::wstring \u624d\u662f\u771f\u6b63\u8de8\u5e73\u53f0\uff0c\u8de8\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u3002 std::wstring : \u5b57\u7b26\u4e32 std::string : \u5b57\u8282\u6570\u7ec4 std::wifstream : \u6587\u672c\u6d41 std::ifstream : \u4e8c\u8fdb\u5236\u6d41 \u770b\u8d77\u6765\u53ea\u8981\u5168\u90e8\u7edf\u4e00 wchar_t \u5c31\u80fd\u5b9e\u73b0\u8de8\u5e73\u53f0\u4e86\uff1f\u662f\u7684\uff0c\u9664\u4e86 Windows\u2026\u2026 \u6807\u51c6\u8ba4\u4e3a wchar_t \u5e94\u8be5\u5305\u542b 0 \u5230 0x10FFFF \u7684\u6240\u6709\u7684 Unicode \u5b57\u7b26\u7801\u70b9\uff0c\u9700\u8981\u662f 32 \u4f4d\u7684\u3002\u7136\u800c Windows \u7684 wchar_t \u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u662f 16 \u4f4d\u7684\uff0c\u9700\u8981\u7528\u4ee3\u7406\u5bf9\u624d\u80fd\u8868\u793a\u7a00\u6709\u5b57\u7b26\uff0c\u5e76\u4e0d\u80fd\u4e00\u4e2a wchar_t \u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002\u8fd9\u5bfc\u81f4\u5373\u4f7f\u7528\u4e86 wchar_t \u8fd8\u662f\u5b58\u5728\u8de8\u5e73\u53f0\u56f0\u96be\u7684\u95ee\u9898\uff1a\u4e00\u4e2a Linux \u7a0b\u5e8f\u7528 wchar_t \u53ef\u80fd\u4f1a\u5229\u7528 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u7279\u6027\uff0c\u65b9\u4fbf\u4e86\u6587\u672c\u5904\u7406\uff0c\u4f46\u79fb\u690d\u5230 Windows \u65f6\uff0c\u53d1\u73b0\u53d8\u6210\u4e86 UTF-16\uff0c\u9700\u8981\u5bf9\u4ee3\u7406\u5bf9\u505a\u7279\u6b8a\u5224\u65ad\u2026\u2026\u6ca1\u6709\u6ee1\u8db3\u8de8\u5e73\u53f0\u7684\u521d\u8877\uff0c\u4e5f\u505a\u4e0d\u5230\u5b9a\u957f\u7f16\u7801\u3002 char32_t \u505a\u5230\u4e86\u8de8\u5e73\u53f0\u7684 UTF-32\uff0c\u4e5f\u80fd\u5bb9\u7eb3\u5168\u90e8 Unicode \u7801\u70b9\uff0c\u53ef\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::to_wstring \uff0c\u5374\u6839\u672c\u6ca1\u6709 std::to_u32string \uff1b\u63d0\u4f9b\u4e86 std::wcout \uff0c\u5374\u6ca1\u6709\u63d0\u4f9b std::u32cout \u2026\u2026 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5bbd\u5b57\u7b26\u6d41\u5f88\u7cdf\u7cd5\uff0c\u8bf4\u662f\u8de8\u5e73\u53f0\uff0c\u8de8\u4e86\u4e2a\u5bc2\u5bde\u3002 wchar_t \u7cfb\u5217\u51fd\u6570 TODO std::wcout \u7684\u4f7f\u7528 TODO std::wfstream \u7684\u4f7f\u7528 TODO C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 TODO C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 TODO Windows \u4e13\u9898 \u9047\u5230\u5b57\u7b26\u7f16\u7801\u96be\u9898\u7684\uff0c\u4e3b\u8981\u662f Windows \u7a0b\u5e8f\u5458\u3002 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 \u4ece Windows NT \u7248\u672c\u5f00\u59cb\uff0c\u5bf9\u4e8e\u6240\u6709\u6d89\u53ca\u5b57\u7b26\u4e32\u7684\uff0c\u5176\u64cd\u4f5c\u7cfb\u7edf API \u63d0\u4f9b\u4e86\u4e24\u5957\u51fd\u6570\u3002 \u4e00\u5957\u662f A \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 A \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileA \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 ANSI\uff08\u5373 GBK\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u53e6\u4e00\u5957\u662f W \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 W \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileW \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 Unicode\uff08\u5373 UTF-16\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u5176\u4e2d CreateFileW \u624d\u662f Windows \u7cfb\u7edf\u771f\u6b63\u7684 API\u3002 \u800c CreateFileA \u662f\u4e3a\u4e86\u517c\u5bb9\u57fa\u4e8e ANSI \u7684\u8001\u7a0b\u5e8f. \u7531\u4e8e ANSI \u5728\u4e0d\u540c\u5730\u533a\u4f1a\u53d8\u5f97\u4e0d\u540c\uff0c\u4f7f\u7528\u8fd9\u7c7b\u51fd\u6570\u5199\u51fa\u7684\u7a0b\u5e8f\u4e0d\u5177\u6709\u56fd\u9645\u901a\u7528\u6027\u3002 \u5176\u5185\u90e8\u7684\u5b9e\u73b0\u53ea\u662f\u7b80\u7b80\u5355\u5355\u5730\u7ed9 const char * \u505a\u4e2a\u8f6c\u6362\uff0c\u4ece GBK \u8f6c\u5230 UTF-16\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528 CreateFileW\u3002 HANDLE CreateFileA(const char *lpFileName) { return CreateFileW(gbk_to_utf16(lpFileName)); } TCHAR \u6d41\u6d3e \u9664\u4e86\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b8f\uff0c\u8fd9\u4e2a\u5b8f\u6ca1\u6709\u4efb\u4f55\u540e\u7f00\uff0c\u4f8b\u5982 CreateFile \u3002 \u5176\u5b9a\u4e49\u5982\u4e0b\uff1a #ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif \u8fd9\u6837\u505a\u7684\u521d\u8877\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u53ef\u4ee5\u5199\u51fa\u4e00\u5957\u9488\u5bf9 MessageBox \u7684\u4ee3\u7801\u3002 \u5f53\u8001\u677f\u60f3\u8981\u57fa\u4e8e Unicode \u65f6\uff0c\u4ed6\u5c31 #define UNICODE \uff0c\u8fd9\u6837 MessageBox \u5c31\u53d8\u6210\u4e86 MessageBoxW \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u53d8\u6210 Unicode \u7684\uff0c\u56fd\u9645\u901a\u7528\u3002 \u5f53\u52b3\u4fdd\u8001\u677f\u60f3\u8981\u57fa\u4e8e ANSI \u65f6\uff0c\u4ed6\u5c31\u4e0d\u5b9a\u4e49 UNICODE \u5b8f\uff0c\u8fd9\u6837\u6240\u6709\u7684 MessageBox \u53c8\u53d8\u56de\u4e86 MessageBoxA \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u53c8\u53d8\u6210 ANSI \u7684\u4e86\u3002 \u6240\u6709\u6709 A/W \u533a\u5206\u7684\u7684 Windows API \u90fd\u6709\u8fd9\u6837\u4e00\u4e2a\u5b8f\uff0c\u6839\u636e UNICODE \u5b8f\u662f\u5426\u5b9a\u4e49\uff0c\u51b3\u5b9a\u91c7\u53d6\u54ea\u5957 API\u3002 \u6211\u4eec\u4f1f\u5927\u7684 Linux \u7cfb\u7edf\u5c31\u6ca1\u6709\u8fd9\u4e2a\u82e6\u607c\uff0c\u65e9\u5c31\u7edf\u4e00 UTF-8 \u4e86\u3002 \u9664\u6b64\u4e4b\u5916\uff0c \u4e2d\u8fd8\u5b9a\u4e49\u4e86 TEXT \u8fd9\u4e2a\u5b8f\u51fd\u6570\u3002 #ifdef UNICODE #define TEXT(s) L##s #else #define TEXT(s) s #endif \u7528\u6cd5\uff1a\u8981\u6c42\u7a0b\u5e8f\u5458\u628a\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u90fd\u7528 TEXT \u5b8f\u5305\u88f9\u3002 Qt \u4e5f\u6709\u7c7b\u4f3c\u7684\u5b8f\u5305\u88f9\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u505a\u6cd5\uff0c tr \uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u89e3\u51b3\u7f16\u7801\u95ee\u9898\uff0c\u800c\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u8bed\u8a00\u7ffb\u8bd1\u95ee\u9898\uff0c\u7a0d\u540e\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u4e00\u4e0b Qt \u4e2d\u7684\u5b57\u7b26\u4e32\u3002 \u5f53 UNICODE \u5b8f\u5b9a\u4e49\u65f6\uff0c TEXT \u4f1a\u81ea\u52a8\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u6dfb\u52a0 L \u524d\u7f00\uff0c\u4f7f\u5f97\u5b57\u7b26\u4e32\u53d8\u6210 Unicode \u7684\u3002\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\uff0c\u5219\u53c8\u53d8\u56de ANSI \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff08\u8ddf\u968f\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\u7684\u8bbe\u5b9a\uff09\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u6bb5\u4ee3\u7801\uff1a #include int main() { MessageBox(NULL, TEXT(\"\u4f60\u597d\uff0c\u4e16\u754c\"), TEXT(\"\u6807\u9898\"), MB_OK); } \u5f53\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u6807\u9898\", MB_OK); } \u5f53\u6ca1\u6709\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u6807\u9898\", MB_OK); } \u6b64\u5916\uff0c\u8fd8\u5b9a\u4e49\u4e86 TCHAR \u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u540c\u6837\u662f\u9488\u5bf9\u662f\u5426\u5b9a\u4e49 UNICODE \u5b8f\u800c\u5b9a\u4e49\u4e86\u4e24\u5957\u7248\u672c\u3002 #ifdef UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif \u8fd8\u4e3a printf \u548c wprintf \u5b9a\u4e49\u4e86 TCHAR \u7248\u672c\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709 strlen \u548c wcslen \uff0c strcpy \u548c wcscpy \uff0c\u7b49\u7b49\u3002 #ifdef UNICODE #define _tprintf wprintf #define _tcscpy wcscpy #define _tcslen wcslen #else #define _tprintf printf #define _tcscpy strcpy #define _tcslen strlen #endif \u4e0d\u89c9\u5f97\u8fd9\u5f88\u9177\u5417\uff1f\u5f88\u7b26\u5408\u6211\u5bf9\u5f3a\u8feb\u75c7\u7684\u60f3\u8c61\uff0c\u79d1\u6280\u5e76\u4e14\u5e26\u7740\u81ed\u5473\u3002 int main() { TCHAR str[] = TEXT(\"\u6bd4\u5c14\u76d6\u5b50\u6211\u6d4b\u8bd5\u4f60\u7684\u7801\"); } (* \u54e6\uff0c\u6211\u662f\u8bf4\uff0c\u6211\u8981\u6d4b\u8bd5\u4f60\u7684\u7f16\u7801\u683c\u5f0f *) \u9700\u8981\u5207\u6362\u65f6\uff0c\u5728 MSVC \u4e2d\uff0c\u6253\u5f00\u6216\u5173\u95ed /DUNICODE \u7f16\u8bd1\u9009\u9879\u5373\u53ef\u3002 \u4e0d\u8981\u89c9\u5f97\u8fd9\u662f\u4ec0\u4e48\u597d\u4e3b\u610f\uff0c\u8fd9\u6837\u505a\u7684\u540e\u679c\u662f\uff0c\u4f60\u5199\u51fa\u7684\u4ee3\u7801\u53ea\u80fd\u5728 Windows \u4e0b\u7f16\u8bd1\u3002 \u5199\u8d77\u6765\u7d2f\u6b7b\u4eba\uff0c\u5b9e\u9645\u54ea\u6709\u90a3\u4e48\u591a\u4e00\u76f4\u5728 ANSI \u548c Unicode \u4e4b\u95f4\u6765\u56de\u5207\u6362\u7684\u9700\u6c42\uff1f \u6211\u7684\u5efa\u8bae\u662f\uff0c\u7edf\u4e00 wchar_t \uff0c\u7edf\u4e00\u5168\u7528 W \u51fd\u6570\uff0c\u618b\u62a0\u62a0\u7d22\u7d22\u7684\u534a\u8fdb\u534a\u9000\u3002 UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f \u4e4b\u524d\u8bf4\u8fc7\u4e86\uff0cWindows \u5e73\u53f0\u5230\u5904\u90fd\u9ed8\u8ba4 GBK \u975e\u5e38\u9ebb\u70e6\uff0c\u8981\u5207\u6362\u5230 UTF-8 \u5de5\u4f5c\u6d41\uff1a \u7f16\u8bd1\u5668\u5f00\u542f /utf-8 \u9009\u9879 \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c system(\"chcp 65001\") \u8bbe\u7f6e\u6587\u4ef6\u7cfb\u7edf\u5b57\u7b26\u4e32\u7f16\u7801\uff0c setlocale(LC_ALL, \".utf-8\") // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 SetConsoleCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u5165\u7f16\u7801\uff0c\u7528\u4e8e std::cin #elif __unix__ // \u53cd\u6b63 Unix \u7cfb\u7edf\u9ed8\u8ba4\u90fd\u662f UTF-8\uff0c\u4e0d\u8bbe\u7f6e\u4e5f\u884c\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u5168\u5c40 locale \u662f\u4e3a\u4e86\u8ba9 iswspace \u63a5\u53d7\u5168\u89d2\u7a7a\u683c\u3001iswpunct \u63a5\u53d7\u5168\u89d2\u9017\u53f7 L'\uff0c' \u7b49 //setlocale(LC_ALL, \"zh_CN.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u4e2d\u6587\u672c\u5730\u5316\uff0c\u53ef\u4f7f strerror \u8f93\u51fa\u4e2d\u6587\uff08\u4f46\u7528\u6237\u5fc5\u987b locale-gen \u8fc7\u4e2d\u6587\uff01\uff09 //setlocale(LC_ALL, \"C.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u8bed\u8a00\u4e2d\u6027 locale\uff0c\u53ea\u5f71\u54cd iswspace\u3001iswpunct \u7b49\u51fd\u6570\uff0c\u4e0d\u4f1a\u4f7f strerror \u7b49\u8f93\u51fa\u4e2d\u6587 setlocale(LC_ALL, \".utf-8\"); // \u82e5\u4e0d\u5e26\u4efb\u4f55\u524d\u7f00\uff08\u63a8\u8350\uff09\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8bed\u8a00 $LANG\uff0c\u4f7f strerror \u81ea\u52a8\u9002\u5e94 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 std::wcout << L\"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u4f60\u90fd\u7edf\u4e00 UTF-8 \u4e86\uff0c\u8fd9\u7834 UTF-16 \u548c UTF-32 \u4e4b\u95f4\u6765\u56de\u8df3\u7684\u7834 wchar_t \u5c31\u522b\u7528\u4e86\u5457\uff01 return 0; } WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == false \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::cout << char(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f GBK \u7f16\u7801\u7684 char \u5e8f\u5217 // \u5982\u679c\u662f\u4e2d\u6587\u5b57\u7b26\uff0cWndProc(WM_CHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u5b57\u8282\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u5df1\u5224\u65ad\u548c\u62fc\u63a5 GBK \u5b57\u7b26\u4e32 return 0; case WM_UNICHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == true \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::wcout << wchar_t(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f UTF-16 \u7f16\u7801\u7684 wchar_t \u5e8f\u5217 // \u5982\u679c\u662f\u4ee3\u7406\u5bf9\uff0cWndProc(WM_UNICHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u7801\u4f4d\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u884c\u628a\u4ee3\u7406\u5bf9\u7ec4\u88c5\u6210\u5b8c\u6574\u7684 Unicode \u7801\u70b9 return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } } \u628a WndProc \u7684\u8f93\u5165\u5b58\u5165 std::u32string \u7684\u6848\u4f8b\uff1a std::u32string result; std::string ansi_buf; std::wstring utf16_buf; std::optional try_ansi_to_utf32(std::string const &s) { try { return boost::locale::conv::to_utf(s, \"\"); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } std::optional try_utf16_to_utf32(std::wstring const &s) { try { return boost::locale::conv::utf_to_utf(s); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: ansi_buf.push_back(char(wParam)); if (auto u = try_ansi_to_utf32(ansi_buf)) { result += u.value(); ansi_buf.clear(); } return 0; case WM_UNICHAR: utf16_buf.push_back(wchar_t(wParam)); if (auto u = try_utf16_to_utf32(utf16_buf)) { result += u.value(); utf16_buf.clear(); } return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); }; \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 \u6807\u51c6\u5e93\u7684 std::string \u6211\u4eec\u4e0d\u518d\u8d58\u8ff0\uff0c\u521a\u624d\u5728\u201c\u5bbd\u5b57\u7b26\u6d41\u201d\u4e2d\u4e5f\u4ecb\u7ecd\u4e86\u5b98\u65b9\u7684\u60f3\u6cd5\uff0c\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u4e13\u9898\u8bfe\u4f1a\u7ee7\u7eed\u8be6\u89e3\u3002 \u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u5173\u4e8e\u5b57\u7b26\u7f16\u7801\u7684\u95ee\u9898\uff0c\u63a2\u7d22\u5404\u5927\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u548c\u5e93\uff0c\u662f\u5982\u4f55\u5c01\u88c5\u5b57\u7b26\u4e32\u7c7b\uff0c\u5982\u4f55\u89e3\u51b3 UTF-8 \u53d8\u957f\u7f16\u7801\uff0cUTF-32 \u538b\u7f29\u7387\u4f4e\u7684\u95ee\u9898\u7684\uff0c\u5e0c\u671b\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u63d0\u4f9b\u4e00\u70b9\u7075\u611f\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u4e00\u4e2a\u597d\u7684\u5e93\u6216\u8bed\u8a00\uff0c\u90fd\u8981\u660e\u786e\u533a\u5206\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u6982\u5ff5\uff0c\u524d\u8005\u662f\u6587\u672c\u5185\u5bb9\uff0c\u540e\u8005\u662f\u7eaf\u4e8c\u8fdb\u5236\u5185\u5bb9\u3002 \u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u201c\u7f16\u7801\u201d\u5f97\u5230\u7eaf\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u800c\u5b57\u8282\u6570\u7ec4\u53ef\u4ee5\u201c\u89e3\u7801\u201d\u5f97\u5230\u539f\u59cb\u5b57\u7b26\u4e32\u3002 \u65e9\u671f\u7684 C \u8bed\u8a00\u5c31\u662f\u56e0\u4e3a\u628a\u5b57\u7b26\u548c\u5b57\u8282\u6df7\u4e3a\u4e00\u8c08\uff0c\u90fd\u4f7f\u7528\u4e86 char \u7c7b\u578b\uff0c\u624d\u4ea7\u751f\u4e86\u540e\u6765\u8fd9\u4e48\u591a\u4e71\u8c61\u3002\u540e\u6765\u901a\u8fc7\u6253\u8865\u4e01\u6253\u4e0a\u771f\u6b63\u7684\u5b57\u7b26 wchar_t \uff0c\u5374\u6ca1\u4ec0\u4e48\u4eba\u7528\uff0c\u800c\u4e14\u8fd8\u88ab Windows \u641e\u6210 16 \u4f4d\uff0c\u53cd\u800c\u4e0d\u8de8\u5e73\u53f0\u4e86\u3002 \u6b64\u5904\u5148\u5217\u4e00\u4e2a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u773c\u4e2d\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u8868\uff0c\u65b9\u4fbf\u4f60\u7406\u89e3\u3002 \u8bed\u8a00 \u5b57\u7b26 \u5b57\u7b26\u4e32 \u6587\u672c\u6d41 \u5b57\u8282\u6570\u7ec4 \u4e8c\u8fdb\u5236\u6d41 \u7f16\u7801/\u89e3\u7801 C char wchar_t * FILE * + fgetwc char * FILE * + fgetc wcstomb / mbstowc C++ wchar_t std::wstring std::wistream std::string / std::vector std::istream std::codecvt Qt QChar QString QTextStream QByteArray QDataStream QTextCodec Python3 str str open('r') bytes open('rb') str.encode() Python2 unicode unicode \u65e0 str open('r') unicode.encode() Java Character String Reader byte[] InputStream Charset.encode C# char string StreamReader byte[] Stream Encoding Rust char String BufRead u8 Read str::from_utf8 JS char String ReadableStream Uint8Array ReadableStream TextEncoder Go rune string Reader byte Reader utf8.DecodeRune PHP string string fopen string fopen mb_convert_encoding Swift Character String String.UnicodeScalarView UInt8 Data String.Encoding Kotlin Char String Reader ByteArray InputStream Charset.encode Obj-C unichar NSString NSInputStream uint8_t NSInputStream NSStringEncoding Lua integer table \u65e0 string io.open require'utf8' \u672c\u8bfe\u4e0d\u4f1a\u8bb2\u89e3\u8fd9\u4e9b\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u5177\u4f53\u7528\u6cd5\uff0c\u53ea\u63d0\u4f9b\u4e00\u4e9b\u6982\u5ff5\uff0c\u8ba9\u4f60\u77e5\u9053\u5927\u5bb6\u90fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u89e6\u7c7b\u65c1\u901a\u3002 Lua \u4e2d UTF-32 \u7684\u5b9a\u957f\u7f16\u7801\uff0c\u8981\u8fd9\u6837\u5b9e\u73b0\uff1a {80, 101, 110, 103} \u3002\u800c\u4ed6\u6240\u8c13\u7684 utf8 \u5e93\uff0c\u5c31\u662f\u8d1f\u8d23\u628a Lua \u81ea\u5df1\u7684 string \u5047\u8bbe\u4e3a utf8 \u7f16\u7801\uff0c\u89e3\u7801\u51fa\u4e00\u4e2a\u4e2a Unicode \u7801\u70b9\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u7ec4\u3002\u4ed6\u751a\u81f3\u4e0d\u662f Lua \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u662f\u4e2a\u7b2c\u4e09\u65b9\u7684\u5e93\uff0c\u8fd8\u662f\u9700\u8981\u7f16\u8bd1\u7684 C \u8bed\u8a00 .so \u6587\u4ef6\u3002\u8fd8\u6709 Lua \u5b5d\u5b50\u501f\u6b64\u786c\u8bf4\u6211\u4eec Lua \u662f\u5929\u751f UTF-8\uff01\u7136\u800c\u62ff\u7740\u4e2a UTF-8 \u7f16\u7801\u7684\u201c\u5b57\u8282\u6570\u7ec4\u201d string \u6765\u6253\u5f00 io.open \u6587\u4ef6\uff0c\u5c31\u4f1a\u62a5\u9519\u627e\u4e0d\u5230\u6587\u4ef6\uff08\u56e0\u4e3a\u4e2d\u56fd\u533a Windows \u7684 GBK\uff09\uff0c\u662f\u4e0d\u662f\u5f88\u597d\u7b11\u5462\uff1f Qt QString Qt \u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 Qt \u7684\u5b57\u8282\u6570\u7ec4\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 \u5b83\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u9645\u4e0a\u662f\u4e2a QChar \u6570\u7ec4\uff0c\u800c QChar \u662f unsigned short \uff0c\u5373 16 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u4e5f\u5c31\u662f UTF-16 \u7f16\u7801\u7684\u7801\u4f4d\u3002 QString str = \"\u4f60\u597d\uff0c\u4e16\u754c\"; // str.size() = 5 // str[0] = QChar(0x4f60) = u'\u4f60' // str[1] = QChar(0x597d) = u'\u597d' // str[2] = QChar(0xff0c) = u'\uff0c' // str[3] = QChar(0x4e16) = u'\u4e16' // str[4] = QChar(0x754c) = u'\u754c' \u53ef\u89c1\uff0c QString \u662f UTF-16 \u7f16\u7801\u7684\uff0c\u5c31\u548c Java \u4e00\u6837\uff0cQt \u4e5f\u662f UTF-16 \u6f6e\u7684\u53d7\u5bb3\u8005\u3002 \u6240\u4ee5\uff0cQString \u4e5f\u5b58\u5728\u7740\u4ee3\u7406\u5bf9\u53d8\u957f\u7f16\u7801\u7684\u95ee\u9898\u3002\u4f46\u81f3\u5c11\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5b57\u7b26\u6765\u8bf4\uff0c\u4e00\u4e2a 16 \u4f4d\u7684 QChar \u90fd\u5bb9\u7eb3\u7684\u4e0b\u4e86\u3002 QTextCodec Qt \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7f16\u7801\u8f6c\u6362\u51fd\u6570\uff0c\u7528\u4e8e\u5c06 QString \u8f6c\u6362\u6210 QByteArray \uff0c\u6216\u8005\u4ece QByteArray \u8f6c\u6362\u6210 QString \uff0c\u8fd9\u4e9b\u51fd\u6570\u540d\u90fd\u662f\u4ee5 to \u6216\u8005 from \u5f00\u5934\u7684\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u540d\uff0c\u4f8b\u5982 fromUtf8 \u3001 toUtf8 \u3001 toLocal8Bits \u3002 \u8fd9\u4e9b\u51fd\u6570\u7684\u5185\u90e8\uff0c\u90fd\u662f\u8c03\u7528 QTextCodec \u7c7b\u5b9e\u73b0\u7684\u8f6c\u6362\u3002 QTextCodec \u662f Qt \u7528\u4e8e\u5904\u7406\u5404\u79cd\u6587\u672c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u7684\u7c7b\u3002\u5b83\u7684\u9759\u6001\u65b9\u6cd5 codecForLocale \u8fd4\u56de\u4e86\u5f53\u524d\u7cfb\u7edf\u7684\u7f16\u7801\uff0c toUnicode \u548c fromUnicode \u5206\u522b\u662f\u5c06 QByteArray \u8f6c\u6362\u6210 QString \uff0c\u6216\u8005\u5c06 QString \u8f6c\u6362\u6210 QByteArray \u3002 QTextCodec *codec = QTextCodec::codecForLocale(); // \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7f16\u7801 QByteArray bytes = codec->fromUnicode(str); // \u5c06 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u5373 char[] QString str = codec->toUnicode(bytes); // \u5c06 QByteArray \u8f6c\u6362\u6210 QString QTextCodec \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u8f6c\u6362\u63a5\u53e3\uff0c\u4f8b\u5982 fromUnicode \u9664\u4e86\u63a5\u53d7 QString \uff0c\u8fd8\u63a5\u53d7 QChar \u6570\u7ec4\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f6c\u6362\u8303\u56f4\u3002 from/toLocal8Bits/Utf8/Latin1/Ascii \u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cQt \u5c01\u88c5\u4e86\u4e00\u4e9b\u5e38\u7528\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u8fd9\u6837\u4f60\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a QTextCodec\u3002\u90fd\u662f to \u548c from \u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u7684\u540d\u79f0\u3002 Local8Bits \u8868\u793a\u8fd0\u884c\u65f6\u68c0\u6d4b\u5230\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u5ba2\u6237\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 QByteArray bytes = str.toLocal8Bits(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801 QString::fromLocal8Bits(bytes); // \u518d\u4ece\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForLocale(); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Utf8 \u8868\u793a\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002 QByteArray bytes = str.toUtf8(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 UTF-8 \u7f16\u7801 QString::fromUtf8(bytes); // \u518d\u4ece UTF-8 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Latin1 \u8868\u793a ISO-8859-1 \u7f16\u7801\uff0c\u53c8\u79f0\u4e3a\u897f\u6b27\u7f16\u7801\uff0c\u5b83\u662f\u4e00\u4e2a\u5355\u5b57\u8282\u7f16\u7801\uff0c\u548c ASCII \u7f16\u7801\u76f8\u4f3c\uff0c\u4f46\u662f\u591a\u4e86 128-255 \u7684\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u6cd5\u8bed\u3001\u5fb7\u8bed\u3001\u897f\u73ed\u7259\u8bed\u3001\u8461\u8404\u7259\u8bed\u7b49\u5b57\u7b26\u3002 QByteArray bytes = str.toLatin1(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 Latin1 \u7f16\u7801 QString::fromLatin1(bytes); // \u518d\u4ece Latin1 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ISO-8859-1\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Ascii \u8868\u793a ASCII \u7f16\u7801\uff0c\u548c Latin1 \u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u65e0\u6cd5\u5904\u7406 128-255 \u8fd9\u4e00\u6bb5\u7684\u5b57\u7b26\u3002 QByteArray bytes = str.toAscii(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 ASCII \u7f16\u7801 QString::fromAscii(bytes); // \u518d\u4ece ASCII \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ASCII\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); \u5b57\u7b26\u4e32\u5e38\u91cf QString str = QStringLiterial(\"\u4f60\u597d\uff0c\u4e16\u754c\"); QStringLiterial \u53ef\u4ee5\u4fdd\u8bc1\uff0c\u8f6c\u6362\u65f6\u91c7\u7528\u7684\u662f\u6240\u8c13\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\uff08\u5b9e\u9645\u5e94\u8be5\u53eb\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\uff09\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5f00\u53d1\u8005\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u662f\u7f16\u8bd1\u671f\u786e\u5b9a\u7684\u3002\u800c\u5982\u679c\u5199 QString::fromLocal8Bits(\"\") \u5c31\u53d8\u6210 \u201cANSI\u201d\uff0c\u5ba2\u6237\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u4e86\u3002\u8fd9\u4e24\u4e2a\u5b57\u7b26\u7f16\u7801\uff0c\u6bd4\u5982\u5728\u4e4b\u524d\u8de8\u56fd galgame \u7684\u6848\u4f8b\u4e2d\uff0c\u5c31\u662f\u4e0d\u540c\u7684\u3002 QTextStream QTextStream \u662f Qt \u63d0\u4f9b\u7684\u6587\u672c\u6d41\u7c7b\uff08\u5e26\u6709\u7f13\u51b2\uff09\uff0c\u5b83\u53ef\u4ee5\u5c06\u6587\u672c\u5199\u5165\u5230\u6587\u4ef6\u3001\u5957\u63a5\u5b57\u3001\u6807\u51c6\u8f93\u51fa\u7b49\u8bbe\u5907\u3002 \u6587\u672c\u6d41\u548c\u4e8c\u8fdb\u5236\u6d41\u4e0d\u540c\uff0c\u4ed6\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u8fc7 QTextStream \u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u3002 \u5f53\u4f60\u5f80 QTextStream \u5199\u5165 QString \u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7f16\u7801\u683c\u5f0f\u81ea\u52a8\u8f6c\u6362\u4e3a QByteArray \uff0c\u7136\u540e\u624d\u5199\u5165\u3002 \u8bfb\u53d6\u65f6\u4e5f\u540c\u7406\uff0c\u4f1a\u628a\u8bfb\u5230\u7684 QByteArray \u901a\u8fc7\u7f16\u7801\u683c\u5f0f\u89e3\u7801\uff0c\u5f97\u5230 QString \u5b57\u7b26\u4e32\u3002 Qt \u503c\u5f97\u79f0\u9053\u7684\u4e00\u70b9\u662f\uff1a\u4ed6\u628a\u6587\u4ef6\u548c\u6587\u4ef6\u6d41\u533a\u5206\u5f00\u6765\uff0c\u6587\u4ef6 QFile \u53ea\u9700\u8981\u8d1f\u8d23\u6253\u5f00\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff1b\u800c\u6587\u672c\u6d41 QTextStream \u624d\u771f\u6b63\u8d1f\u8d23\u6570\u636e\u7684\u7f13\u51b2\uff0c\u89e3\u7801\u7b49\u64cd\u4f5c\u3002\u4f53\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u804c\u8d23\u5355\u4e00\u539f\u5219\u3002 QFile file(\"hello.txt\"); file.open(QIODevice::ReadWrite); QTextStream stream(&file); stream.setCodec(\"UTF-8\"); // \u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f stream << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u5199\u5165 QString\uff0cQTextStream \u4f1a\u81ea\u52a8\u5c06\u5176\u7528 UTF-8 \u7f16\u7801\u4e3a QByteArray \u540e\u5199\u5165 QFile \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u76f4\u63a5\u5199\u5165\u4e8c\u8fdb\u5236\u7684 QByteArray \uff0cQt \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a QDataStream \u7c7b\uff0c\u65b9\u4fbf\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002 \u5982\u6b64\u628a\u4e8c\u8fdb\u5236\u548c\u6587\u672c\u4e25\u683c\u533a\u5206\uff0c\u800c\u4e0d\u662f\u50cf\u53e4\u4ee3 C \u8bed\u8a00\u90a3\u6837\u5b57\u8282\u4e0e\u5b57\u7b26\u6df7\u6dc6\u4e0d\u6e05\uff0c\u5168\u7528\u7cdf\u7cd5\u7684 char \u6765\u8868\u793a\uff0c\u5145\u5206\u4f53\u73b0\u4e86\u5f3a\u7c7b\u578b\u7684\u5b89\u5168\u3002 Python 3 str \u5982\u4f55\u89e3\u51b3 UTF-32 \u5360\u7528\u7a7a\u95f4\u5927\u7684\u75db\u70b9\uff1fPython \u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u7edd\u5bf9\u503c\u5f97\u4e00\u770b\uff01 \u4e3a\u4e86\u65b9\u4fbf\u6587\u672c\u5904\u7406\uff0c\u6700\u597d\u4ee5\u5b9a\u957f\u7f16\u7801\uff0c\u4e5f\u5c31\u662f UTF-32\uff0c\u5b58\u50a8\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u7528 UTF-8 \u6216 UTF-16 \u6765\u5b58\u50a8\u7684\u8bdd\uff0c\u4f1a\u9047\u5230\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff1a \u4f8b\u5982\u50cf\u5b57\u7b26\u4e32\u7d22\u5f15\uff0c\u5b57\u7b26\u4e32\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\uff0c\u8981\u4e48\u7d22\u5f15\u51fa\u6765\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u4e86\uff1b\u8981\u4e48\u5c31\u9700\u8981 O(N) O(N) \u7684\u590d\u6742\u5ea6\uff0c\u9010\u4e00\u904d\u5386\u6bcf\u4e2a\u5b57\u8282\uff0c\u624d\u80fd\u786e\u5b9a\u771f\u6b63\u7684\u4f4d\u7f6e\uff1b\u54ea\u6015\u5168\u662f ASCII \u4e5f\u5f97\u8fd9\u4e48\u505a\uff0c\u56e0\u4e3a\u4e07\u4e00\u521a\u597d\u6709\u4e00\u4e2a\u662f\u4e2d\u6587\u5b57\u7b26\u5462\uff1f \u6240\u4ee5\uff0c\u5bf9\u4e8e\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684 Python \u6765\u8bf4\uff0cUTF-8 \u662f\u65e0\u6cd5\u63a5\u53d7\u7684\uff0c\u4f3c\u4e4e\u53ea\u80fd\u4ee5 UTF-32 \u6765\u5b58\u50a8\uff1f Python \u60f3\u5230\u4e86\u4e00\u4e2a\u5c0f\u5999\u62db\uff1a enum PyUnicodeType { PyUnicodeType_Latin1, PyUnicodeType_UCS2, PyUnicodeType_UCS4, }; struct PyUnicodeString { PyUnicodeType type; union { uint8_t *latin1; uint16_t *ucs2; uint32_t *ucs4; }; }; \u8fd9\u91cc\u7684 union \u662f\u4e00\u4e2a C \u8bed\u8a00\u7279\u6027\uff0c\u4ed6\u5141\u8bb8\u4f60\u628a\u591a\u4e2a\u6210\u5458\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u7a7a\u95f4\u3002\u901a\u5e38\u6765\u8bf4\u9700\u8981\u914d\u5408\u4e00\u4e2a enum \u8868\u793a\u7c7b\u578b\uff0c\u624d\u80fd\u5b89\u5168\u4f7f\u7528\u3002\u73b0\u4ee3 C++ \u7684 std::variant \u66f4\u5b89\u5168\u7684\u53d6\u4ee3\u4e86\u4ed6\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u5916\u6302\u4e00\u4e2a enum \u3002CPython \u89e3\u91ca\u5668\u56e0\u4e3a\u662f C \u8bed\u8a00\u5b9e\u73b0\uff0c\u53ea\u80fd\u7528 union \u6a21\u62df std::variant \u7684\u6548\u679c\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFF \u7684\u5b57\u7b26\uff08Latin-1 \u8303\u56f4\uff09\u65f6\uff0c\u5b9e\u9645\u4e0a\u6ca1\u5fc5\u8981\u5168\u7528\u8d85\u5927\u7684 uint32_t \u6765\u5b58\u50a8\u3002\u5b8c\u5168\u53ef\u4ee5\u53ea\u53d6\u51fa\u4f4e 8 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a\u66f4\u7d27\u51d1\u7684 uint8_t \u6570\u7ec4\uff0c\u5c31\u50cf Latin-1 \u7f16\u7801\u4e00\u6837\u3002 \u4f46\u662f\u5f53\u6309\u7d22\u5f15\u8bfb\u53d6\u5143\u7d20\u65f6\uff0c\u4f1a\u5224\u65ad\u5f53\u524d union \u91cc\u88c5\u7684\u662f\u54ea\u79cd\u7c7b\u578b\uff0c\u5982\u679c\u662f Latin-1 \u7684\uff0c\u90a3\u5c31\u4f1a\u7528\u7ed3\u6784\u4f53\u91cc\u7684 uint8_t * \u6307\u9488\u6765\u7d22\u5f15\u3002 \u8fd9\u6837\uff0c\u5bf9\u4e8e\u5168 ASCII \u7684\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u8001\u8001\u5b9e\u5b9e\u5b58 UTF-32 \u5185\u5b58\u5360\u7528\u76f4\u63a5\u51cf\u5c11\u4e86 75%\uff01\u552f\u4e00\u7684\u4ee3\u4ef7\u662f\u6309\u7d22\u5f15\u8bfb\u5b57\u7b26\u5143\u7d20\u65f6\u9700\u8981\u505a\u4e2a if-else \u5224\u65ad\u3002\u540c\u65f6\u53c8\u4e0d\u5931\u53bb\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff08\u65e9\u671f Unicode \u8303\u56f4\uff09\u65f6\uff0c\u5219\u662f\u53d6\u51fa\u4f4e 16 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a uint16_t \u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u6848\u4e5f\u79f0\u4e3a UCS-2\u3002 \u6ce8\u610f UCS-2 \u5e76\u4e0d\u7b49\u540c\u4e8e UTF-16\uff0cUTF-16 \u662f\u80fd\u591f\u8868\u793a\u5b8c\u6574\u7684 Unicode \u7684\u53d8\u957f\u7f16\u7801\uff08\u6709\u4ee3\u7406\u5bf9\uff09\uff1b\u800c UCS-2 \u662f\u6ca1\u6709\u4ee3\u7406\u5bf9\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u7f3a\u70b9\u662f\u53ea\u80fd\u8868\u793a 0xFFFF \u7684\u8303\u56f4\u3002 \u5bf9\u4e8e\u5927\u90e8\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5185\u5b58\u5360\u7528\u5c31\u51cf\u5c11\u4e86 50%\uff0c\u4e5f\u4e0d\u4e8f\u3002 \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u201c\ud883\udede\u201d\u8fd9\u6837\u7684\uff0c\u8d85\u8fc7 0xFFFF \u7684\u5b57\u7b26\uff0c\u624d\u4f1a\u91c7\u7528 uint32_t \u8001\u8001\u5b9e\u5b9e\u5b58\u50a8\u771f\u6b63\u7684 UTF-32\uff0c\u53c8\u79f0 UCS-4\u3002 \u8fd9\u4e24\u79cd\u53eb\u6cd5\u662f\u7b49\u4ef7\u7684\uff0c\u53cd\u6b63 Unicode \u4ece\u6765\u6ca1\u6709\u8d85\u8fc7 0xFFFFFFFF \u7684\uff0c\u73b0\u5728\u4ed6\u4eec\u90fd\u662f\u5b9a\u957f\u7f16\u7801\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684\u865a\u62df\u673a\u8bed\u8a00\u6765\u8bf4\uff0c\u53cd\u6b63\u672c\u6765\u5c31\u4e0d\u5728\u4e4e if-else \u5206\u652f\u8fd9\u70b9\u5c0f\u5f00\u9500\uff0c\u8fd9\u786e\u5b9e\u662f\u6700\u597d\u7684\u65b9\u6848\u3002 \u7f3a\u70b9\u5c31\u662f\uff0c\u5f53\u4f60\u5f80\u4e00\u4e2a\u5b8c\u5168\u662f ASCII \u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u7a81\u7136\u63d2\u5165\u4e00\u4e2a\u201c\ud883\udede\u201d\u65f6\uff0c\u4f1a\u4ea7\u751f\u5de8\u5927\u7684\u5185\u5b58\u91cd\u5206\u914d\u3002\u867d\u7136\u53ea\u6709\u4e00\u4e2a\u201c\ud883\udede\u201d\uff0c\u4f46\u4e3a\u6b64\uff0c\u5176\u4ed6\u6240\u6709 ASCII \u5b57\u7b26\u90fd\u5f97\u4e3a\u4ed6\u6269\u5f20\u5230 32 \u4f4d\u7684\u5927\u5c0f\u3002\u800c UTF-8 \u548c\u4f20\u7edf\u7684 UTF-32 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4e5f\u4e0d\u5efa\u8bae C++ \u7a0b\u5e8f\u5458\u81ea\u5df1\u624b\u6413\u4e2a\u8fd9\u6837\u7684 union \u5b57\u7b26\u4e32\u3002 Rust &str \u548c String \u800c Rust \u5219\u91c7\u7528\u4e86\u5b57\u7b26\u4e32\u5168\u5458 UTF-8 \u7684\u7b56\u7565\uff0c\u8fd9\u662f\u56e0\u4e3a Rust \u6700\u5e38\u7528\u4e8e\u4e92\u8054\u7f51\u65b9\u9762\u7684\u5e95\u5c42\u7cfb\u7edf\u8f6f\u4ef6\uff0c\u4e92\u8054\u7f51\u6700\u5e38\u7528\u7684\u6587\u672c\u7f16\u7801\u5c31\u662f UTF-8\uff0c\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\uff0c\u4e14\u56fd\u9645\u901a\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4e92\u8054\u7f51\u57fa\u5efa\u6700\u5e38\u89c1\u7684\u5e73\u53f0\u5c31\u662f Linux\uff0c\u4f7f\u7528 UTF-8 \u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u8c03\u7528 Linux \u7cfb\u7edf API \u65e0\u9700\u4efb\u4f55\u8f6c\u6362\u3002\u4e14\u6587\u672c\u6587\u4ef6\u57fa\u672c\u90fd\u53ef\u4ee5\u5047\u5b9a\u662f UTF-8 \u7f16\u7801\uff0c\u5199\u5165\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u590d\u6742\u5ea6\u4f4e\u81f3 O(1) O(1) \u3002\u4f5c\u4e3a\u4ee3\u4ef7\uff0c\u8fd9\u5bfc\u81f4\u6587\u672c\u5904\u7406\u4e0a\u7684\u4e00\u4e9b\u56f0\u96be\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u9700\u8981\u533a\u5206\u662f\u6309\u5b57\u8282\u7d22\u5f15\u8fd8\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u5982\u679c\u786e\u5b9e\u9700\u8981\u6309\u5b57\u7b26\u7d22\u5f15\u7684\u8bdd\uff0c\u590d\u6742\u5ea6\u5c31\u4f1a\u662f O(N) O(N) \u4e86\u3002 \u65e0\u8bba\u5982\u4f55\uff0c\u5982\u679c\u4f60\u9009\u62e9\u4e86 UTF-8 \u6d41\u6d3e\u7684\u8bdd\uff0cRust \u5b57\u7b26\u4e32\u7684\u201c\u8fed\u4ee3\u5668\u53cc\u8f68\u5236\u201d\u786e\u5b9e\u503c\u5f97\u79f0\u9053\uff1a let s = \"\u4f60\u597d\uff0c\u4e16\u754c\"; for c in s.chars() { // \u6309\u5b57\u7b26\u8fed\u4ee3 println!(\"{}\", c); } for b in s.bytes() { // \u6309\u5b57\u8282\u8fed\u4ee3 println!(\"{:02X}\", b); // \u6253\u5370\uff1aE4 BD A0 E5 A5 BD EF BC 8C E4 B8 96 E7 95 8C } Java String Java \u4e5f\u662f UTF-16 \u7684\u53cc\u5b57\u8282\u7f16\u7801\u3002 \u4eae\u70b9\uff1a\u4ed6\u7684 String \u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u65e0\u6cd5\u5c31\u5730\u4fee\u6539\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u6bcf\u6b21\u4ea7\u751f\u4f60\u8c03\u7528 += \u7684\u90fd\u662f\u4e00\u4e2a\u65b0 String\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u3002 \u4e5f\u5c31\u662f\u8bf4\uff1aJava \u7684 String \u867d\u7136\u662f\u201c\u5806\u201d\u4e2d\u7684\u5bf9\u8c61\uff0c\u5374\u65e0\u6cd5\u4ee5\u5f15\u7528\u4f20\u9012\u3002 \u8fd9\u907f\u514d\u4e86\u4ee5\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff1a void registerStudent(String name) { name += \".txt\"; File file(name); file.write(...); } void myTransaction() { String name = \"\u5c0f\u5f6d\"; lib.registerStudent(name); office.registerStudent(name); // \u8fd9\u91cc name \u662f\u5426\u88ab\u4fee\u6539\uff1f } \u5982\u679c Java \u7684 String \u548c\u666e\u901a\u5bf9\u8c61\u4e00\u6837\uff0c\u88ab\u8c03\u7528\u8005\u7684\u4fee\u6539\u53ef\u4ee5\u5bf9\u5916\u90e8\u53ef\u89c1\uff0c\u90a3\u5c82\u4e0d\u662f\u6211\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u4ee5 String \u4e3a\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u90fd\u9700\u8981\u64cd\u5fc3\uff1a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0d\u4f1a\u628a\u6211\u7684\u5b57\u7b26\u4e32\u4fee\u6539\u6389\uff1f \u6240\u4ee5\uff0cJava \u7ed9\u4ed6\u7684\u5bf9\u8c61\u6a21\u578b\u5f00\u4e86\u4e2a\u540e\u95e8\uff1a\u89c4\u5b9a\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u6309\u5f15\u7528\u4f20\u9012\uff0c\u9664\u4e86 String \uff01\u5c31\u53ea\u6709 String \u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u88ab\u8c03\u7528\u8005\u5185\u90e8\u7684\u4fee\u6539\u5bf9\u5916\u4e0d\u53ef\u89c1\u3002 C++ \u548c Rust \u53ea\u9700\u8981\u52a0\u4e2a\u5e38\u5f15\u7528\u5c31\u884c\uff0c\u800c Java \u53d7\u5bb3\u8005\u8981\u8003\u8651\u7684\u5c31\u591a\u4e86\u3002 \u603b\u4e4b\uff0c\u8fd9\u5c31\u662f\u6ca1\u6709 const \u7684\u5783\u573e\u8bed\u8a00\u7684\u4e11\u6001\uff0c\u9700\u8981\u9760\u5404\u79cd\u8bed\u6cd5\u89c4\u5219\u4e0a\u5f00\u6d1e\u624d\u80fd\u5f25\u8865\u8bbe\u8ba1\u65f6\u8003\u8651\u4e0d\u5468\u7684\u7f3a\u9677\uff0c\u5c31\u4e3a\u4e86\u4f3a\u5019\u8fd9\u5e2e\u5f15\u7528\u90fd\u5f04\u4e0d\u660e\u767d\u7684\u5783\u573e\u5c0f\u767d\u3002 \u6211\u662f\u8bf4\u5783\u573e\u56de\u6536 (garbage-collect) \u8bed\u8a00\uff0c\u7b80\u79f0\u5783\u573e\u8bed\u8a00\u3002\u6ca1\u6709\u8bf4 Java \u5783\u573e\u7684\u610f\u601d\uff0c\u4f60\u8bf4\u5bf9\u5427\uff1f\u5783\u573e\u8bed\u8a00\u3002 COW \u5b57\u7b26\u4e32 \u62c5\u5fe7\uff1a\u90a3\u5c82\u4e0d\u662f\u6bcf\u6b21\u6211 += \u5b9e\u9645\u4e0a\u90fd\u767d\u767d\u6df1\u62f7\u8d1d\u4e86\u4e00\u4efd\u65b0\u7684 String \uff1f\u522b\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5177\u4f53\u5b9e\u73b0\u4e0a\uff0cJava \u7684 String \u5728\u5e95\u5c42\u91c7\u7528\u4e86\u548c Qt \u7684 QString \u4e00\u6837\u7684 COW \u673a\u5236\uff1a \u5f53\u4e00\u4e2a QString \u88ab\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5bf9\u5176\u4e2d\u7684 QByteArray \u8fdb\u884c\u6df1\u62f7\u8d1d\uff0c\u800c\u662f\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u3002\u53ea\u6709\u5f53\u5176\u4e2d\u4e00\u4e2a QString \u88ab += \u7b49\u5e26\u6709\u526f\u4f5c\u7528\u64cd\u4f5c\u4fee\u6539\u65f6\uff0c\u624d\u4f1a\u6df1\u62f7\u8d1d\u4e00\u4efd\u65b0\u7684\uff0c\u8ba9\u4f60\u4fee\u6539\u3002\u8fd9\u6837\u5927\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f00\u9500\u3002 COW \u5b57\u7b26\u4e32\u7684\u7f3a\u70b9\u662f\uff1a\u5f53\u4f60\u5199\u591a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u672c\u6765\u591a\u7ebf\u7a0b\u53ea\u8bfb\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5b89\u5168\u7684\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u6709 COW\uff0c\u8fde\u53ea\u8bfb\u8bbf\u95ee\u90fd\u4f1a\u4e0d\u5b89\u5168\u4e86\u3002\u4e4b\u540e\u6211\u4eec\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4f1a\u8be6\u7ec6\u5206\u6790\u8fd9\u662f\u4e3a\u4ec0\u4e48\u3002 \u5176\u5b9e\u65e9\u671f\u7684 C++98 \u4e2d\uff0c std::string \u4e5f\u91c7\u7528\u4e86 COW \u673a\u5236\uff0c\u4f46\u540e\u6765\u56e0\u4e3a\u4e0d\u7b26\u5408\u7ebf\u7a0b\u5b89\u5168\u8981\u6c42\uff0c\u88ab\u8ffd\u6c42\u591a\u7ebf\u7a0b\u7684 C++11 \u8d23\u4ee4\u6539\u6b63\uff0c\u624d\u6709\u4e86\u540e\u6765 std::__cxx11::basic_string \u7684 ABI \u4e0d\u517c\u5bb9\u95ee\u9898\u3002\u6bd5\u7adf\u7ebf\u7a0b\u4e0d\u5b89\u5168\u5b9e\u5728\u592a\u4f24\u4e86\uff0c\u57fa\u672c\u610f\u5473\u7740\u591a\u7ebf\u7a0b\u5c31\u6ca1\u6cd5\u5171\u4eab std::string \u3002\u4e8b\u5b9e\u4e0a\uff0cQt \u6240\u6709\u7684\u5bf9\u8c61 QObject \uff0c\u5305\u62ec QString \u5728\u591a\u7ebf\u7a0b\u4e2d\u4f20\u9012\u65f6\uff0c\u5c31\u9700\u8981\u8c03\u7528 moveTo(QThread) \u8f6c\u79fb\u6240\u6709\u6743\uff0c\u624d\u80fd\u5b89\u5168\u5730\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5c31\u662f\u56e0\u4e3a Qt \u5927\u91cf\u4f7f\u7528\u4e86 COW \u673a\u5236\u3002 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme TODO \u6b63\u89c4\u5316 TODO \u96f6\u5bbd\u7a7a\u683c TODO \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 TODO \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 TODO \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u662f\u4e00\u4e2a 8 \u4f4d\u7f16\u7801\uff0c\u80fd\u8868\u793a 256 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u62c9\u4e01\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u3001\u5e38\u7528\u7684\u897f\u6b27\u5b57\u7b26\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u628a\u4e00\u4e2a Latin-1 \u7f16\u7801\u7684 char \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a wchar_t \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u63a5\u53e3\u6784\u9020 std::wstring \uff0c\u8fd9\u6837 char \u4f1a\u88ab\u9010\u4e2a\u8f6c\u6362\u4e3a wchar_t \u3002 std::string latin1 = \"I love P\\xE9ng\"; // 0xE9: \u00e9 std::wstring wstr(latin1.begin(), latin1.end()); std::wcout << wstr << '\\n'; \u8f93\u51fa\uff1a I love P\u00e9ng \u5e76\u4e0d\u6807\u51c6\u7684\u505a\u6cd5\uff0c\u8fd8\u662f\u5efa\u8bae\u7528 boost::locale::conv::to_utf(latin1, \"Latin-1\") \u3002 Latin-1 \u7684\u5999\u7528 \u7531\u4e8e Latin-1 \u8986\u76d6\u4e86\u6240\u6709\u7684 256 \u4e2a char \u7684\u53ef\u80fd\u503c\uff0c\u4efb\u4f55\u5b57\u8282\u6d41\u90fd\u53ef\u4ee5\u6210\u529f\u89e3\u7801\u3002 GBK \u548c UTF-8 \u6709\u81ea\u7ea0\u9519\u6027\uff0c\u6709\u4e9b\u8f93\u5165\u4f1a\u88ab\u584c\u7f29\u6210\u9519\u8bef\u201c\ufffd\u201d\u3002Latin-1 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u7167\u5355\u5168\u6536\uff01 \u56e0\u6b64\u6709\u65f6\uff0c\u4eba\u4eec\u53ef\u4ee5\u6b3a\u9a97\u4e00\u4e2a\u7f16\u7801\u5668\u8bf4\u201c\u6211\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u662f Latin-1\u201d\uff01\u8fd9\u6837\u7f16\u7801\u5668\u5c31\u4e0d\u4f1a\u5bf9\u8f93\u5165\u7684\u5b57\u8282\u6d41\u505a\u4efb\u4f55\u8f6c\u6362\uff0c\u4ece\u800c\u53ef\u4ee5\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u5f53\u6587\u672c\u6765\u4f20\uff0c\u89e3\u7801\u65f6\u4e5f\u6307\u5b9a Latin-1\uff0c\u539f\u539f\u672c\u672c\u7684\u53d6\u51fa\u6570\u636e\u3002 Base64 \u9632\u4e71\u7801 \u5982\u679c\u8981\u628a\u4e00\u4e32\u4e2d\u6587\u8f93\u5165\u4e00\u4e2a\u4e0d\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\uff0c\u53d1\u9001\u8ba9\u5bf9\u65b9\u6536\u5230\uff0c\u600e\u4e48\u529e\uff1f \u53ef\u4ee5\u7528 Latin-1 \u7f16\u7801\uff0c\u9a97\u8fd9\u4e2a\u8f6f\u4ef6\uff0c\u8ba9\u4ed6\u4ee5\u4e3a\u81ea\u5df1\u6536\u5230\u7684\u662f Latin-1 \u5b57\u7b26\u4e32\uff0c\u53cd\u6b63\u4ed6\u4e5f\u4e0d\u770b\u5185\u5bb9\uff0c\u4ece\u800c\u8ba9\u4ed6\u4e0d\u8981\u505a\u4efb\u4f55\u8f6c\u6362\u64cd\u4f5c\u3002 \u4e0d\u8fc7\u6709\u65f6\u5019\uff0c\u6587\u672c\u6846\u65e0\u6cd5\u8f93\u5165\u90e8\u5206\u7279\u6b8a\u7684\u63a7\u5236\u5b57\u7b26\uff0c\u800c UTF-8 \u5b57\u7b26\u4e32\u7f16\u7801\u51fa\u6765\u7684\u6587\u672c\uff0c\u8d85\u8fc7 0x80 \u7684\u90e8\u5206\uff0c\u53ef\u80fd\u843d\u5165 Latin-1 \u7684\u63a7\u5236\u5b57\u7b26\u4e2d\uff0c\u88ab\u8fd9\u4e2a\u8f6f\u4ef6\u9519\u8bef\u5730\u505a\u4e86\u7279\u6b8a\u5904\u7406\u3002 \u4e3a\u4e86\u907f\u514d\u53ea\u517c\u5bb9\u4e86 ASCII \u7684\u843d\u540e\u8f6f\u4ef6\u7834\u574f\u6211\u4eec\u7684\u5b57\u7b26\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u53ea\u652f\u6301 ASCII \u6587\u672c\u7684\u7f16\u8f91\u6846\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Base64 \u7f16\u7801\u5148\u628a\u4efb\u610f\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u3002 Base64 \u662f\u4e00\u79cd\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\uff0c\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u6bcf 6 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u8f6c\u6362\u4e3a\u4e00\u4e2a\u53ef\u6253\u5370\u7684 ASCII \u5b57\u7b26\uff08\u7528 A-Z a-z 0-9 - / \u8fd9 64 \u4e2a\u5b57\u7b26\u8868\u793a\uff09\u3002\u56e0\u6b64\uff0cBase64 \u7f16\u7801\u540e\u7684\u6587\u672c\uff0c\u6bcf 4 \u4e2a\u5b57\u7b26\u5c31\u6709 3 \u4e2a\u662f\u6709\u6548\u5b57\u7b26\uff0c\u5269\u4e0b\u7684 1 \u4e2a\u5b57\u7b26\u662f\u586b\u5145\u5b57\u7b26 = \u3002 \u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"\u5c0f\u5f6d\u8001\u5e08\" \uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u628a\u5b83\u901a\u8fc7\u90ae\u4ef6\u53d1\u51fa\u53bb\u3002\u800c\u8fd9\u4e2a\u90ae\u4ef6\u670d\u52a1\u5668\u4e0d\u652f\u6301 UTF-8 \u4e5f\u4e0d\u652f\u6301 GBK\uff0c\u53ea\u652f\u6301 ASCII\uff01 \u9996\u5148\u6211\u4eec\u7528 UTF-8 \u7f16\u7801\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff1a 0xE5 0xB0 0x8F 0xE5 0xBD 0xAD 0xE8 0x80 0x81 0xE5 0xB8 0x88 \u7136\u540e\u518d\u7528 Base64 \u4e8c\u6b21\u7f16\u7801\u6210\u666e\u901a\u7684\u53ef\u6253\u5370\u5b57\u6bcd\u548c\u6570\u5b57\u5e8f\u5217\uff1a 5bCP5b2t6ICB5biI \u5bf9\u65b9\u6536\u5230\u8fd9\u4e32\u795e\u79d8\u5b57\u7b26\u540e\uff0c\u518d\u7528 base64 \u89e3\u7801\uff0c\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff0c\u518d\u7528\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\u89e3\u7801\uff0c\u5c31\u80fd\u770b\u5230\u672c\u6765\u7684\u4e2d\u6587\u4e86\u3002 # \u53d1\u9001\u8005\uff1a import base64 secret = base64.b64encode(\"\u5c0f\u5f6d\u8001\u5e08\".encode()) # \u63a5\u6536\u8005\uff1a base64.b64decode(secret).decode() \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u7801 UTF-8 \u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f20\u8f93\u4efb\u610f\u975e\u6587\u672c\u7684\u6587\u4ef6\uff01\u4f8b\u5982\uff0c\u6709\u4eba\u5229\u7528 Base64 \u7f16\u7801\uff0c\u628a jpg \u56fe\u50cf\u6587\u4ef6\u76f4\u63a5\u5185\u5d4c\u5728 md \u6587\u4ef6\u91cc\uff01\uff08md \u6587\u4ef6\u53ea\u652f\u6301\u5305\u542b\u5408\u6cd5\u7684 UTF-8 \u6587\u672c\uff0c\u4e0d\u53ef\u80fd\u5305\u542b jpg \u7684\u4efb\u610f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u53ea\u80fd\u7528 Base64 \u5148\u7f16\u7801\u6210 ASCII \u8303\u56f4\u5185\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u9632\u6b62 md \u7f16\u8bd1\u5668\u62a5 UTF-8 \u89e3\u7801\u9519\u8bef\uff09 \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u8f93\u5165\u4e2d\u6587\u5b9e\u5728\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5148 Base64 \u8f6c\u6362\u6210\u7eaf\u82f1\u6587\u8bd5\u8bd5\u770b\uff0c\u53cd\u6b63\u65e0\u8bba\u8c01\u90fd\u517c\u5bb9 ASCII\u3002\u5982\u679c\u8fd9\u4e2a\u6587\u672c\u6846\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u8fd8\u53ef\u4ee5\u8bd5\u8bd5\u770b\u53ea\u6709 A-Z 0-9 \u7684 Base32 \u7f16\u7801\u3002","title":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b"},{"location":"unicode/#_1","text":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u96c6 ASCII Latin-1 Unicode \u603b\u7ed3 \u5b57\u7b26\u7f16\u7801 UTF-32 UTF-8 \u517c\u5bb9 ASCII \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d UTF-16 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 BOM \u6807\u8bb0 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 UTF-8 \u9635\u8425 UTF-16 \u9635\u8425 UTF-32 \u9635\u8425 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale UTF \u4e4b\u95f4\u4e92\u8f6c GBK \u548c UTF \u4e92\u8f6c UTF \u548c ANSI \u4e92\u8f6c \u5927\u603b\u7ed3 GBK \u548c Shift-JIS \u4e92\u8f6c \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u66f4\u591a\u529f\u80fd\uff1f\uff01 Windows \u7528\u6237\uff1aMultiByteToWideChar MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b Linux \u7528\u6237\uff1aiconv iconv \u547d\u4ee4\u884c\u5de5\u5177 \u672c\u5730\u5316 (locale) \u533a\u5206\u5b57\u7b26\u7c7b\u578b \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u5bbd\u5b57\u7b26\u6d41 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring wchar_t \u7cfb\u5217\u51fd\u6570 std::wcout \u7684\u4f7f\u7528 std::wfstream \u7684\u4f7f\u7528 C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 Windows \u4e13\u9898 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 TCHAR \u6d41\u6d3e UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 Qt QString QTextCodec from/toLocal8Bits/Utf8/Latin1/Ascii \u5b57\u7b26\u4e32\u5e38\u91cf QTextStream Python 3 str Rust &str \u548c String Java String COW \u5b57\u7b26\u4e32 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme \u6b63\u89c4\u5316 \u96f6\u5bbd\u7a7a\u683c \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u7684\u5999\u7528 Base64 \u9632\u4e71\u7801","title":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b"},{"location":"unicode/#_2","text":"\u8ba1\u7b97\u673a\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5b57\u7b26\uff0c\u800c\u662f\u7528\u6570\u5b57\u6765\u4ee3\u66ff\uff0c\u8fd9\u5c31\u662f\u5b57\u7b26\u96c6\uff0c\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\u3002","title":"\u5b57\u7b26\u96c6"},{"location":"unicode/#ascii","text":"ASCII \u4e3a\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u7ec4\u3001\u6807\u70b9\u7b26\u53f7\u7b49 128 \u4e2a\u5b57\u7b26\uff0c\u6bcf\u4e2a\u90fd\u7528\u4e00\u4e2a 0 \u5230 127 \u8303\u56f4\u5185\u7684\u6570\u5b57\u5bf9\u5e94\u3002 \u5982\u679c\u4f60\u60f3\u8981\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c31\u5728\u8fd9\u4e2a\u8868\u91cc\u5bfb\u627e\u5230\u76f8\u5e94\u7684\u6570\u5b57\u7f16\u53f7\uff0c\u7136\u540e\u5b58\u8fd9\u4e2a\u7f16\u53f7\u5373\u53ef\u3002 \u4f8b\u5982\u4e0b\u9762\u7684\u4e00\u4e32\u6570\u5b57\uff1a 80 101 110 103 \u5728 ASCII \u8868\u4e2d\u67e5\u627e\uff0c\u53d1\u73b0\u8fd9\u4e9b\u6570\u5b57\u5206\u522b\u5bf9\u5e94 P \u3001 e \u3001 n \u3001 g \u56db\u4e2a\u5b57\u6bcd\uff0c\u8fde\u8d77\u6765\u5c31\u8fd8\u539f\u5f97\u5230\u4e86\u539f\u672c\u7684\u5b57\u7b26\u4e32\u201cPeng\u201d\u3002","title":"ASCII"},{"location":"unicode/#latin-1","text":"Latin-1 \u6269\u5145\u4e86 ASCII \u5b57\u7b26\u96c6\uff0c\u4fdd\u6301 ASCII \u539f\u6709 0 \u5230 127 \u7684\u90e8\u5206\u6620\u5c04\u4e0d\u53d8\uff0c\u989d\u5916\u8ffd\u52a0\u4e86 128 \u5230 255 \u7684\u6620\u5c04\u5173\u7cfb\u3002\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a EASCII\uff08\u6269\u5c55 ASCII\uff09\u3002","title":"Latin-1"},{"location":"unicode/#unicode","text":"Unicode \u5b57\u7b26\u96c6\u4e3a\u5168\u4e16\u754c\u7684\u6240\u6709\u5b57\u7b26\u90fd\u5bf9\u5e94\u4e86\u4e00\u4e2a\u6574\u6570\u3002 \u5b57\u7b26 \u7f16\u53f7 \u6211 25105 \u6212 25106 \u6213 25107 \u6214 25108 \u6215 25109 \u6216 25110 \u6217 25111 \u6218 25112 \u6219 25113 \u621a 25114 \u51fa\u4e8e\u5386\u53f2\u517c\u5bb9\u6027\u8003\u8651\uff0cUnicode \u5728 0 \u5230 256 \u533a\u95f4\u5185\u7684\u6620\u5c04\u548c ASCII\u3001Latin-1 \u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002 \u5b57\u7b26 \u7f16\u53f7 P 80 e 101 n 110 g 103 Unicode \u7ecf\u8fc7\u4e86\u8bb8\u591a\u7248\u672c\u7684\u53d1\u5c55\uff0c\u65e9\u671f\u7684 Unicode \u53ea\u6536\u5f55\u4e86 65536 (0x10000) \u4e2a\u5b57\u7b26\uff0c\u540e\u6765\u6269\u5145\u5230\u4e86 1114112 (0x110000) \u4e2a\u5b57\u7b26\u3002 \u603b\u4e4b\uff0c\u73b0\u5728 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u867d\u7136\u5360\u7528\u4e86 1114112 \u8fd9\u591a\u683c\u7801\u70b9\u7a7a\u95f4\uff0c\u4e0d\u8fc7\u5176\u4e2d\u5f88\u591a\u90fd\u662f\u7a7a\u53f7\uff0c\u7559\u5f85\u672a\u6765\u6269\u5145\u4f7f\u7528\u3002 Unicode \u5b57\u7b26\u6620\u5c04\u8868\u53ef\u4ee5\u5728\u7f51\u4e0a\u627e\u5230\uff1a https://symbl.cc/en/unicode-table/ https://www.compart.com/en/unicode/","title":"Unicode"},{"location":"unicode/#_3","text":"\u5b57\u7b26\u96c6: \u4ece\u5b57\u7b26\u5230\u6574\u6570\u7684\u4e00\u4e00\u6620\u5c04\u3002 ASCII: \u53ea\u6536\u5f55\u4e86\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 Latin-1: \u5728 ASCII \u57fa\u7840\u4e0a\u8ffd\u52a0\u4e86\u6ce8\u97f3\u5b57\u6bcd\uff0c\u6ee1\u8db3\u6b27\u6d32\u7528\u6237\u9700\u8981\u3002 Unicode: \u6536\u5f55\u4e86\u5168\u4e16\u754c\u6240\u6709\u6587\u5b57\u548c\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 \u8ba1\u7b97\u673a\u5b58\u50a8\u5b57\u7b26\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5b58\u50a8\u4e86\u90a3\u4e2a\u5bf9\u5e94\u7684\u6574\u6570\u3002 \u8fd9\u4e9b\u6574\u6570\u5c31\u88ab\u79f0\u4e3a \u7801\u70b9 (code point) \uff0c\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002 \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u5458\u901a\u5e38\u559c\u6b22\u7528\u5341\u516d\u8fdb\u5236\u4e66\u5199\u6570\u5b57\uff1a \u5b57\u7b26 \u7f16\u53f7 \u6211 0x6211 \u6212 0x6212 \u6213 0x6213 \u6214 0x6214 \u6215 0x6215 \u6216 0x6216 \u6217 0x6217 \u6218 0x6218 \u6219 0x6219 \u621a 0x621A \u4f8b\u5982\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\uff0c\u5728 Unicode \u8868\u4e2d\u7f16\u53f7\u4e3a 0x6211\u3002\u4e8e\u662f\u5f53\u8ba1\u7b97\u673a\u9700\u8981\u8868\u793a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\u65f6\uff0c\u5c31\u7528 0x6211 \u8fd9\u4e2a\u6574\u6570\u4ee3\u66ff\u3002 \u5982\u679c\u8981\u8868\u793a\u591a\u4e2a\u5b57\u7b26\uff0c\u90a3\u5c31\u7528\u4e00\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u5427\uff01 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5904\u7406\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a 0x6211 0x7231 0x30EDE 0x30EDE 0x9762 0x21 \u8fd9\u4e00\u4e32\u6570\u5b57\u4ee3\u66ff\u3002","title":"\u603b\u7ed3"},{"location":"unicode/#_4","text":"Unicode \u53ea\u662f\u6307\u5b9a\u4e86\u6574\u6570\uff0c\u6ca1\u6709\u89c4\u5b9a\u6574\u6570\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5b58\u5728\u3002 \u5b57\u7b26\u7f16\u7801: \u5c06\u5b57\u7b26\u7684\u6574\u6570\u7f16\u53f7\u5e8f\u5217\u5316\u4e3a\u8ba1\u7b97\u673a\u53ef\u76f4\u63a5\u5b58\u50a8\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u5b9e\u9645\u5b58\u5728\u7684\u6574\u6570\u7c7b\u578b\u3002 Unicode \u5b57\u7b26\u53ef\u4ee5\u9009\u7528\u4ee5\u4e0b\u8fd9\u4e9b\u5b57\u7b26\u7f16\u7801\u6765\u5e8f\u5217\u5316\uff1a UTF-32: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u4e2a uint32_t \u6574\u6570\u5b58\u50a8\u3002 UTF-16: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 2 \u4e2a uint16_t \u6574\u6570\u5b58\u50a8\u3002 UTF-8: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u5b58\u50a8\u3002 \u7ffb\u8bd1\u51fa\u6765\u7684\u8fd9\u4e9b\u5c0f\u6574\u6570\u53eb \u7801\u4f4d (code unit) \u3002\u4f8b\u5982\u5bf9\u4e8e UTF-8 \u800c\u8a00\uff0c\u6bcf\u4e2a uint8_t \u5c31\u662f\u4ed6\u7684\u7801\u4f4d\u3002","title":"\u5b57\u7b26\u7f16\u7801"},{"location":"unicode/#utf-32","text":"Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u6700\u5927\u503c 0x10FFFF \u6709 21 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0cC \u8bed\u8a00\u4e2d uint32_t \u80fd\u5bb9\u7eb3 32 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0c\u6240\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u7528 uint32_t \u6570\u7ec4\u6765\u4e00\u4e2a\u4e2a\u5bb9\u7eb3 Unicode \u5b57\u7b26\u7801\u70b9\u3002\u867d\u7136\u6d6a\u8d39\u4e86 11 \u4f4d\uff0c\u4f46\u81f3\u5c11\u6240\u6709 Unicode \u5b57\u7b26\u90fd\u80fd\u5b89\u5168\u5bb9\u7eb3\u3002 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a std::vector s = { 0x00006211, // \u6211 0x00007231, // \u7231 0x00030EDE, // \ud883\udede 0x00030EDE, // \ud883\udede 0x00009762, // \u9762 0x00000021, // ! }; \u8fd9\u4e2a\u6570\u7ec4\u8868\u793a\u3002 UTF-32 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u56fa\u5b9a\u5bf9\u5e94\u4e00\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-32 \u662f \u5b9a\u957f\u7f16\u7801 \u3002\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5c31\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002 \u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u64cd\u4f5c\u3002 \u65e0\u8bba\u5bf9\u6570\u7ec4\u5982\u4f55\u5207\u7247\uff0c\u90fd\u4e0d\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u7834\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u53cd\u8f6c\uff0c\u4e0d\u4f1a\u4ea7\u751f\u7834\u574f\u5b57\u7b26\u7684\u95ee\u9898\u3002 \u7f3a\u70b9\u662f\uff1a \u6d6a\u8d39\u5b58\u50a8\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\uff0c\u59cb\u7ec8\u91c7\u7528 UTF-32 \u5f62\u5f0f\u5904\u7406\u6587\u5b57\u3002 UTF-32 \u4e5f\u88ab\u79f0\u4e3a UCS-4\uff0c\u4ed6\u4fe9\u662f\u540c\u4e49\u8bcd\u3002","title":"UTF-32"},{"location":"unicode/#utf-8","text":"UTF-32 \u867d\u7136\u65b9\u4fbf\u4e86\u6587\u5b57\u5904\u7406\uff0c\u7136\u800c\uff0c\u5374\u6d6a\u8d39\u4e86\u5927\u91cf\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e0d\u5229\u4e8e\u6587\u5b57\u5b58\u50a8\uff01\u4e00\u4e2a\u5b57\u7b26\uff0c\u65e0\u8bba\u4ed6\u662f\u5e38\u7528\u8fd8\u662f\u4e0d\u5e38\u7528\uff0c\u90fd\u8981\u9738\u5360 4 \u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\u3002 Unicode \u7f16\u7801\u5b57\u7b26\u65f6\uff0c\u7279\u610f\u628a\u5e38\u7528\u7684\u5b57\u7b26\u9760\u524d\u6392\u5217\u4e86\u3002 \u4e16\u754c\u4e0a\u5e38\u7528\u8bed\u8a00\u6587\u5b57\u90fd\u88ab\u523b\u610f\u7f16\u7801\u5728\u4e86 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u8d85\u8fc7 0x10000 \u7684\u57fa\u672c\u90fd\u662f\u4e0d\u5e38\u7528\u7684\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\uff0c\u5f88\u591a\u90fd\u662f\u5df2\u7ecf\u65e0\u4eba\u4f7f\u7528\u7684\u53e4\u4ee3\u6587\u5b57\u548c\u751f\u50fb\u5b57\uff0c\u4f8b\u5982\u201c\ud883\udede\u201d\u3002\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8fd9\u4e9b\u5076\u5c14\u4f7f\u7528\u7684\u7f55\u89c1\u6587\u5b57\uff0c\u5c31\u8981\u6c42\u6240\u6709\u6587\u5b57\u90fd\u7528\u540c\u6837\u7684 4 \u5b57\u8282\u5bbd\u5ea6\u5b58\u50a8\uff0c\u5b9e\u5728\u662f\u6709\u70b9\u6d6a\u8d39\u3002 \u5728 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u540c\u6837\u6709\u6309\u7167\u5e38\u7528\u5ea6\u6392\u5e8f\uff1a 0 \u5230 0x7F \u662f\uff08\u6b27\u7f8e\u7528\u6237\uff09\u6700\u5e38\u7528\u7684\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u534a\u89d2\u6807\u70b9\u3002 0x80 \u5230 0x7FF \u662f\u8868\u97f3\u6587\u5b57\u533a\uff0c\u5e38\u7528\u7684\u6ce8\u97f3\u5b57\u6bcd\u3001\u62c9\u4e01\u5b57\u6bcd\u3001\u5e0c\u814a\u5b57\u6bcd\u3001\u897f\u91cc\u5c14\u5b57\u6bcd\u3001\u5e0c\u4f2f\u6765\u5b57\u6bcd\u7b49\u3002 0x800 \u5230 0xFFFF \u662f\u8868\u610f\u6587\u5b57\uff0c\u7b80\u7e41\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u6cf0\u6587\u3001\u9a6c\u6765\u6587\u3001\u963f\u62c9\u4f2f\u6587\u7b49\u3002 0x10000 \u5230 0x10FFFF \u662f\u4e0d\u5e38\u7528\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\u3002 UTF-8 \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u538b\u7f29\u95ee\u9898\u800c\u8bde\u751f\u7684\u3002 UTF-8 \u628a\u4e00\u4e2a\u7801\u70b9\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u7801\u4f4d\uff0c\u4e00\u4e2a\u7801\u4f4d\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u8868\u793a\u3002 0 \u5230 0x7F \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 1 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 3 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002 \u5e8f\u5217\u5316\u89c4\u5219\u5982\u4e0b\uff1a","title":"UTF-8"},{"location":"unicode/#ascii_1","text":"\u5bf9\u4e8e 0 \u5230 0x7F \u7684\u5b57\u7b26\uff0c\u8fd9\u4e2a\u8303\u56f4\u7684\u5b57\u7b26\u9700\u8981 7 \u4f4d\u5b58\u50a8\u3002 \u6211\u4eec\u9009\u62e9\u76f4\u63a5\u5b58\u50a8\u5176\u503c\u3002 \u4f8b\u5982 \u2018P\u2019 \u4f1a\u88ab\u76f4\u63a5\u5b58\u50a8\u5176 Unicode \u503c\u7684 80\uff080x50\uff09\uff1a 01010000 \u7531\u4e8e Unicode \u5728 0 \u5230 0x7F \u8303\u56f4\u5185\u4e0e ASCII \u8868\u76f8\u540c\uff0c\u800c UTF-8 \u53c8\u628a 0 \u5230 0x7F \u7684\u503c\u76f4\u63a5\u5b58\u50a8\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u517c\u5bb9 ASCII\u3002\u8fd9\u4f7f\u5f97\u539f\u672c\u8bbe\u8ba1\u4e8e\u5904\u7406 ASCII \u7684 C \u8bed\u8a00\u51fd\u6570\uff0c\u4f8b\u5982 strlen\u3001strcat\u3001sprintf \u7b49\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u65e0\u7f1d\u5207\u6362\u5230 UTF-8\u3002\u53cd\u4e4b\u4ea6\u7136\uff0c\u4efb\u4f55\u8bbe\u8ba1\u7528\u4e8e UTF-8 \u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5b8c\u5168\u63a5\u53d7 ASCII \u683c\u5f0f\u7684\u8f93\u5165\u6587\u672c\u3002 \u4f46\u90e8\u5206\u6d89\u53ca\u5b57\u7b26\u957f\u5ea6\u7684\u51fd\u6570\u4f1a\u6709\u4e9b\u8bb8\u4e0d\u517c\u5bb9\uff0c\u4f8b\u5982 strlen \u6c42\u51fa\u7684\u957f\u5ea6\u4f1a\u53d8\u6210\u5b57\u8282\u7684\u6570\u91cf\u800c\u4e0d\u662f\u5b57\u7b26\u7684\u6570\u91cf\u4e86\uff0c\u4f8b\u5982 strlen(\"\u6211\u4eec\") \u4f1a\u5f97\u5230 6 \u800c\u4e0d\u662f 2\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002","title":"\u517c\u5bb9 ASCII"},{"location":"unicode/#_5","text":"UTF-8 \u7684\u6784\u9020\u5c31\u50cf\u4e00\u5217\u5c0f\u706b\u8f66\u4e00\u6837\uff0c\u4e0d\u540c\u8303\u56f4\u5185\u7684\u7801\u4f4d\u4f1a\u88ab\u7f16\u7801\u6210\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8f66\uff0c\u4f46\u4ed6\u4eec\u90fd\u6709\u4e00\u4e2a\u8f66\u5934\u3002 \u6839\u636e\u706b\u8f66\u5934\u7684\u201c\u7b49\u7ea7\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u65ad\u51fa\u540e\u9762\u62c9\u7740\u51e0\u8282\u8f66\u53a2\u3002 \u706b\u8f66\u5934\u662f\u4ec0\u4e48\u7b49\u7ea7\u7531\u4ed6\u7684\u4e8c\u8fdb\u5236\u524d\u7f00\u51b3\u5b9a\uff1a \u5982\u679c\u662f 0 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u662f\u5355\u72ec\u4e00\u53f0\u706b\u8f66\u5934\uff0c\u540e\u9762\u6ca1\u6709\u8f66\u53a2\u4e86\uff0c\u8fd9\u8868\u793a\u8f66\u5934\u91cc\u9762\u76f4\u63a5\u88c5\u7740 0 \u5230 0x7F \u8303\u56f4\u7684\u666e\u901a ASCII \u5b57\u7b26\u3002 \u5982\u679c\u662f 110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e00\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u6b27\u6d32\u5b57\u7b26\u3002 \u5982\u679c\u662f 1110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e24\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u4e16\u754c\u5e38\u7528\u5b57\u7b26\u3002 \u5982\u679c\u662f 11110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e09\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u751f\u50fb\u5b57\u7b26\u3002 \u5982\u679c\u662f 10 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u8fd9\u662f\u4e00\u8282\u8f66\u53a2\uff0c\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u8282\u5355\u72ec\u7684\u8f66\u53a2\u5728\u524d\u9762\u65e0\u5934\u9a7e\u9a76\uff0c\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\u3002 \u5c0f\u670b\u53cb\u7528\u5c0f\u53f7\u5217\u8f66\u88c5\uff0c\u5927\u670b\u53cb\u7528\u5927\u53f7\u5217\u8f66\u88c5\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u4e32\u4e8c\u8fdb\u5236\uff1a 11100110 10000010 10000001 \u9996\u5148\uff0c\u770b\u5230\u7b2c\u4e00\u4e2a\u5b57\u8282\uff0c\u662f 1110 \u5f00\u5934\u7684\u4e09\u7ea7\u8f66\u5934\uff01\u8bf4\u660e\u540e\u9762\u8fd8\u6709\u4e24\u8282\u8f66\u53a2\u662f\u5c5e\u4e8e\u4ed6\u7684\u3002\u706b\u8f66\u5934\u4e2d 4 \u4f4d\u7528\u4e8e\u8868\u793a\u8f66\u5934\u7b49\u7ea7\u4e86\uff0c\u5269\u4e0b\u8fd8\u6709 4 \u4f4d\u7528\u4e8e\u88c5\u4e58\u5ba2\u3002 \u8f66\u53a2\u4e5f\u6709\u56fa\u5b9a\u7684\u524d\u7f00\uff0c\u6240\u6709\u7684\u8f66\u53a2\u90fd\u5fc5\u987b\u662f 10 \u5f00\u5934\u7684\u3002\u53bb\u9664\u8fd9\u5f00\u5934\u7684 2 \u4f4d\uff0c\u5269\u4e0b\u7684 6 \u4f4d\u5c31\u662f\u4e58\u5ba2\u3002 \u5bf9\u4e8e\u8fd9\u79cd\u4e09\u7ea7\u5217\u8f66\uff0c4 + 6 + 6 \u603b\u5171 16 \u4f4d\u4e8c\u8fdb\u5236\uff0c\u521a\u597d\u53ef\u4ee5\u88c5\u5f97\u4e0b 0xFFFF \u5185\u7684\u4e58\u5ba2\u3002 0110 000010 000001 \u7f16\u7801\u65f6\u5219\u662f\u53cd\u8fc7\u6765\u3002 \u4e58\u5ba2\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e09\u7247\uff0c\u4f8b\u5982\u5bf9\u4e8e\u201c\u6211\u201d\u8fd9\u4e2a\u4e58\u5ba2\uff0c\u201c\u6211\u201d\u7684\u7801\u70b9\u662f 0x6211\uff0c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u662f\uff1a 110010000010001 \u628a\u4e58\u5ba2\u5207\u5206\u6210\u9ad8 4 \u4f4d\u3001\u4e2d 6 \u4f4d\u548c\u4f4e 6 \u4f4d\uff1a 1100 100000 10001 \u52a0\u4e0a 1110 \u3001 10 \u548c 10 \u524d\u7f00\u540e\uff0c\u5f62\u6210\u4e00\u5217\u706b\u8f66\uff1a 11100110 10000010 10000001 \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u628a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\uff0c\u7f16\u7801\u6210\u4e86\u4e09\u8282\u5217\u8f66\uff0c\u585e\u8fdb\u5b57\u8282\u6d41\u7684\u7f51\u7edc\u96a7\u9053\u91cc\u4e86\u3002 \u603b\u7ed3\uff1a \u524d\u7f00\u662f 0 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 7 \u540d\u3002 \u524d\u7f00\u662f 10 \u7684\u662f\u8f66\u53a2\uff1a\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002 \u524d\u7f00\u662f 110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 5 \u540d + 1 \u8282\u8f66\u53a2\u8f7d\u5ba2 6 \u540d = \u5171 11 \u540d\u3002 \u524d\u7f00\u662f 1110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 4 \u540d + 2 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 16 \u540d\u3002 \u524d\u7f00\u662f 11110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 3 \u540d + 3 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 21 \u540d\u3002 \u9ad8\u7ea7\u8f66\u5934\u88c5\u4e86\u9632\u5f39\u94a2\u677f\uff0c\u8f7d\u5ba2\u7a7a\u95f4\u53d8\u5c11\uff0c\u53ea\u597d\u5300\u5230\u540e\u9762\u7684\u8f66\u53a2\u3002","title":"\u89e3\u7801\u89c4\u5219"},{"location":"unicode/#utf-8_1","text":"\u5982\u679c\u53d1\u73b0 10 \u5f00\u5934\u7684\u72ec\u7acb\u8f66\u53a2\uff0c\u5c31\u8bf4\u660e\u51fa\u95ee\u9898\u4e86\uff0c\u53ef\u80fd\u662f\u706b\u8f66\u88ab\u9519\u8bef\u62e6\u8170\u622a\u65ad\uff0c\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u88ab\u9519\u8bef\u5730\u53cd\u8f6c\u3002\u56e0\u4e3a 10 \u53ea\u53ef\u80fd\u662f\u706b\u8f66\u8f66\u53a2\uff0c\u4e0d\u53ef\u80fd\u51fa\u73b0\u5728\u706b\u8f66\u5934\u90e8\u3002\u6b64\u65f6\u89e3\u7801\u5668\u5e94\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u66ff\u6362\u3002 10000010 10000001 \u5728\u7f51\u7edc\u6536\u53d1\u5305\u65f6\uff0c\u5982\u679c\u4f60\u4e0d\u59a5\u5584\u5904\u7406 TCP \u7c98\u5305\u95ee\u9898\uff0c\u5c31\u53ef\u80fd\u706b\u8f66\u5934\u8fdb\u53bb\u4e86\uff0c\u706b\u8f66\u5c3e\u5df4\u8fd8\u9732\u5728\u96a7\u9053\u5916\u9762\uff0c\u4e00\u6bb5\u5b8c\u6574\u7684\u5217\u8f66\u88ab\u5207\u65ad\uff0c\u5bfc\u81f4 UTF-8 \u89e3\u8bfb\u7684\u65f6\u5019\u51fa\u9519\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u8bbe\u7acb\u4e00\u4e2a\u72b6\u6001\u673a\u6765\u89e3\u7801 UTF-8\u3002C \u8bed\u8a00\u7684 mbstate_t \u5c31\u662f\u8fd9\u79cd\u72b6\u6001\u673a\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\uff0c\u5374\u53d1\u73b0\u91cc\u9762\u88c5\u7740 0x394 (\u201c\u0394\u201d)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u5c31\u80fd\u88c5\u4e0b\u7684\u6b27\u6d32\u5b57\u7b26\uff0c\u5374\u7528\u4e86\u4e09\u7ea7\u706b\u8f66\u5934\u88c5\uff0c\u8bf4\u660e\u88c5\u7bb1\u90a3\u8fb9\u7684\u4eba\u5077\u61d2\u6ee5\u7528\u8d44\u6e90\u4e86\uff01\u8fd9\u79cd\u60c5\u51b5\u4e0b UTF-8 \u89e3\u7801\u5668\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u8981\u4fdd\u8bc1\u7f16\u7801\u7684\u552f\u4e00\u6027\uff0c0x394 \u662f 0x7F \u5230 0x7FF \u8303\u56f4\u7684\uff0c\u5c31\u5e94\u8be5\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u88c5\u3002 \u4ee5\u53ca\uff0c\u5982\u679c\u53d1\u73b0 11111 \u5f00\u5934\u7684\u4e94\u7ea7\u706b\u8f66\u5934\uff0c\u4e5f\u8981\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u6700\u591a\u53ea\u652f\u6301\u56db\u7ea7\u706b\u8f66\u5934\u3002 \u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u56db\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u7684\u5b57\u7b26\u8303\u56f4\u8d85\u8fc7\u4e86 0x10FFFF\uff0c\u8fd9\u8d85\u51fa\u4e86 Unicode \u7684\u8303\u56f4\uff0c\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\u3002\u5982\u679c\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u53d1\u73b0\u5b57\u7b26\u8303\u56f4\u5904\u5728\u4fdd\u7559\u533a 0xD800 \u5230 0xDFFF \u5185\uff0c\u8fd9\u662f Unicode \u627f\u8bfa\u6c38\u4e0d\u52a0\u5165\u5b57\u7b26\u7684\u533a\u95f4\uff08\u7a0d\u540e\u8bb2\u89e3 UTF-16 \u65f6\u4f1a\u89e3\u91ca\u4e3a\u4ec0\u4e48\uff09\uff0c\u4e5f\u8981\u62a5\u9519\u3002\u603b\u4e4b Unicode \u7801\u70b9\u7684\u5408\u6cd5\u8303\u56f4\u662f 0x0 \u5230 0xD7FF\uff0c0xE000 \u5230 0x10FFFF\u3002 \u603b\u4e4b\uff0cUTF-8 \u5177\u6709\u4e00\u5b9a\u7684\u5197\u4f59\u548c\u81ea\u7ea0\u9519\u80fd\u529b\uff0c\u5982\u679c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5dee\u9519\uff0c\u53ef\u80fd\u4f1a\u7206\u51fa\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u3002\u8fd9\u4e2a\u7279\u6b8a\u5b57\u7b26\u662f Unicode \u5b98\u65b9\u89c4\u5b9a\u7684\uff0c\u7801\u70b9\u4e3a 0xFFFD\uff0c\u51fa\u73b0\u4ed6\u5c31\u610f\u5473\u7740 UTF-8 \u89e3\u7801\u5931\u8d25\u4e86\u3002","title":"UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b"},{"location":"unicode/#_6","text":"\u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u4ee5 UTF-8 \u683c\u5f0f\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff1a std::vector s = { 0xE6, 0x88, 0x91, // \u6211\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xE7, 0x88, 0xB1, // \u7231\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xE9, 0x9D, 0xA2, // \u9762\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0x21, // !\uff0c\u8fd9\u662f\u4e2a ASCII \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u76f4\u63a5\u7528\u5355\u4e2a\u706b\u8f66\u5934\u88c5 }; UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u662f\u4e00\u79cd \u53d8\u957f\u7f16\u7801 \u3002\u53d8\u957f\u7f16\u7801\u7684\u7f3a\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e0d\u4e00\u5b9a\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u56e0\u6b64\uff0c\u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\u3002 \u6570\u7ec4\u7684\u5355\u4e2a\u5143\u7d20\u7d22\u5f15\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u53d6\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u3002 \u5bf9\u6570\u7ec4\u7684\u5207\u7247\uff0c\u53ef\u80fd\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u5207\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u4e0d\u4e00\u5b9a\u80fd\u628a\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\uff0c\u56e0\u4e3a\u53ef\u80fd\u4e0d\u614e\u628a\u4e00\u4e2a\u5b57\u7b26\u7684\u591a\u4e2a\u7801\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u5b57\u7b26\u7834\u574f\u3002 \u4f18\u70b9\u662f\uff1a \u8282\u7ea6\u5b58\u50a8\u7a7a\u95f4\u3002 \u6211\u4eec\u63a8\u8350\u53ea\u5728\u7f51\u7edc\u901a\u4fe1\u3001\u786c\u76d8\u5b58\u50a8\u65f6\uff0c\u91c7\u7528 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u6587\u5b57\u3002 \u603b\u4e4b\uff0cUTF-8 \u9002\u5408\u5b58\u50a8\uff0cUTF-32 \u9002\u5408\u5904\u7406\u3002 \u6211\u4eec\u5efa\u8bae\u8ba1\u7b97\u673a\u4ece\u786c\u76d8\u6216\u7f51\u7edc\u4e2d\u8bfb\u51fa UTF-8 \u5b57\u7b26\u4e32\u540e\uff0c\u7acb\u5373\u5c06\u5176\u8f6c\u6362\u4e3a UTF-32\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u6587\u5b57\u5904\u7406\u3002\u5f53\u9700\u8981\u5199\u5165\u786c\u76d8\u6216\u7f51\u7edc\u65f6\uff0c\u518d\u8f6c\u6362\u56de UTF-8\uff0c\u907f\u514d\u786c\u76d8\u5bb9\u91cf\u548c\u7f51\u7edc\u5e26\u5bbd\u7684\u6d6a\u8d39\u3002 \u8ba1\u7b97\u673a\u9700\u8981\u5916\u7801\u548c\u5185\u7801\u4e24\u79cd\uff1a \u5916\u7801=\u786c\u76d8\u4e2d\u7684\u6587\u672c=UTF-32 \u5185\u7801=\u5185\u5b58\u4e2d\u7684\u6587\u672c=UTF-8","title":"\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d"},{"location":"unicode/#utf-16","text":"UTF-16 \u7684\u7b56\u7565\u662f\uff1a\u65e2\u7136\u5927\u591a\u6570\u5e38\u7528\u5b57\u7b26\u7684\u7801\u70b9\u90fd\u5728 0x0 \u5230 0xFFFF \u5185\uff0c\u7528 uint32_t \u6765\u5b58\u50a8\u4e5f\u592a\u6d6a\u8d39\u4e86\u3002\u4ed6\u7684\u65b9\u6848\u5982\u4e0b\uff1a \u5bf9\u4e8e 0x0 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5c31\u7528\u4e00\u4e2a uint16_t \u76f4\u63a5\u5b58\u3002 \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u53cd\u6b63\u4e0d\u5e38\u89c1\uff0c\u5c31\u62c6\u6210\u4e24\u4e2a uint16_t \u5b58\u3002\u8fd9\u4e2a\u62c6\u7684\u65b9\u6848\u5f88\u6709\u8bb2\u7a76\uff0c\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u62c6\uff0c\u7531\u4e8e\u89e3\u7801\u65f6\u6536\u5230\u7684\u662f\u4e2a\u6ca1\u5934\u6ca1\u5c3e\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u65e0\u6cd5\u5206\u8fa8\u8fd9\u5230\u5e95\u662f\u4e24\u4e2a uint16_t \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u8fd8\u662f\u4e00\u4e2a uint16_t \u7684\u666e\u901a\u5b57\u7b26\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u628a\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u201c\ud883\udede\u201d\uff0c0x30EDE\u3002\u62c6\u6210\u4e24\u4e2a uint16_t \uff0c\u5f97\u5230 0x3 \u548c 0x0EDE\u3002\u5982\u679c\u76f4\u63a5\u5b58\u50a8\u8fd9\u4e24\u4e2a uint16_t \uff1a 0x0003 0x0EDE \u4e4b\u540e\u89e3\u7801\u65f6\uff0c\u5148\u8bfb\u5230 0x0003\uff0c\u8fd8\u4f1a\u4ee5\u4e3a\u4ed6\u662f\u5355\u72ec\u7684\u4e00\u4e2a uint16_t \uff0c\u8868\u793a 3 \u53f7\u5b57\u7b26\u201c\u201d\u3002\u540e\u9762\u7684 0x0EDE \u5c31\u53d8\u6210\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 0x0EDE\uff0c\u53d8\u6210\u4e86 0x0EDE \u53f7\u5b57\u7b26 \u201c\u0ede\u201d\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u201c\ud883\udede\u201d\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u6beb\u4e0d\u76f8\u5e72\u7684\u5b57\u7b26\uff0c\u201c\u0ede\u201d\u4e86\u3002 \u4e3a\u4e86\u907f\u514d\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u4e24\u4e2a uint16_t \u9700\u8981\u91c7\u7528\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\u4ee5\u793a\u533a\u5206\u3002\u8ba9\u89e3\u7801\u5668\u4e00\u770b\u5230\uff0c\u5c31\u80fd\u786e\u5b9a\u8fd9\u4e24\u4e2a uint16_t \u9700\u8981\u7ec4\u88c5\u6210\u540c\u4e00\u4e2a\u5b57\u7b26\u3002 \u8fd9\u5c31\u7528\u5230\u4e86\u4e00\u4e2a\u201c\u6f0f\u6d1e\u201d\uff1aUnicode \u5e76\u6ca1\u6709\u628a\u7801\u70b9\u5206\u914d\u7684\u6ee1\u6ee1\u5f53\u5f53\uff0c\u6216\u8bb8\u662f\u51fa\u4e8e\u5148\u89c1\u4e4b\u660e\uff0c\u5728 0xD800 \u5230 0xDFFF \u4e4b\u95f4\u9884\u7559\u4e86\u4e00\u5927\u6bb5\u7a7a\u53f7\uff1a UTF-16 \u5c31\u662f\u5229\u7528\u4e86\u8fd9\u4e00\u6bb5\u7a7a\u95f4\uff0c\u4ed6\u89c4\u5b9a\uff1a0xD800 \u5230 0xDFFF \u4e4b\u95f4\u7684\u7801\u70b9\u5c06\u6c38\u8fdc\u4e0d\u7528\u6765\u8868\u793a\u5b57\u7b26\uff0c\u800c\u662f\u4f5c\u4e3a \u4ee3\u7406\u5bf9 (surrogate-pair) \u3002\u5176\u4e2d 0xD800 \u5230 0xDBFF \u662f \u9ad8\u4f4d\u4ee3\u7406 (high surrogate) \uff0c0xDC00 \u5230 0xDFFF \u662f \u4f4e\u4f4d\u4ee3\u7406 (low surrogate) \u3002\u9ad8\u4ee3\u7406\u5728\u524d\uff0c\u4f4e\u4ee3\u7406\u5728\u540e\u3002 \u4e00\u4e2a\u8d85\u8fc7 0xFFFF \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f1a\u88ab\u62c6\u6210\u4e24\u6bb5\uff0c\u4e00\u6bb5\u653e\u5728\u9ad8\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u6bb5\u653e\u5728\u4f4e\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u524d\u4e00\u540e\u653e\u5165 uint16_t \u5e8f\u5217\u4e2d\u3002 \u642d\u8f7d\u8d85\u5bbd\u8d85\u9650\u8d27\u7269\u7684\u8f66\u8f86\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e24\u6bb5\u518d\u8fdb\u5165\u96a7\u9053\u3002 \u5177\u4f53\u62c6\u5206\u65b9\u6cd5\u5982\u4e0b\uff1a \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7801\u70b9\uff0c\u9996\u5148\u5c06\u5176\u503c\u51cf\u53bb 0x10000\uff0c\u53d8\u6210\u4e00\u4e2a\u8303\u56f4 0x0 \u5230 0xFFFFF \u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u8fd9\u80fd\u4fdd\u8bc1\u4ed6\u4eec\u53ea\u9700 20 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u53ef\u8868\u793a\u3002 \u4f8b\u5982\u201c\ud883\udede\u201d\u5bf9\u5e94\u7684\u7801\u70b9 0x30EDE\uff0c\u51cf\u53bb\u540e\u5c31\u53d8\u6210 0x20EDE\u3002 \u7136\u540e\uff0c\u5199\u51fa 0x20EDE \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a 00100000111011011110 \u603b\u5171 20 \u4f4d\uff0c\u6211\u4eec\u5c06\u5176\u62c6\u6210\u9ad8\u4f4e\u5404 10 \u4f4d\uff1a 0010000011 1011011110 \u5404\u81ea\u5199\u51fa\u76f8\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6570\uff1a 0x083 0x2DE \u56e0\u4e3a\u6700\u591a\u53ea\u6709 10 \u4f4d\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4f1a\u5728 0 \u5230 0x3FF \u7684\u8303\u56f4\u5185\u3002 \u800c 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u9884\u7559\u7684\u7a7a\u95f4\uff0c\u521a\u597d\u53ef\u4ee5\u5206\u522b\u5bb9\u7eb3 0x400 \u4e2a\u6570\uff01 \u6240\u4ee5\uff0c\u6211\u4eec\u5c06\u62c6\u5206\u51fa\u6765\u7684\u4e24\u4e2a 10 \u4f4d\u6570\uff0c\u5206\u522b\u52a0\u4e0a 0xD800 \u548c 0xDC00\uff1a 0xD800+0x083=0xD883 0xDC00+0x2DE=0xDFDE \u8fd9\u4e24\u4e2a\u6570\uff0c\u5fc5\u5b9a\u662f 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u6570\u3002\u800c\u8fd9\u4e24\u4e2a\u8303\u56f4\u90fd\u662f Unicode \u59d4\u5458\u4f1a\u9884\u7559\u7684\u4ee3\u7406\u5bf9\u533a\u95f4\uff0c\u7edd\u5bf9\u6ca1\u6709\u666e\u901a\u5b57\u7b26\u3002\u6240\u4ee5\uff0c\u751f\u6210\u7684\u4e24\u4e2a\u4ee3\u7406\u5bf9\u4e0d\u4f1a\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u53ef\u4ee5\u653e\u5fc3\u653e\u8fdb uint16_t \u6570\u7ec4\uff0c\u89e3\u7801\u5668\u5982\u679c\u68c0\u6d4b\u5230\u4ee3\u7406\u5bf9\uff0c\u5c31\u8bf4\u660e\u662f\u4e24\u8282\u8f66\u53a2\uff0c\u53ef\u4ee5\u653e\u5fc3\u8fde\u7eed\u8bfb\u53d6\u4e24\u4e2a uint16_t \u3002 \u6240\u4ee5\uff0c 0xD883 0xDFDE \u5c31\u662f\u201c\ud883\udede\u201d\u7528 UTF-16 \u7f16\u7801\u540e\u7684\u7ed3\u679c\u3002 \u4ee3\u7406\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\uff0c\u5f53\u89e3\u7801\u5668\u68c0\u6d4b\u5230\u4e00\u4e2a 0xD800 \u5230 0xDBFF \u8303\u56f4\u5185\u7684\u9ad8\u4ee3\u7406\u65f6\uff0c\u5c31\u9884\u793a\u7740\u8fd8\u9700\u8981\u518d\u8bfb\u53d6\u4e00\u4e2a\u4f4e\u4ee3\u7406\uff0c\u624d\u80fd\u62fc\u63a5\u6210\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u3002 \u5982\u679c\u63a5\u4e0b\u6765\u8bfb\u5230\u7684\u4e0d\u662f 0xDC00 \u5230 0xDFFF \u8303\u56f4\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u800c\u662f\u666e\u901a\u5b57\u7b26\u7684\u8bdd\uff0c\u90a3\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u53ef\u80fd\u662f\u4e2d\u95f4\u88ab\u4eba\u4e22\u5305\u4e86\uff0c\u9700\u8981\u62a5\u9519\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u9876\u66ff\u3002 \u53e6\u5916\uff0c\u5982\u679c\u8bfb\u5230\u4e86\u4e00\u4e2a\u5355\u72ec\u5b58\u5728\u7684 0xD800 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u90a3\u4e5f\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u56e0\u4e3a\u4ee3\u7406\u5b57\u7b26\u53ea\u6709\u6210\u5bf9\u51fa\u73b0\u624d\u6709\u610f\u4e49\uff0c\u4f4e\u4ee3\u7406\u5b57\u7b26\u4e0d\u53ef\u80fd\u5355\u72ec\u5728\u5f00\u5934\u51fa\u73b0\u3002 \u53ef\u89c1\uff0cUTF-16 \u548c UTF-8 \u4e00\u6837\uff0c\u90fd\u662f\u201c\u5c0f\u706b\u8f66\u201d\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0cUTF-16 \u540c\u6837\u4e5f\u6709\u7740\u7c7b\u4f3c\u4e8e UTF-8 \u7684\u6297\u5e72\u6270\u673a\u5236\u3002","title":"UTF-16"},{"location":"unicode/#_7","text":"\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u591a\u5b57\u8282\u7684\u6574\u6570\u7c7b\u578b\uff08\u5982 uint16_t \u548c uint32_t \uff09\u9700\u8981\u88ab\u62c6\u6210\u591a\u4e2a\u5b57\u8282\u6765\u5b58\u50a8\u3002\u62c6\u5f00\u540e\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u6309\u4ec0\u4e48\u987a\u5e8f\u5b58\u5165\u5185\u5b58\uff1f\u4e0d\u540c\u7684\u786c\u4ef6\u67b6\u6784\u4ea7\u751f\u4e86\u4e89\u6267\uff1a \u5927\u7aef\u6d3e (bit endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u4e5f\u5c31\u662f\u5927\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5c0f\u7aef\u6d3e (little endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u4e5f\u5c31\u662f\u5c0f\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u8ba1\u7b97\u673a\u7535\u8def\u7684\u8ba1\u7b97\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x78 0x56 0x34 0x12 \u4f8b\u5982\uff0cIntel \u7684 x86 \u67b6\u6784\u548c ARM \u516c\u53f8\u7684 ARM \u67b6\u6784\u90fd\u662f\u5c0f\u7aef\u6d3e\uff0c\u800c Motorola \u516c\u53f8\u7684 68k \u67b6\u6784\u548c Sun \u516c\u53f8\u7684 SPARC \u67b6\u6784\u90fd\u662f\u5927\u7aef\u6d3e\u3002 \u8fd9\u5176\u5b9e\u662f\u5f88\u65e0\u804a\u7684\u4e89\u6267\uff0c\u4e3a\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u6539\u53d8\u8ba1\u7b97\u673a\u7684\u8bbe\u8ba1\u6beb\u65e0\u9053\u7406\uff0c\u6bd5\u7adf\u4e16\u754c\u4e0a\u4e5f\u6709\u4ece\u53f3\u5f80\u5de6\u4e66\u5199\u7684\u6587\u5b57\u548c\u4ece\u4e0a\u5f80\u4e0b\u4e66\u5199\u7684\u6587\u5b57\uff0c\u751a\u81f3\u6709\u5de6\u53f3\u6765\u56de\u4e66\u5199\u7684\u6587\u5b57\u2026\u2026\u5982\u679c\u8981\u4f3a\u5019\u4eba\u7c7b\uff0c\u4f60\u600e\u4e48\u4e0d\u6539\u6210\u5341\u8fdb\u5236\u5462\uff1f\u603b\u4e4b\uff0c\u6211\u8ba4\u4e3a\u5c0f\u7aef\u624d\u662f\u6700\u9002\u5408\u8ba1\u7b97\u673a\u7684\uff0c\u5e02\u9762\u4e0a\u5927\u591a\u6570\u4e3b\u6d41\u786c\u4ef6\u90fd\u662f\u5c0f\u7aef\u67b6\u6784\u3002 \u5728\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u53d1\u6d88\u606f\u548c\u6536\u6d88\u606f\u7684\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u67b6\u6784\uff0c\u5982\u679c\u53d1\u6d88\u606f\u7684\u662f\u5c0f\u7aef\u67b6\u6784\uff0c\u6536\u6d88\u606f\u7684\u662f\u5927\u7aef\u67b6\u6784\uff0c\u90a3\u4e48\u53d1\u51fa\u53bb\u7684\u662f 0x12345678\uff0c\u6536\u5230\u7684\u5c31\u4f1a\u53d8\u6210 0x78563421 \u4e86\u3002 \u56e0\u6b64\u4e92\u8054\u7f51\u4e00\u822c\u89c4\u5b9a\uff0c\u6240\u6709\u591a\u5b57\u8282\u7684\u6570\u636e\u5728\u7f51\u7edc\u5305\u4e2d\u7edf\u4e00\u91c7\u7528\u5927\u7aef\u3002\u5bf9\u4e8e\u5927\u7aef\u67b6\u6784\uff0c\u4ed6\u4eec\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u5bf9\u4e8e\u5c0f\u7aef\u67b6\u6784\uff0c\u5728\u53d1\u5305\u524d\u9700\u8981\u628a\u81ea\u5df1\u7684\u5c0f\u7aef\u6570\u636e\u505a\u5b57\u8282\u5e8f\u53cd\u8f6c\uff0c\u53d8\u6210\u5927\u7aef\u7684\u4ee5\u540e\uff0c\u518d\u53d1\u9001\u3002\u4e4b\u540e\u7684\u7f51\u7edc\u4e13\u9898\u8bfe\u4e2d\u6211\u4eec\u4f1a\u8be6\u89e3\u8fd9\u4e00\u5757\u3002 \u57fa\u4e8e\u5b57\u8282\u7801\u7684\u865a\u62df\u673a\u8bed\u8a00\u901a\u5e38\u4f1a\u89c4\u5b9a\u4e00\u4e2a\u5b57\u8282\u5e8f\uff1a\u50cf Java \u8fd9\u79cd\u9762\u5411\u4e92\u8054\u7f51\u8bed\u8a00\uff0c\u7d22\u6027\u4e5f\u89c4\u5b9a\u4e86\u7edf\u4e00\u91c7\u7528\u5927\u7aef\uff0c\u65e0\u8bba JVM \u8fd0\u884c\u5728\u5927\u7aef\u673a\u5668\u8fd8\u662f\u5c0f\u7aef\u673a\u5668\u4e0a\u3002\u8fd9\u4f7f\u5f97\u4ed6\u4e0e\u4e92\u8054\u7f51\u901a\u4fe1\u6bd4\u8f83\u65b9\u4fbf\uff0c\u800c\u5728 x86 \u548c ARM \u67b6\u6784\u4e0a\uff0c\u4e0e\u672c\u5730\u53ea\u63a5\u53d7\u5c0f\u7aef\u6570\u636e\u7684 API\uff0c\u4f8b\u5982 OpenGL\uff0c\u6c9f\u901a\u8f83\u4e3a\u56f0\u96be\uff0c\u9700\u8981\u505a\u989d\u5916\u7684\u5b57\u8282\u5e8f\u8f6c\u6362\u3002\u800c C# \u4e3b\u6253\u6e38\u620f\u4e1a\u52a1\uff08\u4f8b\u5982 Unity\uff09\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\uff0c\u6240\u4ee5\u89c4\u5b9a\u5168\u90e8\u91c7\u7528\u5c0f\u7aef\u3002\u4f5c\u4e3a\u5e95\u5c42\u7f16\u7a0b\u8bed\u8a00\u7684 C++ \u5219\u662f\u5165\u4e61\u968f\u4fd7\uff0c\u4f60\u7684\u786c\u4ef6\u662f\u4ec0\u4e48\u7aef\uff0c\u4ed6\u5c31\u662f\u4ec0\u4e48\u7aef\uff0c\u4e0d\u4e3b\u52a8\u505a\u4efb\u4f55\u989d\u5916\u7684\u8f6c\u6362\u3002 UTF-16 \u548c UTF-32 \u7684\u7801\u4f4d\u90fd\u662f\u591a\u5b57\u8282\u7684\uff0c\u4e5f\u4f1a\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u3002\u4f8b\u5982\uff0cUTF-16 \u4e2d\u7684 uint16_t \u5e8f\u5217\uff1a 0x1234 0x5678 \u5728\u5927\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5728\u5c0f\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x34 0x12 0x78 0x56 \u8fd9\u6837\u4e00\u6765\uff0cUTF-16 \u548c UTF-32 \u7684\u5b57\u8282\u6d41\uff0c\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u987a\u5e8f\u3002\u8fd9\u7ed9\u8de8\u5e73\u53f0\u7684\u6587\u672c\u5904\u7406\u5e26\u6765\u4e86\u9ebb\u70e6\u3002 \u6240\u4ee5\u5f53\u4f60\u9700\u8981\u628a UTF-16 \u5b58\u5165\u786c\u76d8\u548c\u5728\u7f51\u7edc\u53d1\u9001\u65f6\uff0c\u8fd8\u9700\u8981\u989d\u5916\u6307\u660e\u4f60\u7528\u7684\u662f\u5927\u7aef\u7684 UTF-16 \u8fd8\u662f\u5c0f\u7aef\u7684 UTF-16\u3002 \u56e0\u6b64 UTF-16 \u548c UTF-32 \u8fdb\u4e00\u6b65\u5206\u88c2\u4e3a\uff1a UTF-16LE\uff1a\u5c0f\u7aef\u7684 UTF-16 UTF-16BE\uff1a\u5927\u7aef\u7684 UTF-16 UTF-32LE\uff1a\u5c0f\u7aef\u7684 UTF-32 UTF-32BE\uff1a\u5927\u7aef\u7684 UTF-32 \u5982\u679c\u53ea\u5728\u5185\u5b58\u7684 wchar_t \u4e2d\u4f7f\u7528\u5c31\u4e0d\u7528\u533a\u5206\uff0c\u9ed8\u8ba4\u8ddf\u968f\u5f53\u524d\u673a\u5668\u7684\u5927\u5c0f\u7aef\u3002\u6240\u4ee5 UTF-16 \u548c UTF-32 \u901a\u5e38\u53ea\u4f1a\u51fa\u73b0\u5728\u5185\u5b58\u4e2d\u7528\u4e8e\u5feb\u901f\u5904\u7406\u548c\u8ba1\u7b97\uff0c\u5f88\u5c11\u7528\u5728\u5b58\u50a8\u548c\u901a\u4fe1\u4e2d\u3002 UTF-8 \u662f\u57fa\u4e8e\u5355\u5b57\u8282\u7684\u7801\u4f4d\uff0c\u706b\u8f66\u5934\u7684\u987a\u5e8f\u4e5f\u6709\u4e25\u683c\u89c4\u5b9a\uff0c\u706b\u8f66\u5934\u603b\u662f\u5728\u6700\u524d\uff0c\u6839\u672c\u4e0d\u53d7\u5b57\u8282\u5e8f\u5927\u5c0f\u7aef\u5f71\u54cd\uff0c\u4e5f\u5c31\u6ca1\u6709\u5f71\u54cd\u3002 \u7531\u4e8e\u538b\u7f29\u7387\u4f4e\uff0c\u53c8\u5b58\u5728\u5927\u5c0f\u7aef\u5b57\u8282\u5e8f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u800c\u4e92\u8054\u7f51\u6570\u636e\u9700\u8981\u4fdd\u8bc1\u76f8\u540c\u7684\u5927\u5c0f\u7aef\uff0c\u5728\u6536\u53d1\u5305\u65f6\u9700\u8981\u989d\u5916\u8f6c\u6362\uff0c\u56e0\u800c\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u7f51\u7edc\u3002\u800c UTF-8 \u7684\u5b58\u50a8\u5355\u4f4d\u662f\u5b57\u8282\uff0c\u5929\u751f\u6ca1\u6709\u5927\u5c0f\u7aef\u56f0\u6270\u3002\u66f4\u5999\u7684\u662f\uff0c\u4ed6\u4e14\u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u800c\u4e92\u8054\u7f51\u53c8\u662f\u53e4\u8463\u4e2d\u95f4\u4ef6\u6700\u591a\u7684\u5730\u65b9\u2026\u2026 \u603b\u4e4b\uff0c\u5b8c\u5168\u57fa\u4e8e\u5b57\u8282\u7684 UTF-8 \u662f\u6700\u9002\u5408\u7f51\u7edc\u901a\u4fe1\u548c\u786c\u76d8\u5b58\u50a8\u7684\u6587\u672c\u7f16\u7801\u683c\u5f0f\uff0c\u800c UTF-32 \u662f\u6700\u9002\u5408\u5728\u5185\u5b58\u4e2d\u5904\u7406\u7684\u683c\u5f0f\u3002","title":"\u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89"},{"location":"unicode/#bom","text":"0xFEFF \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u4e0d\u53ef\u89c1\u5b57\u7b26\u201c\ufeff\u201d\uff0c\u8fd9\u662f\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002 \u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u7b26\u52a0\u5728\u6587\u672c\u6587\u4ef6\u7684\u5934\u90e8\uff0c\u544a\u8bc9\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u7528\u4ec0\u4e48\u7f16\u7801\u7684\u3002 \u5982\u679c\u662f UTF-16 \u548c UTF-32\uff0c\u56e0\u4e3a 0xFEFF \u4e0d\u5bf9\u79f0\uff0c\u4ed6\u8fd8\u80fd\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\u3002\u56e0\u6b64 0xFEFF \u88ab\u79f0\u4e3a\u5b57\u8282\u5e8f\u6807\u5fd7\uff08Byte-order-mark\uff0cBOM\uff09\u3002 \u5982\u679c\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u90a3\u4e48\u4ed6\u7167\u5e38\u8bfb\u51fa 0xFEFF\uff0c\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u5728\u6587\u672c\u4e2d\u4e0d\u663e\u793a\uff0c\u4e0d\u5f71\u54cd\u89c6\u89c9\u7ed3\u679c\u3002 \u4e00\u4e9b\u8001\u7684\u7f16\u8bd1\u5668\uff08\u8fdc\u53e4 MinGW\uff0c\u73b0\u5728\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u4f1a\u628a\u5e26\u6709 BOM \u7684 UTF-8 \u7684 .cpp \u6e90\u7801\u6587\u4ef6\uff0c\u5f53\u4f5c\u5934\u90e8\u5e26\u6709\u9519\u8bef\u5b57\u7b26\u7684\u4e71\u7801\u6587\u4ef6\uff0c\u4ece\u800c\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u7684\u8bb0\u4e8b\u672c\u4fdd\u5b58\u4e3a UTF-8 \u65f6\uff0c\u603b\u662f\u4f1a\u52a0\u4e0a BOM\u3002\u5982\u679c\u8bb0\u4e8b\u672c\u53d1\u73b0\u4e00\u4e2a\u6587\u4ef6\u6ca1\u6709 BOM\uff0c\u4f1a\u5f53\u4f5c ANSI\uff08GBK\uff09\u6765\u8bfb\u53d6\u3002 0xFEFF \u5728\u4e0d\u540c\u7684\u7f16\u7801\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1a UTF-8\uff1a 0xEF 0xBB 0xBF \uff0c\u4ed6\u4f1a\u5360\u7528 3 \u5b57\u8282\uff0c\u800c\u4e14\u4e0d\u4f1a\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\uff0c\u56e0\u4e3a UTF-8 \u662f\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u7684\u3002 UTF-16\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE \u3002 UTF-32\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0x00 0x00 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE 0x00 0x00 \u3002 \u56e0\u6b64\uff0c\u5728\u6587\u672c\u5934\u90e8\u52a0\u4e0a BOM \u6709\u52a9\u4e8e\u8f6f\u4ef6\u63a8\u6d4b\u8be5\u6587\u4ef6\u662f\u4ec0\u4e48\u7f16\u7801\u7684\uff08\u5982\u679c\u90a3\u8f6f\u4ef6\u652f\u6301\u89e3\u6790 BOM \u7684\u8bdd\uff09\u3002 \u4f8b\u5982 Windows \u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\u90fd\u88ab\u9ed8\u8ba4\u5047\u5b9a\u4e3a ANSI\uff08GBK\uff09\u7f16\u7801\uff0c\u5982\u679c\u4f60\u8981\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u4e3a UTF-8 \u7f16\u7801\uff0c\u5c31\u9700\u8981\u52a0\u4e0a BOM \u6807\u5fd7\u3002\u5f53 MSVC \u8bfb\u53d6\u65f6\uff0c\u770b\u5230\u5f00\u5934\u662f 0xEF 0xBB 0xBF \uff0c\u5c31\u660e\u767d\u8fd9\u662f\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u6587\u4ef6\u3002\u8fd9\u6837\uff0cMSVC \u5c31\u80fd\u6b63\u786e\u5730\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4e86\u3002\u5982\u679c MSVC \u6ca1\u770b\u5230 BOM\uff0c\u4f1a\u9ed8\u8ba4\u4ee5\u4e3a\u662f ANSI\uff08GBK\uff09\u7f16\u7801\u7684\uff0c\u4ece\u800c\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4f1a\u4e71\u7801\u3002\u5f00\u542f /utf-8 \u9009\u9879\u4e5f\u80fd\u8ba9 MSVC \u628a\u6ca1\u6709 BOM \u7684\u6e90\u7801\u6587\u4ef6\u5f53\u4f5c UTF-8 \u6765\u89e3\u6790\uff0c\u9002\u5408\u8de8\u5e73\u53f0\u5b9d\u5b9d\u4f53\u8d28\u3002","title":"BOM \u6807\u8bb0"},{"location":"unicode/#cc","text":"","title":"C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801"},{"location":"unicode/#_8","text":"\u7c7b\u578b \u5927\u5c0f \u7f16\u7801 \u5b57\u9762\u91cf Linux char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e $LC_ALL \u201chello\u201d Windows char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e \u201chello\u201d Linux wchar_t 4 \u5b57\u8282 UTF-32 L\u201dhello\u201d Windows wchar_t 2 \u5b57\u8282 UTF-16 L\u201dhello\u201d char8_t 1 \u5b57\u8282 UTF-8 u8\u201dhello\u201d char16_t 2 \u5b57\u8282 UTF-16 u\u201dhello\u201d char32_t 4 \u5b57\u8282 UTF-32 U\u201dhello\u201d \u7531\u6b64\u53ef\u89c1\uff0c char \u548c wchar_t \u662f\u4e0d\u8de8\u5e73\u53f0\u7684\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f GBK\u3002\u5bf9\u4e8e\u7f8e\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\u3002 \u5bf9\u4e8e Linux \u7528\u6237\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e13\u95e8\u4fee\u6539\u8fc7\uff0c $LC_ALL \u9ed8\u8ba4\u662f en_US.UTF-8 \u6216 C.UTF-8 \u3002 \u8fd9\u5e26\u6765\u4e86\u5de8\u5927\u7684\u6df7\u6dc6\uff01\u5f88\u591a\u7f8e\u56fd\u7a0b\u5e8f\u5458\u6f5c\u610f\u8bc6\u91cc\u4f1a\u60f3\u5f53\u7136\u5730\u628a char \u5f53\u4f5c UTF-8 \u6765\u7528\u3002\u5f88\u591a\u5f00\u6e90\u9879\u76ee\uff0c\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u5f88\u591a\u56fd\u4eba\u505a\u7684\u9879\u76ee\uff0c\u90fd\u88ab\u8fd9\u79cd\u201c\u60f3\u5f53\u7136\u201d\u4f20\u67d3\u4e86\u3002 \u597d\u6d88\u606f\u662f\u65e0\u8bba\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u662f\u4ec0\u4e48\uff0c\u80af\u5b9a\u517c\u5bb9 ASCII\u3002\u4f8b\u5982 GBK \u548c UTF-8 \u90fd\u517c\u5bb9 ASCII\uff0c\u5426\u5219\u5c31\u548c\u6240\u6709\u7684 C \u8bed\u8a00\u7ecf\u5178\u51fd\u6570\u5982 strlen \uff0c\u6362\u884c\u7b26 '\\n' \uff0c\u8def\u5f84\u5206\u9694\u7b26 '/' \u548c '\\\\' \u51b2\u7a81\u4e86\u3002 wchar_t \u5c31\u597d\u4e00\u4e9b\uff0c\u867d\u7136\u5728 Windows \u7cfb\u7edf\u4e0a\u662f\u7cdf\u7cd5\u7684 UTF-16\uff0c\u4f46\u81f3\u5c11\u7a33\u5b9a\u4e86\uff0c\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u800c\u968f\u610f\u6539\u53d8\uff0c\u53ea\u8981\u4f60\u4e0d\u6253\u7b97\u8de8\u5e73\u53f0\uff0c wchar_t \u5c31\u662f Windows \u7a0b\u5e8f\u7684\u6807\u914d\u3002","title":"\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#utf-8-ascii","text":"UTF-8 \u7684\u706b\u8f66\u5934\u548c\u8f66\u53a2\uff0c\u90fd\u662f 1 \u5f00\u5934\u7684\uff0c\u800c ASCII \u7684\u5355\u4f53\u706b\u8f66\u5934\u6c38\u8fdc\u662f 0 \u5f00\u5934\u3002\u8fd9\u5f88\u91cd\u8981\uff0c\u4e0d\u4ec5\u706b\u8f66\u5934\u9700\u8981\u548c ASCII \u533a\u5206\u5f00\u6765\uff0c\u8f66\u53a2\u4e5f\u9700\u8981\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff1a std::u32string path = \"\u4e00\u4e2a\u8001\u4f2f.txt\"; \u201c\u4e00\u4e2a\u8001\u4f2f\u201d \u8f6c\u6362\u4e3a Unicode \u7801\u70b9\u5206\u522b\u662f\uff1a 0x4E00 0x4E2A 0x8001 0x4F2F \u5982\u679c\u8ba9\u4ed6\u4eec\u539f\u5c01\u4e0d\u52a8\u76f4\u63a5\u5b58\u50a8\u8fdb char \u6570\u7ec4\u91cc\uff1a 0x4E 0x00 0x4E 0x2A 0x80 0x01 0x4F 0x2F \u5c31\u51fa\u95ee\u9898\u4e86\uff01\u9996\u5148\uff0c\u8fd9\u91cc 0x4E00 \u7684 0x00 \u90e8\u5206\uff0c\u4f1a\u88ab C \u8bed\u8a00\u5f53\u4f5c\u662f\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u62ff\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u53bb\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684 open \u51fd\u6570\uff0c\u4ed6\u4f1a\u4ee5\u4e3a\u4f60\u5728\u6253\u5f00 0x4E \u5355\u4e2a\u5b57\u7b26\u7684\u6587\u4ef6\u540d\uff0c\u4e5f\u5c31\u662f \"N\" \u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c0x2F \u5bf9\u5e94\u7684 ASCII \u5b57\u7b26\u662f '/' \uff0c\u662f\u8def\u5f84\u5206\u9694\u7b26\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4ee5\u4e3a\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u5b50\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6 \"N\\x00N*\\x80\\x01O/.txt\" \uff0c\u6587\u4ef6\u5939\u540d\u5b57\u53eb \"N\\x00N*\\x80\\x01O\" \u800c\u6587\u4ef6\u53eb \".txt\" \u3002 \u4e3a\u4e86\u80fd\u8ba9\u9488\u5bf9 ASCII \u8bbe\u8ba1\u7684\u64cd\u4f5c\u7cfb\u7edf API \u652f\u6301\u4e2d\u6587\u6587\u4ef6\u540d\uff0c\u5c31\u53ea\u80fd\u7ed5\u5f00\u6240\u6709 0x7F \u4ee5\u4e0b\u7684\u503c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 UTF-8 \u5bf9\u8f66\u53a2\u4e5f\u5168\u90e8\u62ac\u9ad8\u5230 0x80 \u4ee5\u4e0a\uff0c\u907f\u514d\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u614e\u628a\u8f66\u53a2\u5f53\u4f5c\u662f '/' \u6216 '\\0' \u3002","title":"\u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII"},{"location":"unicode/#utf-8_2","text":"\u7531\u4e8e\u5de8\u5927\u7684\u60ef\u6027\uff0c\u5f88\u591a\u4eba\u90fd\u60f3\u5f53\u7136\u7684\u628a std::string \u5f53\u4f5c UTF-8 \u6765\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6253\u5370\uff0c\u5e38\u89c4\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u662f\u6ca1\u95ee\u9898\u7684\u3002 \u5b57\u7b26\u4e32\u64cd\u4f5c\u6709\u4e0b\u9762\u8fd9\u51e0\u79cd\uff0c\u5f97\u76ca\u4e8e UTF-8 \u4f18\u79c0\u7684\u5e8f\u5217\u5316\u6d89\u53ca\u548c\u5197\u4f59\u6297\u5e72\u6270\u673a\u5236\uff0c\u7edd\u5927\u591a\u6570 ASCII \u652f\u6301\u7684\u64cd\u4f5c\uff0cUTF-8 \u5b57\u7b26\u4e32\u90fd\u80fd\u8f7b\u677e\u80dc\u4efb\uff0c\u552f\u72ec\u5176\u4e2d \u6d89\u53ca\u201c\u7d22\u5f15\u201d\u548c\u201c\u957f\u5ea6\u201d\u7684 \u4e00\u90e8\u5206\u64cd\u4f5c\u4e0d\u884c\u3002\u8fd9\u662f\u7531\u4e8e\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff0c\u5982\u679c\u9700\u8981\u505a\u201c\u7d22\u5f15\u201d\u7c7b\u64cd\u4f5c\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u8f6c\u6362\u6210\u5b9a\u957f\u7684 UTF-32 \u7f16\u7801\u3002 \u64cd\u4f5c UTF-8 UTF-32 GBK \u6c42\u5b57\u7b26\u4e32\u957f\u5ea6 \u00d7 \u221a \u00d7 \u5224\u65ad\u76f8\u7b49 \u221a \u221a \u221a \u5b57\u5178\u5e8f\u7684\u5927\u5c0f\u6bd4\u8f83 \u221a \u221a \u00d7 \u5b57\u7b26\u4e32\u62fc\u63a5 \u221a \u221a \u221a \u641c\u7d22\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u641c\u7d22\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u4e0b\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u904d\u5386\u6240\u6709\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u5b50\u5b57\u7b26\u4e32\u5207\u7247 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u7247 \u00d7 \u221a \u00d7 \u67e5\u627e\u5e76\u66ff\u6362\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u67e5\u627e\u5e76\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u5220\u9664\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u4e3a\u4ec0\u4e48\uff1f\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5b9e\u9a8c\uff1a std::string s = \"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \uff08\u4f7f\u7528 /utf-8 \u7f16\u8bd1\uff09\u8fd0\u884c\u540e\uff0c\u4f1a\u5f97\u5230 6\u3002 \u56e0\u4e3a std::string \u7684 size() \u8fd4\u56de\u7684\u662f char \u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5728 UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u975e ASCII \u7684\u5b57\u7b26\u4f1a\u88ab\u7f16\u7801\u4e3a\u591a\u4e2a char \uff0c\u5bf9\u4e8e\u4e2d\u6587\u800c\u8a00\uff0c\u4e2d\u6587\u90fd\u5728 0x2E80 \u5230 0x9FFF \u8303\u56f4\u5185\uff0c\u5c5e\u4e8e\u4e09\u7ea7\u5217\u8f66\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e2a\u6c49\u5b57\u4f1a\u88ab\u7f16\u7801\u6210 3 \u4e2a char \u3002 char \u662f\u5b57\u8282\uff08\u7801\u4f4d\uff09\u800c\u4e0d\u662f\u771f\u6b63\u7684\u5b57\u7b26\uff08\u7801\u70b9\uff09\u3002\u771f\u6b63\u7684 Unicode \u5b57\u7b26\u5e94\u8be5\u662f char32_t \u7c7b\u578b\u7684\u3002\u8c03\u7528 std::string \u7684 size() \u6216\u8005 strlen \u5f97\u5230\u7684\u53ea\u662f\u201c\u5b57\u8282\u6570\u91cf\u201d\u3002 \u800c UTF-32 \u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\uff08\u7801\u70b9\uff09\u90fd\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684 char32_t \uff08\u7801\u4f4d\uff09\uff0c size() \u5c31\u662f\u771f\u6b63\u7684\u201c\u5b57\u7b26\u6570\u91cf\u201d\uff0c\u8fd9\u5c31\u662f\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 std::u32string s = U\"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \u5982\u679c\u4f60\u7684\u64cd\u4f5c\u53ea\u6d89\u53ca\u5b57\u7b26\u4e32\u67e5\u62fc\u63a5\u4e0e\u67e5\u627e\uff0c\u90a3\u5c31\u53ef\u4ee5\u7528 UTF-8\u3002\u5982\u679c\u5927\u91cf\u6d89\u53ca\u7d22\u5f15\uff0c\u5207\u7247\uff0c\u5355\u4e2a\u5b57\u7b26\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u5fc5\u987b\u7528 UTF-32\uff08\u5426\u5219\u4e00\u9047\u5230\u6c49\u5b57\u5c31\u4f1a\u51fa\u9519\uff09\u3002 std::vector slogan = { \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\", \"\u5168\u4e16\u754c\u7a0b\u5e8f\u5458\u5927\u56e2\u7ed3\u4e07\u5c81\", }; std::string joined; for (auto const &s: slogan) { joined += s; // \u53ea\u662f\u62fc\u63a5\u800c\u5df2\uff0cUTF-8 \u6ca1\u95ee\u9898 } UTF-8 \u6309\u7d22\u5f15\u5207\u7247\u7684\u51fa\u9519\u6848\u4f8b\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-8 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u8282\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\ufffd\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-32 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u7b26\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u53ea\u6709\u5f53\u7d22\u5f15\u6765\u81ea find \u7684\u7ed3\u679c\u65f6\uff0cUTF-8 \u5b57\u7b26\u4e32\u7684\u5207\u7247\u624d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(\"\u516c\"); // pos = 12 fmt::println(\"UTF-8 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u8282\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(U'\u516c'); // pos = 4 fmt::println(\"UTF-32 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u7b26\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u6ce8\u610f\u5230\u8fd9\u91cc UTF-8 \u7684 \"\u516c\" \u9700\u8981\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\u3002 UTF-8 \u65e0\u6cd5\u53d6\u51fa\u5355\u4e2a\u975e ASCII \u5b57\u7b26\uff0c\u5bf9\u4e8e\u5355\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ecd\u7136\u53ea\u80fd\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u8fbe\uff08\u7531\u591a\u4e2a\u5b57\u8282\u7ec4\u6210\uff09\u3002 std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-8 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u8282\uff1a{}\", s[0]); // \u53ef\u80fd\u4f1a\u6253\u5370 \u2018\u00e5\u2019 (0xE5)\uff0c\u56e0\u4e3a\u201c\u5c0f\u201d\u7684 UTF-8 \u7f16\u7801\u662f 0xE5 0xB0 0x8F // \u4e5f\u53ef\u80fd\u662f\u4e71\u7801\u201c\ufffd\u201d\uff0c\u53d6\u51b3\u4e8e\u7ec8\u7aef\u7406\u89e3\u7684\u7f16\u7801\u683c\u5f0f std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-32 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1a{}\", s[0]); // \u4f1a\u6253\u5370 \u2018\u5c0f\u2019 UTF-8 \u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u4e5f\u4f1a\u51fa\u95ee\u9898\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u4e71\u7801 std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u628a\u6309\u5b57\u7b26\u6b63\u5e38\u53cd\u8f6c\uff0c\u5f97\u5230 \u201c\u5c81\u4e07\u8bfe\u5f00\u516c\u5e08\u8001\u5f6d\u5c0f\u201d \u603b\u7ed3\uff1aUTF-8 \u53ea\u80fd\u62fc\u63a5\u3001\u67e5\u627e\u3001\u6253\u5370\u3002\u4e0d\u80fd\u7d22\u5f15\u3001\u5207\u7247\u3001\u53cd\u8f6c\u3002 \u6309\u7d22\u5f15\u5207\u7247\u4e0d\u884c\uff0c\u4f46\u5982\u679c\u7d22\u5f15\u662f find \u51fa\u6765\u7684\u5c31\u6ca1\u95ee\u9898\u3002","title":"UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c"},{"location":"unicode/#ansi-unicode","text":"\u5728 Windows \u5b98\u65b9\u7684\u8bf4\u8f9e\u4e2d\uff0c\u6709\u201cUnicode \u7f16\u7801\u201d\u548c\u201cANSI \u7f16\u7801\u201d\u7684\u8bf4\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528 Windows \u81ea\u5e26\u7684\u8bb0\u4e8b\u672c\u7a0b\u5e8f\uff0c\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u9009\u5355\uff1a \u7ffb\u8bd1\u4e00\u4e0b\uff1a \u201cANSI\u201d\u6307\u7684\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u8bbe\u7f6e\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\u3002 \u6240\u8c13\u201cUnicode\u201d\u5176\u5b9e\u6307\u7684\u662f UTF-16\u3002 \u6240\u8c13\u201cUnicode big endian\u201d\u6307\u7684\u662f\u5927\u7aef UTF-16\u3002 \u201cUTF-8\u201d\u6307\u7684\u662f UTF-8 with BOM \u800c\u4e0d\u662f\u6b63\u5e38\u7684 UTF-8\u3002 \u5b9e\u9645\u4e0a Unicode \u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u53ea\u662f\u628a\u5b57\u7b26\u6620\u5c04\u5230\u6574\u6570\uff0c\u66f4\u6ca1\u6709\u4ec0\u4e48\u5927\u7aef\u5c0f\u7aef\uff0cUTF-16 \u624d\u662f\u7f16\u7801\u683c\u5f0f\u3002 \u800c ANSI \u672c\u6765\u5e94\u8be5\u662f ASCII \u7684\u610f\u601d\uff0c char \u672c\u6765\u5c31\u53ea\u652f\u6301 ASCII\u3002 \u4f46\u7531\u4e8e\u5f53\u65f6\u5404\u56fd\u8feb\u5207\u9700\u8981\u652f\u6301\u81ea\u5df1\u672c\u56fd\u7684\u6587\u5b57\uff0c\u5c31\u5728\u517c\u5bb9 ASCII \u7684\u57fa\u7840\u4e0a\uff0c\u53d1\u5c55\u51fa\u4e86\u81ea\u5df1\u7684\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\u3002\u8fd9\u4e9b\u5f53\u5730\u7279\u4f9b\u7684\u5b57\u7b26\u96c6\u91cc\u53ea\u5305\u542b\u4e86\u672c\u56fd\u6587\u5b57\uff0c\u6240\u6709\u8fd9\u4e9b\u5404\u56fd\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u90fd\u548c UTF-8 \u7c7b\u4f3c\uff0c\u91c7\u7528\u706b\u8f66\u5934\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0c\u5bf9 0 \u5f00\u5934\u7684 ASCII \u90e8\u5206\u4e5f\u90fd\u662f\u517c\u5bb9\u3002\u6240\u4ee5 Windows \u7d22\u6027\u628a ANSI \u5f53\u4f5c\u201c\u5404\u56fd\u672c\u5730\u6587\u5b57\u7f16\u7801\u201d\u7684\u7b80\u79f0\u4e86\u3002\u4f46\u540e\u6765\u4e92\u8054\u7f51\u7684\u51fa\u73b0\uff0c\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4fe1\u606f\u4ea4\u6362\u56f0\u96be\u3002 \u4f8b\u5982\u4f60\u5728\u73a9\u4e00\u4e9b\u65e5\u672c\u7684 galgame \u65f6\uff0c\u4f1a\u53d1\u73b0\u91cc\u9762\u6587\u5b57\u5168\u90e8\u4e71\u7801\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u5728\u5404\u4e2a\u5730\u533a\u53d1\u884c\u7684\u662f\u201c\u7279\u4f9b\u7248\u201d\uff1a\u5728\u4e2d\u56fd\u5927\u9646\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 GBK \u5b57\u7b26\u96c6\uff0c\u5728\u65e5\u672c\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 Shift-JIS \u5b57\u7b26\u96c6\u3002\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u5185\u90e8\u5b58\u50a8\u7684\u662f Shift-JIS \u7684\u90a3\u4e9b\u201c\u6574\u6570\u201d\u3002\u8fd9\u5bfc\u81f4\u65e5\u672c\u7684 galgame \u5728\u4e2d\u56fd\u5927\u9646\u7279\u4f9b\u7684 Windows \u4e2d\uff0c\u628a Shift-JIS \u7684\u201c\u6574\u6570\u201d\u7528 GBK \u7684\u8868\u6765\u89e3\u8bfb\u4e86\uff0c\u4ece\u800c\u4e71\u7801\uff08GBK \u91cc\u7684\u65e5\u6587\u533a\u57df\u5e76\u6ca1\u6709\u548c Shift-JIS \u91cd\u53e0\uff09\u3002\u9700\u8981\u7528 Locale Emulator \u628a Shift-JIS \u7ffb\u8bd1\u6210 Unicode \u8bfb\u7ed9 Windows \u542c\u3002\u5982\u679c\u65e5\u672c\u7a0b\u5e8f\u5458\u4ece\u4e00\u5f00\u59cb\u5c31\u7edf\u4e00\u7528 Unicode \u6765\u5b58\u50a8\uff0c\u4e2d\u56fd\u533a\u73a9\u5bb6\u7684 Windows \u4e5f\u7edf\u4e00\u7528 Unicode \u89e3\u6790\uff0c\u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cUnicode \u7ec4\u7ec7\u51fa\u73b0\u4e86\uff0c\u4ed6\u7684\u4f7f\u547d\u5c31\u662f\u7edf\u4e00\u5168\u4e16\u754c\u7684\u5b57\u7b26\u96c6\uff0c\u4fdd\u8bc1\u5168\u4e16\u754c\u6240\u6709\u7684\u6587\u5b57\u90fd\u80fd\u5728\u5168\u4e16\u754c\u6240\u6709\u7684\u8ba1\u7b97\u673a\u4e0a\u663e\u793a\u51fa\u6765\u3002\u9996\u5148\u521b\u529e\u4e86 Unicode \u5b57\u7b26\u96c6\uff0c\u7136\u540e\u89c4\u5b9a\u4e86 UTF-8\u3001UTF-16\u3001UTF-32 \u4e09\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u6700\u7ec8 UTF-8 \u6210\u4e3a\u5916\u7801\u7684\u4e3b\u6d41\uff0cUTF-32 \u6210\u4e3a\u5185\u7801\u7684\u4e3b\u6d41\u3002 \u63a5\u4e0b\u6765\u4e3a\u4e86\u65b9\u4fbf\u8bb0\u5fc6\uff0c\u6211\u4eec\u7d22\u6027\u5c31\u987a\u7740\u5fae\u8f6f\u7684\u8fd9\u4e2a\u8bf4\u6cd5\uff1a \u7ba1 char \u53eb ANSI\uff1a\u968f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002 \u7ba1 wchar_t \u53eb Unicode\uff1a\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u5728 Linux \u4e0a\u662f UTF-32\u3002","title":"\u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48"},{"location":"unicode/#utf-16_1","text":"\u5fae\u8f6f\u7ba1 UTF-16 \u53eb Unicode \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u95ee\u9898\uff1a \u56e0\u4e3a\u5f53\u5e74 Unicode 5.0 \u7684\u65f6\u5019\u53ea\u6709 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff0c16 \u4f4d\u5c31\u88c5\u5f97\u4e0b\uff0c\u6240\u4ee5\u5f53\u65f6 UTF-16 \u8fd8\u662f\u4e00\u4e2a \u5b9a\u957f\u7f16\u7801 \u3002\u5fae\u8f6f\u4e8e\u662f\u51b3\u5b9a\u628a wchar_t \u5b9a\u4e49\u6210 2 \u5b57\u8282\uff0c\u5e76\u5728 NT \u5185\u6838\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528\u90fd\u5347\u7ea7\u6210\u4e86\u57fa\u4e8e wchar_t \u5b57\u7b26\u4e32\u7684 \u201cW \u7cfb\u201d API\u3002 \u6bd4\u5c14\u76d6\u5b50\u5f53\u65f6\u4ee5\u4e3a\u8fd9\u6837 UTF-16 \u5b9a\u957f\u5185\u7801\u5c31\u4e00\u52b3\u6c38\u9038\u4e86\uff0c\u5e76\u53f7\u53ec\u6240\u6709\u7a0b\u5e8f\u90fd\u6539\u7528 UTF-16 \u505a\u5185\u7801\uff0c\u522b\u7528 \u201cA \u7cfb\u201d API \u4e86\u3002 \u8d77\u521d\uff0c\u6240\u6709\u4eba\u90fd\u4ee5\u4e3a UTF-16 \u5c31\u662f\u6700\u7ec8\u7b54\u6848\u3002 \u6ca1\u60f3\u5230\u540e\u6765 Unicode \u59d4\u5458\u4f1a\u201c\u80cc\u523a\u201d\u4e86\u6bd4\u5c14\u76d6\u5b50\uff01\u5077\u5077\u628a\u8303\u56f4\u66f4\u65b0\u5230\u4e86 0x10FFFF\uff0c\u7a81\u7834\u4e86 16 \u4f4d\u6574\u6570\u7684\u5bb9\u91cf\u3002\u539f\u6765\u7684 UTF-16 \u5df2\u7ecf\u5bb9\u7eb3\u4e0d\u4e0b\uff0c\u53ea\u597d\u5229\u7528\u4e4b\u524d\u9884\u7559\u7684 0xD800 \u5230 0xDFFF \u7a7a\u53f7\u533a\u95f4\u4e11\u964b\u5730\u5b9e\u73b0\u4e86\u53d8\u957f\u7f16\u7801\u3002 \u76f4\u5230 UTF-16 \u4e00\u591c\u4e4b\u95f4\u6210\u4e86\u4e11\u964b\u7684 \u53d8\u957f\u7f16\u7801 \u3002 \u95f9\u4e86\u534a\u5929\uff0cWindows \u8d39\u5fc3\u8d39\u529b\u66ff Unicode \u59d4\u5458\u4f1a\u597d\u4e0d\u5bb9\u6613\u63a8\u5e7f\u7684 wchar_t \uff0c\u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801 \u7684\u597d\u5904\u3002\u53ef \u201cW \u7cfb\u201d API \u5374\u53c8\u710a\u6b7b\u5728\u4e86 NT \u5185\u6838\u6700\u5e95\u5c42\uff0c\u53cd\u590d\u6765\u5751\u7b2c\u4e00\u6b21\u7528 Windows \u7f16\u7a0b\u7684\u521d\u5b66\u8005\u3002 \u6bd4\u5c14\u76d6\u5b50\uff1a\u4f60\u8fd9\u6837\u663e\u5f97\u6211\u5f88\u5c0f\u4e11\u8bf6\uff1f \u9664 Windows \u5916\uff0cJava \u4e5f\u662f\u201cUTF-16 \u80cc\u523a\u201d\u7684\u53d7\u5bb3\u8005\uff0c\u4ed6\u4eec\u60f3\u5f53\u7136\u7684\u628a char \u5b9a\u4e49\u4e3a UTF-16\uff0c\u4ee5\u4e3a\u8fd9\u5c31\u662f\u672a\u6765\u6c38\u4e45\u7684\u5b9a\u957f\u5185\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u2026\u2026 \u76f4\u5230 Unicode \u52a0\u5165\u4e86 0x10FFFF\uff0cJava \u4e0d\u5f97\u4e0d\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e2a Character \u4f5c\u4e3a UTF-32 \u5b57\u7b26\uff0c\u8fd8\u5f04\u4e2a char \u5230 Character \u7684\u8f6c\u6362\uff0c\u597d\u4e0d\u5c34\u5c2c\uff01 Linux \u6210\u7acb\u4e8e 1991 \u5e74\uff0c\u5f53\u65f6 Unicode \u4e5f\u624d\u521a\u521a\u51fa\u73b0\u3002Unicode \u5ba3\u5e03\u52a0\u5165 0x10FFFF \u540e\uff0cLinux \u624d\u5f00\u59cb\u5f15\u5165\u652f\u6301 Unicode\u3002\u5728\u77e5\u9053\u4e86 Unicode \u5305\u542b 0x10FFFF \u540e\uff0c\u4ed6\u4eec\u4e00\u5f00\u59cb\u5c31\u628a wchar_t \u5b9a\u4e49\u6210 4 \u5b57\u8282\uff0c\u9003\u8fc7\u4e86 UTF-16 \u7684\u80cc\u523a\u3002 \u540e\u6765\u65b0\u51fa\u7684\u8bed\u8a00\uff0c\u5982 Python 3\u3001Go\u3001Rust\u3001Swift\u3001Kotlin\uff0c\u628a\u5b57\u7b26\u94a6\u5b9a\u4e3a UTF-32 \u4e86\u3002\u4ed6\u4eec\u53ea\u6709\u5728\u8c03\u7528 Windows API \u65f6\uff0c\u624d\u4f1a\u4e34\u65f6\u8f6c\u6362\u4e3a UTF-16 \u6765\u8c03\u7528\uff0c\u9664\u6b64\u4e4b\u5916\u518d\u65e0 UTF-16 \u51fa\u73b0\u3002 \u8bb8\u591a\u7cdf\u7cd5\u7684\u535a\u5ba2\u58f0\u79f0\uff1a\u662f\u56e0\u4e3a\u201cUTF-16 \u6700\u6709\u5229\u4e8e\u4e2d\u6587\u538b\u7f29\u201d\uff0c\u6240\u4ee5 Java \u548c Windows \u624d\u91c7\u7528\u7684\uff1f\u7136\u800c\u5c31\u6211\u4e86\u89e3\u5230\u7684\u5b9e\u9645\u60c5\u51b5\u662f\u56e0\u4e3a\u4ed6\u4eec\u9519\u8bef\u7684\u4ee5\u4e3a 0xFFFF \u662f Unicode \u7684\u4e0a\u9650\u624d\u9519\u8bef\u91c7\u7528\u4e86\uff0c\u4e0d\u7136\u4e3a\u4ec0\u4e48\u540e\u6765\u7684\u65b0\u8bed\u8a00\u90fd\u91c7\u7528\u4e86 UTF-32 \u5185\u7801 + UTF-8 \u5916\u7801\u7684\u7ec4\u5408\uff1f\u800c\u4e14\u5728\u5916\u7801\u4e2d\u91c7\u7528 UTF-8 \u6216 UTF-16 \u538b\u7f29\u786e\u5b9e\u6ca1\u95ee\u9898\uff0c\u4f46\u662f Java \u548c Windows \u7684\u5931\u8bef\u5728\u4e8e\u628a UTF-16 \u5f53\u4f5c\u5185\u7801\u4e86\uff01\u5185\u7801\u5c31\u7406\u5e94\u662f\u5b9a\u957f\u7f16\u7801\u7684\u624d\u65b9\u4fbf\uff0c\u5982\u679c\u4f60\u6709\u4e0d\u540c\u60f3\u6cd5\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002 \u603b\u4e4b\uff0cUTF-16 \u662f\u7cdf\u7c95\uff0c\u4f46\u4ed6\u662f Windows \u552f\u4e00\u5b8c\u6574\u652f\u6301\u7684 Unicode \u63a5\u53e3\u3002\u4e0d\u5efa\u8bae\u8f6f\u4ef6\u5185\u90e8\u7528 UTF-16 \u5b58\u50a8\u6587\u5b57\uff0c\u4f60\u53ef\u4ee5\u7528\u66f4\u7d27\u51d1\u7684 UTF-8 \u6216\u66f4\u65b9\u4fbf\u5207\u7247\u7684 UTF-32\uff0c\u53ea\u9700\u5728\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u524d\u4e34\u65f6\u8f6c\u6362\u6210 UTF-16 \u5c31\u884c\u3002","title":"\u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a"},{"location":"unicode/#stdu8string","text":"\u5fc5\u987b\u6307\u51fa\uff1a\u5728 std::string \u4e2d\u88c5 UTF-8 \u5e76\u4e0d\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728 std::u8string \u91cc\u540c\u6837\u53ef\u4ee5\u88c5 GBK\u3002\u8fd9\u5c31\u597d\u6bd4\u4e00\u4e2a\u540d\u53eb Age \u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b9e\u9645\u5374\u88c5\u7740\u6027\u522b\u4e00\u6837\u3002 enum Age { // \u9519\u8bef\u793a\u8303 Male, Female, Custom, }; // \u9664\u4e86\u8ff7\u60d1\u540c\u4e8b\u5916\uff0c\u628a\u5e74\u9f84\u548c\u6027\u522b\u7684\u7c7b\u578b\u6df7\u7528\u6ca1\u6709\u597d\u5904 void registerStudent(Age age, Age sex); \u533a\u5206\u7c7b\u578b\u53ea\u662f\u5927\u591a\u6570\u4eba\u8bbe\u8ba1\u63a5\u53e3\u7684\u89c4\u8303\uff0c\u53ea\u662f\u65b9\u4fbf\u4f60\u901a\u8fc7\u770b\u51fd\u6570\u63a5\u53e3\u4e00\u773c\u533a\u5206\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u7684\u662f\u4ec0\u4e48\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u6ca1\u6709\u5f3a\u5236\u6027\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e00\u770b\u5c31\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u9700\u8981\u7684\u662f\u4ec0\u4e48\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 void thisFuncAcceptsANSI(std::string msg); void thisFuncAcceptsUTF8(std::u8string msg); void thisFuncAcceptsUTF16(std::u16string msg); void thisFuncAcceptsUnicode(std::wstring msg); void thisFuncAcceptsUTF32(std::u32string msg); \u6ca1\u6709 char8_t \u4e4b\u524d\uff0c\u7528\u7c7b\u578b\u522b\u540d\u540c\u6837\u53ef\u4ee5\u8d77\u5230\u5dee\u4e0d\u591a\u7684\u8bf4\u660e\u6548\u679c\uff08\u7f3a\u70b9\u662f\u65e0\u6cd5\u91cd\u8f7d\uff09\uff1a using ANSIString = std::string; using UTF8String = std::string; using UTF16String = std::vector; void thisFuncAcceptsANSI(ANSIString msg); void thisFuncAcceptsUTF8(UTF8String msg); void thisFuncAcceptsUTF16(UTF16String msg); \u4e4b\u6240\u4ee5\u6211\u4f1a\u8bf4\uff0c std::string \u5e94\u8be5\u88c5 ANSI \u5b57\u7b26\u4e32\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u6807\u51c6\u5e93\u5b98\u65b9\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5047\u5b9a std::string \u7c7b\u578b\u662f ANSI \u7f16\u7801\u683c\u5f0f\uff08GBK\uff09\u3002\u5e76\u4e0d\u662f\u8bf4\uff0c\u4f60\u4e0d\u80fd\u7528 std::string \u5b58\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u7684\u5185\u5bb9\u3002 \u5982\u679c\u4f60\u5c31\u662f\u60f3\u7528 std::string \u88c5 UTF-8 \u4e5f\u53ef\u4ee5\uff0c\u53ea\u4e0d\u8fc7\u4f60\u8981\u6ce8\u610f\u5728\u4f20\u5165\u6240\u6709\u4f7f\u7528\u4e86\u6587\u4ef6\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5982 fopen \uff0c std::ifstream \u7684\u6784\u9020\u51fd\u6570\u524d\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u8f6c\u6362\uff0c\u8f6c\u6210 GBK \u7684 std::string \u6216 UTF-16 \u7684 std::wstring \u540e\uff0c\u624d\u80fd\u4f7f\u7528\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u5982\u679c\u4f60\u59cb\u7ec8\u7528 std::u8string \u88c5 UTF-8\uff0c\u90a3\u4e48\u5f53\u4f60\u628a\u5b83\u8f93\u5165\u4e00\u4e2a\u63a5\u53d7 ANSI \u7684\u666e\u901a std::string \u53c2\u6570\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef\uff0c\u5f3a\u8feb\u4f60\u91cd\u65b0\u6e05\u9192\uff0c\u6216\u662f\u5f3a\u8feb\u4f60\u4f7f\u7528\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\uff0c\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u8fd9\u4e2a\u8f6c\u6362\u51fd\u6570\u7684\u5199\u6cd5\u3002 \u4f8b\u5982\u5f53\u4f60\u4f7f\u7528 std::cout << u8string \u65f6\u4f1a\u62a5\u9519\uff0c\u8feb\u4f7f\u4f60\u6539\u4e3a std::cout << u8toansi(u8string) \u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff0c\u4ece\u800c\u907f\u514d\u4e86\u628a UTF-8 \u7684\u5b57\u7b26\u4e32\u6253\u5370\u5230\u4e86\u53ea\u652f\u6301 GBK \u7684\u63a7\u5236\u53f0\u4e0a\u3002 \u5176\u4e2d\u8f6c\u6362\u51fd\u6570\u7b7e\u540d\u4e3a std::string u8toansi(std::u8string s) \uff0c\u5f88\u53ef\u60dc\uff0c\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u51fd\u6570\uff0c\u76f4\u5230 C++26 \u524d\uff0c\u6807\u51c6\u5e93\u5bf9\u5b57\u7b26\u7f16\u7801\u652f\u6301\u4e00\u76f4\u5f88\u5dee\uff0c\u4f60\u4e0d\u5f97\u4e0d\u81ea\u5df1\u5b9e\u73b0\u6216\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u3002 \u603b\u4e4b\uff0c char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u5411\u8c03\u7528\u8005\u6697\u793a\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5982\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a thisFuncAcceptUTF8(std::u8string msg); \u5982\u679c\u8c03\u7528\u8005\u559c\u6b22\u7528 std::string \u88c5 UTF-8 \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\uff1a std::string msg; // \u8c03\u7528\u8005\u786e\u4fe1\uff0c\u8fd9\u4e2a msg \u867d\u7136\u662f `std::string`\uff0c\u4f46\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f UTF-8 // \u90a3\u4e48\u4ed6\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e3a u8string\uff0c\u6765\u8bc1\u660e\u81ea\u5df1\u5934\u8111\u6e05\u9192 thisFuncAcceptUTF8(std::u8string((char8_t *)msg.data(), msg.size()));","title":"\u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae"},{"location":"unicode/#_9","text":"C++ \u5b98\u65b9\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e24\u79cd\u5b57\u7b26\u96c6\u3002\u4e00\u79cd\u662f \u6e90\u7801\u5b57\u7b26\u96c6 (source charset) \uff0c\u4e00\u79cd\u662f \u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u3002 \u8fd9\u771f\u662f\u7cdf\u7cd5\u7684\u672f\u8bed\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u8fd9\u4e2a\u540d\u5b57\u5177\u6709\u8bef\u5bfc\u6027\uff0c\u4ed6\u548c\u8fd0\u884c\u65f6\u6839\u672c\u6ca1\u6709\u5173\u7cfb\uff0c\u660e\u660e\u662f\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\u3002\u6240\u4ee5\u5c0f\u5f6d\u8001\u5e08\u66ff\u4ed6\u6539\u4e2a\u540d\u5b57\uff0c\u5b9e\u9645\u5e94\u8be5\u53eb\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u96c6\u201d\u3002 \u800c\u4e14\u4ed6\u4eec\u53eb\u5b57\u7b26\u96c6\u4e5f\u4e0d\u5408\u7406\uff0c\u5e94\u8be5\u53eb\u5b57\u7b26\u7f16\u7801\u624d\u5bf9\uff0cUTF-8 \u548c UTF-16 \u90fd\u662f Unicode \u5b57\u7b26\u96c6\u7684\u4e24\u79cd\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u4ed6\u4eec\u660e\u663e\u662f\u4e0d\u540c\u7684\u3002 \u7136\u540e\uff0c\u518d\u5f15\u5165\u4e00\u4e2a\u771f\u6b63\u7684\uff0c\u8fd0\u884c\u65f6\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u8f6f\u4ef6\u5ba2\u6237\u7535\u8111\u7684\u533a\u57df\u8bbe\u7f6e\u3002 \u6700\u7ec8\uff0c\u7ecf\u8fc7\u5c0f\u5f6d\u8001\u5e08\u6539\u826f\u7684\u672f\u8bed\u5982\u4e0b\uff1a \u6e90\u7801\u5b57\u7b26\u7f16\u7801: .cpp \u6e90\u7801\u6587\u4ef6\u65f6\u7528\u7684\u5b57\u7b26\u7f16\u7801\u3002\u4f8b\u5982\u7a0b\u5e8f\u5458\u7528\u8bb0\u4e8b\u672c\u4fdd\u5b58 .cpp \u6e90\u7801\u6587\u4ef6\u65f6\uff0c\u9009\u62e9 \u201cUTF-8\u201d \u4fdd\u5b58\u5c31\u662f UTF-8\uff0c\u9009\u62e9 \u201cANSI\u201d \u4fdd\u5b58\u5c31\u662f GBK\u3002 \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f char \u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u5b57\u7b26\u7f16\u7801\u3002\u9ed8\u8ba4\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff08\u5f00\u53d1\u8005\uff09\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f\u6211\u4eec\u7684\u7a0b\u5e8f\u5728\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fd0\u884c\u65f6\uff0c\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf API \u7684 const char * \u671f\u671b\u63a5\u53d7\u600e\u6837\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002\u9ed8\u8ba4\u662f\u5ba2\u6237\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd9\u4e09\u4e2a\u53ef\u4ee5\u5404\u6709\u4e0d\u540c\u3002 \u5176\u4e2d \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801 \u548c \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801 \u7684\u4e0d\u5339\u914d\uff0c\u662f Windows \u8f6f\u4ef6\u51fa\u73b0\u4e71\u7801\u7684\u4e3b\u8981\u539f\u56e0\u3002 \u800c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u53ea\u4e8b\u5173\u4f60\u5982\u4f55\u4fdd\u5b58\u6e90\u7801\uff0c\u53ea\u662f\u8ba9\u7f16\u8bd1\u5668\u80fd\u591f\u6210\u529f\u8bfb\u53d6\u4f60\u7684\u6e90\u7801\uff0c\u5bf9\u8fd0\u884c\u65f6\u7684\u4e71\u7801\u95ee\u9898\u6ca1\u6709\u5f71\u54cd\u3002\u7f16\u8bd1\u5668\u8bfb\u5b8c\u6e90\u7801\u540e\uff0c\u8981\u5728\u5e38\u91cf\u533a\u751f\u6210\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u8fd8\u662f\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u7684\u3002 \u4f8b\u5982\u4e4b\u524d\u8bf4\u7684\u65e5\u672c galgame \u5728\u4e2d\u56fd\u7535\u8111\u4e0a\u6253\u5f00\u7206\u51fa\u4e71\u7801\uff0c\u5c31\u662f\u56e0\u4e3a\u662f\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u4e86 galgame\uff08\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u4e3a Shift-JIS\uff09\uff0c\u5728\u4e2d\u56fd\u5ba2\u6237\u7535\u8111\u4e0a\u6253\u5f00\uff08\u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801\u4e3a GBK\uff09\u5bfc\u81f4\u7684\u3002 \u65e5\u672c\u7a0b\u5e8f\u5458\u4f7f\u7528\u4ec0\u4e48\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u6839\u672c\u65e0\u5173\u7d27\u8981\u2026\u2026\u54ea\u6015\u4ed6\u4eec\u4f7f\u7528\u4e86 UTF-8 \u4fdd\u5b58\u6e90\u7801\uff0cMSVC \u7f16\u8bd1\u65f6\u4ecd\u7136\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a Shift-JIS \u7f16\u7801\u7684\u5b57\u9762\u91cf\u6765\u5b58\u50a8\u5728\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5e38\u91cf\u533a\u4e2d\u3002 \u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a\u8fd9\u4e9b\u82b1\u91cc\u80e1\u54e8\u73a9\u5e94\u7edf\u4e00\u4e3a UTF-8\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u8f6c\u6362\u6765\u8f6c\u6362\u53bb\u4e86\uff1f\u8fd8\u4e0d\u662f\u56e0\u4e3a\u5386\u53f2\u9057\u7559\uff0c\u4e00\u4e9b\u52b3\u4fdd\u7a0b\u5e8f\u5458\u4e0d\u80af\u628a\u4ed6\u4eec GBK \u7f16\u7801\u7684\u6e90\u7801\u6539\u6210 UTF-8 \u4fdd\u5b58\uff0c\u800c\u4e14\uff0cWindows \u4e5f\u5b8c\u5168\u4e0d\u63d0\u4f9b\u57fa\u4e8e UTF-8 \u7684\u8de8\u5e73\u53f0 API\uff08\u53ea\u63d0\u4f9b GBK \u548c UTF-16 \u4e24\u79cd\uff0c\u5c31\u662f\u4e0d\u7ed9 UTF-8 \u7684\uff0c\u975e\u5e38\u6076\u5fc3\u4eba\uff09\u3002\u6240\u4ee5 MSVC \u81f3\u4eca\u4ecd\u7136\u9ed8\u8ba4\u662f GBK \u7f16\u7801\u7684\uff08\u66f4\u51c6\u786e\u7684\u8bf4\u662f ANSI\uff0c\u8ddf\u968f\u4f60\u7cfb\u7edf\u7684\u533a\u57df\u8bbe\u7f6e\u800c\u53d8\uff0c\u5728\u4e2d\u56fd\u5c31 GBK\uff0c\u5728\u7f8e\u56fd\u5c31 UTF-8\uff0c\u5728\u6b27\u6d32\u5c31 Latin-1\uff0c\u975e\u5e38\u7684\u53cc\u6807\uff09\u3002\u5fae\u8f6f\u5404\u79cd\u626f\u76ae\u6548\u7387\u4f4e\u4e0b\uff0cAPI \u5f04\u4e86\u4e00\u5957\u53c8\u4e00\u5957\u4e92\u76f8\u6781\u9650\u62c9\u626f\uff0c\u800c\u6211\u4eec Linux \u548c GCC \u65e9\u5df2\u9ed8\u8ba4\u5c31\u662f UTF-8\u2026\u2026 \u6211\u7406\u89e3\u4f60\u73b0\u5728\u5927\u8111\u5e72\u70e7\u7684\u5fc3\u60c5\u3002\u4f3a\u5019\u8fd9\u4e9b\u5386\u53f2\u7b54\u8fa9\u5f88\u590d\u6742\uff0c\u4e5f\u5f88\u65e0\u804a\uff0c\u6beb\u65e0\u610f\u4e49\uff01\u53ea\u662f\u4e3a\u4e86\u64e6\u53cd Unicode \u52b3\u4fdd\u7684\u5c41\u80a1\u3002","title":"\u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6"},{"location":"unicode/#_10","text":"\u5bf9\u4e8e\u8de8\u5e73\u53f0\u8f6f\u4ef6\u6765\u8bf4\uff0c\u6211\u63a8\u8350\u5927\u5bb6\u628a\u4e09\u4e2a\u5168\u90e8\u8bbe\u4e3a UTF-8\uff01\uff08\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4e3b\u8981\u662f\u4f3a\u5019 MSVC\uff09 Linux + GCC \u7528\u6237\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u4f60\u4eec\u6240\u6709\u5b57\u7b26\u96c6\u9ed8\u8ba4\u7684\u8bbe\u5b9a\u5c31\u662f UTF-8\u3002 Windows + MSVC \u7528\u6237\u8bf7\u5f00\u542f /utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\uff0c\u73b0\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u90fd\u662f UTF-8 \u4e86\u3002 Windows + MinGW \u7528\u6237\u8bf7\u5f00\u542f -finput-charset=utf-8 \u548c -fexec-charset=utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\u3002 \u6240\u6709\u6e90\u7801\u6587\u4ef6\u7edf\u4e00\u4ee5 UTF-8 \u7f16\u7801\u4fdd\u5b58\uff0c\u4e14\u5c3d\u91cf\u5728\u6700\u524d\u9762\u52a0\u4e0a 0xFEFF \u8fd9\u4e2a BOM \u6807\u8bb0\uff0c\u9632\u6b62 MSVC \u8111\u62bd\u5f53\u4f5c GBK \u6765\u8bfb\u53d6\u3002 \u5728 main \u51fd\u6570\u524d\uff0c\u52a0\u4e24\u884c\uff1a // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 return 0; } \u8fd9\u6837\u4e00\u5957\u6253\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u6587\u672c\u7f16\u8f91\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u7801\uff0c\u4f60\u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u8bfb\u53d6\u6e90\u7801\uff0c\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6b63\u786e\u7684\u628a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8def\u5f84\u8f6c\u4e3a UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u3002 \u5728 CMake \u4e2d\uff0c\u53ea\u5bf9 MSVC \u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a if (MSVC) target_compile_options(\u4f60\u7684\u7a0b\u5e8f PRIVATE /utf-8) else() \u4e5f\u53ef\u4ee5\u5728\u6700\u524d\u9762 add_compile_options \uff0c\u5b9e\u73b0\u5bf9\u6240\u6709\u4e4b\u540e\u5b9a\u4e49\u7684\u7a0b\u5e8f\u5168\u5c40\u542f\u7528\u8be5\u9009\u9879\u3002 \u5728\u6211\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u90fd\u4f1a\u8fd9\u6837\u5f00\u542f\uff0c\u89e3\u51b3 MSVC \u4e0d\u8de8\u5e73\u53f0\u7684\u95ee\u9898\uff1a if (MSVC) add_compile_options(/Zc:preprocessor /utf-8 /DNOMINMAX /D_USE_MATH_DEFINES /EHsc /bigobj) else() if (WIN32) add_compile_options(-finput-charset=utf-8 -fexec-charset=utf-8) endif() add_compile_options(-Wall -Wextra -Werror=return-type) endif() add_executable(\u4f60\u7684\u7a0b\u5e8f \u4f60\u7684\u6587\u4ef6.cpp) # \u81ea\u52a8\u7ee7\u627f\u4e86\u4e0a\u9762\u6240\u6709\u7684\u7f16\u8bd1\u5668\u9009\u9879","title":"\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a"},{"location":"unicode/#utf-8-locale","text":"Windows \u5b98\u65b9\u63d0\u4f9b\u7684\u771f\u6b63 API \u662f _wfopen \u3002 fopen \u53ea\u662f\u4ed6\u4eec\u63d0\u4f9b\u7684\u201cPOSIX \u517c\u5bb9\u5c42\u201d \u5305\u88c5\uff0c\u5176\u4f1a\u628a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u901a\u8fc7 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u540e\uff0c\u8f6c\u53d1\u7ed9 _wfopen \u3002 \u51fa\u4e8e\u8de8\u5e73\u53f0\u7684\u8981\u6c42\uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 _wfopen \u8fd9\u79cd\u5176\u4ed6\u5e73\u53f0\u6ca1\u6709\u7684\u51fd\u6570\uff0c\u4e5f\u4e0d\u60f3\u7528\u90a3\u8fde 2 \u5b57\u8282 4 \u5b57\u8282\u90fd\u98d8\u5ffd\u4e0d\u5b9a\u7684 wchar_t \uff0c\u66f4\u4e0d\u60f3\u8ba9 std::string \u5b58\u6839\u672c\u4e0d\u80fd\u8de8\u5e73\u53f0\u7684 GBK\u3002 \u53ea\u8981\u8ba9 fopen \u7684 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u51fd\u6570\u66ff\u6362\u6210 \u201cUTF-8 \u5230 UTF-16\u201d \u5c31\u884c\u4e86\u3002\u8fc7\u53bb\uff0c\u6211\u4eec\u65e0\u6cd5\u66ff\u6362\uff0c\u6700\u65b0\u7684 Windows \u5728\u4e00\u6b21\u66f4\u65b0\u4e2d\uff0c\u652f\u6301\u4e86 \".utf-8\" locale \u8fd9\u4e00\u9ed1\u79d1\u6280\uff0c\u4e13\u95e8\u6ee1\u8db3\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u3002 // \u9ed8\u8ba4 locale fopen(\"\u4f60\u597d.txt\") == _wfopen(gbk_to_utf16(\"\u4f60\u597d.txt\")); // \u8bbe\u7f6e\u4e86 utf-8 locale \u540e fopen(\"\u4f60\u597d.txt\") == _wfopen(utf8_to_utf16(\"\u4f60\u597d.txt\")); \u82e5\u4e0d\u8bbe\u7f6e setlocale(LC_ALL, \".utf-8\") \uff0c\u5219 fopen \u548c ifstream \u9ed8\u8ba4\u4f1a\u628a\u4f60\u63d0\u4f9b\u7684 const char * \u6587\u4ef6\u8def\u5f84\uff0c\u5f53\u4f5c GBK \u7f16\u7801\u7684\uff0c\u800c\u6211\u4eec\u8bbe\u7f6e\u4e86 /utf-8 \u6216 -fexec-charset=utf-8 \u540e\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7f16\u7801\u5df2\u7ecf\u662f UTF-8 \u4e86\uff0c\u8fd9\u6837 UTF-8 \u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u8f93\u5165\u8fdb\u671f\u671b const char * \u7684 fopen \u53c2\u6570\uff0c\u5c31\u4f1a\u51fa\u4e71\u7801\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c .utf-8 locale \u53ea\u662f\u5f71\u54cd\u4e86\u6807\u51c6\u5e93\uff01\u5e76\u4e0d\u6539\u53d8\u7cfb\u7edf API\u3002 \u76f4\u63a5\u8c03\u7528\u7cfb\u7edf API \u65f6\uff0cA \u7cfb API \u4ecd\u7136\u6709\u95ee\u9898\u3002 MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u63d0\u793a\", MB_OK); // \u4e0d\u884c\uff0c.utf-8 \u53ea\u662f\u8ba9\u6807\u51c6\u5e93\u53d8\u6210 UTF-8 \u63a5\u53e3\u4e86\uff0cA \u7cfb Windows API \u4ecd\u7136\u662f GBK MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u63d0\u793a\", MB_OK); // \u6ca1\u95ee\u9898\uff01\u7528 UTF-16 \u7684 wchar_t \u5b57\u9762\u91cf\u6765\u8c03\u7528 W \u63a5\u53e3\u603b\u662f\u6ca1\u95ee\u9898\u7684 \u8fd8\u662f\u9700\u8981\u6211\u4eec\u624b\u52a8\u8f6c\u6362 UTF-8 \u5230 UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u2026\u2026\u4f46\u662f\u53cd\u6b63\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u5f88\u5c11\u9700\u8981\u76f4\u63a5\u8c03\u7528 Windows API\uff0c\u90fd\u662f\u901a\u8fc7\u901a\u7528\u7684 C/C++ \u6807\u51c6\u5e93\uff0c\u56e0\u6b64 .utf-8 locale \u53ef\u80fd\u662f\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u60f3\u8fdb\u519b UTF-8 \u7684\u6700\u4f73\u9009\u62e9\u3002","title":".utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684"},{"location":"unicode/#b-wchar_t","text":"\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\uff0c\u90fd\u662f\u9488\u5bf9 char \u7684\uff0c\u53ea\u6709 char \u88ab\u6545\u610f\u9488\u5bf9\u4e86\uff0c\u5b58\u5728\u5b57\u7b26\u7f16\u7801\u4e0d\u7edf\u4e00\u7684\u95ee\u9898\u3002 \u5982\u679c\u5168\u90e8\u7528 wchar_t \u7684\u8bdd\uff0c\u867d\u7136\u5728 Linux \u4e0a\u662f UTF-32\uff0c\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u4e0d\u7edf\u4e00\u4e86\u3002\u4f46\u81f3\u5c11\u5728\u540c\u4e00\u4e2a Windows \u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u90fd\u662f\u7edf\u4e00\u7684 UTF-16\u3002 \u6240\u4ee5\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u662f\u5168\u9762\u91c7\u7528 wchar_t \u548c std::wstring \uff0c\u8fd9\u6837\u65e0\u8bba\u4f60\u7684\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\u5982\u4f55\uff0c\u90fd\u5bf9 wchar_t \u548c\u57fa\u4e8e const wchar_t * \u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002 C \u8bed\u8a00\u6807\u51c6\u6ca1\u6709 _wfopen \uff0c\u4f46\u662f std::ifstream \u6709\u57fa\u4e8e std::wstring \u7684\u6784\u9020\u51fd\u6570\uff0c\u5c31 C++ \u6807\u51c6\u5e93\u6765\u770b std::wstring \u7684\u652f\u6301\u8fd8\u662f\u5f88\u4e30\u5bcc\u7684\uff0c\u57fa\u672c std::string \u6709\u7684 std::wstring \u90fd\u6709\uff0c\u4f8b\u5982 std::to_string \u548c std::to_wstring \uff0c std::cout \u548c std::wcout \u3002\u672c\u7ae0\u8282\u6700\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5bbd\u5b57\u7b26\u6d41\u7684\u7528\u6cd5\u3002 \u7f3a\u70b9\u662f\uff0c\u9996\u5148\u6bcf\u6b21\u90fd\u9700\u8981\u5199 L\"\u4f60\u597d\" \u8fd9\u4e2a L \u524d\u7f00\u5f88\u9ebb\u70e6\uff0c\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u4e14\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u90fd\u5728\u7528 std::string \uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b std::wstring \u7684 API\u3002 \u4f8b\u5982 openvdb \u7684\u6587\u4ef6\u5199\u5165\u51fd\u6570\uff1a void openvdb::io::File::write(std::string const &filename); \u8fd9\u6837\u5c31\u5f88\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f60\u5185\u90e8\u5168\u662f UTF-16 \u7684 std::wstring \u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u8c03\u7528\u7b2c\u4e09\u65b9\u5e93\u524d\u5c31\u9700\u8981\u8f6c\u6210 GBK \u7684 std::string \u3002\u53ef\u4ee5\u7528 boost::locale::conv::to_utf \u8fd9\u4e2a\u51fd\u6570\u8f6c\u6362\uff0c\u4f46\u4e5f\u5f88\u9ebb\u70e6\uff0c\u800c\u4e14\u5982\u679c std::wstring \u542b\u6709 GBK \u8303\u56f4\u4e4b\u5916\u7684 \u201c\ud883\udede\u201d\uff0cGBK \u65e0\u6cd5\u8868\u793a\uff0c\u53c8\u4f1a\u6709\u7f16\u7801\u5931\u8d25\u7684\u95ee\u9898\u3002 \u8fd8\u6709 stbi_load \u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u662f\u53ea\u63d0\u4f9b\u4e86 const char * \u7684\u63a5\u53e3\uff0c\u591a\u4e86\u53bb\u4e86\u3002 setlocale(LC_ALL, \".utf-8\") \u7684\u597d\u5904\u662f\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5168\u81ea\u52a8\u90fd\u4ece GBK \u65e0\u7f1d\u5207\u6362\u5230 UTF-8\uff0c\u800c\u4e0d\u7528\u5bf9\u4ed6\u4eec\u7684\u6e90\u7801\u505a\u4efb\u4f55\u66f4\u6539\u3002\u56e0\u4e3a\u4ed6\u4eec\u5185\u90e8\u90fd\u662f\u8c03\u7528\u7684 fopen \u548c ifstream \u3002","title":"\u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e"},{"location":"unicode/#u8","text":"\u4e2d\u56fd\u533a Windows\uff0cMSVC\uff0c\u7f16\u8bd1\u9009\u9879\uff1a /std:c++17 std::string s = \"\u4f60\u597d\"; hexdump(s); // C4 E3 BA C3 (GBK) std::string s = u8\"\u4f60\u597d\"; hexdump(s); // E4 BD A0 E5 A5 BD (UTF-8) u8 \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u7f16\u7801\u5b58\u50a8\u3002\u65e0\u8bba\u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u662f\u4e0d\u662f UTF-8\u3002 \u7f16\u8bd1\u5668\u4fdd\u8bc1\u4f1a\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u8f6c\u6362\u4e3a UTF-8 \u7f16\u7801\u7684 char \u5b57\u8282\u5e8f\u5217\uff0c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u533a\u3002 \u8fd9\u5bf9\u4e8e\u5df2\u7ecf\u8bbe\u7f6e\u4e86 /utf-8 \u9009\u9879\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u5df2\u7ecf\u4fdd\u8bc1\u662f UTF-8 \u7684\u6211\u4eec\u6765\u8bf4\u6beb\u65e0\u4f5c\u7528\u3002\u53ea\u662f\u5bf9\u4e8e\u4e0d\u7528 /utf-8 \u7684\u540c\u5b66\uff0c\u4ed6\u4eec\u60f3\u8981\u4e34\u65f6\u521b\u5efa\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5c31\u53ef\u4ee5\u7528 u8 \u524d\u7f00\u3002 \u5728 C++17 \u548c\u4e4b\u524d\uff0c u8\"\u4f60\u597d\" \u4ea7\u751f\u7684\u662f const char [] \u7c7b\u578b\u7684\u5e38\u91cf\u3002 \u5728 C++20 \u4e2d\uff0c\u5f15\u5165\u4e86 char8_t \u3002\u7136\u540e\uff0c\u4ed6\u4eec\u89c4\u5b9a\uff0c u8\"\u4f60\u597d\" \u73b0\u5728\u4ea7\u751f\u7684\u662f const char8_t [] \u7c7b\u578b\u7684\u5e38\u91cf\u4e86\u3002 \u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u4ee5\u524d\u4f60\u5199\u7684\uff1a std::string s = u8\"\u4f60\u597d\"; \u73b0\u5728\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u56e0\u4e3a const char8_t [] \u65e0\u6cd5\u7528\u4e8e\u6784\u9020\u53ea\u652f\u6301 const char [] \u7684 std::string \u3002 \u597d\u5728 C++23 \u53c8\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u5141\u8bb8 const char8_t [] \u9690\u5f0f\u8f6c\u6362\u4e3a const char [] \uff0cC++17 \u4e4b\u524d\u7684\u8fd9\u79cd\u4ee3\u7801\u53c8\u80fd\u6b63\u5e38\u901a\u8fc7\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u60f3\u5feb\u4e50\u5730\u7528 u8 \u5b57\u9762\u91cf\uff0c\u8981\u4e48 C++17\uff0c\u8981\u4e48 C++23\uff0c\u8df3\u8fc7 C++20 \u6bd4\u8f83\u597d\u3002 \u4f60\u53ef\u4ee5\u770b\u5230\uff0cC++ \u7248\u672c\u7684\u66f4\u65b0\u5e76\u4e0d\u662f 100% \u5b8c\u5168\u5411\u524d\u517c\u5bb9\u7684\uff0c\u6709\u65f6\u4e5f\u4f1a\u6709\u7834\u574f\u6027\u7684\u53d8\u66f4\uff0c\u4f46\u6bd4\u8f83\u5c11\uff0c\u5e73\u65f6\u611f\u89c9\u4e0d\u5230\u3002\u6bd4\u5982 C++11 \u4e4b\u524d auto \u5c31\u6709\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u540e\u6765\u51b3\u5b9a\u8fd9\u4e2a\u529f\u80fd\u6ca1\u4ec0\u4e48\u7528\uff0c\u5c31\u628a auto \u6539\u6210\u53e6\u4e00\u4e2a\u610f\u601d\u4e86\u3002 \u9664\u4e86 u8 \u4ee5\u5916\uff0c\u8fd8\u6709\u8fd9\u4e9b\uff1a \u524d\u7f00 \u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u201c\u4f60\u597d\u201d \u8fd0\u884c\u5b57\u7b26\u96c6 (ANSI) const char [] L\u201d\u4f60\u597d\u201d Windows \u4e0a UTF-16\uff1bLinux \u4e0a UTF-32 const wchar_t [] u8\u201d\u4f60\u597d\u201d UTF-8 const char8_t [] u\u201d\u4f60\u597d\u201d UTF-16 const char16_t [] U\u201d\u4f60\u597d\u201d UTF-32 const char32_t []","title":"u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528"},{"location":"unicode/#_11","text":"","title":"\u9009\u62e9\u4f60\u7684\u9635\u8425\uff01"},{"location":"unicode/#ansi","text":"\u628a\u5b57\u7b26\u4e32\u5f53\u4f5c\u7eaf\u7cb9\u7684\u201c\u5b57\u8282\u6d41\u201d\uff0c\u65e0\u89c6\u5b57\u7b26\u7f16\u7801\u3002\u6216\u8005\u8bf4\uff0c\u4f60\u4ece\u7cfb\u7edf\u8f93\u5165\u8fdb\u6765\u7684\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u6211\u5c31\u5b58\u50a8\u7684\u4ec0\u4e48\u7f16\u7801\u3002\u5bf9\u4e8e Unicode \u5219\u91c7\u53d6\u5b8c\u5168\u6446\u70c2\u7684\u6001\u5ea6\uff0c\u5b8c\u5168\u65e0\u89c6 Unicode \u7684\u5b58\u5728\u3002 \u9002\u7528\u573a\u666f\uff1a\u901a\u5e38\u4e0e\u6587\u5b57\u5904\u7406\u9886\u57df\u65e0\u5173\u7684\u8f6f\u4ef6\u4f1a\u91c7\u53d6\u8fd9\u79cd\u65b9\u6848\u3002 \u4f18\u70b9\uff1a\u65b9\u4fbf\uff0c\u4e14\u5185\u90e8\u5bf9\u5b57\u7b26\u4e32\u65e0\u4efb\u4f55\u8f6c\u6362\u548c\u5224\u65ad\uff0c\u6548\u7387\u6700\u9ad8\u3002 \u7f3a\u70b9\uff1a\u5728\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u8bfb\u5199\u5e26\u6709\u4e2d\u6587\u7684\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u4f1a\u9971\u53d7\u4e71\u7801\u548c\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u56f0\u6270\u3002 \u65b9\u6cd5\uff1a\u5b8c\u5168\u4f7f\u7528 const char * \u548c std::string \u3002 \u4ee3\u8868\u4f5c\uff1aLinux \u6587\u4ef6\u7cfb\u7edf ext4\u3001Lua \u7f16\u7a0b\u8bed\u8a00\u3001\u73b0\u4ee3 Python \u4e2d\u7684 bytes \u7c7b\u578b\u3001HTTP \u7684 ? \u53c2\u6570\u3001\u65e9\u671f FAT32 \u6587\u4ef6\u7cfb\u7edf\u7b49\u3002 \u8fd9\u7c7b\u8f6f\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u521d\u5b66\u8005\u5199\u6cd5\uff0c\u5982\u679c\u4f60\u4ece\u672a\u60f3\u8fc7\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u4ece\u4e0d\u4e86\u89e3 wchar_t \u3001 char32_t \u4e4b\u95f4\u7684\u6218\u4e89\uff0c\u53ea\u77e5\u9053 char \uff0c\u90a3\u4e48\u4f60\u5df2\u7ecf\u81ea\u52a8\u5728\u6b64\u9635\u8425\u91cc\u3002 \u6709\u4eba\u8bf4 Linux \u6587\u4ef6\u7cfb\u7edf\u662f UTF-8\uff1f\u5e76\u4e0d\u662f\uff01Linux \u6587\u4ef6\u7cfb\u7edf\u6839\u672c\u4e0d\u4f1a\u68c0\u9a8c\u4f60\u7684\u6587\u4ef6\u540d\u662f\u4e0d\u662f\u5408\u6cd5\u7684 UTF-8\uff0c\u53ea\u4e0d\u8fc7\u662f\u56e0\u4e3a\u4f60\u8bbe\u5b9a\u4e86 export LC_ALL=zh_CN.UTF-8 \uff0c\u8fd9\u4f1a\u4f7f\u6240\u6709\u7a0b\u5e8f\uff08\u5305\u62ec\u7ec8\u7aef\u6a21\u62df\u5668\uff09\u5047\u5b9a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5185\u5bb9\u90fd\u6309 UTF-8 \u7f16\u7801\uff0c\u4ece\u800c\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u5404\u7c7b API \u65f6\uff08\u5982 open\u3001write\uff09\u90fd\u4f1a\u4f7f\u7528 UTF-8 \u7f16\u7801\u7684 const char * \u8f93\u5165\uff0c\u5728 Linux \u7cfb\u7edf API \u770b\u6765\uff0c\u6240\u8c13\u201c\u6587\u4ef6\u540d\u201d\u53ea\u662f\u7eaf\u7cb9\u7684\u5b57\u8282\u6d41\uff0c\u53ea\u8981\u4fdd\u8bc1\u4e0d\u5305\u542b '/' \u548c '\\0' \uff0c\u65e0\u8bba\u4f60\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4ed6\u90fd\u4e0d\u5728\u4e4e\u3002\u800c\u6240\u6709\u7684 locale \u90fd\u517c\u5bb9 ASCII\uff0c\u6240\u4ee5\u7edd\u4e0d\u4f1a\u51fa\u73b0\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57\u7f16\u7801\u540e\u4ea7\u751f '/' \u7684\u60c5\u51b5\uff08\u4f8b\u5982 GB2312 \u4f1a\u628a\u4e00\u4e2a\u4e2d\u6587\u7f16\u7801\u6210\u4e24\u4e2a 0x80 \u5230 0xFF \u533a\u95f4\u7684\u5b57\u8282\uff0c\u548c ASCII \u7684\u8303\u56f4\u6ca1\u6709\u91cd\u53e0\uff0c\u66f4\u4e0d\u53ef\u80fd\u51fa\u73b0 '/' \uff09\uff0c\u5373\u4f7f\u6362\u6210 export LC_ALL=zh_CN.GB2312 \uff0cLinux \u6587\u4ef6\u7cfb\u7edf\u4e00\u6837\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u8bfb\u53d6\u4f60\u4e4b\u524d\u4ee5 UTF-8 \u5199\u5165\u7684\u6587\u4ef6\u4f1a\u53d8\u6210\u4e71\u7801\u800c\u5df2\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a\u7684 Windows \u800c\u8a00\uff0c\u4ed6\u7684\u6240\u6709 A \u51fd\u6570\u53ea\u652f\u6301 GBK \u7f16\u7801\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60 Lua \u4e2d\u628a\u5b57\u7b26\u4e32\u201c\u5f53\u4f5c\u201d UTF-8 \u6765\u7528\u3002\u90a3\u4e48\u5f53\u4f60\u5728\u8c03\u7528 Lua \u7684 io.open \u524d\uff0c\u9700\u8981\u5148\u505a\u4e00\u4e2a UTF-8 \u5230 GBK \u7684\u8f6c\u6362\uff0c\u8fd9\u8fd8\u4f1a\u5bfc\u81f4\u4e22\u5931\u90e8\u5206\u4e0d\u5728 GBK \u5185\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u5305\u542b Emoji\uff0c\u90a3\u5c31\u4f1a\u53d8\u6210 ??? \u4e71\u7801\u3002\u800c\u4f7f\u7528 W \u51fd\u6570\u7684 UTF-16 \u5c31\u4e0d\u4f1a\uff0c\u56e0\u4e3a UTF-16 \u80fd\u5bb9\u7eb3\u5b8c\u6574\u7684 Unicode \u6620\u5c04\u3002\u800c\u5b8c\u5168\u6446\u70c2\u7684 Lua\uff0c\u5176 io.open \u53ea\u662f\u4f7f\u7528 C \u8bed\u8a00\u5e93\u51fd\u6570 fopen \uff0c fopen \u53c8\u662f\u57fa\u4e8e Windows \u7684 A \u7cfb\u5217\u51fd\u6570\uff0cLua \u53c8\u6ca1\u6709\u63d0\u4f9b\u5bf9 Windows C \u8fd0\u884c\u65f6\u5e93\u7279\u6709\u7684 _wfopen \u51fd\u6570\u7684\u5c01\u88c5\uff0c\u4ece\u800c\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6253\u5f00\u4e00\u4e2a\u5e26\u6709 Emoji \u7684\u6587\u4ef6\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 ANSI \u9635\u8425\uff0c\u4f60\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0cchar \u6ee1\u5929\u98de\u6446\u70c2\u3002","title":"ANSI \u9635\u8425"},{"location":"unicode/#utf-8_3","text":"\u652f\u6301 Unicode\uff0c\u5b57\u7b26\u4e32\u7edf\u4e00\u4ee5 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u3001\u5904\u7406\u548c\u4f20\u8f93\u3002 \u5e94\u7528\u573a\u666f\uff1a\u5e38\u89c1\u4e8e\u6587\u5b57\u5904\u7406\u9700\u6c42\u4e0d\u5927\uff0c\u4f46\u6709\u5f3a\u70c8\u7684\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u7279\u522b\u662f\u4e92\u8054\u7f51\u65b9\u9762\u7684\u8f6f\u4ef6\u3002\u4ed6\u4eec\u901a\u5e38\u53ea\u7528\u5230\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3001\u67e5\u627e\u3001\u5207\u7247\u901a\u5e38\u4e5f\u53ea\u662f\u5728\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u6587\u4ef6\u5206\u9694\u7b26 '/' \uff09\u3002\u4e5f\u975e\u5e38\u9002\u5408\u4e3b\u8981\u9762\u5bf9\u7684\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0cUTF-8 \u662f\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u6700\u9ad8\u7684\uff0c\u6240\u4ee5\u4e5f\u5e7f\u6cdb\u7528\u4e8e\u7f16\u8bd1\u5668\u3001\u6570\u636e\u5e93\u4e4b\u7c7b\u7684\u573a\u666f\u3002\u540c\u65f6\u56e0\u4e3a UTF-8 \u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u4f7f\u5f97\u4ed6\u80fd\u8f7b\u6613\u9002\u914d\u8fdc\u53e4\u7684 C \u8bed\u8a00\u7a0b\u5e8f\u548c\u5e93\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-8 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8de8\u5e73\u53f0\uff0c\u5728\u7f51\u7edc\u4f20\u8f93\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u7801\uff0cUTF-8 \u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u7f16\u7801\u683c\u5f0f\uff0c\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684 UTF-8 \u8f6f\u4ef6\u53ef\u4ee5\u968f\u610f\u5171\u4eab\u6587\u672c\u6570\u636e\u3002\u517c\u5bb9 ASCII\uff0c\u65b9\u4fbf\u590d\u7528\u73b0\u6709\u5e93\u548c\u751f\u6001\u3002\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u9ad8\uff0c\u5bf9\u4e2d\u6587\u6587\u672c\u4e5f\u4e0d\u7b97\u592a\u5dee\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e\u5e95\u5c42 API \u5747\u91c7\u7528 UTF-16 \u7684 Windows \u7cfb\u7edf\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u635f\u5931\u3002\u4e14\u5b57\u7b26\u4e32\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4f1a\u53d8\u6210 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002 \u4ee3\u8868\u4f5c\uff1aRust \u8bed\u8a00\u3001Go \u8bed\u8a00\u3001CMake \u6784\u5efa\u7cfb\u7edf\u3001Julia \u8bed\u8a00\u7b49\u3002 \u5728 C++ \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 u8\"\u4f60\u597d\" \u521b\u5efa\u4e00\u4e2a\u4fdd\u8bc1\u5185\u90e8\u662f UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u7c7b\u578b\u4e3a char8_t [] \u3002 \u5982\u679c\u7528\u65e0\u524d\u7f00\u7684 \"\u4f60\u597d\" \u521b\u5efa\uff0c\u5219 MSVC \u9ed8\u8ba4\u4f1a\u4ee5\u7f16\u8bd1\u8005\u6240\u5728\u7cfb\u7edf\u7684\u201c\u533a\u57df\u8bbe\u7f6e (locale)\u201d \u4f5c\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u7f16\u7801\u683c\u5f0f\uff08\u800c\u4e0d\u662f\u8fd0\u884c\u8005\u7684\u533a\u57df\u8bbe\u7f6e\uff01\uff09\uff0c\u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8ba9 MSVC \u7f16\u8bd1\u5668\u9ed8\u8ba4\u91c7\u7528 UTF-8 \u7f16\u7801\uff0c\u5373\u8ba9 \"\u4f60\u597d\" \u548c u8\"\u4f60\u597d\" \u4e00\u6837\u91c7\u7528 UTF-8\u3002\u800c GCC \u9ed8\u8ba4\u5c31\u662f UTF-8\uff0c\u9664\u975e\u624b\u52a8\u6307\u5b9a -fexec-charset=GBK \u6765\u5207\u6362\u5230 GBK\u3002\u7a0d\u540e\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u7f16\u8bd1\u5668\u7684\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002 \u5047\u8bbe\u4f60\u901a\u8fc7 /utf-8 \u6216 -fexec-charset=utf-8 \u641e\u5b9a\u4e86\u7f16\u8bd1\u671f\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u3002\u63a5\u4e0b\u6765\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6587\u4ef6\u7cfb\u7edf\u3002 Linux \u6587\u4ef6\u7cfb\u7edf\u5185\u90e8\uff0c\u5747\u4f7f\u7528 8 \u4f4d\u7c7b\u578b char \u5b58\u50a8\uff0c\u5c06\u6587\u4ef6\u540d\u5f53\u4f5c\u5e73\u51e1\u7684\u5b57\u8282\u6d41\uff0c\u4e0d\u4f1a\u505a\u4efb\u4f55\u8f6c\u6362\u3002\u56e0\u6b64\u4f60\u7528 UTF-8 \u521b\u5efa\u548c\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5176\u4ed6\u4f7f\u7528 UTF-8 \u533a\u57df\u8bbe\u7f6e\u7684\u8f6f\u4ef6\u90fd\u53ef\u4ee5\u7167\u5e38\u6253\u5f00\uff0c\u4e0d\u4f1a\u6709\u4e71\u7801\u95ee\u9898\u3002 \u5176\u5b9e Windows \u4e0a\u4ee5 GBK \u7f16\u7801\u7684\u538b\u7f29\u6587\u4ef6\u6216\u6587\u672c\u6587\u4ef6\uff0c\u62f7\u8d1d\u5230 Linux \u4e0a\u6253\u5f00\u51fa\u73b0\u4e71\u7801\u95ee\u9898\uff0c\u5c31\u662f\u56e0\u4e3a Linux \u7684\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u90fd\u662f UTF-8 \u7684\u3002\u5b9e\u9645\u4e0a\u5982\u679c\u628a\u4f60\u7684\u6587\u4ef6\u62f7\u7ed9\u4e00\u4e2a\u7f8e\u56fd\u7684 Windows \u7528\u6237\uff0c\u4ed6\u4e5f\u4f1a\u770b\u5230\u4e71\u7801\uff0c\u56e0\u4e3a\u7f8e\u56fd\u5927\u533a\u7684 Windows \u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\uff0c\u800c\u4e2d\u56fd\u5927\u533a\u7684\u662f GBK\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8bb2\u5230\u89e3\u51b3\u65b9\u6848\u3002 \u800c Windows \u7684 NTFS \u6587\u4ef6\u7cfb\u7edf\uff0c\u91c7\u7528 16 \u4f4d\u7684 wchar_t \u5b58\u50a8\uff0cWindows \u7684\u6240\u6709 API\uff0c\u4e5f\u90fd\u662f\u57fa\u4e8e wchar_t \u7684\uff0cWindows \u5185\u6838\u5185\u90e8\u4e5f\u90fd\u7528 wchar_t \u50a8\u5b58\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u53ea\u6709\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6d41\u4f1a\u7528 char \u5b58\u50a8\u3002\u8fd9\u7c7b\u57fa\u4e8e wchar_t \u7684\u7cfb\u7edf API \u90fd\u6709\u4e00\u4e2a W \u540e\u7f00\uff0c\u4f8b\u5982\uff1a MessageBoxW(NULL, L\"\u4f60\u597d\", L\"\u6807\u9898\", MB_OK); \u8fd9\u4e2a MessageBoxW \u51fd\u6570\uff0c\u53ea\u63a5\u53d7 const wchar_t * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002 L\"\u4f60\u597d\" \u662f\u4e00\u4e2a wchar_t [] \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b83\u7684\u5185\u90e8\u7f16\u7801\u7c7b\u578b\u56fa\u5b9a\u662f UTF-16\uff0c\u4e0d\u4f1a\u968f\u7740\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002\u4e4b\u540e\u7684\u4e00\u8282\u4e2d\u4f1a\u8be6\u7ec6\u8bb2\u89e3 W \u548c A \u51fd\u6570\u7684\u95ee\u9898\u3002 \u867d\u7136\u4e5f\u6709\u63d0\u4f9b A \u540e\u7f00\u7684\u7cfb\u5217\u51fd\u6570\uff0c\u4ed6\u4eec\u548c W \u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u662f\u63a5\u53d7 const char * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u95ee\u9898\u5728\u4e8e\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u90fd\u5fc5\u987b\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f GBK \u7f16\u7801\uff01\u800c\u4e14\u65e0\u6cd5\u4fee\u6539\u3002 \u5f53\u8c03\u7528 A \u7cfb\u51fd\u6570\u65f6\uff0c\u4ed6\u4eec\u5185\u90e8\u4f1a\u628a GBK \u7f16\u7801\u8f6c\u6362\u4e3a UTF-16 \u7f16\u7801\uff0c\u7136\u540e\u8c03\u7528 Windows \u5185\u6838\u3002 \u8fd9\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\uff0c\u800c\u6240\u6709\u7684 C/C++ \u6807\u51c6\u5e93\u90fd\u662f\u57fa\u4e8e A \u51fd\u6570\u7684\uff01\u5982\u679c\u4f60\u7528 const char * \u5b57\u7b26\u4e32\u8c03\u7528 C \u6807\u51c6\u5e93\uff0c\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86 A \u51fd\u6570\u3002\u800c A \u51fd\u6570\u53ea\u63a5\u53d7 GBK\uff0c\u4f46\u4f60\u5374\u8f93\u5165\u4e86 UTF-8\uff01\u4ece\u800c UTF-8 \u4e2d\u6240\u6709\u9664 ASCII \u4ee5\u5916\u7684\uff0c\u5404\u79cd\u4e2d\u6587\u5b57\u7b26\u3001Emoji \u90fd\u4f1a\u53d8\u6210\u4e71\u7801\u3002 \u4f8b\u5982 fopen \u51fd\u6570\uff0c\u53ea\u6709 fopen(const char *path, const char *mode) \u8fd9\u4e00\u4e2a\u57fa\u4e8e char \u7684\u7248\u672c\uff0c\u91cc\u9762\u4e5f\u662f\u76f4\u63a5\u8c03\u7528\u7684 A \u51fd\u6570\uff0c\u5b8c\u5168\u4e0d\u7ed9\u6211\u9009\u62e9\u7684\u7a7a\u95f4\u3002\u867d\u7136 Windows \u4e5f\u63d0\u4f9b\u4e86 _wfopen(const wchar_t *path, const wchar_t *mode) \uff0c\u4f46\u90a3\u65e2\u4e0d\u662f POSIX \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u4e0d\u662f C \u8bed\u8a00\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u7528\u8fd9\u6837\u7684\u51fd\u6570\u5c31\u610f\u5473\u7740\u65e0\u6cd5\u8de8\u5e73\u53f0\u3002 Windows \u5b98\u65b9\u8ba4\u4e3a\uff1a W \u51fd\u6570\u624d\u662f\u771f\u6b63\u7684 API\uff0c A \u51fd\u6570\u53ea\u662f\u5e94\u4ed8\u4e0d\u542c\u8bdd\u7684\u5b9d\u5b9d\u3002\u53ef\u4f60\u5c31\u6ca1\u53d1\u73b0\u4f60\u81ea\u5df1\u7684 C/C++ \u6807\u51c6\u5e93\u4e5f\u5168\u90e8\u5728\u8c03\u7528\u7684 A \u51fd\u6570\u4e48\uff1f \u603b\u4e4b\uff0c A \u51fd\u6570\u662f\u6b8b\u5e9f\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u7528 W \u51fd\u6570\uff0c\u5c3d\u7ba1 UTF-16 \u662f\u5386\u53f2\u503a\uff0c\u4f46\u6211\u4eec\u522b\u65e0\u9009\u62e9\uff0c W \u51fd\u6570\u662f\u552f\u4e00\u80fd\u652f\u6301\u5b8c\u6574 Unicode \u5b57\u7b26\u8f93\u5165\u7684\u65b9\u5f0f\u3002 // \u5047\u8bbe\u8fd9\u6bb5 C++ \u4ee3\u7801\u4f7f\u7528 /utf-8 \u9009\u9879\u7f16\u8bd1\uff1a std::ifstream f(\"\u4f60\u597d.txt\"); // \u627e\u4e0d\u5230\u6587\u4ef6\uff0c\u5373\u4f7f\u201c\u4f60\u597d.txt\u201d\u5b58\u5728 std::ofstream f(\"\u4f60\u597d.txt\"); // \u4f1a\u521b\u5efa\u4e00\u4e2a\u4e71\u7801\u6587\u4ef6 \u6b63\u786e\u7684\u505a\u6cd5\u662f\u91c7\u7528 std::filesystem::u8path \u8fd9\u4e2a\u51fd\u6570\u505a UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\uff1a // C++17\uff0c\u9700\u8981\u7528 u8path \u8fd9\u4e2a\u51fd\u6570\u6784\u9020 path \u5bf9\u8c61\uff1a std::ifstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); // C++20 \u5f15\u5165 char8_t\uff0c\u533a\u5206\u4e8e\u666e\u901a char\uff0cpath \u7c7b\u4e5f\u6709\u4e86\u9488\u5bf9 const char8_t * \u7684\u6784\u9020\u51fd\u6570\u91cd\u8f7d\uff1a std::ifstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::filesystem::path \u7c7b\u7684 c_str() \u5728 Windows \u4e0a\u8fd4\u56de const wchar_t * \uff0c\u5728 Linux \u4e0a\u8fd4\u56de const char * \u3002\u8fd9\u5f88\u5408\u7406\uff0c\u56e0\u4e3a Windows \u6587\u4ef6\u7cfb\u7edf\u786e\u5b9e\u4ee5 wchar_t \u5b58\u50a8\u8def\u5f84\u540d\uff0c\u800c Linux \u6587\u4ef6\u7cfb\u7edf\u5b8c\u5168\u7528 char \u3002 \u6bcf\u6b21\u9700\u8981\u52a0 std::filesystem::u8path \u4e5f\u633a\u9ebb\u70e6\u7684\uff0c\u5bb9\u6613\u5fd8\u8bb0\uff0c\u4e00\u5fd8\u8bb0\u5c31\u65e0\u6cd5\u8bbf\u95ee\u4e2d\u6587\u76ee\u5f55\u3002 \u5f88\u591a\u8f6f\u4ef6\u5728 Windows \u4e0a\u65e0\u6cd5\u652f\u6301\u4e2d\u6587\u8def\u5f84\u540d\uff0c\u5c31\u662f\u56e0\u4e3a\u4ed6\u4eec\u4e60\u60ef\u4e86 Linux \u6216 MacOS \u7684\u5168 UTF-8 \u73af\u5883\uff0c\u5bf9\u6587\u4ef6\u8def\u5f84\u6ca1\u6709\u4efb\u4f55\u8f6c\u6362\u3002\u800c Windows \u5e95\u5c42\u5168\u662f UTF-16\uff0c\u6839\u672c\u6ca1\u6709\u63d0\u4f9b UTF-8 \u7684 API\uff0c\u4f60 UTF-8 \u53ea\u80fd\u8f6c\u6362\u6210 UTF-16 \u624d\u80fd\u907f\u514d\u4e2d\u6587\u4e71\u7801\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u6b7b\u6d3b\u4e0d\u80af\u63a5\u53d7\u660e\u6446\u7740\u5df2\u7ecf\u662f\u56fd\u9645\u901a\u7528\u6807\u51c6\u7684 UTF-8\uff0cA \u51fd\u6570\u7684\u7f16\u7801\u8fde\u5f53\u524d\u8fdb\u7a0b\u5207\u6362\u7684\u65b9\u6cd5\u90fd\u4e0d\u7ed9\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u5e94\u8be5\u7531 Windows \u5168\u8d23\u627f\u62c5\u3002 \u597d\u6d88\u606f\u662f\uff0c\u6700\u8fd1 MSVC \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6848\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\u5f00\u5934\uff0c\u52a0\u4e0a setlocale(LC_ALL, \".utf-8\") \u5c31\u53ef\u4ee5\u8ba9 C \u548c C++ \u6807\u51c6\u5e93\u8fdb\u5165 UTF-8 \u6a21\u5f0f\uff1a\u4e0d\u518d\u8c03\u7528 A \u7cfb\u51fd\u6570\u64cd\u4f5c\u6587\u4ef6\uff0c\u800c\u662f\u4f1a\u628a\u6587\u4ef6\u540d\u4ece UTF-8 \u8f6c\u6362\u6210 UTF-16 \u540e\u518d\u8c03\u7528\u771f\u6b63\u7a33\u5b9a\u7684 W \u7cfb\u51fd\u6570\u3002 setlocale(LC_ALL, \".utf-8\"); // \u53ea\u9700\u8981\u8fd9\u4e00\u884c FILE *fp = fopen(u8\"\u4f60\u597d.txt\", \"r\"); // \u53ef\u4ee5\u4e86 std::ifstream fin(u8\"\u4f60\u597d.txt\"); // \u53ef\u4ee5\u4e86 setlocale(LC_ALL, \".utf-8\"); \u53ea\u662f\u628a C \u6807\u51c6\u5e93\u7684 const char * \u53c2\u6570\u53d8\u6210\u4e86\u63a5\u53d7 UTF-8\uff0c\u5e76\u4e0d\u4f1a\u8ba9\u7cfb\u7edf\u7684 A \u51fd\u6570\u4e5f\u53d8\u6210 UTF-8 \u54e6\uff0c\u8c03\u7528\u672c\u5730 API \u65f6\u4ecd\u9700 UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-8 \u9635\u8425\uff0c\u5f00\u542f /utf-8 \uff0c\u7a0b\u5e8f\u5f00\u5934\u5199 setlocale(LC_ALL, \".utf-8\") \u3002Linux \u7528\u6237\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u3002 \u770b\u770b\u5404\u5927\u8f6f\u4ef6\u7ad9\u5728 UTF-8 \u9635\u8425\u7684\u7406\u7531\uff1a CMake\uff1a\u4f5c\u4e3a\u8de8\u5e73\u53f0\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u4e3a\u4e86\u8ba9\u9879\u76ee\u7684 CMakeLists.txt \u80fd\u8de8\u5e73\u53f0\u5171\u7528\u800c\u4e0d\u5fc5\u91cd\u5199\uff0c\u4ed6\u7406\u6240\u5f53\u7136\u5730\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\uff1a\u6240\u6709 CMakeLists.txt \u90fd\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u4e66\u5199\uff0c\u4e14\u7edf\u4e00\u4f7f\u7528\u6b63\u659c\u6760 '/' \u8def\u5f84\u5206\u9694\u7b26\u3002 CMake \u4f1a\u81ea\u52a8\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u5c06 UTF-8 \u5b57\u7b26\u4e32\u8f6c\u6362\u6210 UTF-16 \u540e\uff0c\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u5728 Linux \u7cfb\u7edf\u4e0a\u5219\u4e0d\u505a\u8f6c\u6362\u3002\u5728 Windows \u7cfb\u7edf\u4e0a\u8fd8\u4f1a\u81ea\u52a8\u628a\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u6b63\u659c\u6760 '/' \u8f6c\u6362\u6210 Windows \u4e13\u5c5e\u7684\u53cd\u659c\u6760 '\\\\' \uff0c\u65e0\u9700\u7528\u6237\u64cd\u5fc3\u3002 \u5c0f\u5f6d\u8001\u5e08\u81ea\u4e3b\u7814\u53d1\u7684 Zeno \u8282\u70b9\u4eff\u771f\u8f6f\u4ef6\uff1a\u7531\u4e8e\u4fdd\u5b58\u7684\u9879\u76ee\u5de5\u7a0b\u6587\u4ef6\u9700\u8981\u5728 Linux \u548c Windows \u5e73\u53f0\u4e0a\u4e92\u901a\uff0c\u4e0d\u80fd\u91c7\u7528 Windows \u5404\u81ea\u4e3a\u653f\u7684 GBK \u683c\u5f0f\uff0c\u4e14\u5de5\u7a0b\u6587\u4ef6\u5185\u5bb9\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\u4e2d\u3002 Rust \u548c Go\uff1a\u4e25\u683c\u533a\u5206\u201c\u5b57\u7b26 (32 \u4f4d)\u201d\u548c\u201c\u5b57\u8282 (8 \u4f4d)\u201d\u7684\u6982\u5ff5\u3002\u5728\u5b57\u7b26\u4e32\u7c7b\u578b\u4e2d\u5b58\u50a8\u5b57\u8282\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u4ee5\u5b57\u8282\u65b9\u5f0f\u8bfb\u53d6\u6216\u4ee5\u5b57\u7b26\u65b9\u5f0f\u8bfb\u53d6\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-8 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5904\u7406\u5207\u7247\u548c\u7d22\u5f15\u65f6\u4e0d\u65b9\u4fbf\u3002 \u7f16\u7a0b\u8bed\u8a00 \u5b57\u7b26\u7c7b\u578b (32 \u4f4d) \u5b57\u8282\u7c7b\u578b (8 \u4f4d) Rust char u8 Go rune byte Julia Char UInt8 \u4e3a\u6b64\uff0c\u8fd9\u4e9b\u8bed\u8a00\u90fd\u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e24\u5957 API\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u8282\u7d22\u5f15\u3002\u6309\u5b57\u7b26\u7d22\u5f15\u65f6\uff0c\u4f1a\u4ece\u5934\u5f00\u59cb\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\uff0c\u76f4\u5230\u89e3\u6790\u5230\u60f3\u8981\u7684\u5b57\u7b26\u4e3a\u6b62\uff0c\u590d\u6742\u5ea6 O(N) O(N) \u3002\u6309\u5b57\u8282\u7d22\u5f15\u65f6\uff0c\u76f4\u63a5\u8df3\u5230\u6307\u5b9a\u5b57\u8282\uff0c\u65e0\u9700\u89e3\u6790\uff0c\u590d\u6742\u5ea6 O(1) O(1) \u3002 let s = \"\u4f60\u597d\"; // \u6309\u5b57\u7b26\u904d\u5386 for c in s.chars() { // c: char println!(\"{}\", c); } // \u6309\u5b57\u8282\u904d\u5386 for b in s.bytes() { // b: u8 println!(\"{:02x}\", b); } \u5728 C++ \u4e2d\uff0c\u82e5\u8981\u91c7\u7528\u8fd9\u79cd UTF-8 \u65b9\u6848\uff0c\u53ef\u4ee5\u4f7f\u7528 utfcpp \u8fd9\u4e2a\u5e93\uff1a https://github.com/nemtrif/utfcpp \u7a0d\u540e\u6211\u4eec\u4f1a\u4ee5\u6848\u4f8b\u8be6\u7ec6\u6f14\u793a\u8fd9\u4e2a\u5e93\u7684\u7528\u6cd5\uff0c\u4e5f\u4f1a\u5c1d\u8bd5\u81ea\u5df1\u624b\u6413\u3002 \u65b9\u6cd51\uff1a\u4f7f\u7528 utf8to32 \u4e00\u6b21\u6027\u5b8c\u6210\u8f6c\u6362\uff0c\u7528\u5b8c\u540e\u518d\u8f6c\u56de\u53bb\u3002 std::string s = \"\u4f60\u597d\"; std::u32string u32 = utf8::utf8to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\u574f'; s = utf8::utf32to8(u32); fmt::println(\"{}\", s); // \u4f60\u574f \u65b9\u6cd52\uff1a utfcpp \u4e5f\u5c01\u88c5\u4e86\u4e00\u4e2a utf8::iterator \u8fed\u4ee3\u5668\u9002\u914d\u5668\uff0c\u6548\u679c\u7c7b\u4f3c\u4e8e Rust \u7684 .chars() \uff0c\u53ef\u4ee5\u5b57\u7b26\u800c\u4e0d\u662f\u5b57\u8282\u904d\u5386\u5b57\u7b26\u4e32\u5bb9\u5668\u3002 char s[] = \"\u4f60\u597d\"; utf8::unchecked::iterator bit(s); utf8::unchecked::iterator eit(s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u5b89\u5168\uff08\u5e26\u8fb9\u754c\u68c0\u6d4b\uff09\u7684\u7248\u672c char s[] = \"\u4f60\u597d\"; utf8::iterator bit(s, s, s + strlen(s)); utf8::iterator eit(s + strlen(s), s, s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u57fa\u4e8e std::string \u7684\u7248\u672c std::string s = \"\u4f60\u597d\"; utf8::iterator bit(s.begin(), s.begin(), s.end()); utf8::iterator eit(s.end(), s.begin(), s.end()); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } \u7531\u4e8e\u8fed\u4ee3\u5668\u63a5\u53e3\u590d\u6742\u96be\u61c2\uff0c\u5efa\u8bae\u5148\u5c01\u88c5\u6210\u5e26\u6709 begin() \u548c end() \u7684 range \u5bf9\u8c61\uff0c\u65b9\u4fbf\u4f7f\u7528 C++17 range-based loop \u8bed\u6cd5\u76f4\u89c2\u904d\u5386\uff1a template struct Utf8Range { utf8::iterator bit; utf8::iterator eit; template Utf8Range(T &&t) : bit(std::begin(t), std::begin(t), std::end(t)) , eit(std::end(t), std::begin(t), std::end(t)) {} auto begin() const { return bit; } auto end() const { return eit; } }; template Utf8Range(T &&t) -> Utf8Range; // \u4ee5\u4e0b\u662f\u65b0\u7c7b\u7684\u4f7f\u7528\u65b9\u6cd5 std::string s = \"\u4f60\u597d\"; for (char32_t c : Utf8Range(s)) { fmt::println(\"U+{:04X}\", c); }","title":"UTF-8 \u9635\u8425"},{"location":"unicode/#utf-16_2","text":"\u652f\u6301 Unicode \u8fc7\u65e9\uff0c\u8bef\u4ee5\u4e3a 0xFFFF \u5c31\u662f Unicode \u7684\u4e0a\u9650\u3002 \u4e00\u5f00\u59cb\uff0c\u4eba\u4eec\u9519\u8bef\u5730\u628a UTF-16 \u5f53\u6210\u6c38\u8fdc\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e71\u7801\u95ee\u9898\uff0c\u6240\u4ee5\u90a3\u6bb5\u65f6\u671f\u7684\u8f6f\u4ef6\u90fd\u5927\u4e3e\u4f7f\u7528 UTF-16 \u4f5c\u4e3a\u5185\u7801\u3002\u6ca1\u60f3\u5230\u540e\u6765 Unicode \u53c8\u5f15\u5165 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u800c\u73b0\u6709\u7684\u5df2\u7ecf\u91c7\u7528\u4e86 16 \u4f4d\u5185\u7801\u7684\u8f6f\u4ef6\u53c8\u5df2\u7ecf\u65e0\u6cd5\u6839\u9664\uff0c\u53ea\u597d\u4f7f\u7528\u201c\u4ee3\u7406\u5bf9\u201d\u673a\u5236\uff0c\u589e\u91cf\u66f4\u65b0\u4fee\u590d\u4e86\u73b0\u6709\u7684 16 \u4f4d\u5185\u7801\u8f6f\u4ef6\u3002UTF-16 \u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u597d\u5904\uff0c\u7559\u4e0b\u5386\u53f2\u503a\u3002 \u4e8b\u5b9e\u4e0a\uff0cUnicode \u5df2\u7ecf\u65e0\u6cd5\u7ee7\u7eed\u6269\u5bb9\u7a81\u7834 0x10FFFF\uff0c\u5c31\u662f\u56e0\u4e3a\u53cc uint16_t \u7f16\u7801\u7684\u4ee3\u7406\u5bf9\u6700\u591a\u53ea\u80fd\u5bb9\u7eb3\u989d\u5916 0x100000 \u4e2a\u5b57\u7b26\u7684\u7a7a\u95f4\u3002\u672c\u6765 UTF-8 \u4e00\u5f00\u59cb\u7684\u8349\u6848\u662f\u6253\u7b97\u6700\u591a\u652f\u6301 8 \u8282\u5217\u8f66\uff0c\u5b8c\u5168\u5bb9\u7eb3\u9ad8\u8fbe 0x7FFFFFFF \u8303\u56f4\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u8ba9 Windows \u8fd8\u80fd\u7ee7\u7eed\u7528\uff0cUnicode \u624d\u88ab\u8feb\u6b62\u6b65 0x10FFFF\uff0cUTF-8 \u4e5f\u7ec8\u7ed3\u4e8e 4 \u8282\u5217\u8f66\u3002 \u5e94\u7528\u573a\u666f\uff1a\u901a\u5e38\u8ba4\u4e3a\uff0cUTF-16 \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff0c\u65b0\u8f6f\u4ef6\u4e0d\u5e94\u8be5\u518d\u4f7f\u7528 UTF-16\u3002\u53ea\u6709\u5728\u548c\u8fd9\u4e9b\u7cdf\u7c95\u8f6f\u4ef6\u7684 API \u6253\u4ea4\u9053\u65f6\uff0c\u624d\u5fc5\u987b\u8f6c\u6362\u4e3a UTF-16\u3002\u4f46\u4e5f\u6709\u4eba\u6307\u51fa\uff1aUTF-16 \u662f\u7eaf\u4e2d\u6587\u538b\u7f29\u7387\u6700\u9ad8\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6240\u4ee5 UTF-16 \u8fd8\u6bd4\u8f83\u9002\u5408\u7eaf\u4e2d\u6587\u6216\u4ee5\u4e2d\u6587\u5185\u5bb9\u4e3a\u4e3b\u7684\u6587\u672c\u6570\u636e\u538b\u7f29\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-16 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8c03\u7528 Windows \u7cfb\u7edf API \u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u76f4\u63a5\u5c31\u80fd\u8c03\u7528\uff0c\u6700\u9002\u5408 Windows \u672c\u5730\u5f00\u53d1\uff0c\u975e\u8de8\u5e73\u53f0\u3002\u4e14\u5bf9\u7eaf\u4e2d\u6587\u5185\u5bb9\u53ef\u6bd4 UTF-8 \u989d\u5916\u8282\u7701 33% \u7a7a\u95f4\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e Windows \u4ee5\u5916\u7684\u7cfb\u7edf\u5c31\u9700\u8981\u8f6c\u6362\u56de UTF-8\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u5f00\u9500\u3002\u4e14\u5982\u679c\u5b58\u50a8\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u7eaf\u82f1\u6587\uff0c\u5982 XML \u4ee3\u7801\u7b49\uff0c\u5185\u5b58\u5360\u7528\u4f1a\u6bd4 UTF-8 \u7ffb\u500d\u3002\u800c\u4e14 UTF-16 \u4ecd\u7136\u662f\u53d8\u957f\u7f16\u7801\uff0c\u867d\u7136\u51fa\u73b0\u53d8\u957f\u7684\u6982\u7387\u8f83\u4f4e\uff0c\u4f46\u4e0d\u4e3a 0\uff0c\u4ecd\u9700\u8981\u5f00\u53d1\u8005\u505a\u7279\u6b8a\u5904\u7406\u3002\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u4f1a\u5bfc\u81f4\u751f\u50fb\u5b57\u7b26\u51fa\u9519\uff0c\u5b57\u7b26\u4e32\u4ee5\u7801\u70b9\u4e3a\u5355\u4f4d\u7684\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4ecd\u7136 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002\u5e76\u4e14 UTF-16 \u6709\u5927\u5c0f\u7aef\u8f6c\u6362\u7684\u95ee\u9898\u3002 \u4ee3\u8868\u4f5c\uff1aWindows \u7cfb\u7edf API\u3001Java \u8bed\u8a00\u3001Windows \u6587\u4ef6\u7cfb\u7edf (NTFS)\u3001Qt\u3001Word\u3001JSON\uff0c\u4ed6\u4eec\u90fd\u662f UTF-16 \u7684\u53d7\u5bb3\u8005\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-16 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-16 \u4f9d\u7136\u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5904\u7406\u6ca1\u95ee\u9898\uff0c\u751f\u50fb\u5b57\u5c31\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4e14\u56e0\u4e3a\u51fa\u73b0\u6982\u7387\u4f4e\uff0c\u5f88\u5bb9\u6613\u4e0d\u53d1\u73b0\uff0c\u57cb\u4e0b\u9690\u60a3\u3002 Java \u5c31\u662f\u53d7\u5230\u4e86 UTF-16 \u5386\u53f2\u503a\u5f71\u54cd\uff0c char \u662f 16 \u4f4d\u7684\u7801\u4f4d\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\uff0c\u771f\u6b63\u7684\u4e00\u4e2a\u5b57\u7b26\u662f 32 \u4f4d\u7684 Character \u7c7b\u578b\u3002 \u7f16\u7a0b\u8bed\u8a00 \u7801\u70b9\u7c7b\u578b (32 \u4f4d) \u7801\u4f4d\u7c7b\u578b (16 \u4f4d) Java Character char Java \u7684 Character \u7c7b\u578b\u662f\u4e00\u4e2a 32 \u4f4d\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u5305\u542b\u4e86\u4e00\u4e2a Unicode \u7801\u4f4d\u3002 char \u7c7b\u578b\u662f\u4e00\u4e2a 16 \u4f4d\u7684\u503c\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e2a UTF-16 \u7f16\u7801\u7684\u7801\u70b9\u3002 String \u7684 charAt() \u65b9\u6cd5\u8fd4\u56de\u7684\u662f char \u7c7b\u578b\u7684\u7801\u4f4d\uff08\u7c7b\u4f3c\u4e8e\u5b57\u8282\uff09\uff0c\u5982\u679c\u8981\u83b7\u53d6 Character \u7c7b\u578b\u7684\u5b8c\u6574\u5b57\u7b26\uff0c\u5fc5\u987b\u4f7f\u7528 codePointAt() \u65b9\u6cd5\u3002\u8fd9\u662f Java \u8bed\u8a00\u8bbe\u8ba1\u4e0a\u7684\u4e00\u4e2a\u5931\u8bef\uff0c\u5df2\u7ecf\u65e0\u6cd5\u6539\u53d8\u3002 \u800c\u540e\u7eed\u65b0\u51fa\u7684 Kotlin \u662f Java \u7684\u5408\u6cd5\u7ee7\u627f\u8005\uff0c\u4ed6\u679c\u65ad\u653e\u5f03 UTF-16\uff0c\u52a0\u5165\u4e86 UTF-32 \u9635\u8425\u3002\u53ef\u89c1\uff0c\u8001\u8f6f\u4ef6\u575a\u6301\u7528 UTF-32 \u662f\u56e0\u4e3a\u4ed6\u4eec\u79ef\u91cd\u96be\u8fd4\uff0c\u65b0\u8f6f\u4ef6\u518d UTF-16 \u5c31\u662f\u81ea\u4f5c\u5b7d\u4e86\uff01 \u603b\u7ed3\uff1a\u4e0d\u8981\u652f\u6301 UTF-16 \u9635\u8425\uff0c\u9664\u975e\u4f60\u88ab\u8feb\u7ef4\u62a4\u53f2\u5c71\u3002 \u4f8b\u5982\u5c0f\u5f6d\u8001\u5e08\u53d1\u5fae\u4fe1\u670b\u53cb\u5708\u65f6\uff0c\u8f93\u5165 Emoji \u8868\u60c5\u540e\u526a\u5207\uff0c\u518d\u7c98\u8d34\uff0c\u5c31\u548c\u53d1\u73b0\u4e00\u4e2a Emoji \u88ab\u5207\u65ad\u6210\u4e86\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4e71\u7801\u7684\u5f62\u5f0f\u663e\u73b0\u3002\u4f30\u8ba1\u662f\u56e0\u4e3a\u5fae\u4fe1\u57fa\u4e8e Java \u7f16\u5199\uff0c\u75bc\u900a\u7a0b\u5e8f\u5458\u5bf9 UTF-16 \u4ee3\u7406\u5bf9\u5904\u7406\u7684\u4e0d\u5229\u7d22\u3002 Java \u4e2d\u4ee5\u7801\u70b9\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5199\u6cd5\uff1a String s = \"\u4f60\u597d\"; // \u6309\u7801\u70b9\u904d\u5386 for (int i = 0; i < s.length();) { Character c = s.codePointAt(i); System.out.println(String.format(\"U+%04X\", c)); i += Character.charCount(c); } // \u6309\u7801\u4f4d\u904d\u5386 for (char c : s.toCharArray()) { System.out.println(String.format(\"U+%04X\", (int) c)); } \u7531\u4e8e JSON \u662f\u548c Java \u4e00\u5757\u53d1\u660e\u7684\u3002\u5bf9\u4e8e\u8d85\u51fa 0xFFFF \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u91c7\u7528\u7684\u8f6c\u4e49\uff0c\u4e5f\u662f\u57fa\u4e8e UTF-16 \u7f16\u7801\u3002\u5373\u540c\u4e00\u4e2a\u5b57\u4f1a\u53d8\u6210\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4fdd\u8bc1 JSON \u6587\u4ef6\u603b\u662f ASCII \u683c\u5f0f\uff0c\u907f\u514d Windows \u7684 GBK \u7f16\u7801\u4e71\u505a\u989d\u5916\u7684\u5e72\u6270\u3002 // \u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7 {\"name\": \"\ud883\udede\"} {\"name\": \"\\ud883\\udfde\"} \u5728\u521a\u521a\u4ecb\u7ecd\u7684 C++ \u5e93 utfcpp \u4e2d\uff0c\u4e5f\u6709\u9488\u5bf9 UTF-16 \u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5982 utf16to32 \uff1a std::u16string s = u\"\u4f60\u597d\"; std::u32string u32 = utf16::utf16to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\ud883\udede'; s = utf16::utf32to16(u32); fmt::println(\"{}\", s); // \u4f60\ud883\udede fmt::println(\"{}\", u32.size()); // 2 fmt::println(\"{}\", s.size()); // 3","title":"UTF-16 \u9635\u8425"},{"location":"unicode/#utf-32_1","text":"\u652f\u6301 Unicode\uff0c\u6bcf\u4e2a\u7801\u70b9\u90fd\u7528\u4e00\u4e2a uint32_t \u6216 char32_t \u8868\u793a\u3002 \u5e94\u7528\u573a\u666f\uff1a\u9002\u5408\u9700\u8981\u7ecf\u5e38\u5904\u7406\u6587\u5b57\u7684\u9886\u57df\uff0c\u5982\u6587\u672c\u7f16\u8f91\u5668\u3001\u6d4f\u89c8\u5668\u7b49\u3002\u4f46\u4e0d\u9002\u5408\u5b58\u50a8\u548c\u4f20\u8f93\uff0c\u56e0\u4e3a\u6d6a\u8d39\u786c\u76d8\u548c\u7f51\u7edc\u5e26\u5bbd\u3002\u5b57\u7b26\u4e32\u4e00\u822c\u90fd\u957f\u671f\u4ee5 UTF-8 \u5b58\u50a8\uff0c\u53ea\u6709\u5728\u9700\u8981\u9891\u7e41\u7d22\u5f15\u7801\u4f4d\u65f6\uff0c\u624d\u9700\u8981\u8f6c\u6362\u4e3a UTF-32\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-32 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u3001\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u90fd\u662f O(1) O(1) \u7684\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u5f53\u4f5c\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u968f\u610f\u5904\u7406\u3002\u4f8b\u5982\u4f60\u53ef\u4ee5\u8bbe\u60f3\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff0c\u9700\u8981\u652f\u6301\u201c\u9000\u683c\u201d\u64cd\u4f5c\uff0c\u5982\u679c\u662f UTF-8 \u548c UTF-16 \u5c31\u9700\u8981\u7e41\u7410\u7684\u5224\u65ad\u4ee3\u7406\u5bf9\u3001\u5404\u79cd\u8f66\u53a2\uff0c\u800c UTF-32 \u7684\u5b57\u7b26\u4e32\u53ea\u9700\u8981\u4e00\u6b21 pop_back \u5c31\u641e\u5b9a\u4e86\u3002 \u7f3a\u70b9\uff1a\u6d6a\u8d39\u7a7a\u95f4\u5927\uff0c\u901a\u5e38\u5728\u4fdd\u5b58\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8f6c\u6362\u56de UTF-8 \u540e\u518d\u5199\u5165\u6587\u4ef6\uff0c\u6709\u4e00\u5b9a\u6027\u80fd\u5f00\u9500\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-32 \u9635\u8425\uff0c\u8bf7\u5168\u90e8\u4f7f\u7528 char32_t \u548c std::u32string \u3002\u5b57\u9762\u91cf\u5168\u7528 U\"\u4f60\u597d\" \u7684\u5f62\u5f0f\u4e66\u5199\uff0c\u8bfb\u6587\u4ef6\u65f6\u8f6c\u4e3a UTF-32\uff0c\u5199\u6587\u4ef6\u65f6\u8f6c\u56de UTF-8\u3002","title":"UTF-32 \u9635\u8425"},{"location":"unicode/#_12","text":"\u7531\u4e8e C++26 \u524d\u6807\u51c6\u5e93\u5bf9\u7f16\u7801\u8f6c\u6362\u51e0\u4e4e\u6ca1\u6709\u652f\u6301\uff0c\u5728 C++ \u4e2d\u8f6c\u6362\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u90fd\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\u3002","title":"\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362"},{"location":"unicode/#utf-utfcpp","text":"\u5982\u679c\u4f60\u53ea\u662f\u9700\u8981\u4e0d\u540c UTF \u683c\u5f0f\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u6ca1\u6709\u5904\u7406 GBK \u7b49\u7684\u9700\u6c42\uff1a\u90a3\u4e48\u4e4b\u524d\u5df2\u7ecf\u4ecb\u7ecd\u4e86 utfcpp \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5e93\uff0c\u5df2\u7ecf\u591f\u7528\u3002 #include \"utf8/cpp20.h\" std::u8string s8 = u8\"\u4f60\u597d\"; std::u16string s16 = utf8::utf8to16(s8); std::u32string s32 = utf8::utf8to32(s8); std::string s = utf8::utf16to8(s16); s8 = utf8::utf16tou8(s16); \u6700\u540e\u8fd9\u4e24\u4e2a\u533a\u522b\u5728\u4e8e\uff0c utf16to8 \u8fd4\u56de std::string \uff0c utf16tou8 \u8fd4\u56de std::u8string \uff0c\u91cc\u9762\u90fd\u662f UTF-8 \u7f16\u7801\u7684\uff0c\u4e0d\u8fc7\u6709\u7684\u4eba\u559c\u6b22\u7528 std::string \u6765\u5b58\u50a8 UTF-8\uff0c\u4e0d\u559c\u6b22 std::u8string \uff0c\u6216\u8005\u8bf4\u4ed6\u4eec\u6ca1\u6709 C++20\uff0c\u4e0d\u652f\u6301 std::u8string \uff0c\u56e0\u6b64\u8fd9\u4e2a\u5e93\u6ee1\u8db3\u4ed6\u4eec\u7684\u4e0d\u540c\u9700\u8981\u3002\u4f46\u662f std::u8string \u4f5c\u4e3a\u53c2\u6570\u65f6\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u53c2\u6570\u53ef\u4ee5\u81ea\u52a8\u91cd\u8f7d\uff0c\u800c\u8fd4\u56de\u503c\u4e0d\u884c\u3002 \u7f3a\u70b9\u662f\u4ed6\u4e0d\u80fd\u5904\u7406 GBK\u3001Shift-JIS \u7b49\u975e Unicode \u7f16\u7801\uff0c\u4e5f\u4e0d\u80fd\u81ea\u52a8\u68c0\u6d4b\u5f53\u524d\u7684 ANSI \u533a\u57df\u8bbe\u7f6e\u3002","title":"\u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp"},{"location":"unicode/#boostlocale","text":"\u5982\u679c\u4f60\u8fd8\u8981\u652f\u6301\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u6bd4\u5982 GBK\u3001Shift-JIS\u3001Latin-1\u3002 \u4e00\u79cd\u662f C \u8bed\u8a00\u7684 iconv \uff0c\u53e6\u4e00\u79cd\u662f\u73b0\u4ee3 C++ \u7684 boost::locale \u3002 \u867d\u7136\u529f\u80fd\u5dee\u4e0d\u591a\uff0c\u5e95\u5c42\u90fd\u662f\u8c03\u7528 icu \u7684\u3002 boost::locale \u7684 API \u66f4\u52a0\u53cb\u597d\uff0c\u800c\u4e14\u662f\u73b0\u4ee3 C++ \u98ce\u683c\u7684\u3002 # Ubuntu \u7528\u6237\u5b89\u88c5 Boost.locale \u65b9\u6cd5\uff1a $ sudo apt-get install libboost-locale-dev # Arch Linux \u7528\u6237\u5b89\u88c5 Boost \u5168\u5bb6\u6876\u65b9\u6cd5\uff1a $ sudo pacman -S boost \u4e0d\u559c\u6b22 Boost \u7684\u4eba\u6709\u96be\u4e86\u3002","title":"\u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale"},{"location":"unicode/#utf","text":"\u4f7f\u7528 boost::locale::conv::utf_to_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::utf_to_utf; int main() { std::string s8 = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c UTF-32\uff1a std::u32string s32 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-16\uff1a std::u16string s16 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-8\uff1a s8 = utf_to_utf(s32); std::cout << s8 << '\\n'; return 0; } \u6a21\u677f\u53c2\u6570\u4e2d\uff0c\u53ea\u9700\u6307\u5b9a\u8f6c\u6362\u5230\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5c31\u884c\uff0c\u6765\u81ea\u4ec0\u4e48\u7c7b\u578b\uff0c\u4ed6\u81ea\u5df1\u4f1a\u91cd\u8f7d\u7684\u3002 \u6bd4\u5982\u4ece char32_t \u8f6c\u5230 char16_t \uff0c\u53ea\u9700\u8981 utf_to_utf \u5c31\u53ef\u4ee5\uff0c\u975e\u5e38\u65b9\u4fbf\u3002 \u7f16\u8bd1\uff1a $ g++ -std=c++17 -lboost_locale main.cpp \u8f93\u51fa\uff1a \u4f60\u597d \u5efa\u8bae\u7528\u540c\u6837\u8de8\u5e73\u53f0\u7684 CMake \u94fe\u63a5 Boost\uff0c\u5426\u5219 Windows \u7528\u6237\u8981\u6709\u96be\u4e86\u2026\u2026 find_package(Boost REQUIRED COMPONENTS locale) target_link_libraries(\u4f60\u7684\u7a0b\u5e8f Boost::locale) \u4e0d\u8fc7 boost::locale \u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u90a3\u5c31\u662f\u4e0d\u652f\u6301 char8_t \u548c std::u8string \u3002 char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff01 \u7531\u4e8e Boost \u8f83\u8001\uff0c\u6ca1\u6709\u53ca\u65f6\u8ddf\u8fdb\uff0c\u6240\u4ee5\u4ed6\u5e76\u6ca1\u6709\u5b9e\u73b0\u9488\u5bf9 char8_t \u7684\u7279\u5316\uff0c\u5982\u679c\u4f7f\u7528\u4e86 utf_to_utf \u4f1a\u62a5 undefined reference \u9519\u8bef\uff0c\u5373\u627e\u4e0d\u5230\u7b26\u53f7\u3002\u6539\u6210 utf_to_utf \u5c31\u6ca1\u95ee\u9898\u4e86\u3002","title":"UTF \u4e4b\u95f4\u4e92\u8f6c"},{"location":"unicode/#gbk-utf","text":"\u4f7f\u7528 boost::locale::conv::to/from_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::to_utf; using boost::locale::conv::from_utf; int main() { std::string s = \"\u4f60\u597d\"; // \u4ece GBK \u8f6c\u5230 UTF-16 std::wstring ws = to_utf(s, \"GBK\"); std::wcout << ws << '\\n'; // \u4ece UTF-16 \u8f6c\u56de GBK s = from_utf(ws, \"GBK\"); std::wcout << s << '\\n'; return 0; } \u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f GBK \u3001 Shift-JIS \u3001 Latin1 \u7b49\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u5728\u770b\u5230\u3002 \u8fd9\u91cc to_utf \u4f1a\u81ea\u52a8\u5224\u65ad wchar_t \u7684\u5927\u5c0f\u3002\u5982\u679c\u662f 2 \u5b57\u8282\uff08Windows \u5e73\u53f0\u60c5\u51b5\uff09\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-16\uff0c\u5982\u679c\u662f 4 \u5b57\u8282\uff08Linux \u5e73\u53f0\u60c5\u51b5\uff09\uff0c\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-32\u3002 \u800c to_char \u5219\u662f\u65e0\u8bba\u4ec0\u4e48\u5e73\u53f0\uff0c\u90fd\u4f1a\u8f6c\u4e3a UTF-16\u3002 from_utf \u4e0d\u9700\u8981\u6307\u5b9a\u4efb\u4f55\u6a21\u677f\u53c2\u6570\uff0c\u56e0\u4e3a\u4ed6\u603b\u662f\u8fd4\u56de std::string \uff08ANSI \u6216 GBK \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u53c2\u6570\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4f1a\u81ea\u52a8\u901a\u8fc7\u91cd\u8f7d\u5224\u65ad\uff0c\u4f8b\u5982 from_utf(ws, \"GBK\") \u8fd9\u91cc\u7684\u53c2\u6570\u662f wchar_t \uff0c\u90a3\u4e48\u5728 Windows \u4e0a\uff0c\u4ed6\u4f1a\u68c0\u6d4b\u5230 wchar_t \u662f 2 \u5b57\u8282\uff0c\u5c31\u8ba4\u4e3a\u662f UTF-16 \u5230 GBK \u7684\u8f6c\u6362\u3002","title":"GBK \u548c UTF \u4e92\u8f6c"},{"location":"unicode/#utf-ansi","text":"\u6211\u4eec\u7a0b\u5e8f\u7684\u7528\u6237\u4e0d\u4e00\u5b9a\u662f\u4e2d\u56fd\u7528\u6237\uff08GBK\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4fc4\u7f57\u65af\u7528\u6237\uff08CP1251\uff09\u3001\u65e5\u672c\u7528\u6237\uff08Shift-JIS\uff09\u3001\u897f\u73ed\u7259\u7528\u6237\uff08CP1252\uff09\u7b49\u3002 \u5982\u679c\u8981\u91c7\u7528\u7528\u6237\u7684\u533a\u57df\u8bbe\u7f6e\uff0c\u5373\u201cANSI\u201d\uff0c\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u7559\u7a7a\uff08 \"\" \uff09\u3002 \u7a7a\u5b57\u7b26\u4e32\u5c31\u8868\u793a\u5f53\u524d\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e86\uff0c\u5728\u4e2d\u56fd\u5927\u533a\u7b49\u4ef7\u4e8e \"GBK\" \uff0c\u4fc4\u7f57\u65af\u5927\u533a\u7b49\u4ef7\u4e8e \"CP1251\" \u7b49\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::to_utf; int main() { std::string u8s = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c ANSI std::string s = from_utf(u8s, \"\"); // ANSI \u8f6c UTF-8 u8s = to_utf(s, \"\"); return 0; }","title":"UTF \u548c ANSI \u4e92\u8f6c"},{"location":"unicode/#_13","text":"\u51fd\u6570\u540d\u79f0 \u4ece \u5230 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-16 utf_to_utf UTF-x UTF-32 utf_to_utf UTF-x Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 UTF-x \u8868\u793a\u53d6\u51b3\u4e8e\u53c2\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u5982\u679c\u53c2\u6570\u662f char16_t \u7684\u5b57\u7b26\u4e32 std::u16string \uff0c\u90a3 x \u5c31\u662f 16\u3002 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-16 to_utf(\"GBK\", string) GBK UTF-32 to_utf(\"GBK\", string) GBK Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-32 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 from_utf(\"GBK\", string) UTF-8 GBK from_utf(\"GBK\", u16string) UTF-16 GBK from_utf(\"GBK\", u32string) UTF-32 GBK from_utf(\"GBK\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 GBK from_utf(\"\", string) UTF-8 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u16string) UTF-16 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u32string) UTF-32 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u533a\u57df\u8bbe\u7f6e","title":"\u5927\u603b\u7ed3"},{"location":"unicode/#gbk-shift-jis","text":"#include #include using boost::locale::conv::between; using boost::locale::conv::from_utf; int main() { // \u521b\u5efa\u4e00\u4e2a Shift-JIS \u5b57\u7b26\u4e32 std::string jis = from_utf(u8\"\u65e5\u672c\u8a9e\", \"Shift-JIS\"); // \u4ece Shift-JIS \u8f6c\u5230 GBK std::string gbk = between(jis, \"GBK\", \"Shift-JIS\"); std::cout << gbk << '\\n'; // \u4ece GBK \u8f6c\u56de Shift-JIS jis = between(gbk, \"Shift-JIS\", \"GBK\"); std::cout << jis << '\\n'; return 0; } \u6ce8\u610f\uff01\u662f\u76ee\u6807\u7f16\u7801\u5728\u524d\uff01\u5982\u679c\u4f60\u8981\u4ece Shift-JIS \u8f6c\u6210 GBK\uff0c\u90a3\u4e48\u9700\u8981 between(jis, \"GBK\", \"Shift-JIS\") \uff0c\u8fd9\u771f\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\u3002\u4e0d\u4ec5 GBK \u548c Shift-JIS \u53ef\u80fd\u4e0d\u5c0f\u5fc3\u5f04\u53cd\u4e86\uff0c\u7f16\u8bd1\u5668\uff0c\u4e00\u70b9\u63d0\u793a\u90fd\u6ca1\u6709\uff0c\u800c\u4e14 jis \u548c \u201cGBK\u201d \u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5f88\u5bb9\u6613\u5927\u8111\u641e\u6df7\u3002\u8ba9\u6211\u6765\u8bbe\u8ba1\u7684\u8bdd\uff0c\u6211\u4f1a\u8fd9\u6837\u63d0\u4f9b API\uff1a decode(jis, Encoding::ShiftJIS).encode(Encoding::GBK) \uff0c\u5176\u4e2d Encoding \u662f\u4e00\u4e2a\u679a\u4e3e\uff0c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e0d\u4ec5\u907f\u514d\u72af\u9519\u7684\u673a\u4f1a\uff0c\u770b\u8d77\u6765\u4e5f\u66f4\u8f7b\u677e\u3002\u4e4b\u540e\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u9898\u8bfe\u4e2d\uff0c\u4f1a\u518d\u8be6\u7ec6\u8bb2\u89e3\u4ec0\u4e48\u662f\u597d\u7684 API \u8bbe\u8ba1\u3002","title":"GBK \u548c Shift-JIS \u4e92\u8f6c"},{"location":"unicode/#_14","text":"\u5982\u679c\u9047\u5230\u65e0\u6cd5\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u8be5\u5982\u4f55\u5904\u7f6e\uff1f \u9ed8\u8ba4\u60c5\u51b5\u4e0b Boost \u4f1a\u5ffd\u89c6\u9519\u8bef\uff0c\u7f16\u7801\u5931\u8d25\u7684\u5b57\u7b26\u4f1a\u88ab\u4e22\u5f03\u3002 #include #include using boost::locale::conv::from_utf; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\"); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 std::cout << gbk << '\\n'; // \u5728 Windows \u7684 GBK \u7ec8\u7aef\u4e0a\uff0c\u53ea\u663e\u793a\u201c\u6211\u7231\u9762\u201d return 0; } \u53ef\u4ee5\u7528 method_type \u8fd9\u4e2a\u679a\u4e3e\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7684\u65b9\u5f0f\u3002 \u9ed8\u8ba4\u662f skip \uff0c\u8df3\u8fc7\u6240\u6709\u89e3\u7801\u51fa\u9519\u7684\u5730\u65b9\uff08\u5bfc\u81f4\u201c\ud883\udede\u201d\u4e22\u5931\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u5207\u6362\u5230 stop \uff0c\u5f53\u9047\u5230\u89e3\u7801\u9519\u8bef\u65f6\uff0c\u4f1a\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\", method_type::stop); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 // from_utf \u4f1a\u629b\u51fa `conversion_error` \u5f02\u5e38 std::cout << gbk << '\\n'; return 0; } \u4e3e\u4f8b\uff1a\u5c1d\u8bd5\u4ee5 GBK \u4fdd\u5b58\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u6539\u4e3a\u5e26\u6709 BOM \u7684 UTF-8\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; using boost::locale::conv::conversion_error; void try_save(std::u32string content, std::wstring path) { std::string binary; try { // \u5c1d\u8bd5\u5c06 UTF-32 \u8f6c\u6210 GBK \u7f16\u7801 binary = from_utf(content, \"GBK\", method_type::stop); } catch (conversion_error const &e) { // \u82e5 GBK \u65e0\u6cd5\u8868\u793a // \u6539\u7528\u524d\u9762\u5e26\u6709 BOM \u7684 UTF-8 \u7f16\u7801 binary = \"\\xEF\\xBB\\xBF\" + utf_to_utf(content); } std::ofstream(path) << binary; } \u4e3e\u4f8b\uff1a\u652f\u6301 UTF-8 \u5b57\u7b26\u4e32\uff08\u800c\u4e0d\u662f ANSI \u5b57\u7b26\u4e32\uff09\u7684\u6253\u5370\u51fd\u6570\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::utf_to_utf; void u8print(std::string msg) { std::cout << from_utf(msg, \"\"); // \u6216\u8005\uff1a // std::wcout << utf_to_utf(msg, \"\"); } \u66f4\u591a\u7ec6\u8282\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1ahttps://www.boost.org/doc/libs/1_81_0/libs/locale/doc/html/group__codepage.html","title":"\u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5"},{"location":"unicode/#_15","text":"\u7f16\u7801\u8f6c\u6362\u53ea\u662f boost::locale::conv \u8fd9\u4e2a\u5b50\u6a21\u5757\u4e0b\u7684\u4e00\u4e2a\u5c0f\u529f\u80fd\u800c\u5df2\uff01 boost::locale \u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u5982\u6309\u7167\u5730\u57df\u8bed\u8a00\u89c4\u8303\u683c\u5f0f\u5316\u6570\u5b57\u3001\u8d27\u5e01\u3001\u65e5\u671f\u3001\u65f6\u95f4\u7b49\uff0c\u4e0b\u4e00\u5c0f\u8282\u4e2d\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u3002\u5b8c\u5168\u662f std::locale \u7684\u4e0a\u4f4d\u66ff\u4ee3\u3002 Boost \u54ea\u91cc\u90fd\u597d\uff0c\u4f60\u60f3\u8981\u7684\u529f\u80fd\u5e94\u6709\u5c3d\u6709\u3002\u800c\u4e14\u4e0d\u9700\u8981 C++20\uff0c\u5f88\u4f4e\u7248\u672c\u7684 C++ \u4e5f\u80fd\u7528\u3002\u552f\u4e00\u7f3a\u70b9\u53ef\u80fd\u5c31\u662f\u592a\u80a5\u4e86\uff0c\u7f16\u8bd1\u6162\u3002","title":"\u66f4\u591a\u529f\u80fd\uff1f\uff01"},{"location":"unicode/#windows-multibytetowidechar","text":"\u5982\u679c\u4f60\u662f Windows \u7a0b\u5e8f\u5458\uff0c\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u4e14\u9700\u8981\u5728 Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u89c4\u5b9a\u7684 ANSI\uff08\u5728\u4e2d\u56fd\u533a\u662f GBK\uff09\u7f16\u7801\u548c UTF-16 \u4e4b\u95f4\u8f6c\u6362\uff1a \u53ef\u4ee5\u7528 Windows \u5b98\u65b9\u63d0\u4f9b\u7684 MultiByteToWideChar \u548c WideCharToMultiByte \u51fd\u6570\u3002 \u8fd9\u4e24\u4e2a\u51fd\u6570\u56e0\u4e3a C \u8bed\u8a00\u7279\u8272\u7684\u7f18\u6545\uff0c\u53c2\u6570\u6bd4\u8f83\u591a\u800c\u6742\uff0c\u5efa\u8bae\u81ea\u5df1\u52a8\u624b\u5c01\u88c5\u6210\u66f4\u6613\u7528\u7684 C++ \u51fd\u6570\uff1a std::wstring ansi_to_wstring(const std::string &s) { // ACP = ANSI Code Page\uff0c\u544a\u8bc9\u4ed6\u5b57\u7b26\u4e32\u91cc\u7684\u662f\u5f53\u524d\u533a\u57df\u8bbe\u7f6e\u6307\u5b9a\u7684\u7f16\u7801\uff08\u5728\u4e2d\u56fd\u533a\uff0cANSI \u5c31\u662f GBK \u4e86\uff09 int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_ansi(const std::wstring &ws) { int len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } std::wstring utf8_to_wstring(const std::string &s) { int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_utf8(const std::wstring &ws) { int len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } C \u8bed\u8a00\u7279\u8272\uff1a\u6240\u6709\u8981\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff0c\u90fd\u9700\u8981\u8c03\u7528\u4e24\u904d\uff0c\u7b2c\u4e00\u6ce2\u5148\u6c42\u51fa\u957f\u5ea6\uff0c\u7b2c\u4e8c\u6ce2\u624d\u5199\u5165\u3002\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u4e0e\u5185\u5b58\u5206\u914d\u5668\u8026\u5408\uff0c\u6240\u6709\u7684 C \u98ce\u683c API \u90fd\u662f\u8fd9\u6837\u3002","title":"Windows \u7528\u6237\uff1aMultiByteToWideChar"},{"location":"unicode/#messageboxa","text":"\u590d\u73b0\u6761\u4ef6\uff1a Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e3a\u4e2d\u6587 (GBK)\u3002 \u4f7f\u7528 MSVC \u7684 /utf-8 \u9009\u9879\u7f16\u8bd1\u3002 #include int main() { MessageBoxA(nullptr, \"\u6211\u7231\ud883\udede\ud883\udede\u9762\", \"\u6807\u9898\", MB_OK); // \u4f1a\u53d8\u6210\u4e71\u7801 return 0; }","title":"MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b"},{"location":"unicode/#linux-iconv","text":"\u5982\u679c\u4f60\u662f Linux \u7528\u6237\uff0c\u4e14\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u53ef\u4ee5\u4f7f\u7528 C \u8bed\u8a00\u7684 iconv \u5e93\u3002 iconv \u4e5f\u6709 Windows \u7684\u7248\u672c\uff0c\u4f46\u5b89\u88c5\u6bd4\u8f83\u56f0\u96be\u3002\u5982\u679c\u4f60\u8fde iconv \u90fd\u641e\u5f97\u5b9a\uff0c\u6ca1\u7406\u7531 Boost \u641e\u4e0d\u5b9a\u3002 #include #include std::string convert(std::string const &s, char const *from, char const *to) { iconv_t cd = iconv_open(to, from); if (cd == (iconv_t)-1) { throw std::runtime_error(\"iconv_open failed\"); } auto in = s.data(); auto inbytesleft = s.size(); size_t outbytesleft = inbytesleft * 4; std::string buffer(outbytesleft, 0); auto out = buffer.data(); iconv(cd, &in, &inbytesleft, &out, &outbytesleft); iconv_close(cd); buffer.resize(buffer.size() - outbytesleft); return buffer; } // \u4e3e\u4f8b\uff1aUTF-8 \u8f6c GBK std::string utf8_to_gbk(std::string const &s) { return convert(s, \"UTF-8\", \"GBK\"); } // \u4e3e\u4f8b\uff1aGBK \u8f6c UTF-8 std::string gbk_to_utf8(std::string const &s) { return convert(s, \"GBK\", \"UTF-8\"); }","title":"Linux \u7528\u6237\uff1aiconv"},{"location":"unicode/#iconv","text":"iconv \u4e0d\u4ec5\u662f\u4e00\u4e2a\u5e93\uff0c\u4e5f\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5927\u591a Linux \u53d1\u884c\u7248\u90fd\u81ea\u5e26\u4e86\uff09\u3002\u7528\u6cd5\u5982\u4e0b\uff1a iconv -f \u6765\u81ea\u4ec0\u4e48\u7f16\u7801 -t \u5230\u4ec0\u4e48\u7f16\u7801 (\u8f93\u5165\u6587\u4ef6\u540d...) > \u8f93\u51fa\u6587\u4ef6\u540d \u5982\u4e0d\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u9ed8\u8ba4\u4ece\u7ec8\u7aef\u8f93\u5165\u6d41\u8bfb\u53d6\u3002 \u5982\u4e0d\u4f7f\u7528 > \u8f93\u51fa\u6587\u4ef6\u540d \u91cd\u5b9a\u5411\u8f93\u51fa\uff0c\u5219\u9ed8\u8ba4\u8f93\u51fa\u5230\u7ec8\u7aef\u3002 \u53ef\u4ee5\u7528 echo \u914d\u5408\u7ba1\u9053\u6765\u521b\u5efa\u8f93\u5165\u6d41\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 \u6b64\u5904\u663e\u793a\u4e71\u7801\u662f\u56e0\u4e3a\u6211\u7684\u7ec8\u7aef\u662f UTF-8 \u683c\u5f0f\uff0c\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 iconv \u8f93\u51fa\u7684 GBK \u683c\u5f0f\u6570\u636e\u3002 \u628a\u201c\u6211\u7231\u5c0f\u5f6d\u8001\u5e08\u201d\u8f6c\u6362\u4e3a GBK \u683c\u5f0f\u5199\u5165 gbk.txt \uff0c\u7136\u540e\u518d\u91cd\u65b0\u8fd8\u539f\u56de UTF-8 \u683c\u5f0f\u67e5\u770b\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK > gbk.txt $ cat gbk.txt \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 $ iconv -f GBK -t UTF-8 gbk.txt \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 Windows \u53ef\u80fd\u4e5f\u6709\u7c7b\u4f3c\u7684\u5de5\u5177\uff0c\u6bd4\u5982 iconv.exe \uff0c\u4f46\u6211\u6ca1\u627e\u5230\u3002","title":"iconv \u547d\u4ee4\u884c\u5de5\u5177"},{"location":"unicode/#locale","text":"\u672c\u5730\u5316\u662f\u6307\u6839\u636e\u7528\u6237\u7684\u8bed\u8a00\u3001\u5730\u533a\u7b49\u73af\u5883\uff0c\u663e\u793a\u4e0d\u540c\u7684\u754c\u9762\u3002\u6bd4\u5982\u8bf4\uff0c\u540c\u6837\u662f\u6587\u4ef6\u83dc\u5355\uff0c\u4e2d\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201c\u6587\u4ef6\u201d\u3001\u82f1\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201cFile\u201d\u3002","title":"\u672c\u5730\u5316 (locale)"},{"location":"unicode/#_16","text":"C \u8bed\u8a00\u63d0\u4f9b\u4e86 \u5934\u6587\u4ef6\uff0c\u91cc\u9762\u5c01\u88c5\u4e86\u5927\u91cf\u5f62\u5982 isspace \u3001 isdigit \u8fd9\u6837\u7684\u5224\u65ad\u5b57\u7b26\u5206\u7c7b\u7684\u51fd\u6570\u3002 #include C++ \u5bf9\u5176\u5b9e\u65bd\u4e86\u518d\u5c01\u88c5\uff0c\u6539\u540d\u4e3a \u3002\u82e5\u4f60\u5bfc\u5165\u7684\u662f\u8be5\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e26\u6709 std \u540d\u5b57\u7a7a\u95f4\u524d\u7f00\u7684\u65b9\u5f0f std::isspace \uff0c std::isdigit \u8bbf\u95ee\u4e86\uff0c\u770b\u8d77\u6765\u66f4\u52a0\u4e13\u4e1a\uff08\u786e\u4fe1\uff09\u3002 #include \u51fd\u6570\u6e05\u5355\uff1a \u51fd\u6570\u540d\u79f0 \u5224\u65ad\u7684\u5b57\u7b26\u7c7b\u578b isascii 0 \u5230 0x7F \u7684\u6240\u6709 ASCII \u5b57\u7b26 isalpha \u5927\u5c0f\u5199\u5b57\u6bcd A-Z a-z isupper \u5927\u5199\u5b57\u6bcd A-Z islower \u5c0f\u5199\u5b57\u6bcd a-z isdigit \u6570\u5b57 0-9 isxdigit \u5341\u516d\u8fdb\u5236\u6570\u5b57 A-F a-f 0-9 isprint \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5305\u62ec\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u6807\u70b9\u7b49 isgraph \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7a7a\u683c iscntrl \u63a7\u5236\u5b57\u7b26\uff0c\u9664\u53ef\u6253\u5370\u5b57\u7b26\u5916\u7684\u5168\u90e8 isspace \u7a7a\u767d\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u6362\u884c\u3001\u56de\u8f66\u3001\u5236\u8868\u7b26\u7b49 ispunct \u6807\u70b9\u7b26\u53f7 isalnum \u5b57\u6bcd\u6216\u6570\u5b57 \u66f4\u8be6\u7ec6\u7684\u8868\u683c\u53ef\u4ee5\u770b\uff1a https://en.cppreference.com/w/cpp/string/byte/isspace","title":"\u533a\u5206\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#_17","text":"TODO","title":"\u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#stdlocale","text":"TODO","title":"\u533a\u57df\u8bbe\u7f6e\u4e0e std::locale"},{"location":"unicode/#_18","text":"TODO","title":"\u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316"},{"location":"unicode/#_19","text":"\u72ed\u4e49\u7684\u6c49\u5b57\uff1a0x4E00 \u5230 0x9FA5\uff08\u201c\u4e00\u201d\u5230\u201c\u9fa5\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\uff1a0x2E80 \u5230 0x9FFF\uff08\u201c\u2e80\u201d\u5230\u201c\u9fff\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\u5305\u542b\u4e86\u51e0\u4e4e\u6240\u6709\u4e2d\u65e5\u97e9\u4f7f\u7528\u7684\u6c49\u5b57\u5b57\u7b26\uff0c\u800c\u72ed\u4e49\u7684\u6c49\u5b57\u53ea\u662f\u4e2d\u6587\u91cc\u6700\u5e38\u7528\u7684\u4e00\u90e8\u5206\u3002 TODO","title":"\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f"},{"location":"unicode/#_20","text":"\u4e4b\u6240\u4ee5\u628a\u5bbd\u5b57\u7b26\u6d41\u653e\u5230\u6700\u540e\uff0c\u662f\u56e0\u4e3a\uff0c\u9996\u5148 iostream \u672c\u6765\u5c31\u662f\u4e00\u4e2a\u5931\u8d25\u7684\u8bbe\u8ba1\u3002 \u5c0f\u5f6d\u8001\u5e08\u5728\u672c\u4e66\u5f00\u5934\u5c31\u591a\u6b21\u5f3a\u8c03\u8fc7\u4ed6\u662f format \u5b5d\u5b50\u3002 \u800c\u5bbd\u5b57\u7b26 wchar_t \u672c\u8eab\u5c31\u5145\u65a5\u7740\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff08\u4f8b\u5982 Windows \u88ab UTF-16 \u80cc\u523a\uff09\u3002 \u73b0\u5728 iostream \u4e0e wchar_t \u4e00\u8d77\u51fa\u73b0\u5728\u6211\u9762\u524d\uff0c\u4e0d\u80fd\u8bf4\u662f\u68a6\u5e7b\u8054\u52a8\u5427\uff0c\u81f3\u5c11\u4e5f\u53ef\u4ee5\u8bf4\u662f\u7b54\u8fa9\u8d85\u4eba\u4e86\u3002 \u603b\u4e4b\uff0c\u6211\u4e2a\u4eba\u8fd8\u662f\u63a8\u8350\u7a0b\u5e8f\u5185\u90e8\u4ee5 UTF-8\uff08 char8_t \uff09\u6216 UTF-32\uff08 char32_t \uff09\u7684\u5b57\u7b26\u4e32\u6765\u5904\u7406\u4e07\u7269\u3002 UTF-8 \u6216 UTF-32 \u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u4e2d\u6587\u5904\u7406\u9700\u6c42\u662f\u5426\u65fa\u76db\uff0c\u662f\u5426\u5728\u4e4e\u7a7a\u95f4\uff0c\u662f\u5426\u9700\u8981\u5207\u7247\u548c\u7d22\u5f15\u7b49\u3002 \u5f53\u9700\u8981\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u518d\u7528 boost::locale \u3001 utfcpp \u7b49\u5de5\u5177\u8f6c\u6362\u6210 ANSI\uff08 char \uff09\u6216 UTF-16\uff08 wchar_t \uff09\u3002 \u5bf9\u4e8e Linux \u7528\u6237\uff0c\u4e5f\u53ef\u4ee5\u68c0\u6d4b\u5982\u679c\u662f Linux \u7cfb\u7edf\uff0c\u5219\u4ec0\u4e48\u8f6c\u6362\u90fd\u4e0d\u505a\uff0c\u56e0\u4e3a Linux \u7528\u6237\u51e0\u4e4e\u90fd\u662f UTF-8\uff0c\u90a3\u4e48 const char8_t * \u53ef\u4ee5\u5f3a\u8f6c\u4e3a const char * \u800c\u4e0d\u7528\u4efb\u4f55\u989d\u5916\u5f00\u9500\u3002 std::string to_os_string(std::string const &u8s) { #if _WIN32 // UTF-8 \u5230 ANSI return boost::locale::conv::from_utf(u8s, \"\"); #elif __linux__ // \u4e0d\u8f6c\u6362 return u8s; #else #error \"Unsupported system.\" #endif } \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u5b9e\u5728\u8981\u5b66\u7cdf\u7cd5\u7684\u5bbd\u5b57\u7b26\u6d41\uff0c\u90a3\u6211\u4e5f\u5949\u966a\u5230\u5e95\u3002","title":"\u5bbd\u5b57\u7b26\u6d41"},{"location":"unicode/#stdwstring","text":"\u5728\u4ed6\u4eec\u770b\u6765\uff0c std::string \u662f\u5df2\u7ecf\u5e9f\u5f03\u7684\u3002\u4ed6\u4eec\u8ba4\u4e3a std::wstring \u624d\u662f\u771f\u6b63\u8de8\u5e73\u53f0\uff0c\u8de8\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u3002 std::wstring : \u5b57\u7b26\u4e32 std::string : \u5b57\u8282\u6570\u7ec4 std::wifstream : \u6587\u672c\u6d41 std::ifstream : \u4e8c\u8fdb\u5236\u6d41 \u770b\u8d77\u6765\u53ea\u8981\u5168\u90e8\u7edf\u4e00 wchar_t \u5c31\u80fd\u5b9e\u73b0\u8de8\u5e73\u53f0\u4e86\uff1f\u662f\u7684\uff0c\u9664\u4e86 Windows\u2026\u2026 \u6807\u51c6\u8ba4\u4e3a wchar_t \u5e94\u8be5\u5305\u542b 0 \u5230 0x10FFFF \u7684\u6240\u6709\u7684 Unicode \u5b57\u7b26\u7801\u70b9\uff0c\u9700\u8981\u662f 32 \u4f4d\u7684\u3002\u7136\u800c Windows \u7684 wchar_t \u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u662f 16 \u4f4d\u7684\uff0c\u9700\u8981\u7528\u4ee3\u7406\u5bf9\u624d\u80fd\u8868\u793a\u7a00\u6709\u5b57\u7b26\uff0c\u5e76\u4e0d\u80fd\u4e00\u4e2a wchar_t \u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002\u8fd9\u5bfc\u81f4\u5373\u4f7f\u7528\u4e86 wchar_t \u8fd8\u662f\u5b58\u5728\u8de8\u5e73\u53f0\u56f0\u96be\u7684\u95ee\u9898\uff1a\u4e00\u4e2a Linux \u7a0b\u5e8f\u7528 wchar_t \u53ef\u80fd\u4f1a\u5229\u7528 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u7279\u6027\uff0c\u65b9\u4fbf\u4e86\u6587\u672c\u5904\u7406\uff0c\u4f46\u79fb\u690d\u5230 Windows \u65f6\uff0c\u53d1\u73b0\u53d8\u6210\u4e86 UTF-16\uff0c\u9700\u8981\u5bf9\u4ee3\u7406\u5bf9\u505a\u7279\u6b8a\u5224\u65ad\u2026\u2026\u6ca1\u6709\u6ee1\u8db3\u8de8\u5e73\u53f0\u7684\u521d\u8877\uff0c\u4e5f\u505a\u4e0d\u5230\u5b9a\u957f\u7f16\u7801\u3002 char32_t \u505a\u5230\u4e86\u8de8\u5e73\u53f0\u7684 UTF-32\uff0c\u4e5f\u80fd\u5bb9\u7eb3\u5168\u90e8 Unicode \u7801\u70b9\uff0c\u53ef\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::to_wstring \uff0c\u5374\u6839\u672c\u6ca1\u6709 std::to_u32string \uff1b\u63d0\u4f9b\u4e86 std::wcout \uff0c\u5374\u6ca1\u6709\u63d0\u4f9b std::u32cout \u2026\u2026 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5bbd\u5b57\u7b26\u6d41\u5f88\u7cdf\u7cd5\uff0c\u8bf4\u662f\u8de8\u5e73\u53f0\uff0c\u8de8\u4e86\u4e2a\u5bc2\u5bde\u3002","title":"\u5b98\u65b9\u773c\u4e2d\u7684 std::wstring"},{"location":"unicode/#wchar_t","text":"TODO","title":"wchar_t \u7cfb\u5217\u51fd\u6570"},{"location":"unicode/#stdwcout","text":"TODO","title":"std::wcout \u7684\u4f7f\u7528"},{"location":"unicode/#stdwfstream","text":"TODO","title":"std::wfstream \u7684\u4f7f\u7528"},{"location":"unicode/#c","text":"TODO","title":"C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362"},{"location":"unicode/#c-codecvt","text":"TODO","title":"C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 <codecvt>"},{"location":"unicode/#windows","text":"\u9047\u5230\u5b57\u7b26\u7f16\u7801\u96be\u9898\u7684\uff0c\u4e3b\u8981\u662f Windows \u7a0b\u5e8f\u5458\u3002","title":"Windows \u4e13\u9898"},{"location":"unicode/#windows-api-w","text":"\u4ece Windows NT \u7248\u672c\u5f00\u59cb\uff0c\u5bf9\u4e8e\u6240\u6709\u6d89\u53ca\u5b57\u7b26\u4e32\u7684\uff0c\u5176\u64cd\u4f5c\u7cfb\u7edf API \u63d0\u4f9b\u4e86\u4e24\u5957\u51fd\u6570\u3002 \u4e00\u5957\u662f A \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 A \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileA \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 ANSI\uff08\u5373 GBK\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u53e6\u4e00\u5957\u662f W \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 W \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileW \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 Unicode\uff08\u5373 UTF-16\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u5176\u4e2d CreateFileW \u624d\u662f Windows \u7cfb\u7edf\u771f\u6b63\u7684 API\u3002 \u800c CreateFileA \u662f\u4e3a\u4e86\u517c\u5bb9\u57fa\u4e8e ANSI \u7684\u8001\u7a0b\u5e8f. \u7531\u4e8e ANSI \u5728\u4e0d\u540c\u5730\u533a\u4f1a\u53d8\u5f97\u4e0d\u540c\uff0c\u4f7f\u7528\u8fd9\u7c7b\u51fd\u6570\u5199\u51fa\u7684\u7a0b\u5e8f\u4e0d\u5177\u6709\u56fd\u9645\u901a\u7528\u6027\u3002 \u5176\u5185\u90e8\u7684\u5b9e\u73b0\u53ea\u662f\u7b80\u7b80\u5355\u5355\u5730\u7ed9 const char * \u505a\u4e2a\u8f6c\u6362\uff0c\u4ece GBK \u8f6c\u5230 UTF-16\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528 CreateFileW\u3002 HANDLE CreateFileA(const char *lpFileName) { return CreateFileW(gbk_to_utf16(lpFileName)); }","title":"Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570"},{"location":"unicode/#tchar","text":"\u9664\u4e86\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b8f\uff0c\u8fd9\u4e2a\u5b8f\u6ca1\u6709\u4efb\u4f55\u540e\u7f00\uff0c\u4f8b\u5982 CreateFile \u3002 \u5176\u5b9a\u4e49\u5982\u4e0b\uff1a #ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif \u8fd9\u6837\u505a\u7684\u521d\u8877\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u53ef\u4ee5\u5199\u51fa\u4e00\u5957\u9488\u5bf9 MessageBox \u7684\u4ee3\u7801\u3002 \u5f53\u8001\u677f\u60f3\u8981\u57fa\u4e8e Unicode \u65f6\uff0c\u4ed6\u5c31 #define UNICODE \uff0c\u8fd9\u6837 MessageBox \u5c31\u53d8\u6210\u4e86 MessageBoxW \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u53d8\u6210 Unicode \u7684\uff0c\u56fd\u9645\u901a\u7528\u3002 \u5f53\u52b3\u4fdd\u8001\u677f\u60f3\u8981\u57fa\u4e8e ANSI \u65f6\uff0c\u4ed6\u5c31\u4e0d\u5b9a\u4e49 UNICODE \u5b8f\uff0c\u8fd9\u6837\u6240\u6709\u7684 MessageBox \u53c8\u53d8\u56de\u4e86 MessageBoxA \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u53c8\u53d8\u6210 ANSI \u7684\u4e86\u3002 \u6240\u6709\u6709 A/W \u533a\u5206\u7684\u7684 Windows API \u90fd\u6709\u8fd9\u6837\u4e00\u4e2a\u5b8f\uff0c\u6839\u636e UNICODE \u5b8f\u662f\u5426\u5b9a\u4e49\uff0c\u51b3\u5b9a\u91c7\u53d6\u54ea\u5957 API\u3002 \u6211\u4eec\u4f1f\u5927\u7684 Linux \u7cfb\u7edf\u5c31\u6ca1\u6709\u8fd9\u4e2a\u82e6\u607c\uff0c\u65e9\u5c31\u7edf\u4e00 UTF-8 \u4e86\u3002 \u9664\u6b64\u4e4b\u5916\uff0c \u4e2d\u8fd8\u5b9a\u4e49\u4e86 TEXT \u8fd9\u4e2a\u5b8f\u51fd\u6570\u3002 #ifdef UNICODE #define TEXT(s) L##s #else #define TEXT(s) s #endif \u7528\u6cd5\uff1a\u8981\u6c42\u7a0b\u5e8f\u5458\u628a\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u90fd\u7528 TEXT \u5b8f\u5305\u88f9\u3002 Qt \u4e5f\u6709\u7c7b\u4f3c\u7684\u5b8f\u5305\u88f9\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u505a\u6cd5\uff0c tr \uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u89e3\u51b3\u7f16\u7801\u95ee\u9898\uff0c\u800c\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u8bed\u8a00\u7ffb\u8bd1\u95ee\u9898\uff0c\u7a0d\u540e\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u4e00\u4e0b Qt \u4e2d\u7684\u5b57\u7b26\u4e32\u3002 \u5f53 UNICODE \u5b8f\u5b9a\u4e49\u65f6\uff0c TEXT \u4f1a\u81ea\u52a8\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u6dfb\u52a0 L \u524d\u7f00\uff0c\u4f7f\u5f97\u5b57\u7b26\u4e32\u53d8\u6210 Unicode \u7684\u3002\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\uff0c\u5219\u53c8\u53d8\u56de ANSI \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff08\u8ddf\u968f\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\u7684\u8bbe\u5b9a\uff09\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u6bb5\u4ee3\u7801\uff1a #include int main() { MessageBox(NULL, TEXT(\"\u4f60\u597d\uff0c\u4e16\u754c\"), TEXT(\"\u6807\u9898\"), MB_OK); } \u5f53\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u6807\u9898\", MB_OK); } \u5f53\u6ca1\u6709\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u6807\u9898\", MB_OK); } \u6b64\u5916\uff0c\u8fd8\u5b9a\u4e49\u4e86 TCHAR \u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u540c\u6837\u662f\u9488\u5bf9\u662f\u5426\u5b9a\u4e49 UNICODE \u5b8f\u800c\u5b9a\u4e49\u4e86\u4e24\u5957\u7248\u672c\u3002 #ifdef UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif \u8fd8\u4e3a printf \u548c wprintf \u5b9a\u4e49\u4e86 TCHAR \u7248\u672c\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709 strlen \u548c wcslen \uff0c strcpy \u548c wcscpy \uff0c\u7b49\u7b49\u3002 #ifdef UNICODE #define _tprintf wprintf #define _tcscpy wcscpy #define _tcslen wcslen #else #define _tprintf printf #define _tcscpy strcpy #define _tcslen strlen #endif \u4e0d\u89c9\u5f97\u8fd9\u5f88\u9177\u5417\uff1f\u5f88\u7b26\u5408\u6211\u5bf9\u5f3a\u8feb\u75c7\u7684\u60f3\u8c61\uff0c\u79d1\u6280\u5e76\u4e14\u5e26\u7740\u81ed\u5473\u3002 int main() { TCHAR str[] = TEXT(\"\u6bd4\u5c14\u76d6\u5b50\u6211\u6d4b\u8bd5\u4f60\u7684\u7801\"); } (* \u54e6\uff0c\u6211\u662f\u8bf4\uff0c\u6211\u8981\u6d4b\u8bd5\u4f60\u7684\u7f16\u7801\u683c\u5f0f *) \u9700\u8981\u5207\u6362\u65f6\uff0c\u5728 MSVC \u4e2d\uff0c\u6253\u5f00\u6216\u5173\u95ed /DUNICODE \u7f16\u8bd1\u9009\u9879\u5373\u53ef\u3002 \u4e0d\u8981\u89c9\u5f97\u8fd9\u662f\u4ec0\u4e48\u597d\u4e3b\u610f\uff0c\u8fd9\u6837\u505a\u7684\u540e\u679c\u662f\uff0c\u4f60\u5199\u51fa\u7684\u4ee3\u7801\u53ea\u80fd\u5728 Windows \u4e0b\u7f16\u8bd1\u3002 \u5199\u8d77\u6765\u7d2f\u6b7b\u4eba\uff0c\u5b9e\u9645\u54ea\u6709\u90a3\u4e48\u591a\u4e00\u76f4\u5728 ANSI \u548c Unicode \u4e4b\u95f4\u6765\u56de\u5207\u6362\u7684\u9700\u6c42\uff1f \u6211\u7684\u5efa\u8bae\u662f\uff0c\u7edf\u4e00 wchar_t \uff0c\u7edf\u4e00\u5168\u7528 W \u51fd\u6570\uff0c\u618b\u62a0\u62a0\u7d22\u7d22\u7684\u534a\u8fdb\u534a\u9000\u3002","title":"TCHAR \u6d41\u6d3e"},{"location":"unicode/#utf-8_4","text":"\u4e4b\u524d\u8bf4\u8fc7\u4e86\uff0cWindows \u5e73\u53f0\u5230\u5904\u90fd\u9ed8\u8ba4 GBK \u975e\u5e38\u9ebb\u70e6\uff0c\u8981\u5207\u6362\u5230 UTF-8 \u5de5\u4f5c\u6d41\uff1a \u7f16\u8bd1\u5668\u5f00\u542f /utf-8 \u9009\u9879 \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c system(\"chcp 65001\") \u8bbe\u7f6e\u6587\u4ef6\u7cfb\u7edf\u5b57\u7b26\u4e32\u7f16\u7801\uff0c setlocale(LC_ALL, \".utf-8\") // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 SetConsoleCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u5165\u7f16\u7801\uff0c\u7528\u4e8e std::cin #elif __unix__ // \u53cd\u6b63 Unix \u7cfb\u7edf\u9ed8\u8ba4\u90fd\u662f UTF-8\uff0c\u4e0d\u8bbe\u7f6e\u4e5f\u884c\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u5168\u5c40 locale \u662f\u4e3a\u4e86\u8ba9 iswspace \u63a5\u53d7\u5168\u89d2\u7a7a\u683c\u3001iswpunct \u63a5\u53d7\u5168\u89d2\u9017\u53f7 L'\uff0c' \u7b49 //setlocale(LC_ALL, \"zh_CN.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u4e2d\u6587\u672c\u5730\u5316\uff0c\u53ef\u4f7f strerror \u8f93\u51fa\u4e2d\u6587\uff08\u4f46\u7528\u6237\u5fc5\u987b locale-gen \u8fc7\u4e2d\u6587\uff01\uff09 //setlocale(LC_ALL, \"C.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u8bed\u8a00\u4e2d\u6027 locale\uff0c\u53ea\u5f71\u54cd iswspace\u3001iswpunct \u7b49\u51fd\u6570\uff0c\u4e0d\u4f1a\u4f7f strerror \u7b49\u8f93\u51fa\u4e2d\u6587 setlocale(LC_ALL, \".utf-8\"); // \u82e5\u4e0d\u5e26\u4efb\u4f55\u524d\u7f00\uff08\u63a8\u8350\uff09\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8bed\u8a00 $LANG\uff0c\u4f7f strerror \u81ea\u52a8\u9002\u5e94 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 std::wcout << L\"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u4f60\u90fd\u7edf\u4e00 UTF-8 \u4e86\uff0c\u8fd9\u7834 UTF-16 \u548c UTF-32 \u4e4b\u95f4\u6765\u56de\u8df3\u7684\u7834 wchar_t \u5c31\u522b\u7528\u4e86\u5457\uff01 return 0; }","title":"UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f"},{"location":"unicode/#wndproc","text":"LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == false \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::cout << char(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f GBK \u7f16\u7801\u7684 char \u5e8f\u5217 // \u5982\u679c\u662f\u4e2d\u6587\u5b57\u7b26\uff0cWndProc(WM_CHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u5b57\u8282\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u5df1\u5224\u65ad\u548c\u62fc\u63a5 GBK \u5b57\u7b26\u4e32 return 0; case WM_UNICHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == true \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::wcout << wchar_t(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f UTF-16 \u7f16\u7801\u7684 wchar_t \u5e8f\u5217 // \u5982\u679c\u662f\u4ee3\u7406\u5bf9\uff0cWndProc(WM_UNICHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u7801\u4f4d\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u884c\u628a\u4ee3\u7406\u5bf9\u7ec4\u88c5\u6210\u5b8c\u6574\u7684 Unicode \u7801\u70b9 return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } } \u628a WndProc \u7684\u8f93\u5165\u5b58\u5165 std::u32string \u7684\u6848\u4f8b\uff1a std::u32string result; std::string ansi_buf; std::wstring utf16_buf; std::optional try_ansi_to_utf32(std::string const &s) { try { return boost::locale::conv::to_utf(s, \"\"); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } std::optional try_utf16_to_utf32(std::wstring const &s) { try { return boost::locale::conv::utf_to_utf(s); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: ansi_buf.push_back(char(wParam)); if (auto u = try_ansi_to_utf32(ansi_buf)) { result += u.value(); ansi_buf.clear(); } return 0; case WM_UNICHAR: utf16_buf.push_back(wchar_t(wParam)); if (auto u = try_utf16_to_utf32(utf16_buf)) { result += u.value(); utf16_buf.clear(); } return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); };","title":"WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165"},{"location":"unicode/#_21","text":"\u6807\u51c6\u5e93\u7684 std::string \u6211\u4eec\u4e0d\u518d\u8d58\u8ff0\uff0c\u521a\u624d\u5728\u201c\u5bbd\u5b57\u7b26\u6d41\u201d\u4e2d\u4e5f\u4ecb\u7ecd\u4e86\u5b98\u65b9\u7684\u60f3\u6cd5\uff0c\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u4e13\u9898\u8bfe\u4f1a\u7ee7\u7eed\u8be6\u89e3\u3002 \u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u5173\u4e8e\u5b57\u7b26\u7f16\u7801\u7684\u95ee\u9898\uff0c\u63a2\u7d22\u5404\u5927\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u548c\u5e93\uff0c\u662f\u5982\u4f55\u5c01\u88c5\u5b57\u7b26\u4e32\u7c7b\uff0c\u5982\u4f55\u89e3\u51b3 UTF-8 \u53d8\u957f\u7f16\u7801\uff0cUTF-32 \u538b\u7f29\u7387\u4f4e\u7684\u95ee\u9898\u7684\uff0c\u5e0c\u671b\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u63d0\u4f9b\u4e00\u70b9\u7075\u611f\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u4e00\u4e2a\u597d\u7684\u5e93\u6216\u8bed\u8a00\uff0c\u90fd\u8981\u660e\u786e\u533a\u5206\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u6982\u5ff5\uff0c\u524d\u8005\u662f\u6587\u672c\u5185\u5bb9\uff0c\u540e\u8005\u662f\u7eaf\u4e8c\u8fdb\u5236\u5185\u5bb9\u3002 \u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u201c\u7f16\u7801\u201d\u5f97\u5230\u7eaf\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u800c\u5b57\u8282\u6570\u7ec4\u53ef\u4ee5\u201c\u89e3\u7801\u201d\u5f97\u5230\u539f\u59cb\u5b57\u7b26\u4e32\u3002 \u65e9\u671f\u7684 C \u8bed\u8a00\u5c31\u662f\u56e0\u4e3a\u628a\u5b57\u7b26\u548c\u5b57\u8282\u6df7\u4e3a\u4e00\u8c08\uff0c\u90fd\u4f7f\u7528\u4e86 char \u7c7b\u578b\uff0c\u624d\u4ea7\u751f\u4e86\u540e\u6765\u8fd9\u4e48\u591a\u4e71\u8c61\u3002\u540e\u6765\u901a\u8fc7\u6253\u8865\u4e01\u6253\u4e0a\u771f\u6b63\u7684\u5b57\u7b26 wchar_t \uff0c\u5374\u6ca1\u4ec0\u4e48\u4eba\u7528\uff0c\u800c\u4e14\u8fd8\u88ab Windows \u641e\u6210 16 \u4f4d\uff0c\u53cd\u800c\u4e0d\u8de8\u5e73\u53f0\u4e86\u3002 \u6b64\u5904\u5148\u5217\u4e00\u4e2a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u773c\u4e2d\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u8868\uff0c\u65b9\u4fbf\u4f60\u7406\u89e3\u3002 \u8bed\u8a00 \u5b57\u7b26 \u5b57\u7b26\u4e32 \u6587\u672c\u6d41 \u5b57\u8282\u6570\u7ec4 \u4e8c\u8fdb\u5236\u6d41 \u7f16\u7801/\u89e3\u7801 C char wchar_t * FILE * + fgetwc char * FILE * + fgetc wcstomb / mbstowc C++ wchar_t std::wstring std::wistream std::string / std::vector std::istream std::codecvt Qt QChar QString QTextStream QByteArray QDataStream QTextCodec Python3 str str open('r') bytes open('rb') str.encode() Python2 unicode unicode \u65e0 str open('r') unicode.encode() Java Character String Reader byte[] InputStream Charset.encode C# char string StreamReader byte[] Stream Encoding Rust char String BufRead u8 Read str::from_utf8 JS char String ReadableStream Uint8Array ReadableStream TextEncoder Go rune string Reader byte Reader utf8.DecodeRune PHP string string fopen string fopen mb_convert_encoding Swift Character String String.UnicodeScalarView UInt8 Data String.Encoding Kotlin Char String Reader ByteArray InputStream Charset.encode Obj-C unichar NSString NSInputStream uint8_t NSInputStream NSStringEncoding Lua integer table \u65e0 string io.open require'utf8' \u672c\u8bfe\u4e0d\u4f1a\u8bb2\u89e3\u8fd9\u4e9b\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u5177\u4f53\u7528\u6cd5\uff0c\u53ea\u63d0\u4f9b\u4e00\u4e9b\u6982\u5ff5\uff0c\u8ba9\u4f60\u77e5\u9053\u5927\u5bb6\u90fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u89e6\u7c7b\u65c1\u901a\u3002 Lua \u4e2d UTF-32 \u7684\u5b9a\u957f\u7f16\u7801\uff0c\u8981\u8fd9\u6837\u5b9e\u73b0\uff1a {80, 101, 110, 103} \u3002\u800c\u4ed6\u6240\u8c13\u7684 utf8 \u5e93\uff0c\u5c31\u662f\u8d1f\u8d23\u628a Lua \u81ea\u5df1\u7684 string \u5047\u8bbe\u4e3a utf8 \u7f16\u7801\uff0c\u89e3\u7801\u51fa\u4e00\u4e2a\u4e2a Unicode \u7801\u70b9\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u7ec4\u3002\u4ed6\u751a\u81f3\u4e0d\u662f Lua \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u662f\u4e2a\u7b2c\u4e09\u65b9\u7684\u5e93\uff0c\u8fd8\u662f\u9700\u8981\u7f16\u8bd1\u7684 C \u8bed\u8a00 .so \u6587\u4ef6\u3002\u8fd8\u6709 Lua \u5b5d\u5b50\u501f\u6b64\u786c\u8bf4\u6211\u4eec Lua \u662f\u5929\u751f UTF-8\uff01\u7136\u800c\u62ff\u7740\u4e2a UTF-8 \u7f16\u7801\u7684\u201c\u5b57\u8282\u6570\u7ec4\u201d string \u6765\u6253\u5f00 io.open \u6587\u4ef6\uff0c\u5c31\u4f1a\u62a5\u9519\u627e\u4e0d\u5230\u6587\u4ef6\uff08\u56e0\u4e3a\u4e2d\u56fd\u533a Windows \u7684 GBK\uff09\uff0c\u662f\u4e0d\u662f\u5f88\u597d\u7b11\u5462\uff1f","title":"\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76"},{"location":"unicode/#qt-qstring","text":"Qt \u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 Qt \u7684\u5b57\u8282\u6570\u7ec4\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 \u5b83\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u9645\u4e0a\u662f\u4e2a QChar \u6570\u7ec4\uff0c\u800c QChar \u662f unsigned short \uff0c\u5373 16 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u4e5f\u5c31\u662f UTF-16 \u7f16\u7801\u7684\u7801\u4f4d\u3002 QString str = \"\u4f60\u597d\uff0c\u4e16\u754c\"; // str.size() = 5 // str[0] = QChar(0x4f60) = u'\u4f60' // str[1] = QChar(0x597d) = u'\u597d' // str[2] = QChar(0xff0c) = u'\uff0c' // str[3] = QChar(0x4e16) = u'\u4e16' // str[4] = QChar(0x754c) = u'\u754c' \u53ef\u89c1\uff0c QString \u662f UTF-16 \u7f16\u7801\u7684\uff0c\u5c31\u548c Java \u4e00\u6837\uff0cQt \u4e5f\u662f UTF-16 \u6f6e\u7684\u53d7\u5bb3\u8005\u3002 \u6240\u4ee5\uff0cQString \u4e5f\u5b58\u5728\u7740\u4ee3\u7406\u5bf9\u53d8\u957f\u7f16\u7801\u7684\u95ee\u9898\u3002\u4f46\u81f3\u5c11\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5b57\u7b26\u6765\u8bf4\uff0c\u4e00\u4e2a 16 \u4f4d\u7684 QChar \u90fd\u5bb9\u7eb3\u7684\u4e0b\u4e86\u3002","title":"Qt QString"},{"location":"unicode/#qtextcodec","text":"Qt \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7f16\u7801\u8f6c\u6362\u51fd\u6570\uff0c\u7528\u4e8e\u5c06 QString \u8f6c\u6362\u6210 QByteArray \uff0c\u6216\u8005\u4ece QByteArray \u8f6c\u6362\u6210 QString \uff0c\u8fd9\u4e9b\u51fd\u6570\u540d\u90fd\u662f\u4ee5 to \u6216\u8005 from \u5f00\u5934\u7684\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u540d\uff0c\u4f8b\u5982 fromUtf8 \u3001 toUtf8 \u3001 toLocal8Bits \u3002 \u8fd9\u4e9b\u51fd\u6570\u7684\u5185\u90e8\uff0c\u90fd\u662f\u8c03\u7528 QTextCodec \u7c7b\u5b9e\u73b0\u7684\u8f6c\u6362\u3002 QTextCodec \u662f Qt \u7528\u4e8e\u5904\u7406\u5404\u79cd\u6587\u672c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u7684\u7c7b\u3002\u5b83\u7684\u9759\u6001\u65b9\u6cd5 codecForLocale \u8fd4\u56de\u4e86\u5f53\u524d\u7cfb\u7edf\u7684\u7f16\u7801\uff0c toUnicode \u548c fromUnicode \u5206\u522b\u662f\u5c06 QByteArray \u8f6c\u6362\u6210 QString \uff0c\u6216\u8005\u5c06 QString \u8f6c\u6362\u6210 QByteArray \u3002 QTextCodec *codec = QTextCodec::codecForLocale(); // \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7f16\u7801 QByteArray bytes = codec->fromUnicode(str); // \u5c06 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u5373 char[] QString str = codec->toUnicode(bytes); // \u5c06 QByteArray \u8f6c\u6362\u6210 QString QTextCodec \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u8f6c\u6362\u63a5\u53e3\uff0c\u4f8b\u5982 fromUnicode \u9664\u4e86\u63a5\u53d7 QString \uff0c\u8fd8\u63a5\u53d7 QChar \u6570\u7ec4\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f6c\u6362\u8303\u56f4\u3002","title":"QTextCodec"},{"location":"unicode/#fromtolocal8bitsutf8latin1ascii","text":"\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cQt \u5c01\u88c5\u4e86\u4e00\u4e9b\u5e38\u7528\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u8fd9\u6837\u4f60\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a QTextCodec\u3002\u90fd\u662f to \u548c from \u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u7684\u540d\u79f0\u3002 Local8Bits \u8868\u793a\u8fd0\u884c\u65f6\u68c0\u6d4b\u5230\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u5ba2\u6237\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 QByteArray bytes = str.toLocal8Bits(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801 QString::fromLocal8Bits(bytes); // \u518d\u4ece\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForLocale(); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Utf8 \u8868\u793a\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002 QByteArray bytes = str.toUtf8(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 UTF-8 \u7f16\u7801 QString::fromUtf8(bytes); // \u518d\u4ece UTF-8 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Latin1 \u8868\u793a ISO-8859-1 \u7f16\u7801\uff0c\u53c8\u79f0\u4e3a\u897f\u6b27\u7f16\u7801\uff0c\u5b83\u662f\u4e00\u4e2a\u5355\u5b57\u8282\u7f16\u7801\uff0c\u548c ASCII \u7f16\u7801\u76f8\u4f3c\uff0c\u4f46\u662f\u591a\u4e86 128-255 \u7684\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u6cd5\u8bed\u3001\u5fb7\u8bed\u3001\u897f\u73ed\u7259\u8bed\u3001\u8461\u8404\u7259\u8bed\u7b49\u5b57\u7b26\u3002 QByteArray bytes = str.toLatin1(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 Latin1 \u7f16\u7801 QString::fromLatin1(bytes); // \u518d\u4ece Latin1 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ISO-8859-1\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Ascii \u8868\u793a ASCII \u7f16\u7801\uff0c\u548c Latin1 \u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u65e0\u6cd5\u5904\u7406 128-255 \u8fd9\u4e00\u6bb5\u7684\u5b57\u7b26\u3002 QByteArray bytes = str.toAscii(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 ASCII \u7f16\u7801 QString::fromAscii(bytes); // \u518d\u4ece ASCII \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ASCII\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes);","title":"from/toLocal8Bits/Utf8/Latin1/Ascii"},{"location":"unicode/#_22","text":"QString str = QStringLiterial(\"\u4f60\u597d\uff0c\u4e16\u754c\"); QStringLiterial \u53ef\u4ee5\u4fdd\u8bc1\uff0c\u8f6c\u6362\u65f6\u91c7\u7528\u7684\u662f\u6240\u8c13\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\uff08\u5b9e\u9645\u5e94\u8be5\u53eb\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\uff09\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5f00\u53d1\u8005\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u662f\u7f16\u8bd1\u671f\u786e\u5b9a\u7684\u3002\u800c\u5982\u679c\u5199 QString::fromLocal8Bits(\"\") \u5c31\u53d8\u6210 \u201cANSI\u201d\uff0c\u5ba2\u6237\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u4e86\u3002\u8fd9\u4e24\u4e2a\u5b57\u7b26\u7f16\u7801\uff0c\u6bd4\u5982\u5728\u4e4b\u524d\u8de8\u56fd galgame \u7684\u6848\u4f8b\u4e2d\uff0c\u5c31\u662f\u4e0d\u540c\u7684\u3002","title":"\u5b57\u7b26\u4e32\u5e38\u91cf"},{"location":"unicode/#qtextstream","text":"QTextStream \u662f Qt \u63d0\u4f9b\u7684\u6587\u672c\u6d41\u7c7b\uff08\u5e26\u6709\u7f13\u51b2\uff09\uff0c\u5b83\u53ef\u4ee5\u5c06\u6587\u672c\u5199\u5165\u5230\u6587\u4ef6\u3001\u5957\u63a5\u5b57\u3001\u6807\u51c6\u8f93\u51fa\u7b49\u8bbe\u5907\u3002 \u6587\u672c\u6d41\u548c\u4e8c\u8fdb\u5236\u6d41\u4e0d\u540c\uff0c\u4ed6\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u8fc7 QTextStream \u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u3002 \u5f53\u4f60\u5f80 QTextStream \u5199\u5165 QString \u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7f16\u7801\u683c\u5f0f\u81ea\u52a8\u8f6c\u6362\u4e3a QByteArray \uff0c\u7136\u540e\u624d\u5199\u5165\u3002 \u8bfb\u53d6\u65f6\u4e5f\u540c\u7406\uff0c\u4f1a\u628a\u8bfb\u5230\u7684 QByteArray \u901a\u8fc7\u7f16\u7801\u683c\u5f0f\u89e3\u7801\uff0c\u5f97\u5230 QString \u5b57\u7b26\u4e32\u3002 Qt \u503c\u5f97\u79f0\u9053\u7684\u4e00\u70b9\u662f\uff1a\u4ed6\u628a\u6587\u4ef6\u548c\u6587\u4ef6\u6d41\u533a\u5206\u5f00\u6765\uff0c\u6587\u4ef6 QFile \u53ea\u9700\u8981\u8d1f\u8d23\u6253\u5f00\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff1b\u800c\u6587\u672c\u6d41 QTextStream \u624d\u771f\u6b63\u8d1f\u8d23\u6570\u636e\u7684\u7f13\u51b2\uff0c\u89e3\u7801\u7b49\u64cd\u4f5c\u3002\u4f53\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u804c\u8d23\u5355\u4e00\u539f\u5219\u3002 QFile file(\"hello.txt\"); file.open(QIODevice::ReadWrite); QTextStream stream(&file); stream.setCodec(\"UTF-8\"); // \u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f stream << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u5199\u5165 QString\uff0cQTextStream \u4f1a\u81ea\u52a8\u5c06\u5176\u7528 UTF-8 \u7f16\u7801\u4e3a QByteArray \u540e\u5199\u5165 QFile \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u76f4\u63a5\u5199\u5165\u4e8c\u8fdb\u5236\u7684 QByteArray \uff0cQt \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a QDataStream \u7c7b\uff0c\u65b9\u4fbf\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002 \u5982\u6b64\u628a\u4e8c\u8fdb\u5236\u548c\u6587\u672c\u4e25\u683c\u533a\u5206\uff0c\u800c\u4e0d\u662f\u50cf\u53e4\u4ee3 C \u8bed\u8a00\u90a3\u6837\u5b57\u8282\u4e0e\u5b57\u7b26\u6df7\u6dc6\u4e0d\u6e05\uff0c\u5168\u7528\u7cdf\u7cd5\u7684 char \u6765\u8868\u793a\uff0c\u5145\u5206\u4f53\u73b0\u4e86\u5f3a\u7c7b\u578b\u7684\u5b89\u5168\u3002","title":"QTextStream"},{"location":"unicode/#python-3-str","text":"\u5982\u4f55\u89e3\u51b3 UTF-32 \u5360\u7528\u7a7a\u95f4\u5927\u7684\u75db\u70b9\uff1fPython \u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u7edd\u5bf9\u503c\u5f97\u4e00\u770b\uff01 \u4e3a\u4e86\u65b9\u4fbf\u6587\u672c\u5904\u7406\uff0c\u6700\u597d\u4ee5\u5b9a\u957f\u7f16\u7801\uff0c\u4e5f\u5c31\u662f UTF-32\uff0c\u5b58\u50a8\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u7528 UTF-8 \u6216 UTF-16 \u6765\u5b58\u50a8\u7684\u8bdd\uff0c\u4f1a\u9047\u5230\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff1a \u4f8b\u5982\u50cf\u5b57\u7b26\u4e32\u7d22\u5f15\uff0c\u5b57\u7b26\u4e32\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\uff0c\u8981\u4e48\u7d22\u5f15\u51fa\u6765\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u4e86\uff1b\u8981\u4e48\u5c31\u9700\u8981 O(N) O(N) \u7684\u590d\u6742\u5ea6\uff0c\u9010\u4e00\u904d\u5386\u6bcf\u4e2a\u5b57\u8282\uff0c\u624d\u80fd\u786e\u5b9a\u771f\u6b63\u7684\u4f4d\u7f6e\uff1b\u54ea\u6015\u5168\u662f ASCII \u4e5f\u5f97\u8fd9\u4e48\u505a\uff0c\u56e0\u4e3a\u4e07\u4e00\u521a\u597d\u6709\u4e00\u4e2a\u662f\u4e2d\u6587\u5b57\u7b26\u5462\uff1f \u6240\u4ee5\uff0c\u5bf9\u4e8e\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684 Python \u6765\u8bf4\uff0cUTF-8 \u662f\u65e0\u6cd5\u63a5\u53d7\u7684\uff0c\u4f3c\u4e4e\u53ea\u80fd\u4ee5 UTF-32 \u6765\u5b58\u50a8\uff1f Python \u60f3\u5230\u4e86\u4e00\u4e2a\u5c0f\u5999\u62db\uff1a enum PyUnicodeType { PyUnicodeType_Latin1, PyUnicodeType_UCS2, PyUnicodeType_UCS4, }; struct PyUnicodeString { PyUnicodeType type; union { uint8_t *latin1; uint16_t *ucs2; uint32_t *ucs4; }; }; \u8fd9\u91cc\u7684 union \u662f\u4e00\u4e2a C \u8bed\u8a00\u7279\u6027\uff0c\u4ed6\u5141\u8bb8\u4f60\u628a\u591a\u4e2a\u6210\u5458\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u7a7a\u95f4\u3002\u901a\u5e38\u6765\u8bf4\u9700\u8981\u914d\u5408\u4e00\u4e2a enum \u8868\u793a\u7c7b\u578b\uff0c\u624d\u80fd\u5b89\u5168\u4f7f\u7528\u3002\u73b0\u4ee3 C++ \u7684 std::variant \u66f4\u5b89\u5168\u7684\u53d6\u4ee3\u4e86\u4ed6\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u5916\u6302\u4e00\u4e2a enum \u3002CPython \u89e3\u91ca\u5668\u56e0\u4e3a\u662f C \u8bed\u8a00\u5b9e\u73b0\uff0c\u53ea\u80fd\u7528 union \u6a21\u62df std::variant \u7684\u6548\u679c\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFF \u7684\u5b57\u7b26\uff08Latin-1 \u8303\u56f4\uff09\u65f6\uff0c\u5b9e\u9645\u4e0a\u6ca1\u5fc5\u8981\u5168\u7528\u8d85\u5927\u7684 uint32_t \u6765\u5b58\u50a8\u3002\u5b8c\u5168\u53ef\u4ee5\u53ea\u53d6\u51fa\u4f4e 8 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a\u66f4\u7d27\u51d1\u7684 uint8_t \u6570\u7ec4\uff0c\u5c31\u50cf Latin-1 \u7f16\u7801\u4e00\u6837\u3002 \u4f46\u662f\u5f53\u6309\u7d22\u5f15\u8bfb\u53d6\u5143\u7d20\u65f6\uff0c\u4f1a\u5224\u65ad\u5f53\u524d union \u91cc\u88c5\u7684\u662f\u54ea\u79cd\u7c7b\u578b\uff0c\u5982\u679c\u662f Latin-1 \u7684\uff0c\u90a3\u5c31\u4f1a\u7528\u7ed3\u6784\u4f53\u91cc\u7684 uint8_t * \u6307\u9488\u6765\u7d22\u5f15\u3002 \u8fd9\u6837\uff0c\u5bf9\u4e8e\u5168 ASCII \u7684\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u8001\u8001\u5b9e\u5b9e\u5b58 UTF-32 \u5185\u5b58\u5360\u7528\u76f4\u63a5\u51cf\u5c11\u4e86 75%\uff01\u552f\u4e00\u7684\u4ee3\u4ef7\u662f\u6309\u7d22\u5f15\u8bfb\u5b57\u7b26\u5143\u7d20\u65f6\u9700\u8981\u505a\u4e2a if-else \u5224\u65ad\u3002\u540c\u65f6\u53c8\u4e0d\u5931\u53bb\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff08\u65e9\u671f Unicode \u8303\u56f4\uff09\u65f6\uff0c\u5219\u662f\u53d6\u51fa\u4f4e 16 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a uint16_t \u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u6848\u4e5f\u79f0\u4e3a UCS-2\u3002 \u6ce8\u610f UCS-2 \u5e76\u4e0d\u7b49\u540c\u4e8e UTF-16\uff0cUTF-16 \u662f\u80fd\u591f\u8868\u793a\u5b8c\u6574\u7684 Unicode \u7684\u53d8\u957f\u7f16\u7801\uff08\u6709\u4ee3\u7406\u5bf9\uff09\uff1b\u800c UCS-2 \u662f\u6ca1\u6709\u4ee3\u7406\u5bf9\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u7f3a\u70b9\u662f\u53ea\u80fd\u8868\u793a 0xFFFF \u7684\u8303\u56f4\u3002 \u5bf9\u4e8e\u5927\u90e8\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5185\u5b58\u5360\u7528\u5c31\u51cf\u5c11\u4e86 50%\uff0c\u4e5f\u4e0d\u4e8f\u3002 \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u201c\ud883\udede\u201d\u8fd9\u6837\u7684\uff0c\u8d85\u8fc7 0xFFFF \u7684\u5b57\u7b26\uff0c\u624d\u4f1a\u91c7\u7528 uint32_t \u8001\u8001\u5b9e\u5b9e\u5b58\u50a8\u771f\u6b63\u7684 UTF-32\uff0c\u53c8\u79f0 UCS-4\u3002 \u8fd9\u4e24\u79cd\u53eb\u6cd5\u662f\u7b49\u4ef7\u7684\uff0c\u53cd\u6b63 Unicode \u4ece\u6765\u6ca1\u6709\u8d85\u8fc7 0xFFFFFFFF \u7684\uff0c\u73b0\u5728\u4ed6\u4eec\u90fd\u662f\u5b9a\u957f\u7f16\u7801\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684\u865a\u62df\u673a\u8bed\u8a00\u6765\u8bf4\uff0c\u53cd\u6b63\u672c\u6765\u5c31\u4e0d\u5728\u4e4e if-else \u5206\u652f\u8fd9\u70b9\u5c0f\u5f00\u9500\uff0c\u8fd9\u786e\u5b9e\u662f\u6700\u597d\u7684\u65b9\u6848\u3002 \u7f3a\u70b9\u5c31\u662f\uff0c\u5f53\u4f60\u5f80\u4e00\u4e2a\u5b8c\u5168\u662f ASCII \u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u7a81\u7136\u63d2\u5165\u4e00\u4e2a\u201c\ud883\udede\u201d\u65f6\uff0c\u4f1a\u4ea7\u751f\u5de8\u5927\u7684\u5185\u5b58\u91cd\u5206\u914d\u3002\u867d\u7136\u53ea\u6709\u4e00\u4e2a\u201c\ud883\udede\u201d\uff0c\u4f46\u4e3a\u6b64\uff0c\u5176\u4ed6\u6240\u6709 ASCII \u5b57\u7b26\u90fd\u5f97\u4e3a\u4ed6\u6269\u5f20\u5230 32 \u4f4d\u7684\u5927\u5c0f\u3002\u800c UTF-8 \u548c\u4f20\u7edf\u7684 UTF-32 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4e5f\u4e0d\u5efa\u8bae C++ \u7a0b\u5e8f\u5458\u81ea\u5df1\u624b\u6413\u4e2a\u8fd9\u6837\u7684 union \u5b57\u7b26\u4e32\u3002","title":"Python 3 str"},{"location":"unicode/#rust-str-string","text":"\u800c Rust \u5219\u91c7\u7528\u4e86\u5b57\u7b26\u4e32\u5168\u5458 UTF-8 \u7684\u7b56\u7565\uff0c\u8fd9\u662f\u56e0\u4e3a Rust \u6700\u5e38\u7528\u4e8e\u4e92\u8054\u7f51\u65b9\u9762\u7684\u5e95\u5c42\u7cfb\u7edf\u8f6f\u4ef6\uff0c\u4e92\u8054\u7f51\u6700\u5e38\u7528\u7684\u6587\u672c\u7f16\u7801\u5c31\u662f UTF-8\uff0c\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\uff0c\u4e14\u56fd\u9645\u901a\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4e92\u8054\u7f51\u57fa\u5efa\u6700\u5e38\u89c1\u7684\u5e73\u53f0\u5c31\u662f Linux\uff0c\u4f7f\u7528 UTF-8 \u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u8c03\u7528 Linux \u7cfb\u7edf API \u65e0\u9700\u4efb\u4f55\u8f6c\u6362\u3002\u4e14\u6587\u672c\u6587\u4ef6\u57fa\u672c\u90fd\u53ef\u4ee5\u5047\u5b9a\u662f UTF-8 \u7f16\u7801\uff0c\u5199\u5165\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u590d\u6742\u5ea6\u4f4e\u81f3 O(1) O(1) \u3002\u4f5c\u4e3a\u4ee3\u4ef7\uff0c\u8fd9\u5bfc\u81f4\u6587\u672c\u5904\u7406\u4e0a\u7684\u4e00\u4e9b\u56f0\u96be\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u9700\u8981\u533a\u5206\u662f\u6309\u5b57\u8282\u7d22\u5f15\u8fd8\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u5982\u679c\u786e\u5b9e\u9700\u8981\u6309\u5b57\u7b26\u7d22\u5f15\u7684\u8bdd\uff0c\u590d\u6742\u5ea6\u5c31\u4f1a\u662f O(N) O(N) \u4e86\u3002 \u65e0\u8bba\u5982\u4f55\uff0c\u5982\u679c\u4f60\u9009\u62e9\u4e86 UTF-8 \u6d41\u6d3e\u7684\u8bdd\uff0cRust \u5b57\u7b26\u4e32\u7684\u201c\u8fed\u4ee3\u5668\u53cc\u8f68\u5236\u201d\u786e\u5b9e\u503c\u5f97\u79f0\u9053\uff1a let s = \"\u4f60\u597d\uff0c\u4e16\u754c\"; for c in s.chars() { // \u6309\u5b57\u7b26\u8fed\u4ee3 println!(\"{}\", c); } for b in s.bytes() { // \u6309\u5b57\u8282\u8fed\u4ee3 println!(\"{:02X}\", b); // \u6253\u5370\uff1aE4 BD A0 E5 A5 BD EF BC 8C E4 B8 96 E7 95 8C }","title":"Rust &str \u548c String"},{"location":"unicode/#java-string","text":"Java \u4e5f\u662f UTF-16 \u7684\u53cc\u5b57\u8282\u7f16\u7801\u3002 \u4eae\u70b9\uff1a\u4ed6\u7684 String \u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u65e0\u6cd5\u5c31\u5730\u4fee\u6539\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u6bcf\u6b21\u4ea7\u751f\u4f60\u8c03\u7528 += \u7684\u90fd\u662f\u4e00\u4e2a\u65b0 String\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u3002 \u4e5f\u5c31\u662f\u8bf4\uff1aJava \u7684 String \u867d\u7136\u662f\u201c\u5806\u201d\u4e2d\u7684\u5bf9\u8c61\uff0c\u5374\u65e0\u6cd5\u4ee5\u5f15\u7528\u4f20\u9012\u3002 \u8fd9\u907f\u514d\u4e86\u4ee5\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff1a void registerStudent(String name) { name += \".txt\"; File file(name); file.write(...); } void myTransaction() { String name = \"\u5c0f\u5f6d\"; lib.registerStudent(name); office.registerStudent(name); // \u8fd9\u91cc name \u662f\u5426\u88ab\u4fee\u6539\uff1f } \u5982\u679c Java \u7684 String \u548c\u666e\u901a\u5bf9\u8c61\u4e00\u6837\uff0c\u88ab\u8c03\u7528\u8005\u7684\u4fee\u6539\u53ef\u4ee5\u5bf9\u5916\u90e8\u53ef\u89c1\uff0c\u90a3\u5c82\u4e0d\u662f\u6211\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u4ee5 String \u4e3a\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u90fd\u9700\u8981\u64cd\u5fc3\uff1a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0d\u4f1a\u628a\u6211\u7684\u5b57\u7b26\u4e32\u4fee\u6539\u6389\uff1f \u6240\u4ee5\uff0cJava \u7ed9\u4ed6\u7684\u5bf9\u8c61\u6a21\u578b\u5f00\u4e86\u4e2a\u540e\u95e8\uff1a\u89c4\u5b9a\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u6309\u5f15\u7528\u4f20\u9012\uff0c\u9664\u4e86 String \uff01\u5c31\u53ea\u6709 String \u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u88ab\u8c03\u7528\u8005\u5185\u90e8\u7684\u4fee\u6539\u5bf9\u5916\u4e0d\u53ef\u89c1\u3002 C++ \u548c Rust \u53ea\u9700\u8981\u52a0\u4e2a\u5e38\u5f15\u7528\u5c31\u884c\uff0c\u800c Java \u53d7\u5bb3\u8005\u8981\u8003\u8651\u7684\u5c31\u591a\u4e86\u3002 \u603b\u4e4b\uff0c\u8fd9\u5c31\u662f\u6ca1\u6709 const \u7684\u5783\u573e\u8bed\u8a00\u7684\u4e11\u6001\uff0c\u9700\u8981\u9760\u5404\u79cd\u8bed\u6cd5\u89c4\u5219\u4e0a\u5f00\u6d1e\u624d\u80fd\u5f25\u8865\u8bbe\u8ba1\u65f6\u8003\u8651\u4e0d\u5468\u7684\u7f3a\u9677\uff0c\u5c31\u4e3a\u4e86\u4f3a\u5019\u8fd9\u5e2e\u5f15\u7528\u90fd\u5f04\u4e0d\u660e\u767d\u7684\u5783\u573e\u5c0f\u767d\u3002 \u6211\u662f\u8bf4\u5783\u573e\u56de\u6536 (garbage-collect) \u8bed\u8a00\uff0c\u7b80\u79f0\u5783\u573e\u8bed\u8a00\u3002\u6ca1\u6709\u8bf4 Java \u5783\u573e\u7684\u610f\u601d\uff0c\u4f60\u8bf4\u5bf9\u5427\uff1f\u5783\u573e\u8bed\u8a00\u3002","title":"Java String"},{"location":"unicode/#cow","text":"\u62c5\u5fe7\uff1a\u90a3\u5c82\u4e0d\u662f\u6bcf\u6b21\u6211 += \u5b9e\u9645\u4e0a\u90fd\u767d\u767d\u6df1\u62f7\u8d1d\u4e86\u4e00\u4efd\u65b0\u7684 String \uff1f\u522b\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5177\u4f53\u5b9e\u73b0\u4e0a\uff0cJava \u7684 String \u5728\u5e95\u5c42\u91c7\u7528\u4e86\u548c Qt \u7684 QString \u4e00\u6837\u7684 COW \u673a\u5236\uff1a \u5f53\u4e00\u4e2a QString \u88ab\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5bf9\u5176\u4e2d\u7684 QByteArray \u8fdb\u884c\u6df1\u62f7\u8d1d\uff0c\u800c\u662f\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u3002\u53ea\u6709\u5f53\u5176\u4e2d\u4e00\u4e2a QString \u88ab += \u7b49\u5e26\u6709\u526f\u4f5c\u7528\u64cd\u4f5c\u4fee\u6539\u65f6\uff0c\u624d\u4f1a\u6df1\u62f7\u8d1d\u4e00\u4efd\u65b0\u7684\uff0c\u8ba9\u4f60\u4fee\u6539\u3002\u8fd9\u6837\u5927\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f00\u9500\u3002 COW \u5b57\u7b26\u4e32\u7684\u7f3a\u70b9\u662f\uff1a\u5f53\u4f60\u5199\u591a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u672c\u6765\u591a\u7ebf\u7a0b\u53ea\u8bfb\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5b89\u5168\u7684\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u6709 COW\uff0c\u8fde\u53ea\u8bfb\u8bbf\u95ee\u90fd\u4f1a\u4e0d\u5b89\u5168\u4e86\u3002\u4e4b\u540e\u6211\u4eec\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4f1a\u8be6\u7ec6\u5206\u6790\u8fd9\u662f\u4e3a\u4ec0\u4e48\u3002 \u5176\u5b9e\u65e9\u671f\u7684 C++98 \u4e2d\uff0c std::string \u4e5f\u91c7\u7528\u4e86 COW \u673a\u5236\uff0c\u4f46\u540e\u6765\u56e0\u4e3a\u4e0d\u7b26\u5408\u7ebf\u7a0b\u5b89\u5168\u8981\u6c42\uff0c\u88ab\u8ffd\u6c42\u591a\u7ebf\u7a0b\u7684 C++11 \u8d23\u4ee4\u6539\u6b63\uff0c\u624d\u6709\u4e86\u540e\u6765 std::__cxx11::basic_string \u7684 ABI \u4e0d\u517c\u5bb9\u95ee\u9898\u3002\u6bd5\u7adf\u7ebf\u7a0b\u4e0d\u5b89\u5168\u5b9e\u5728\u592a\u4f24\u4e86\uff0c\u57fa\u672c\u610f\u5473\u7740\u591a\u7ebf\u7a0b\u5c31\u6ca1\u6cd5\u5171\u4eab std::string \u3002\u4e8b\u5b9e\u4e0a\uff0cQt \u6240\u6709\u7684\u5bf9\u8c61 QObject \uff0c\u5305\u62ec QString \u5728\u591a\u7ebf\u7a0b\u4e2d\u4f20\u9012\u65f6\uff0c\u5c31\u9700\u8981\u8c03\u7528 moveTo(QThread) \u8f6c\u79fb\u6240\u6709\u6743\uff0c\u624d\u80fd\u5b89\u5168\u5730\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5c31\u662f\u56e0\u4e3a Qt \u5927\u91cf\u4f7f\u7528\u4e86 COW \u673a\u5236\u3002","title":"COW \u5b57\u7b26\u4e32"},{"location":"unicode/#unicode_1","text":"","title":"Unicode \u77e5\u8bc6\u8fdb\u9636"},{"location":"unicode/#_23","text":"","title":"\u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97"},{"location":"unicode/#grapheme","text":"TODO","title":"Grapheme"},{"location":"unicode/#_24","text":"TODO","title":"\u6b63\u89c4\u5316"},{"location":"unicode/#_25","text":"TODO","title":"\u96f6\u5bbd\u7a7a\u683c"},{"location":"unicode/#_26","text":"TODO","title":"\u7279\u6b8a\u63a7\u5236\u5b57\u7b26"},{"location":"unicode/#unicode_2","text":"TODO","title":"\u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26"},{"location":"unicode/#_27","text":"","title":"\u9ed1\u6697\u5c0f\u6280\u5de7"},{"location":"unicode/#latin-1_1","text":"Latin-1 \u662f\u4e00\u4e2a 8 \u4f4d\u7f16\u7801\uff0c\u80fd\u8868\u793a 256 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u62c9\u4e01\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u3001\u5e38\u7528\u7684\u897f\u6b27\u5b57\u7b26\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u628a\u4e00\u4e2a Latin-1 \u7f16\u7801\u7684 char \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a wchar_t \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u63a5\u53e3\u6784\u9020 std::wstring \uff0c\u8fd9\u6837 char \u4f1a\u88ab\u9010\u4e2a\u8f6c\u6362\u4e3a wchar_t \u3002 std::string latin1 = \"I love P\\xE9ng\"; // 0xE9: \u00e9 std::wstring wstr(latin1.begin(), latin1.end()); std::wcout << wstr << '\\n'; \u8f93\u51fa\uff1a I love P\u00e9ng \u5e76\u4e0d\u6807\u51c6\u7684\u505a\u6cd5\uff0c\u8fd8\u662f\u5efa\u8bae\u7528 boost::locale::conv::to_utf(latin1, \"Latin-1\") \u3002","title":"Latin-1 \u7684\u8f6c\u6362"},{"location":"unicode/#latin-1_2","text":"\u7531\u4e8e Latin-1 \u8986\u76d6\u4e86\u6240\u6709\u7684 256 \u4e2a char \u7684\u53ef\u80fd\u503c\uff0c\u4efb\u4f55\u5b57\u8282\u6d41\u90fd\u53ef\u4ee5\u6210\u529f\u89e3\u7801\u3002 GBK \u548c UTF-8 \u6709\u81ea\u7ea0\u9519\u6027\uff0c\u6709\u4e9b\u8f93\u5165\u4f1a\u88ab\u584c\u7f29\u6210\u9519\u8bef\u201c\ufffd\u201d\u3002Latin-1 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u7167\u5355\u5168\u6536\uff01 \u56e0\u6b64\u6709\u65f6\uff0c\u4eba\u4eec\u53ef\u4ee5\u6b3a\u9a97\u4e00\u4e2a\u7f16\u7801\u5668\u8bf4\u201c\u6211\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u662f Latin-1\u201d\uff01\u8fd9\u6837\u7f16\u7801\u5668\u5c31\u4e0d\u4f1a\u5bf9\u8f93\u5165\u7684\u5b57\u8282\u6d41\u505a\u4efb\u4f55\u8f6c\u6362\uff0c\u4ece\u800c\u53ef\u4ee5\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u5f53\u6587\u672c\u6765\u4f20\uff0c\u89e3\u7801\u65f6\u4e5f\u6307\u5b9a Latin-1\uff0c\u539f\u539f\u672c\u672c\u7684\u53d6\u51fa\u6570\u636e\u3002","title":"Latin-1 \u7684\u5999\u7528"},{"location":"unicode/#base64","text":"\u5982\u679c\u8981\u628a\u4e00\u4e32\u4e2d\u6587\u8f93\u5165\u4e00\u4e2a\u4e0d\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\uff0c\u53d1\u9001\u8ba9\u5bf9\u65b9\u6536\u5230\uff0c\u600e\u4e48\u529e\uff1f \u53ef\u4ee5\u7528 Latin-1 \u7f16\u7801\uff0c\u9a97\u8fd9\u4e2a\u8f6f\u4ef6\uff0c\u8ba9\u4ed6\u4ee5\u4e3a\u81ea\u5df1\u6536\u5230\u7684\u662f Latin-1 \u5b57\u7b26\u4e32\uff0c\u53cd\u6b63\u4ed6\u4e5f\u4e0d\u770b\u5185\u5bb9\uff0c\u4ece\u800c\u8ba9\u4ed6\u4e0d\u8981\u505a\u4efb\u4f55\u8f6c\u6362\u64cd\u4f5c\u3002 \u4e0d\u8fc7\u6709\u65f6\u5019\uff0c\u6587\u672c\u6846\u65e0\u6cd5\u8f93\u5165\u90e8\u5206\u7279\u6b8a\u7684\u63a7\u5236\u5b57\u7b26\uff0c\u800c UTF-8 \u5b57\u7b26\u4e32\u7f16\u7801\u51fa\u6765\u7684\u6587\u672c\uff0c\u8d85\u8fc7 0x80 \u7684\u90e8\u5206\uff0c\u53ef\u80fd\u843d\u5165 Latin-1 \u7684\u63a7\u5236\u5b57\u7b26\u4e2d\uff0c\u88ab\u8fd9\u4e2a\u8f6f\u4ef6\u9519\u8bef\u5730\u505a\u4e86\u7279\u6b8a\u5904\u7406\u3002 \u4e3a\u4e86\u907f\u514d\u53ea\u517c\u5bb9\u4e86 ASCII \u7684\u843d\u540e\u8f6f\u4ef6\u7834\u574f\u6211\u4eec\u7684\u5b57\u7b26\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u53ea\u652f\u6301 ASCII \u6587\u672c\u7684\u7f16\u8f91\u6846\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Base64 \u7f16\u7801\u5148\u628a\u4efb\u610f\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u3002 Base64 \u662f\u4e00\u79cd\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\uff0c\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u6bcf 6 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u8f6c\u6362\u4e3a\u4e00\u4e2a\u53ef\u6253\u5370\u7684 ASCII \u5b57\u7b26\uff08\u7528 A-Z a-z 0-9 - / \u8fd9 64 \u4e2a\u5b57\u7b26\u8868\u793a\uff09\u3002\u56e0\u6b64\uff0cBase64 \u7f16\u7801\u540e\u7684\u6587\u672c\uff0c\u6bcf 4 \u4e2a\u5b57\u7b26\u5c31\u6709 3 \u4e2a\u662f\u6709\u6548\u5b57\u7b26\uff0c\u5269\u4e0b\u7684 1 \u4e2a\u5b57\u7b26\u662f\u586b\u5145\u5b57\u7b26 = \u3002 \u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"\u5c0f\u5f6d\u8001\u5e08\" \uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u628a\u5b83\u901a\u8fc7\u90ae\u4ef6\u53d1\u51fa\u53bb\u3002\u800c\u8fd9\u4e2a\u90ae\u4ef6\u670d\u52a1\u5668\u4e0d\u652f\u6301 UTF-8 \u4e5f\u4e0d\u652f\u6301 GBK\uff0c\u53ea\u652f\u6301 ASCII\uff01 \u9996\u5148\u6211\u4eec\u7528 UTF-8 \u7f16\u7801\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff1a 0xE5 0xB0 0x8F 0xE5 0xBD 0xAD 0xE8 0x80 0x81 0xE5 0xB8 0x88 \u7136\u540e\u518d\u7528 Base64 \u4e8c\u6b21\u7f16\u7801\u6210\u666e\u901a\u7684\u53ef\u6253\u5370\u5b57\u6bcd\u548c\u6570\u5b57\u5e8f\u5217\uff1a 5bCP5b2t6ICB5biI \u5bf9\u65b9\u6536\u5230\u8fd9\u4e32\u795e\u79d8\u5b57\u7b26\u540e\uff0c\u518d\u7528 base64 \u89e3\u7801\uff0c\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff0c\u518d\u7528\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\u89e3\u7801\uff0c\u5c31\u80fd\u770b\u5230\u672c\u6765\u7684\u4e2d\u6587\u4e86\u3002 # \u53d1\u9001\u8005\uff1a import base64 secret = base64.b64encode(\"\u5c0f\u5f6d\u8001\u5e08\".encode()) # \u63a5\u6536\u8005\uff1a base64.b64decode(secret).decode() \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u7801 UTF-8 \u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f20\u8f93\u4efb\u610f\u975e\u6587\u672c\u7684\u6587\u4ef6\uff01\u4f8b\u5982\uff0c\u6709\u4eba\u5229\u7528 Base64 \u7f16\u7801\uff0c\u628a jpg \u56fe\u50cf\u6587\u4ef6\u76f4\u63a5\u5185\u5d4c\u5728 md \u6587\u4ef6\u91cc\uff01\uff08md \u6587\u4ef6\u53ea\u652f\u6301\u5305\u542b\u5408\u6cd5\u7684 UTF-8 \u6587\u672c\uff0c\u4e0d\u53ef\u80fd\u5305\u542b jpg \u7684\u4efb\u610f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u53ea\u80fd\u7528 Base64 \u5148\u7f16\u7801\u6210 ASCII \u8303\u56f4\u5185\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u9632\u6b62 md \u7f16\u8bd1\u5668\u62a5 UTF-8 \u89e3\u7801\u9519\u8bef\uff09 \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u8f93\u5165\u4e2d\u6587\u5b9e\u5728\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5148 Base64 \u8f6c\u6362\u6210\u7eaf\u82f1\u6587\u8bd5\u8bd5\u770b\uff0c\u53cd\u6b63\u65e0\u8bba\u8c01\u90fd\u517c\u5bb9 ASCII\u3002\u5982\u679c\u8fd9\u4e2a\u6587\u672c\u6846\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u8fd8\u53ef\u4ee5\u8bd5\u8bd5\u770b\u53ea\u6709 A-Z 0-9 \u7684 Base32 \u7f16\u7801\u3002","title":"Base64 \u9632\u4e71\u7801"},{"location":"vartypes/","text":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) \u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) TODO","title":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5)"},{"location":"vartypes/#_1","text":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) TODO","title":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5)"}]} \ No newline at end of file +{"config":{"indexing":"full","lang":["en"],"min_search_length":3,"prebuild_index":false,"separator":"[\\s\\-]+"},"docs":[{"location":"","text":"\u5c0f\u5f6d\u8001\u5e08\u73b0\u4ee3 C++ \u5927\u5178 \u5c0f\u5f6d\u5927\u5178\u662f\u4e00\u672c\u5173\u4e8e\u73b0\u4ee3 C++ \u7f16\u7a0b\u7684\u6743\u5a01\u6307\u5357\uff0c\u5b83\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u77e5\u8bc6\u5230\u9ad8\u7ea7\u6280\u5de7\u7684\u5185\u5bb9\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u9605\u8bfb\u3002\u672c\u4e66\u7531\u5c0f\u5f6d\u8001\u5e08\u4eb2\u81ea\u7f16\u5199\uff0c\u901a\u8fc7\u7b80\u5355\u6613\u61c2\u7684\u8bed\u8a00\u548c\u4e30\u5bcc\u7684\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 C++ \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u5b66\u4f1a\u5982\u4f55\u8fd0\u7528\u5b83\u4eec\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002 \u6562\u627f\u8bfa\uff1a\u571f\u6728\u8001\u54e5\u4e5f\u80fd\u770b\u61c2\uff01 \u524d\u8a00 \u63a8\u8350\u7528\u624b\u673a\u6216\u5e73\u677f \u7ad6\u5c4f \u89c2\u770b\uff0c\u53ef\u4ee5\u5728\u5e8a\u6216\u6c99\u53d1\u4e0a\u8eba\u7740\u3002 \u7528\u7535\u8111\u770b\u7684\u8bdd\uff0c\u53ef\u4ee5\u6309 WIN + \u2190 \uff0c\u628a\u672c\u4e66\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u653e\u5728\u5c4f\u5e55\u5de6\u4fa7\uff0c\u53f3\u4fa7\u662f\u4f60\u7684 IDE\u3002\u4e00\u8fb9\u770b\u4e00\u8fb9\u81ea\u5df1\u52a8\u624b\u505a\u5b9e\u9a8c\u3002 \u8bf7\u5750\u548c\u653e\u5bbd\u3002 \u53ef\u4ee5\u6309\u987a\u5e8f\u9605\u8bfb\uff0c\u4e5f\u53ef\u4ee5\u5728\u672c\u9875\u9762\u4e0a\u65b9\u5bfc\u822a\u680f\u7684\u201c\u7ae0\u8282\u5217\u8868\u201d\u4e2d\uff0c\u9009\u62e9\u611f\u5174\u8da3\u7684\u7ae0\u8282\u9605\u8bfb\u3002 \u672c\u4e66\u5b8c\u5168\u5f00\u6e90\u548c\u514d\u8d39\uff0cGitHub \u4ed3\u5e93\uff1a https://github.com/parallel101/cppguidebook \u5982\u679c\u4f60\u662f\u5728\u4ed8\u8d39\u7fa4\u4e2d\u201c\u4e70\u201d\u5230\u672c\u4e66\uff0c\u6216\u8005\u6253\u7740\u5c0f\u5f6d\u8001\u5e08\u540d\u53f7\u5356\u8bfe\uff0c\u8bf4\u660e\u4f60\u53ef\u80fd\u662f\u79c1\u6709\u5236\u7684\u53d7\u5bb3\u8005\u3002\u56e0\u4e3a\u5c0f\u5f6d\u8001\u5e08\u4ece\u6765\u6ca1\u6709\u4ed8\u8d39\u624d\u80fd\u770b\u7684\u8bfe\u7a0b\uff0c\u6240\u6709\u5c0f\u5f6d\u8001\u5e08\u8bfe\u7a0b\u90fd\u5bf9\u5168\u7403\u4e92\u8054\u7f51\u5f00\u653e\u3002 \u5982\u9700\u79bb\u7ebf\u67e5\u770b\uff0c\u53ef\u4ee5\u524d\u5f80 GitHub Release \u9875\u9762 \u4e0b\u8f7d PDF \u6587\u4ef6\u3002 \u5982\u679c\u4f60\u5728\u9605\u8bfb\u8fc7\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u53ef\u4ee5\u5728 GitHub Issues \u4e2d\u63d0\u51fa\uff0c\u5c0f\u5f6d\u8001\u5e08\u4f1a\u5c3d\u529b\u89e3\u7b54\u3002 \u4e5f\u53ef\u4ee5\u5728 B \u7ad9 \u53d1\u79c1\u4fe1\u7ed9\u5c0f\u5f6d\u8001\u5e08\u54e6\u3002 \u672c\u4e66\u8fd8\u5728\u6301\u7eed\u66f4\u65b0\u4e2d\u2026\u2026\u8981\u8ffd\u756a\u7684\u8bdd\uff0c\u53ef\u4ee5\u5728 GitHub \u70b9\u4e00\u4e0b\u53f3\u4e0a\u89d2\u7684 \u201cWatch\u201d \u6309\u94ae\uff0c\u6bcf\u5f53\u5c0f\u5f6d\u8001\u5e08\u63d0\u4ea4\u65b0 commit\uff0cGitHub \u4f1a\u5411\u4f60\u53d1\u9001\u4e00\u5cf0\u7535\u5b50\u90ae\u4ef6\uff0c\u63d0\u9192\u4f60\u5c0f\u5f6d\u8001\u5e08\u66f4\u65b0\u4e86\u3002 \u66f4\u65b0\u65f6\u95f4\uff1ahttps://github.com/parallel101/cppguidebook \u683c\u5f0f\u7ea6\u5b9a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u6e29\u99a8\u63d0\u793a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u53ef\u80fd\u72af\u9519\u7684\u8b66\u544a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u7b11\u8bdd\u6216\u8da3\u5473\u5bd3\u8a00\u6545\u4e8b \u7528\u8fd9\u79cd\u989c\u8272\u4e66\u5199\u7684\u662f\u8865\u5145\u8bf4\u660e\u7684\u8bfe\u5916\u9605\u8bfb\uff0c\u770b\u4e0d\u61c2\u4e5f\u6ca1\u5173\u7cfb \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u662f\u521d\u5b66\u8005\u53ef\u6682\u65f6\u4e0d\u7528\u7406\u89e3\u7684\u7ec6\u8282 \u672f\u8bed\u540d\u79f0: \u8fd9\u91cc\u662f\u672f\u8bed\u7684\u5b9a\u4e49\u3002 \u89c2\u524d\u987b\u77e5 \u4e0e\u5927\u591a\u6570\u73b0\u6709\u6559\u6750\u4e0d\u540c\u7684\u662f\uff0c\u672c\u8bfe\u7a0b\u5c06\u4f1a\u91c7\u7528\u201c\u5012\u53d9\u201d\u7684\u5f62\u5f0f\uff0c\u4ece\u6700\u65b0\u7684 C++23 \u8bb2\u8d77\uff01\u7136\u540e\u8bb2 C++20\u3001C++17\u3001C++14\u3001C++11\uff0c\u6162\u6162\u8bb2\u5230\u6700\u539f\u59cb\u7684 C++98\u3002 \u4e0d\u7528\u62c5\u5fc3\uff0c\u8d8a\u662f\u73b0\u4ee3\u7684 C++\uff0c\u5b66\u8d77\u6765\u53cd\u800c\u66f4\u5bb9\u6613\uff01\u53cd\u800c\u53e4\u4ee3 C++ \u624d \u53c8\u81ed\u53c8\u957f \u3002 \u5f88\u591a\u540c\u5b66\u60f3\u5f53\u7136\u5730\u8bef\u4ee5\u4e3a C++98 \u6700\u7b80\u5355\uff0c\u54fc\u54e7\u54fc\u54e7\u8d39\u8001\u5927\u52b2\u4ece C++98 \u5f00\u59cb\u5b66\uff0c\u624d\u662f\u9519\u8bef\u7684\u3002 \u4e3a\u4e86\u5e94\u4ed8\u7f3a\u80f3\u818a\u5c11\u817f\u7684 C++98\uff0c\u4eba\u4eec\u53d1\u660e\u4e86\u5404\u79cd \u7e41\u7410\u65e0\u8c13 \u7684\u5199\u6cd5\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u65e9\u5c31\u5df2\u7ecf\u88ab\u66f4 \u7b80\u6d01\u76f4\u89c2 \u7684\u5199\u6cd5\u66ff\u4ee3\u4e86\u3002 \u4f8b\u5982\u6240\u8c13\u7684 safe-bool idiom\uff0c\u5199\u8d77\u6765\u53c8\u81ed\u53c8\u957f\uff0cC++11 \u5f15\u5165\u4e00\u4e2a explicit \u5173\u952e\u5b57\u76f4\u63a5\u5c31\u79d2\u4e86\u3002\u7ed3\u679c\u8fd8\u6709\u4e00\u6279\u52b3\u4fdd\u6559\u6750\u5927\u5439\u7279\u5439 safe-bool idiom\uff0c\u5439\u5f97\u597d\u50cf\u662f\u4e2a\u4ec0\u4e48\u9ad8\u5927\u4e0a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u6837\uff0c\u4e0d\u8fc7\u662f\u4e2a\u5e94\u4ed8 C++98 \u8bed\u8a00\u7f3a\u9677\u7684\u8e69\u811a\u73a9\u610f\u3002 \u5c31\u597d\u6bd4\u4e00\u4e2a \u8001\u5916 \u60f3\u8981\u5b66\u4e60\u6c49\u8bed\uff0c\u4ed6\u9996\u5148\u80af\u5b9a\u662f\u4ece \u73b0\u4ee3\u6c49\u8bed \u5b66\u8d77\uff01\u800c\u4e0d\u662f\u4e0a\u6765\u5c31\u6559\u4ed6 \u6587\u8a00\u6587 \u3002 \u5373\u4f7f\u8fd9\u4e2a\u8001\u5916\u7684\u804c\u4e1a\u5c31\u662f\u201c\u8003\u53e4\u201d\uff0c\u6216\u8005\u4ed6\u5bf9\u201c\u53e4\u4ee3\u6587\u5b66\u201d\u611f\u5174\u8da3\uff0c\u4e5f\u4e0d\u53ef\u80fd\u81ea\u5b66\u6587\u8a00\u6587\u7684\u540c\u65f6\u5b8c\u5168\u8df3\u8fc7\u73b0\u4ee3\u6c49\u8bed\u3002 \u5f53\u6211\u4eec\u5b66\u4e60\u4e2d\u6587\u65f6\uff0c\u4f60\u80af\u5b9a\u5e0c\u671b\u5148\u5b66\u73b0\u4ee3\u6c49\u8bed\uff0c\u518d\u5b66\u6587\u8a00\u6587\uff0c\u518d\u5b66\u7532\u9aa8\u6587\uff0c\u518d\u5b66 brainf* * k\uff0c\u800c\u4e0d\u662f\u53cd\u8fc7\u6765\u3002 \u5bf9\u4e8e C++ \u521d\u5b66\u8005\u4e5f\u662f\u5982\u6b64\uff1a\u6211\u4eec\u9996\u5148\u5b66\u4f1a\u7b80\u5355\u660e\u4e86\u7684\uff0c\u7b26\u5408\u73b0\u4ee3\u4eba\u601d\u7ef4\u7684 C++23\uff0c\u518d\u9010\u6e10\u56de\u5230\u4e13\u4e3a\u4f3a\u5019\u201c\u53e4\u4ee3\u5f00\u53d1\u73af\u5883\u201d\u7684 C++98\u3002 \u4f60\u7684\u751f\u4ea7\u73af\u5883\u53ef\u80fd\u4e0d\u5141\u8bb8\u7528\u4e0a C++20 \u751a\u81f3 C++23 \u7684\u65b0\u6807\u51c6\u3002 \u522b\u62c5\u5fc3\uff0c\u5c0f\u5f6d\u8001\u5e08\u6559\u4f1a\u4f60 C++23 \u7684\u6b63\u5e38\u5199\u6cd5\u540e\uff0c\u4f1a\u8bb2\u89e3\u5982\u4f55\u5728 C++14\u3001C++98 \u4e2d\u5199\u51fa\u540c\u6837\u7684\u6548\u679c\u3002 \u8fd9\u6837\u4f60\u5b66\u4e60\u7684\u65f6\u5019\u601d\u8def\u6e05\u6670\uff0c\u4e0d\u7528\u88ab\u7e41\u7410\u7684 C++98 \u201c\u5947\u6280\u6deb\u5de7\u201d\u5e72\u6270\uff0c\u5b66\u8d77\u6765\u4e8b\u534a\u529f\u500d\uff1b\u4f46\u4e5f\u201c\u5403\u8fc7\u89c1\u8fc7\u201d\uff0c\u77e5\u9053\u53e4\u4ee3 C++98 \u7684\u5e94\u5bf9\u7b56\u7565\u3002 \u76ee\u524d\u4f01\u4e1a\u91cc\u4e3b\u6d41\u4f7f\u7528\u7684\u662f C++14 \u548c C++17\u3002\u4f8b\u5982\u8c37\u6b4c\u5c31\u660e\u786e\u89c4\u5b9a\u8981\u6c42 C++17\u3002 \u4e3e\u4e2a\u4f8b\u5b50 \u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4f60\u53ef\u80fd\u770b\u4e0d\u61c2\uff0c\u4f46\u53ea\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5411\u4f60\u8bf4\u660e\uff1a\u8d8a\u662f\u65b0\u7684 C++ \u6807\u51c6\uff0c\u53cd\u800c\u8d8a\u5bb9\u6613\u5b66\uff01 \u4f8b\u5982\uff0c\u5728\u6a21\u677f\u5143\u7f16\u7a0b\u4e2d\uff0c\u8981\u68c0\u6d4b\u4e00\u4e2a\u7c7b\u578b T \u662f\u5426\u62e5\u6709 foo() \u8fd9\u4e00\u6210\u5458\u51fd\u6570\u3002\u5982\u679c\u5b58\u5728\uff0c\u624d\u4f1a\u8c03\u7528\u3002 \u5728 C++20 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f88\u65b9\u4fbf\u7684 requires \u8bed\u6cd5\uff0c\u8f7b\u677e\u68c0\u6d4b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u80fd\u5408\u6cd5\u901a\u8fc7\u7f16\u8bd1\u3002\u5982\u679c\u80fd\uff0c requires \u8bed\u53e5\u4f1a\u8fd4\u56de true \u3002\u7136\u540e\u7528\u4e00\u4e2a if constexpr \u8fdb\u884c\u7f16\u8bd1\u671f\u5206\u652f\u5224\u65ad\uff0c\u5373\u53ef\u5b9e\u73b0\u68c0\u6d4b\u5230\u5b58\u5728\u5219\u8c03\u7528\u3002 template void try_call_foo(T &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } \u4f46\u4ec5\u4ec5\u662f\u56de\u5230 C++17\uff0c\u6ca1\u6709 requires \u8bed\u6cd5\uff0c\u6211\u4eec\u53ea\u80fd\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a trait \u7c7b\uff0c\u5e76\u8fd0\u7528\u70e6\u4eba\u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u68c0\u6d4b\u8868\u8fbe\u5f0f\u662f\u5426\u7684\u5408\u6cd5\uff0c\u53c8\u81ed\u53c8\u957f\u3002 template struct has_foo { inline constexpr bool value = false; }; template struct has_foo().foo())>> { inline constexpr bool value = true; }; template void try_call_foo(T &t) { if constexpr (has_foo::value) { t.foo(); } } \u5982\u679c\u56de\u5230 C++14\uff0c\u60c5\u51b5\u5c31\u66f4\u7cdf\u7cd5\u4e86\uff01 if constexpr \u662f C++17 \u7684\u7279\u6027\uff0c\u6ca1\u6709\u4ed6\uff0c\u8981\u5b9e\u73b0\u7f16\u8bd1\u671f\u5206\u652f\uff0c\u6211\u4eec\u5c31\u5f97\u7528 enable_if_t \u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a try_call_foo \u51fd\u6570\uff0c\u4e92\u76f8\u91cd\u8f7d\uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo())>> { static constexpr bool value = true; }; template ::value, int> = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int> = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++11\uff0c\u60c5\u51b5\u8fdb\u4e00\u6b65\u6076\u5316\uff01 enable_if_t \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5c0f\u52a9\u624b\u5df2\u7ecf\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u4f7f\u7528\u6bd4\u4ed6\u66f4\u5e95\u5c42\u7684 enable_if \u6a21\u677f\u7c7b\uff0c\u624b\u52a8\u53d6\u51fa ::type \uff0c\u5e76\u4e14\u9700\u8981 typename \u4fee\u9970\uff0c\u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff01\u5e76\u4e14 void_t \u4e5f\u4e0d\u80fd\u7528\u4e86\uff0c\u8981\u7528\u9017\u53f7\u8868\u8fbe\u5f0f\u5c0f\u6280\u5de7\u624d\u80fd\u8ba9 decltype \u56fa\u5b9a\u8fd4\u56de void\u2026\u2026 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo(), (void)0)> { static constexpr bool value = true; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++98\uff0c\u90a3\u53c8\u8981\u7f6a\u52a0\u4e00\u7b49\uff01 enable_if \u548c \u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u7684\u5e2e\u624b\u7c7b\uff0c\u5728 C++98 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9e\u73b0 enable_if \u2026\u2026 declval \u4e5f\u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u4e2d\u7684\u5e2e\u624b\u51fd\u6570\u2026\u2026\u5047\u8bbe\u4f60\u81ea\u5df1\u597d\u4e0d\u5bb9\u6613\u5b9e\u73b0\u51fa\u6765\u4e86 enable_if \u548c declval \uff0c\u8fd8\u6ca1\u5b8c\uff1a\u56e0\u4e3a constexpr \u5728 C++98 \u4e2d\u4e5f\u4e0d\u5b58\u5728\u4e86\uff01\u4f60\u65e0\u6cd5\u5b9a\u4e49 value \u6210\u5458\u53d8\u91cf\u4e3a\u7f16\u8bd1\u671f\u5e38\u91cf\uff0c\u6211\u4eec\u53ea\u597d\u53c8\u7528\u4e00\u4e2a\u62bd\u8c61\u7684\u679a\u4e3e\u5c0f\u6280\u5de7\u6765\u5b9e\u73b0\u5b9a\u4e49\u7c7b\u6210\u5458\u5e38\u91cf\u7684\u6548\u679c\u3002 template struct has_foo { enum { value = 0 }; }; template struct has_foo().foo(), (void)0)> { enum { value = 1 }; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u6b64\u5197\u957f\u96be\u61c2\u7684\u62bd\u8c61 C++98 \u4ee3\u7801\uff0c\u4eff\u4f5b\u662f\u201c\u52a0\u5bc6\u201d\u8fc7\u7684\u4ee3\u7801\u4e00\u6837\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u5b9e\u73b0\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u6210\u5458\u51fd\u6570 foo\u2026\u2026 \u5982\u679c\u56de\u5230 C \u8bed\u8a00\uff0c\u90a3\u4e48\u4f60\u751a\u81f3\u90fd\u4e0d\u7528\u68c0\u6d4b\u4e86\u3002\u56e0\u4e3a\u4f1f\u5927\u7684 C \u8bed\u8a00\u8fde\u6210\u5458\u51fd\u6570\u90fd\u6ca1\u6709\uff0c\u4f55\u8c08\u201c\u68c0\u6d4b\u6210\u5458\u51fd\u6570\u662f\u5426\u5b58\u5728\u201d\uff1f \u53cd\u89c2 C++20 \u7684\u5199\u6cd5\uff0c\u4e00\u773c\u5c31\u770b\u660e\u767d\u4ee3\u7801\u7684\u903b\u8f91\u662f\u4ec0\u4e48\uff0c\u8868\u8fbe\u4f60\u8be5\u8868\u8fbe\u7684\uff0c\u800c\u4e0d\u662f\u8ff7\u5931\u4e8e\u4f3a\u5019\u5404\u79cd\u8bed\u8a00\u7f3a\u9677\uff0c\u5e72\u6270\u6211\u4eec\u5b66\u4e60\u3002 void try_call_foo(auto &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } // \u4ece\u6b8b\u5e9f\u7684 C++98 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u5c31\u88ab\u8fd9\u4e9b\u65e0\u8c13\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u626d\u66f2\u4e86\uff0c\u800c\u4f7f\u5f97\u771f\u6b63\u5e94\u8be5\u8868\u8fbe\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u6df9\u6ca1\u5728\u53c8\u81ed\u53c8\u957f\u7684\u53e4\u4ee3\u6280\u5de7\u4e2d\u3002 // \u4ece\u73b0\u4ee3\u7684 C++23 \u5b66\u8d77\uff0c\u5148\u77e5\u9053\u6b63\u5e38\u7684\u5199\u6cd5\u201c\u7406\u5e94\u201d\u662f\u4ec0\u4e48\u6837\u3002\u5de5\u4f5c\u4e2d\u7528\u4e0d\u4e0a C++23\uff1f\u6211\u4f1a\u5411\u4f60\u4ecb\u7ecd\uff0c\u5982\u679c\u8981\u5012\u9000\u56de C++14\uff0c\u53e4\u4ee3\u4eba\u90fd\u662f\u7528\u4ec0\u4e48\u201c\u5947\u6280\u6deb\u5de7\u201d\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 // \u8fd9\u6837\u4f60\u6700\u540e\u540c\u6837\u53ef\u4ee5\u9002\u5e94\u516c\u53f8\u8981\u6c42\u7684 C++14 \u73af\u5883\u3002\u4f46\u662f\u4ece C++23 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u53c8\u4e0d\u4f1a\u88ab\u5e94\u4ed8\u53e4\u4ee3\u8bed\u8a00\u7f3a\u9677\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u6270\u4e71\uff0c\u5b66\u8d77\u6765\u5c31\u4e8b\u534a\u529f\u500d\u3002 \u65e2\u7136\u73b0\u4ee3 C++ \u8fd9\u4e48\u597d\uff0c\u4e3a\u4ec0\u4e48\u5b66\u6821\u4e0d\u4ece\u73b0\u4ee3 C++ \u6559\u8d77\uff0c\u6559\u8d77\u6765\u8fd8\u8f7b\u677e\uff1f\u56e0\u4e3a\u52b3\u4fdd\u8001\u5e08\u4fdd\uff0c\u61d2\u5f97\u63a5\u89e6\u65b0\u77e5\u8bc6\uff0c\u8ba4\u4e3a\u201c\u7956\u5b97\u4e4b\u6cd5\u4e0d\u53ef\u53d8\u201d\uff0c\u201c\u7248\u53f7\u7a33\u5b9a\u538b\u5012\u4e00\u5207\u201d\u3002","title":"\u524d\u8a00"},{"location":"#c","text":"\u5c0f\u5f6d\u5927\u5178\u662f\u4e00\u672c\u5173\u4e8e\u73b0\u4ee3 C++ \u7f16\u7a0b\u7684\u6743\u5a01\u6307\u5357\uff0c\u5b83\u6db5\u76d6\u4e86\u4ece\u57fa\u7840\u77e5\u8bc6\u5230\u9ad8\u7ea7\u6280\u5de7\u7684\u5185\u5bb9\uff0c\u9002\u5408\u521d\u5b66\u8005\u548c\u6709\u7ecf\u9a8c\u7684\u7a0b\u5e8f\u5458\u9605\u8bfb\u3002\u672c\u4e66\u7531\u5c0f\u5f6d\u8001\u5e08\u4eb2\u81ea\u7f16\u5199\uff0c\u901a\u8fc7\u7b80\u5355\u6613\u61c2\u7684\u8bed\u8a00\u548c\u4e30\u5bcc\u7684\u793a\u4f8b\uff0c\u5e2e\u52a9\u8bfb\u8005\u5feb\u901f\u638c\u63e1 C++ \u7684\u6838\u5fc3\u6982\u5ff5\uff0c\u5e76\u5b66\u4f1a\u5982\u4f55\u8fd0\u7528\u5b83\u4eec\u6765\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\u3002 \u6562\u627f\u8bfa\uff1a\u571f\u6728\u8001\u54e5\u4e5f\u80fd\u770b\u61c2\uff01","title":"\u5c0f\u5f6d\u8001\u5e08\u73b0\u4ee3 C++ \u5927\u5178"},{"location":"#_1","text":"\u63a8\u8350\u7528\u624b\u673a\u6216\u5e73\u677f \u7ad6\u5c4f \u89c2\u770b\uff0c\u53ef\u4ee5\u5728\u5e8a\u6216\u6c99\u53d1\u4e0a\u8eba\u7740\u3002 \u7528\u7535\u8111\u770b\u7684\u8bdd\uff0c\u53ef\u4ee5\u6309 WIN + \u2190 \uff0c\u628a\u672c\u4e66\u7684\u6d4f\u89c8\u5668\u7a97\u53e3\u653e\u5728\u5c4f\u5e55\u5de6\u4fa7\uff0c\u53f3\u4fa7\u662f\u4f60\u7684 IDE\u3002\u4e00\u8fb9\u770b\u4e00\u8fb9\u81ea\u5df1\u52a8\u624b\u505a\u5b9e\u9a8c\u3002 \u8bf7\u5750\u548c\u653e\u5bbd\u3002 \u53ef\u4ee5\u6309\u987a\u5e8f\u9605\u8bfb\uff0c\u4e5f\u53ef\u4ee5\u5728\u672c\u9875\u9762\u4e0a\u65b9\u5bfc\u822a\u680f\u7684\u201c\u7ae0\u8282\u5217\u8868\u201d\u4e2d\uff0c\u9009\u62e9\u611f\u5174\u8da3\u7684\u7ae0\u8282\u9605\u8bfb\u3002 \u672c\u4e66\u5b8c\u5168\u5f00\u6e90\u548c\u514d\u8d39\uff0cGitHub \u4ed3\u5e93\uff1a https://github.com/parallel101/cppguidebook \u5982\u679c\u4f60\u662f\u5728\u4ed8\u8d39\u7fa4\u4e2d\u201c\u4e70\u201d\u5230\u672c\u4e66\uff0c\u6216\u8005\u6253\u7740\u5c0f\u5f6d\u8001\u5e08\u540d\u53f7\u5356\u8bfe\uff0c\u8bf4\u660e\u4f60\u53ef\u80fd\u662f\u79c1\u6709\u5236\u7684\u53d7\u5bb3\u8005\u3002\u56e0\u4e3a\u5c0f\u5f6d\u8001\u5e08\u4ece\u6765\u6ca1\u6709\u4ed8\u8d39\u624d\u80fd\u770b\u7684\u8bfe\u7a0b\uff0c\u6240\u6709\u5c0f\u5f6d\u8001\u5e08\u8bfe\u7a0b\u90fd\u5bf9\u5168\u7403\u4e92\u8054\u7f51\u5f00\u653e\u3002 \u5982\u9700\u79bb\u7ebf\u67e5\u770b\uff0c\u53ef\u4ee5\u524d\u5f80 GitHub Release \u9875\u9762 \u4e0b\u8f7d PDF \u6587\u4ef6\u3002 \u5982\u679c\u4f60\u5728\u9605\u8bfb\u8fc7\u7a0b\u4e2d\u9047\u5230\u4efb\u4f55\u95ee\u9898\uff0c\u53ef\u4ee5\u5728 GitHub Issues \u4e2d\u63d0\u51fa\uff0c\u5c0f\u5f6d\u8001\u5e08\u4f1a\u5c3d\u529b\u89e3\u7b54\u3002 \u4e5f\u53ef\u4ee5\u5728 B \u7ad9 \u53d1\u79c1\u4fe1\u7ed9\u5c0f\u5f6d\u8001\u5e08\u54e6\u3002 \u672c\u4e66\u8fd8\u5728\u6301\u7eed\u66f4\u65b0\u4e2d\u2026\u2026\u8981\u8ffd\u756a\u7684\u8bdd\uff0c\u53ef\u4ee5\u5728 GitHub \u70b9\u4e00\u4e0b\u53f3\u4e0a\u89d2\u7684 \u201cWatch\u201d \u6309\u94ae\uff0c\u6bcf\u5f53\u5c0f\u5f6d\u8001\u5e08\u63d0\u4ea4\u65b0 commit\uff0cGitHub \u4f1a\u5411\u4f60\u53d1\u9001\u4e00\u5cf0\u7535\u5b50\u90ae\u4ef6\uff0c\u63d0\u9192\u4f60\u5c0f\u5f6d\u8001\u5e08\u66f4\u65b0\u4e86\u3002 \u66f4\u65b0\u65f6\u95f4\uff1ahttps://github.com/parallel101/cppguidebook","title":"\u524d\u8a00"},{"location":"#_2","text":"\u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u6e29\u99a8\u63d0\u793a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u53ef\u80fd\u72af\u9519\u7684\u8b66\u544a \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u5185\u5bb9\u662f\u7b11\u8bdd\u6216\u8da3\u5473\u5bd3\u8a00\u6545\u4e8b \u7528\u8fd9\u79cd\u989c\u8272\u4e66\u5199\u7684\u662f\u8865\u5145\u8bf4\u660e\u7684\u8bfe\u5916\u9605\u8bfb\uff0c\u770b\u4e0d\u61c2\u4e5f\u6ca1\u5173\u7cfb \u7528\u8fd9\u79cd\u989c\u8272\u5b57\u4f53\u4e66\u5199\u7684\u662f\u521d\u5b66\u8005\u53ef\u6682\u65f6\u4e0d\u7528\u7406\u89e3\u7684\u7ec6\u8282 \u672f\u8bed\u540d\u79f0: \u8fd9\u91cc\u662f\u672f\u8bed\u7684\u5b9a\u4e49\u3002","title":"\u683c\u5f0f\u7ea6\u5b9a"},{"location":"#_3","text":"\u4e0e\u5927\u591a\u6570\u73b0\u6709\u6559\u6750\u4e0d\u540c\u7684\u662f\uff0c\u672c\u8bfe\u7a0b\u5c06\u4f1a\u91c7\u7528\u201c\u5012\u53d9\u201d\u7684\u5f62\u5f0f\uff0c\u4ece\u6700\u65b0\u7684 C++23 \u8bb2\u8d77\uff01\u7136\u540e\u8bb2 C++20\u3001C++17\u3001C++14\u3001C++11\uff0c\u6162\u6162\u8bb2\u5230\u6700\u539f\u59cb\u7684 C++98\u3002 \u4e0d\u7528\u62c5\u5fc3\uff0c\u8d8a\u662f\u73b0\u4ee3\u7684 C++\uff0c\u5b66\u8d77\u6765\u53cd\u800c\u66f4\u5bb9\u6613\uff01\u53cd\u800c\u53e4\u4ee3 C++ \u624d \u53c8\u81ed\u53c8\u957f \u3002 \u5f88\u591a\u540c\u5b66\u60f3\u5f53\u7136\u5730\u8bef\u4ee5\u4e3a C++98 \u6700\u7b80\u5355\uff0c\u54fc\u54e7\u54fc\u54e7\u8d39\u8001\u5927\u52b2\u4ece C++98 \u5f00\u59cb\u5b66\uff0c\u624d\u662f\u9519\u8bef\u7684\u3002 \u4e3a\u4e86\u5e94\u4ed8\u7f3a\u80f3\u818a\u5c11\u817f\u7684 C++98\uff0c\u4eba\u4eec\u53d1\u660e\u4e86\u5404\u79cd \u7e41\u7410\u65e0\u8c13 \u7684\u5199\u6cd5\uff0c\u5728\u73b0\u4ee3 C++ \u4e2d\uff0c\u65e9\u5c31\u5df2\u7ecf\u88ab\u66f4 \u7b80\u6d01\u76f4\u89c2 \u7684\u5199\u6cd5\u66ff\u4ee3\u4e86\u3002 \u4f8b\u5982\u6240\u8c13\u7684 safe-bool idiom\uff0c\u5199\u8d77\u6765\u53c8\u81ed\u53c8\u957f\uff0cC++11 \u5f15\u5165\u4e00\u4e2a explicit \u5173\u952e\u5b57\u76f4\u63a5\u5c31\u79d2\u4e86\u3002\u7ed3\u679c\u8fd8\u6709\u4e00\u6279\u52b3\u4fdd\u6559\u6750\u5927\u5439\u7279\u5439 safe-bool idiom\uff0c\u5439\u5f97\u597d\u50cf\u662f\u4e2a\u4ec0\u4e48\u9ad8\u5927\u4e0a\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e00\u6837\uff0c\u4e0d\u8fc7\u662f\u4e2a\u5e94\u4ed8 C++98 \u8bed\u8a00\u7f3a\u9677\u7684\u8e69\u811a\u73a9\u610f\u3002 \u5c31\u597d\u6bd4\u4e00\u4e2a \u8001\u5916 \u60f3\u8981\u5b66\u4e60\u6c49\u8bed\uff0c\u4ed6\u9996\u5148\u80af\u5b9a\u662f\u4ece \u73b0\u4ee3\u6c49\u8bed \u5b66\u8d77\uff01\u800c\u4e0d\u662f\u4e0a\u6765\u5c31\u6559\u4ed6 \u6587\u8a00\u6587 \u3002 \u5373\u4f7f\u8fd9\u4e2a\u8001\u5916\u7684\u804c\u4e1a\u5c31\u662f\u201c\u8003\u53e4\u201d\uff0c\u6216\u8005\u4ed6\u5bf9\u201c\u53e4\u4ee3\u6587\u5b66\u201d\u611f\u5174\u8da3\uff0c\u4e5f\u4e0d\u53ef\u80fd\u81ea\u5b66\u6587\u8a00\u6587\u7684\u540c\u65f6\u5b8c\u5168\u8df3\u8fc7\u73b0\u4ee3\u6c49\u8bed\u3002 \u5f53\u6211\u4eec\u5b66\u4e60\u4e2d\u6587\u65f6\uff0c\u4f60\u80af\u5b9a\u5e0c\u671b\u5148\u5b66\u73b0\u4ee3\u6c49\u8bed\uff0c\u518d\u5b66\u6587\u8a00\u6587\uff0c\u518d\u5b66\u7532\u9aa8\u6587\uff0c\u518d\u5b66 brainf* * k\uff0c\u800c\u4e0d\u662f\u53cd\u8fc7\u6765\u3002 \u5bf9\u4e8e C++ \u521d\u5b66\u8005\u4e5f\u662f\u5982\u6b64\uff1a\u6211\u4eec\u9996\u5148\u5b66\u4f1a\u7b80\u5355\u660e\u4e86\u7684\uff0c\u7b26\u5408\u73b0\u4ee3\u4eba\u601d\u7ef4\u7684 C++23\uff0c\u518d\u9010\u6e10\u56de\u5230\u4e13\u4e3a\u4f3a\u5019\u201c\u53e4\u4ee3\u5f00\u53d1\u73af\u5883\u201d\u7684 C++98\u3002 \u4f60\u7684\u751f\u4ea7\u73af\u5883\u53ef\u80fd\u4e0d\u5141\u8bb8\u7528\u4e0a C++20 \u751a\u81f3 C++23 \u7684\u65b0\u6807\u51c6\u3002 \u522b\u62c5\u5fc3\uff0c\u5c0f\u5f6d\u8001\u5e08\u6559\u4f1a\u4f60 C++23 \u7684\u6b63\u5e38\u5199\u6cd5\u540e\uff0c\u4f1a\u8bb2\u89e3\u5982\u4f55\u5728 C++14\u3001C++98 \u4e2d\u5199\u51fa\u540c\u6837\u7684\u6548\u679c\u3002 \u8fd9\u6837\u4f60\u5b66\u4e60\u7684\u65f6\u5019\u601d\u8def\u6e05\u6670\uff0c\u4e0d\u7528\u88ab\u7e41\u7410\u7684 C++98 \u201c\u5947\u6280\u6deb\u5de7\u201d\u5e72\u6270\uff0c\u5b66\u8d77\u6765\u4e8b\u534a\u529f\u500d\uff1b\u4f46\u4e5f\u201c\u5403\u8fc7\u89c1\u8fc7\u201d\uff0c\u77e5\u9053\u53e4\u4ee3 C++98 \u7684\u5e94\u5bf9\u7b56\u7565\u3002 \u76ee\u524d\u4f01\u4e1a\u91cc\u4e3b\u6d41\u4f7f\u7528\u7684\u662f C++14 \u548c C++17\u3002\u4f8b\u5982\u8c37\u6b4c\u5c31\u660e\u786e\u89c4\u5b9a\u8981\u6c42 C++17\u3002","title":"\u89c2\u524d\u987b\u77e5"},{"location":"#_4","text":"\u63a5\u4e0b\u6765\u7684\u4f8b\u5b50\u4f60\u53ef\u80fd\u770b\u4e0d\u61c2\uff0c\u4f46\u53ea\u9700\u8981\u8bb0\u4f4f\u8fd9\u4e2a\u4f8b\u5b50\u662f\u5411\u4f60\u8bf4\u660e\uff1a\u8d8a\u662f\u65b0\u7684 C++ \u6807\u51c6\uff0c\u53cd\u800c\u8d8a\u5bb9\u6613\u5b66\uff01 \u4f8b\u5982\uff0c\u5728\u6a21\u677f\u5143\u7f16\u7a0b\u4e2d\uff0c\u8981\u68c0\u6d4b\u4e00\u4e2a\u7c7b\u578b T \u662f\u5426\u62e5\u6709 foo() \u8fd9\u4e00\u6210\u5458\u51fd\u6570\u3002\u5982\u679c\u5b58\u5728\uff0c\u624d\u4f1a\u8c03\u7528\u3002 \u5728 C++20 \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5f88\u65b9\u4fbf\u7684 requires \u8bed\u6cd5\uff0c\u8f7b\u677e\u68c0\u6d4b\u4e00\u4e2a\u8868\u8fbe\u5f0f\u662f\u5426\u80fd\u5408\u6cd5\u901a\u8fc7\u7f16\u8bd1\u3002\u5982\u679c\u80fd\uff0c requires \u8bed\u53e5\u4f1a\u8fd4\u56de true \u3002\u7136\u540e\u7528\u4e00\u4e2a if constexpr \u8fdb\u884c\u7f16\u8bd1\u671f\u5206\u652f\u5224\u65ad\uff0c\u5373\u53ef\u5b9e\u73b0\u68c0\u6d4b\u5230\u5b58\u5728\u5219\u8c03\u7528\u3002 template void try_call_foo(T &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } \u4f46\u4ec5\u4ec5\u662f\u56de\u5230 C++17\uff0c\u6ca1\u6709 requires \u8bed\u6cd5\uff0c\u6211\u4eec\u53ea\u80fd\u81ea\u5df1\u5b9a\u4e49\u4e00\u4e2a trait \u7c7b\uff0c\u5e76\u8fd0\u7528\u70e6\u4eba\u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u68c0\u6d4b\u8868\u8fbe\u5f0f\u662f\u5426\u7684\u5408\u6cd5\uff0c\u53c8\u81ed\u53c8\u957f\u3002 template struct has_foo { inline constexpr bool value = false; }; template struct has_foo().foo())>> { inline constexpr bool value = true; }; template void try_call_foo(T &t) { if constexpr (has_foo::value) { t.foo(); } } \u5982\u679c\u56de\u5230 C++14\uff0c\u60c5\u51b5\u5c31\u66f4\u7cdf\u7cd5\u4e86\uff01 if constexpr \u662f C++17 \u7684\u7279\u6027\uff0c\u6ca1\u6709\u4ed6\uff0c\u8981\u5b9e\u73b0\u7f16\u8bd1\u671f\u5206\u652f\uff0c\u6211\u4eec\u5c31\u5f97\u7528 enable_if_t \u7684 SFINAE \u5c0f\u6280\u5de7\uff0c\u9700\u8981\u5b9a\u4e49\u4e24\u4e2a try_call_foo \u51fd\u6570\uff0c\u4e92\u76f8\u91cd\u8f7d\uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo())>> { static constexpr bool value = true; }; template ::value, int> = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int> = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++11\uff0c\u60c5\u51b5\u8fdb\u4e00\u6b65\u6076\u5316\uff01 enable_if_t \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5c0f\u52a9\u624b\u5df2\u7ecf\u4e0d\u5b58\u5728\uff0c\u9700\u8981\u4f7f\u7528\u6bd4\u4ed6\u66f4\u5e95\u5c42\u7684 enable_if \u6a21\u677f\u7c7b\uff0c\u624b\u52a8\u53d6\u51fa ::type \uff0c\u5e76\u4e14\u9700\u8981 typename \u4fee\u9970\uff0c\u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff01\u5e76\u4e14 void_t \u4e5f\u4e0d\u80fd\u7528\u4e86\uff0c\u8981\u7528\u9017\u53f7\u8868\u8fbe\u5f0f\u5c0f\u6280\u5de7\u624d\u80fd\u8ba9 decltype \u56fa\u5b9a\u8fd4\u56de void\u2026\u2026 template struct has_foo { static constexpr bool value = false; }; template struct has_foo().foo(), (void)0)> { static constexpr bool value = true; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u679c\u56de\u5230 C++98\uff0c\u90a3\u53c8\u8981\u7f6a\u52a0\u4e00\u7b49\uff01 enable_if \u548c \u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u7684\u5e2e\u624b\u7c7b\uff0c\u5728 C++98 \u4e2d\uff0c\u6211\u4eec\u9700\u8981\u81ea\u5df1\u5b9e\u73b0 enable_if \u2026\u2026 declval \u4e5f\u662f C++11 \u5f15\u5165\u7684 \u5934\u6587\u4ef6\u4e2d\u7684\u5e2e\u624b\u51fd\u6570\u2026\u2026\u5047\u8bbe\u4f60\u81ea\u5df1\u597d\u4e0d\u5bb9\u6613\u5b9e\u73b0\u51fa\u6765\u4e86 enable_if \u548c declval \uff0c\u8fd8\u6ca1\u5b8c\uff1a\u56e0\u4e3a constexpr \u5728 C++98 \u4e2d\u4e5f\u4e0d\u5b58\u5728\u4e86\uff01\u4f60\u65e0\u6cd5\u5b9a\u4e49 value \u6210\u5458\u53d8\u91cf\u4e3a\u7f16\u8bd1\u671f\u5e38\u91cf\uff0c\u6211\u4eec\u53ea\u597d\u53c8\u7528\u4e00\u4e2a\u62bd\u8c61\u7684\u679a\u4e3e\u5c0f\u6280\u5de7\u6765\u5b9e\u73b0\u5b9a\u4e49\u7c7b\u6210\u5458\u5e38\u91cf\u7684\u6548\u679c\u3002 template struct has_foo { enum { value = 0 }; }; template struct has_foo().foo(), (void)0)> { enum { value = 1 }; }; template ::value, int>::type = 0> void try_call_foo(T &t) { t.foo(); } template ::value, int>::type = 0> void try_call_foo(T &) { } \u5982\u6b64\u5197\u957f\u96be\u61c2\u7684\u62bd\u8c61 C++98 \u4ee3\u7801\uff0c\u4eff\u4f5b\u662f\u201c\u52a0\u5bc6\u201d\u8fc7\u7684\u4ee3\u7801\u4e00\u6837\uff0c\u4ec5\u4ec5\u662f\u4e3a\u4e86\u5b9e\u73b0\u68c0\u6d4b\u662f\u5426\u5b58\u5728\u6210\u5458\u51fd\u6570 foo\u2026\u2026 \u5982\u679c\u56de\u5230 C \u8bed\u8a00\uff0c\u90a3\u4e48\u4f60\u751a\u81f3\u90fd\u4e0d\u7528\u68c0\u6d4b\u4e86\u3002\u56e0\u4e3a\u4f1f\u5927\u7684 C \u8bed\u8a00\u8fde\u6210\u5458\u51fd\u6570\u90fd\u6ca1\u6709\uff0c\u4f55\u8c08\u201c\u68c0\u6d4b\u6210\u5458\u51fd\u6570\u662f\u5426\u5b58\u5728\u201d\uff1f \u53cd\u89c2 C++20 \u7684\u5199\u6cd5\uff0c\u4e00\u773c\u5c31\u770b\u660e\u767d\u4ee3\u7801\u7684\u903b\u8f91\u662f\u4ec0\u4e48\uff0c\u8868\u8fbe\u4f60\u8be5\u8868\u8fbe\u7684\uff0c\u800c\u4e0d\u662f\u8ff7\u5931\u4e8e\u4f3a\u5019\u5404\u79cd\u8bed\u8a00\u7f3a\u9677\uff0c\u5e72\u6270\u6211\u4eec\u5b66\u4e60\u3002 void try_call_foo(auto &t) { if constexpr (requires { t.foo(); }) { t.foo(); } } // \u4ece\u6b8b\u5e9f\u7684 C++98 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u5c31\u88ab\u8fd9\u4e9b\u65e0\u8c13\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u626d\u66f2\u4e86\uff0c\u800c\u4f7f\u5f97\u771f\u6b63\u5e94\u8be5\u8868\u8fbe\u7684\u4ee3\u7801\u903b\u8f91\uff0c\u6df9\u6ca1\u5728\u53c8\u81ed\u53c8\u957f\u7684\u53e4\u4ee3\u6280\u5de7\u4e2d\u3002 // \u4ece\u73b0\u4ee3\u7684 C++23 \u5b66\u8d77\uff0c\u5148\u77e5\u9053\u6b63\u5e38\u7684\u5199\u6cd5\u201c\u7406\u5e94\u201d\u662f\u4ec0\u4e48\u6837\u3002\u5de5\u4f5c\u4e2d\u7528\u4e0d\u4e0a C++23\uff1f\u6211\u4f1a\u5411\u4f60\u4ecb\u7ecd\uff0c\u5982\u679c\u8981\u5012\u9000\u56de C++14\uff0c\u53e4\u4ee3\u4eba\u90fd\u662f\u7528\u4ec0\u4e48\u201c\u5947\u6280\u6deb\u5de7\u201d\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\u3002 // \u8fd9\u6837\u4f60\u6700\u540e\u540c\u6837\u53ef\u4ee5\u9002\u5e94\u516c\u53f8\u8981\u6c42\u7684 C++14 \u73af\u5883\u3002\u4f46\u662f\u4ece C++23 \u5b66\u8d77\uff0c\u4f60\u7684\u601d\u7ef4\u53c8\u4e0d\u4f1a\u88ab\u5e94\u4ed8\u53e4\u4ee3\u8bed\u8a00\u7f3a\u9677\u7684\u201c\u5947\u6280\u6deb\u5de7\u201d\u6270\u4e71\uff0c\u5b66\u8d77\u6765\u5c31\u4e8b\u534a\u529f\u500d\u3002 \u65e2\u7136\u73b0\u4ee3 C++ \u8fd9\u4e48\u597d\uff0c\u4e3a\u4ec0\u4e48\u5b66\u6821\u4e0d\u4ece\u73b0\u4ee3 C++ \u6559\u8d77\uff0c\u6559\u8d77\u6765\u8fd8\u8f7b\u677e\uff1f\u56e0\u4e3a\u52b3\u4fdd\u8001\u5e08\u4fdd\uff0c\u61d2\u5f97\u63a5\u89e6\u65b0\u77e5\u8bc6\uff0c\u8ba4\u4e3a\u201c\u7956\u5b97\u4e4b\u6cd5\u4e0d\u53ef\u53d8\u201d\uff0c\u201c\u7248\u53f7\u7a33\u5b9a\u538b\u5012\u4e00\u5207\u201d\u3002","title":"\u4e3e\u4e2a\u4f8b\u5b50"},{"location":"design/","text":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 \u4e0d\u8981\u53bb\u6307\u6325\u4e0b\u9762\u600e\u4e48\u505a\uff01 \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. \u4f60\u597d O(N) O(N) \u554a\uff01","title":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5)"},{"location":"design/#_1","text":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5) \u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9 \u4e0d\u8981\u53bb\u6307\u6325\u4e0b\u9762\u600e\u4e48\u505a\uff01","title":"\u8bbe\u8ba1\u6a21\u5f0f (\u672a\u5b8c\u5de5)"},{"location":"design/#_2","text":"x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. x = {-b \\pm \\sqrt{b^2-4ac} \\over 2a}. \u4f60\u597d O(N) O(N) \u554a\uff01","title":"\u52a8\u6001\u7c7b\u578b\u7684\u7f3a\u70b9"},{"location":"functional/","text":"\u51fd\u6570\u5f0f\u7f16\u7a0b \u51fd\u6570\u5f0f\u7f16\u7a0b \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u7528\u51fd\u6570\u5c01\u88c5 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u4e8c\u6b21\u5c01\u88c5 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570 \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum = {}\", s); return 0; } \u8fd9\u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u7ec4\u6c42\u548c\u7684\u7b80\u5355\u7a0b\u5e8f\u3002 \u4f46\u662f\uff0c\u4ed6\u53ea\u80fd\u8ba1\u7b97\u6570\u7ec4 a \u7684\u6c42\u548c\uff0c\u65e0\u6cd5\u590d\u7528\u3002 \u5982\u679c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u6570\u7ec4 b \u4e5f\u9700\u8981\u6c42\u548c\u7684\u8bdd\uff0c\u5c31\u5f97\u628a\u6574\u4e2a\u6c42\u548c\u7684 for \u5faa\u73af\u91cd\u65b0\u5199\u4e00\u904d\uff1a int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum of a = {}\", s); std::vector b = {5, 6, 7, 8}; s = 0; for (int i = 0; i < a.size(); i++) { s += b[i]; } fmt::println(\"sum of b = {}\", s); return 0; } \u8fd9\u5c31\u51fa\u73b0\u4e86\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5927\u5fcc\uff1a\u4ee3\u7801\u91cd\u590d\u3002 \u4f8b\u5982\uff0c\u4f60\u6709\u5439\u7a7a\u8c03\u7684\u9700\u6c42\uff0c\u548c\u5145\u624b\u673a\u7684\u9700\u6c42\u3002\u4f60\u4e3a\u4e86\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u9700\u6c42\uff0c\u8d2d\u4e70\u4e86\u4e24\u53f0\u53d1\u7535\u673a\uff0c\u5206\u522b\u4e3a\u7a7a\u8c03\u548c\u624b\u673a\u4f9b\u7535\u3002\u7b2c\u4e8c\u5929\uff0c\u4f60\u53c8\u4ea7\u751f\u4e86\u73a9\u7535\u8111\u9700\u6c42\uff0c\u4e8e\u662f\u4f60\u53c8\u8d2d\u4e70\u4e00\u53f0\u53d1\u7535\u673a\uff0c\u4e13\u4e3a\u7535\u8111\u4f9b\u7535\u2026\u2026\u771f\u662f\u6d6a\u8d39\uff01 \u91cd\u590d\u7684\u4ee3\u7801\u4e0d\u4ec5\u5f71\u54cd\u4ee3\u7801\u7684 \u53ef\u8bfb\u6027 \uff0c\u4e5f\u589e\u52a0\u4e86 \u7ef4\u62a4 \u4ee3\u7801\u7684\u6210\u672c\u3002 \u770b\u8d77\u6765\u4e71\u7cdf\u7cdf\u7684\uff0c\u4fe1\u606f\u5bc6\u5ea6\u4f4e\uff0c\u8ba9\u4eba\u4e00\u773c\u770b\u4e0d\u51fa\u4ee3\u7801\u5728\u5e72\u4ec0\u4e48\u7684\u529f\u80fd \u5f88\u5bb9\u6613\u5199\u9519\uff0c\u770b\u8d70\u773c\uff0c\u96be\u8c03\u8bd5 \u590d\u5236\u7c98\u8d34\u8fc7\u7a0b\u4e2d\uff0c\u5bb9\u6613\u6f0f\u6539\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684 s += b[i] \u53ef\u80fd\u5199\u6210 s += a[i] \u800c\u81ea\u5df1\u4e0d\u53d1\u73b0 \u6539\u8d77\u6765\u4e0d\u65b9\u4fbf\uff0c\u5f53\u6211\u4eec\u7684\u9700\u6c42\u53d8\u66f4\u65f6\uff0c\u9700\u8981\u591a\u5904\u4fee\u6539\uff0c\u6bd4\u5982\u5f53\u6211\u9700\u8981\u6539\u4e3a\u8ba1\u7b97\u4e58\u79ef\u65f6\uff0c\u9700\u8981\u628a\u4e24\u4e2a\u5730\u65b9\u90fd\u6539\u6210 s *= \u6539\u4e86\u4ee5\u540e\u53ef\u80fd\u6f0f\u6539\u4e00\u90e8\u5206\uff0c\u7559\u4e0b Bug \u9690\u60a3 \u654f\u6377\u5f00\u53d1\u9700\u8981\u53cd\u590d\u4fee\u6539\u4ee3\u7801\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u8c03\u8bd5 += \u548c -= \u7684\u533a\u522b\uff0c\u770b\u7ed3\u679c\u53d8\u5316\uff0c\u5982\u679c\u4e00\u6b21\u5207\u6362\u9700\u8981\u6539\u591a\u5904\uff0c\u5c31\u5f71\u54cd\u4e86\u8c03\u8bd5\u901f\u5ea6 \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u5982\u679c\u4f60\u8fd8\u662f\u559c\u6b22\u201c\u4e00\u672c\u9053\u201d\u5199\u6cd5\u7684\u8bdd\uff0c\u4e0d\u59a8\u60f3\u60f3\u770b\uff0c\u5b8c\u5168\u4e0d\u7528\u4efb\u4f55\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u628a fmt::println \u548c std::vector \u8fd9\u4e9b\u51fd\u6570\u5168\u90e8\u62c6\u89e3\u6210\u4e00\u4e2a\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u90a3\u8fd9\u6574\u4e2a\u7a0b\u5e8f\u4f1a\u6709\u591a\u96be\u5199\uff1f int main() { #ifdef _WIN32 int *a = (int *)VirtualAlloc(NULL, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else int *a = (int *)mmap(NULL, 4 * sizeof(int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; int s = 0; for (int i = 0; i < 4; i++) { s += a[i]; } char buffer[64]; buffer[0] = 's'; buffer[1] = 'u'; buffer[2] = 'm'; buffer[3] = ' '; buffer[4] = '='; buffer[5] = ' '; // \u4f8b\u5982\uff0c\u5982\u679c\u8981\u4fee\u6539\u6b64\u5904\u7684\u63d0\u793a\u6587\u672c\uff0c\u751a\u81f3\u9700\u8981\u4fee\u6539\u540e\u9762\u7684 len \u53d8\u91cf... int len = 6; int x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif int *b = (int *)a; b[0] = 4; b[1] = 5; b[2] = 6; b[3] = 7; int s = 0; for (int i = 0; i < 4; i++) { s += b[i]; } len = 6; x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif #ifdef _WIN32 VirtualFree(a, 0, MEM_RELEASE); #else munmap(a); #endif return 0; } \u4e0d\u4ec5\u5b8c\u5168\u6ca1\u6709\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\uff0c\u751a\u81f3\u90fd\u6ca1\u6709\u53ef\u79fb\u690d\u6027\u3002 \u9664\u975e\u4f60\u53ea\u5199\u5e94\u4ed8\u5bfc\u5e08\u7684\u201c\u4e00\u6b21\u6027\u201d\u7a0b\u5e8f\uff0c\u4e00\u65e6\u8981\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u53ef\u907f\u514d\u7684\u8981\u81ea\u5df1\u5c01\u88c5\u51fd\u6570\u6216\u7c7b\u3002\u7f51\u4e0a\u6240\u6709\u9f13\u5439\u201c\u4e0d\u5c01\u88c5\u201d\u201c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u9762\u5b50\u5de5\u7a0b\u201d\u7684\u53cd\u667a\u8a00\u8bba\uff0c\u90fd\u662f\u6ca1\u6709\u505a\u8fc7\u5927\u578b\u9879\u76ee\u7684\u3002 \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u5f88\u591a\u8bbe\u8ba1\u6a21\u5f0f\u6559\u6750\u7247\u9762\u5f3a\u8c03 \u53ef\u8bfb\u6027 \uff0c\u4eff\u4f5b\u8bbe\u8ba1\u6a21\u5f0f\u5c31\u662f\u4e3a\u4e86\u201c\u4f18\u96c5\u201d\u201c\u9ad8\u5927\u4e0a\u201d\u201c\u7f8e\u5b66\u201d\uff1f\u4f7f\u5f97\u5f88\u591a\u4eba\u8ba4\u4e3a\uff0c\u201c\u6211\u8fd9\u4e2a\u662f\u81ea\u5df1\u7684\u9879\u76ee\uff0c\u4e0d\u7528\u7f8e\u5316\u7ed9\u9886\u5bfc\u770b\u201d\u800c\u62d2\u7edd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b9e\u9645\u4e0a\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4e3b\u8981\u4ef7\u503c\u5728\u4e8e \u65b9\u4fbf\u540e\u7eed\u4fee\u6539 *\uff01 \u4f8b\u5982 B \u7ad9\u4ee5\u524d\u53ea\u652f\u6301\u4e0a\u4f20\u666e\u901a\u89c6\u9891\uff0c\u73b0\u5728\u53d4\u53d4\u7a81\u7136\u63d0\u51fa\uff1a\u8981\u652f\u6301\u4e92\u52a8\u89c6\u9891\uff0c\u5145\u7535\u89c6\u9891\uff0c\u89c6\u9891\u5408\u96c6\uff0c\u8fd8\u5e9f\u9664\u4e86\u89c6\u9891\u5206 p\uff0c\u8fd8\u8981\u652f\u6301\u4e0a\u4f20\u77ed\u89c6\u9891\uff0c\u7ad6\u5c4f\u5f00\u5173\u7b49\u2026\u2026\u6bcf\u4e00\u4e2a\u53d4\u53d4\u7684\u8981\u6c42\uff0c\u90fd\u9700\u8981\u5927\u91cf\u7a0b\u5e8f\u5458\u4fee\u6539\u4ee3\u7801\uff0c\u65e0\u8bba\u6d89\u53ca\u524d\u7aef\u8fd8\u662f\u540e\u7aef\u3002 \u4e0e\u5efa\u7b51\u3001\u7ed8\u753b\u7b49\u9886\u57df\u4e0d\u540c\uff0c\u4e00\u6b21\u4ea4\u4ed8\u5b8c\u6bd5\u5c31\u53ef\u4ee5\u51e0\u4e4e\u6c38\u4e45\u4f7f\u7528\u3002\u800c\u8f6f\u4ef6\u5f00\u53d1\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u66f4\uff0c\u90fd\u5bfc\u81f4\u4ee3\u7801\u9700\u8981\u4fee\u6539\u3002\u5f00\u53d1\u4eba\u5458\u51e0\u4e4e\u9700\u8981\u4e00\u76f4\u56f4\u7ed5\u7740\u8f6f\u4ef6\u4ee3\u7801\uff0c\u4e0d\u65ad\u7684\u4fee\u6539\u3002\u8c03\u67e5\u8868\u660e\uff0c\u7a0b\u5e8f\u5458 90% \u7684\u65f6\u95f4\u82b1\u5728 \u6539\u4ee3\u7801 \u4e0a\uff0c \u5199\u4ee3\u7801 \u53ea\u5360 10%\u3002 \u8f6f\u4ef6\u5c31\u50cf\u751f\u7269\uff0c\u8981\u4e0d\u65ad\u8fdb\u5316\uff0c\u8f6f\u4ef6\u4e0d\u66f4\u65b0\u4e0d\u7ef4\u62a4\u4e86\u7b49\u4e8e\u6b7b\u3002\u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6\u9010\u6e10\u53d8\u5f97\u81c3\u80bf\u96be\u4ee5\u4fee\u6539\uff0c\u65e0\u6cd5\u9002\u5e94\u65b0\u9700\u6c42\uff0c\u90a3\u4ed6\u5c31\u50cf\u5df2\u7ecf\u5931\u53bb\u8fdb\u5316\u80fd\u529b\u7684\u751f\u7269\u79cd\u7fa4\uff0c\u5982\u300a\u4e09\u4f53\u300b\u4e16\u754c\u89c2\u4e2d\u201c\u5b89\u987f\u201d\u5230\u6fb3\u5927\u5229\u4e9a\u4fdd\u7559\u533a\u91cc\u201c\u7edd\u80b2\u201d\u7684\u4eba\u7c7b\uff0c\u88ab\u6dd8\u6c70\u53ea\u662f\u65f6\u95f4\u95ee\u9898\u3002 \u5982\u679c\u6211\u4eec\u80fd\u5728 \u5199\u4ee3\u7801 \u9636\u6bb5\uff0c\u5c31\u628a\u7a0b\u5e8f\u51c6\u5907\u5f97 \u6613\u4e8e\u540e\u7eed\u4fee\u6539 \uff0c\u90a3\u5c31\u53ef\u4ee5\u5728\u540e\u7eed 90% \u7684 \u6539\u4ee3\u7801 \u9636\u6bb5\u7701\u4e0b\u65e0\u6570\u65f6\u95f4\u3002 \u5982\u4f55\u8ba9\u4ee3\u7801\u6613\u4e8e\u4fee\u6539\uff1f\u524d\u4eba\u603b\u7ed3\u51fa\u4e00\u7cfb\u5217\u5e38\u7528\u7684\u5199\u6cd5\uff0c\u8fd9\u7c7b\u5199\u6cd5\u6709\u52a9\u4e8e\u8ba9\u540e\u7eed\u4fee\u6539\u66f4\u5bb9\u6613\uff0c\u5404\u81ea\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u5408\uff0c\u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u3002 \u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u6700\u57fa\u7840\u7684\u4e00\u70b9\uff0c\u5c31\u662f\u907f\u514d\u91cd\u590d\uff01 \u5f53\u4f60\u6709\u5f88\u591a\u5730\u65b9\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\u65f6\uff0c\u4e00\u65e6\u9700\u8981\u6d89\u53ca\u4fee\u6539\u8fd9\u90e8\u5206\u903b\u8f91\u65f6\uff0c\u5c31\u9700\u8981\u5230\u6bcf\u4e00\u4e2a\u51fa\u73b0\u4e86\u8fd9\u4e2a\u903b\u8f91\u7684\u4ee3\u7801\u4e2d\uff0c\u53bb\u9010\u4e00\u4fee\u6539\u3002 \u4f8b\u5982\u4f60\u7684\u540d\u5b57\uff0c\u5728\u51fa\u751f\u8bc1\uff0c\u8eab\u4efd\u8bc1\uff0c\u5b66\u751f\u8bc1\uff0c\u6bd5\u4e1a\u8bc1\uff0c\u623f\u4ea7\u8bc1\uff0c\u9a7e\u9a76\u8bc1\uff0c\u5404\u79cd\u5730\u65b9\u90fd\u51fa\u73b0\u4e86\u3002\u90a3\u4e48\u4f60\u8981\u6539\u540d\u7684\u8bdd\uff0c\u6240\u6709\u8fd9\u4e9b\u8bc1\u4ef6\u90fd\u9700\u8981\u91cd\u65b0\u5370\u5237\uff01\u5982\u679c\u80fd\u628a\u4ed6\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u201c\u7edf\u4e00\u8bc1\u201d\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u4fee\u6539\u201c\u7edf\u4e00\u8bc1\u201d\u4e0a\u7684\u540d\u5b57\u5c31\u884c\u4e86\u3002 \u4e0d\u8fc7\uff0c\u73b0\u5b9e\u4e2d\u5e76\u6ca1\u6709\u9891\u7e41\u6539\u540d\u5b57\u7684\u9700\u6c42\uff0c\u8fd9\u8bf4\u660e\uff1a \u5bf9\u4e8e\u4e0d\u5e38\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u53ef\u4ee5\u5bb9\u5fcd\u4e00\u5b9a\u7684\u91cd\u590d\u3002 \u8d8a\u662f\u672a\u6765\u6709\u53ef\u80fd\u4fee\u6539\u7684\uff0c\u5c31\u8d8a\u9700\u8981\u8bbe\u8ba1\u6a21\u5f0f\u964d\u91cd\uff01 \u4f8b\u5982\u6570\u5b66\u5e38\u6570 PI = 3.1415926535897\uff0c\u8fd9\u8f88\u5b50\u90fd\u4e0d\u53ef\u80fd\u51fa\u73b0\u4fee\u6539\u7684\u9700\u6c42\uff0c\u90a3\u5199\u6b7b\u4e5f\u6ca1\u5173\u7cfb\u3002\u5982\u679c\u8981\u628a PI \u5b9a\u4e49\u6210\u5b8f\uff0c\u53ea\u662f\u51fa\u4e8e\u201c\u8bb0\u4e0d\u4f4f\u201d\u201c\u5199\u8d77\u6765\u592a\u957f\u4e86\u201d\u201c\u590d\u5236\u7c98\u8d34\u9ebb\u70e6\u201d\u3002\u6240\u4ee5\u5bf9\u4e8e PI \u8fd9\u79cd\u4e0d\u4f1a\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u964d\u91cd\u53ea\u662f\u589e\u52a0 \u53ef\u8bfb\u6027 \uff0c\u800c\u4e0d\u662f \u53ef\u4fee\u6539\u6027 \u3002 \u4f46\u662f\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\uff01\u9700\u6c42\u7684\u5343\u53d8\u4e07\u5316\u603b\u662f\u8d85\u51fa\u4f60\u7684\u60f3\u8c61\u3002 \u4f8b\u5982\u4f60\u505a\u4e86\u4e00\u4e2a\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u6e38\u620f\uff0c\u9700\u8981\u7528\u5230\u91cd\u529b\u52a0\u901f\u5ea6 g = 9.8\uff0c\u4f60\u60f3\u5f53\u7136\u8ba4\u4e3a g \u4ee5\u540e\u4e0d\u53ef\u80fd\u4fee\u6539\u3002\u8001\u677f\u4e5f\u4fe1\u8a93\u65e6\u65e6\u5411\u4f60\u4fdd\u8bc1\uff1a\u201c\u6ca1\u4e8b\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u4e0d\u4f1a\u6539\u53d8\u3002\u201d\u4f60\u5c31\u5199\u6b7b\u5728\u4ee3\u7801\u91cc\u4e86\u3002 \u6ca1\u60f3\u5230\uff0c\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u8001\u677f\u7a81\u7136\u8981\u6c42\u4f60\u52a0\u5165\u201c\u6708\u7403\u7ae0\u201d\u5173\u5361\uff0c\u5728\u8fd9\u4e9b\u5173\u5361\u4e2d\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u662f g = 1.6\u3002 \u5982\u679c\u4f60\u4e00\u5f00\u59cb\u5c31\u5df2\u7ecf\u628a g \u63d0\u53d6\u51fa\u6765\uff0c\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a struct Level { const double g = 9.8; void physics_sim() { bird.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f pig.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f } }; \u90a3\u4e48\u8981\u652f\u6301\u6708\u7403\u5173\u5361\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5c31\u53ef\u4ee5\u4e86\u3002 struct Level { double g; Level(Chapter chapter) { if (chapter == ChapterMoon) { g = 1.6; } else { g = 9.8; } } void physics_sim() { bird.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g pig.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g } }; \u5c0f\u5f6d\u8001\u5e08\u4e4b\u524d\u505a zeno \u65f6\uff0c\u8be2\u95ee\u8981\u4e0d\u8981\u628a\u6e32\u67d3\u7ba1\u7ebf\u8282\u70b9\u5316\uff0c\u65b9\u4fbf\u7528\u6237\u52a8\u6001\u7f16\u7a0b\uff1f\u5f20\u7329\u7329\u5c31\u662f\u4fe1\u8a93\u65e6\u65e6\u9053\uff1a\u201c\u6e32\u67d3\u662f\u4e00\u4e2a\u9ad8\u5ea6\u6210\u719f\u9886\u57df\uff0c\u4e0d\u4f1a\u6709\u591a\u5c11\u4fee\u6539\u9700\u6c42\u7684\u3002\u201d\u5c0f\u5f6d\u8001\u5e08\u9042\u5199\u6b7b\u4e86\u6e32\u67d3\u7ba1\u7ebf\uff0c\u4e13\u4e3a\u6027\u80fd\u6781\u5ea6\u4f18\u5316\uff0c\u51e0\u4e2a\u6708\u540e\uff0c\u5f20\u7329\u7329\u7f9e\u7b54\u7b54\u627e\u5230\u5c0f\u5f6d\u8001\u5e08\uff1a\u201c\u5c0f\u5f6d\u8001\u5e08\uff0c\u90a3\u4e2a\uff0c\u6e32\u67d3\uff0c\u80fd\u4e0d\u80fd\u6539\u6210\u8282\u70b9\u554a\u2026\u2026\u201d\u3002\u8fd9\u4e2a\u6545\u4e8b\u544a\u8bc9\u6211\u4eec\uff0c\u7532\u65b9\u7684\u4fe1\u8a93\u65e6\u65e6\u653e\u7684\u4e00\u4e2a\u5c41\u90fd\u4e0d\u80fd\u4fe1\u3002 \u7528\u51fd\u6570\u5c01\u88c5 \u51fd\u6570\u5c31\u662f\u6765\u5e2e\u4f60\u89e3\u51b3\u4ee3\u7801\u91cd\u590d\u95ee\u9898\u7684\uff01\u8981\u9886\uff1a \u628a\u5171\u540c\u7684\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002 void sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } fmt::println(\"sum of v = {}\", s); } int main() { std::vector a = {1, 2, 3, 4}; sum(a); std::vector b = {5, 6, 7, 8}; sum(b); return 0; } \u8fd9\u6837 main \u51fd\u6570\u91cc\u5c31\u53ef\u4ee5\u53ea\u5173\u5fc3\u8981\u6c42\u548c\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u7528\u5173\u5fc3\u6c42\u548c\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u4e86\u3002\u4e8b\u540e\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u628a sum \u7684\u5185\u5bb9\u5077\u5077\u6362\u6389\uff0c\u6362\u6210\u5e76\u884c\u7684\u7b97\u6cd5\uff0cmain \u4e5f\u4e0d\u7528\u77e5\u9053\u3002\u8fd9\u5c31\u662f \u5c01\u88c5 \uff0c\u53ef\u4ee5\u628a\u91cd\u590d\u7684\u516c\u5171\u90e8\u5206\u62bd\u53d6\u51fa\u6765\uff0c\u65b9\u4fbf\u4ee5\u540e\u4fee\u6539\u4ee3\u7801\u3002 sum \u51fd\u6570\u76f8\u5f53\u4e8e\uff0c\u5f53\u9700\u8981\u5439\u7a7a\u8c03\u65f6\uff0c\u63d2\u4e0a\u7a7a\u8c03\u63d2\u5ea7\u3002\u5f53\u9700\u8981\u7ed9\u624b\u673a\u5145\u7535\u65f6\uff0c\u63d2\u4e0a\u624b\u673a\u5145\u7535\u5668\u3002\u4f60\u4e0d\u9700\u8981\u5173\u5fc3\u63d2\u5ea7\u91cc\u7684\u7535\u54ea\u91cc\u6765\uff0c\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4f1a\u66ff\u4f60\u60f3\u529e\u6cd5\u89e3\u51b3\uff0c\u60f3\u529e\u6cd5\u4f18\u5316\uff0c\u60f3\u529e\u6cd5\u5347\u7ea7\u5230\u7eff\u8272\u80fd\u6e90\u3002\u4f60\u53ea\u9700\u8981\u5439\u7740\u7a7a\u8c03\u7ed9\u4f60\u6b63\u5728\u5f00\u53d1\u7684\u624b\u673a App \u4f18\u5316\u5c31\u884c\u4e86\uff0c\u5927\u5927\u51cf\u8f7b\u7a0b\u5e8f\u5458\u5fc3\u667a\u8d1f\u62c5\u3002 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u4f46\u662f\uff01\u8fd9\u6bb5\u4ee3\u7801\u4ecd\u7136\u6709\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u628a sum \u6c42\u548c\u7684\u7ed3\u679c\uff0c\u76f4\u63a5\u5728 sum \u91cc\u6253\u5370\u4e86\u51fa\u6765\u3002sum \u91cc\u5199\u6b7b\u4e86\uff0c\u6c42\u5b8c\u548c\u4e4b\u540e\u53ea\u80fd\u76f4\u63a5\u6253\u5370\uff0c\u8c03\u7528\u8005 main \u6839\u672c\u65e0\u6cd5\u63a7\u5236\u3002 \u8fd9\u662f\u4e00\u79cd\u9519\u8bef\u7684\u5c01\u88c5\uff0c\u6216\u8005\u8bf4\uff0c\u5c01\u88c5\u8fc7\u5934\u4e86\u3002 \u4f60\u628a\u624b\u673a\u5145\u7535\u5668 (fmt::println) \u710a\u6b7b\u5728\u4e86\u63d2\u5ea7 (sum) \u4e0a\uff0c\u73b0\u5728\u8fd9\u4e2a\u63d2\u5ea7\u53ea\u80fd\u7ed9\u624b\u673a\u5145\u7535 (\u7528\u4e8e\u76f4\u63a5\u6253\u5370) \u4e86\uff0c\u4e0d\u80fd\u7ed9\u7b14\u8bb0\u672c\u7535\u8111\u5145\u7535 (\u6c42\u548c\u7ed3\u679c\u4e0d\u76f4\u63a5\u7528\u4e8e\u6253\u5370) \u4e86\uff01\u5c3d\u7ba1\u901a\u8fc7\u66f4\u6362\u5145\u7535\u7ebf (\u53c2\u6570 v)\uff0c\u8fd8\u53ef\u4ee5\u652f\u6301\u652f\u6301\u5b89\u5353 (a) \u548c\u82f9\u679c (b) \u4e24\u79cd\u624b\u673a\u7684\u5145\u7535\uff0c\u4f46\u8fd9\u6837\u710a\u6b7b\u7684\u63d2\u5ea7\u5df2\u7ecf\u548c\u7b14\u8bb0\u672c\u7535\u8111\u65e0\u7f18\u4e86\u3002 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u5f88\u660e\u663e\uff0c\u201c\u6253\u5370\u201d\u548c\u201c\u6c42\u548c\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u64cd\u4f5c\uff0c\u4e0d\u5e94\u8be5\u710a\u6b7b\u5728\u4e00\u5757\u3002 sum \u51fd\u6570\u7684\u672c\u804c\u5de5\u4f5c\u662f\u201c\u6570\u7ec4\u6c42\u548c\u201d\uff0c\u4e0d\u5e94\u8be5\u9644\u8d60\u6253\u5370\u529f\u80fd\u3002 sum \u8ba1\u7b97\u51fa\u6c42\u548c\u7ed3\u679c\u540e\uff0c\u76f4\u63a5 return \u5373\u53ef\u3002 \u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u7ed3\u679c\uff0c\u662f\u8c03\u7528\u8005 main \u7684\u4e8b\uff0c\u6b63\u5982\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4e0d\u4f1a\u7ba1\u4f60\u7528\u4ed6\u63d0\u4f9b\u7684\u7535\u6765\u5439\u7a7a\u8c03\u8fd8\u662f\u73a9\u6e38\u620f\u4e00\u6837\uff0c\u53ea\u8981\u4e0d\u59a8\u788d\u5230\u5176\u4ed6\u5c45\u6c11\u7684\u6b63\u5e38\u7528\u7535\u3002 int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum of a = {}\", sum(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"sum of b = {}\", sum(b)); return 0; } \u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8bf4\u7684 \u804c\u8d23\u5355\u4e00\u539f\u5219 \u3002 \u4e8c\u6b21\u5c01\u88c5 \u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e2a\u51fd\u6570 average\uff0c\u4ed6\u53ef\u4ee5\u57fa\u4e8e sum \u5b9e\u73b0\uff1a int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } double average(std::vector const &v) { return (double)sum(v) / v.size(); } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"average of a = {}\", average(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"average of b = {}\", average(b)); return 0; } \u8fdb\u4e00\u6b65\u5c01\u88c5\u4e00\u4e2a\u6253\u5370\u6570\u7ec4\u6240\u6709\u7edf\u8ba1\u5b66\u4fe1\u606f\u7684\u51fd\u6570\uff1a void print_statistics(std::vector const &v) { if (v.empty()) { fmt::println(\"this is empty...\"); } else { fmt::println(\"sum: {}\", sum(v)); fmt::println(\"average: {}\", average(v)); fmt::println(\"min: {}\", min(v)); fmt::println(\"max: {}\", max(v)); } } int main() { std::vector a = {1, 2, 3, 4}; print_statistics(a); std::vector b = {5, 6, 7, 8}; print_statistics(b); return 0; } \u66b4\u9732 API \u65f6\uff0c\u8981\u540c\u65f6\u63d0\u4f9b\u5e95\u5c42\u7684 API \u548c\u9ad8\u5c42\u5c01\u88c5\u7684 API\u3002\u7528\u6237\u5982\u679c\u60f3\u8981\u63a7\u5236\u66f4\u591a\u7ec6\u8282\u53ef\u4ee5\u8c03\u7528\u5e95\u5c42 API\uff0c\u60f3\u8981\u7701\u4e8b\u7684\u7528\u6237\u53ef\u4ee5\u8c03\u7528\u9ad8\u5c42\u5c01\u88c5\u597d\u7684 API\u3002 \u9ad8\u5c42\u5c01\u88c5 API \u5e94\u5f53\u53ef\u4ee5\u5b8c\u5168\u901a\u8fc7\u8c03\u7528\u5e95\u5c42 API \u5b9e\u73b0\uff0c\u63d0\u4f9b\u9ad8\u5c42 API \u53ea\u662f\u65b9\u4fbf\u521d\u7ea7\u7528\u6237\u4f7f\u7528\u548c\u7406\u89e3\u3002 \u4f8b\u5982 libcurl \u5c31\u63d0\u4f9b\u4e86 curl_easy \u548c curl_multi \u4e24\u5957 API\u3002 - `curl_multi` \u63d0\u4f9b\u4e86\u8d85\u8be6\u7ec6\u7684\u53c2\u6570\uff0c\u628a\u6bcf\u4e2a\u64cd\u4f5c\u5206\u62c6\u6210\u591a\u6b65\uff0c\u65b9\u4fbf\u7528\u6237\u63d2\u624b\u7ec6\u8282\uff0c\u6ee1\u8db3\u9ad8\u7ea7\u7528\u6237\u7684\u5b9a\u5236\u5316\u9700\u6c42\uff0c\u4f46\u592a\u8fc7\u590d\u6742\uff0c\u96be\u4ee5\u5b66\u4e60\u3002 - `curl_easy` \u662f\u5bf9 `curl_multi` \u7684\u518d\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684 API\uff0c\u4f46\u662f\u5bf9\u5177\u4f53\u7ec6\u8282\u5c31\u96be\u4ee5\u64cd\u63a7\u4e86\uff0c\u9002\u5408\u521d\u5b66\u8005\u4e0a\u624b\u3002 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c Linux \u5185\u6838\u4e3a\u4ec0\u4e48\u575a\u6301\u4f7f\u7528 TAB=8 \u4e3a\u4ee3\u7801\u98ce\u683c\uff1f TODO\uff1a\u8fd8\u5728\u5199 \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u4f60\u4ea7\u751f\u4e86\u4e24\u4e2a\u9700\u6c42\uff0c\u5206\u522b\u5c01\u88c5\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a sum \u6c42\u6240\u6709\u5143\u7d20\u7684\u548c product \u6c42\u6240\u6709\u5143\u7d20\u7684\u79ef int sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret += v[i]; } return ret; } int product(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret *= v[i]; } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", sum(a)); fmt::println(\"product: {}\", product(a)); return 0; } \u6ce8\u610f\u5230 sum \u548c product \u7684\u5185\u5bb9\u51e0\u4e4e\u5982\u51fa\u4e00\u8f99\uff0c\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff1a sum \u7684\u5faa\u73af\u4f53\u4e3a += \uff1b product \u7684\u5faa\u73af\u4f53\u4e3a *= \u3002 \u8fd9\u79cd\u51fd\u6570\u4f53\u5185\u6709\u90e8\u5206\u4ee3\u7801\u91cd\u590d\uff0c\u4f46\u53c8\u6709\u7279\u5b9a\u90e8\u5206\u4e0d\u540c\uff0c\u96be\u4ee5\u62bd\u79bb\u3002 \u8be5\u600e\u4e48\u590d\u7528\u8fd9\u91cd\u590d\u7684\u90e8\u5206\u4ee3\u7801\u5462\uff1f \u6211\u4eec\u8981\u628a sum \u548c product \u5408\u5e76\u6210\u4e00\u4e2a\u51fd\u6570 generic_sum \u3002\u7136\u540e\u901a\u8fc7\u51fd\u6570\u53c2\u6570\uff0c\u628a\u5dee\u5f02\u90e8\u5206\uff080\u3001 += \uff09\u201c\u6ce8\u5165\u201d\u5230\u4e24\u4e2a\u51fd\u6570\u539f\u672c\u4e0d\u540c\u5730\u65b9\u3002 \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u5982\u4f55\u8868\u793a\u6211\u8fd9\u4e2a\u51fd\u6570\u662f\u8981\u505a\u6c42\u548c += \u8fd8\u662f\u6c42\u79ef *= \uff1f \u8ba9\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\uff1a enum Mode { ADD, // \u6c42\u548c\u64cd\u4f5c MUL, // \u6c42\u79ef\u64cd\u4f5c }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { // \u51fd\u6570\u5185\u5224\u65ad\u679a\u4e3e\uff0c\u51b3\u5b9a\u8981\u505a\u4ec0\u4e48\u64cd\u4f5c ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", generic_sum(a, ADD)); // \u7528\u6237\u6307\u5b9a\u4ed6\u60f3\u8981\u7684\u64cd\u4f5c fmt::println(\"product: {}\", generic_sum(a, MUL)); return 0; } \u7136\u800c\uff0c\u5982\u679c\u7528\u6237\u73b0\u5728\u60f3\u8981\u6c42\u6570\u7ec4\u7684 \u6700\u5927\u503c \u5462\uff1f \u679a\u4e3e\u4e2d\u8fd8\u6ca1\u6709\u5b9e\u73b0\u6700\u5927\u503c\u7684\u64cd\u4f5c\u2026\u2026\u8981\u652f\u6301\uff0c\u5c31\u5f97\u624b\u5fd9\u811a\u4e71\u5730\u53bb\u4fee\u6539 generic_sum \u51fd\u6570\u548c Mode \u679a\u4e3e\u539f\u672c\u7684\u5b9a\u4e49\uff0c\u771f\u9ebb\u70e6\uff01 enum Mode { ADD, MUL, MAX, // ***\u6539*** }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } else if (mode == MAX) { // ***\u6539*** ret = std::max(ret, v[i]); // ***\u6539*** } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, MAX); // ***\u6539*** return 0; } \u6211\u7528 // ***\u6539*** \u6307\u793a\u4e86\u6240\u6709\u9700\u8981\u6539\u52a8\u7684\u5730\u65b9\u3002 \u4e3a\u4e86\u589e\u52a0\u4e00\u4e2a\u6c42\u6700\u5927\u503c\u7684\u64cd\u4f5c\uff0c\u5c31\u9700\u8981\u4e09\u5904\u5206\u6563\u5728\u5404\u5730\u7684\u6539\u52a8\uff01 \u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u5bb9\u6613\u6284\u6f0f\uff0c\u6284\u9519\uff0c\u6bd4\u5982 MAX \u4e0d\u5c0f\u5fc3\u6253\u9519\u6210 MUL \u4e86\uff0c\u81ea\u5df1\u5374\u6ca1\u53d1\u73b0\uff0c\u7559\u4e0b BUG \u9690\u60a3\u3002 \u8fd9\u6837\u5199\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u5fc3\u667a\u8d1f\u62c5\u6781\u5927\uff0c\u6574\u5929\u5c31\u63d0\u5fc3\u540a\u80c6\u7740\u4e1c\u4e00\u5757\uff0c\u897f\u4e00\u5757\u7684\u6563\u88c5\u4ee3\u7801\uff0c\u62c5\u5fc3\u7740\u6709\u6ca1\u6709\u54ea\u4e2a\u5730\u65b9\u5199\u9519\u5199\u6f0f\uff0c\u4e25\u91cd\u59a8\u788d\u4e86\u5f00\u53d1\u6548\u7387\u3002 \u5e76\u4e14\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e0d\u80fd\u9002\u5e94\u9700\u6c42\u7684\u53d8\u5316\uff1a\u5047\u5982\u6211\u9700\u8981\u652f\u6301 MIN \u5462\uff1f\u53c8\u5f97\u6539\u4e09\u4e2a\u5730\u65b9\uff01\u8fd9\u8fdd\u80cc\u4e86\u8bbe\u8ba1\u6a21\u5f0f\u7684 \u5f00\u95ed\u539f\u5219 \u3002 \u5f00\u95ed\u539f\u5219: \u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\u3002\u6307\u7684\u662f\u8f6f\u4ef6\u5728\u9002\u5e94\u9700\u6c42\u53d8\u5316\u65f6\uff0c\u5e94\u5c3d\u91cf\u901a\u8fc7 \u6269\u5c55\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 \u4fee\u6539\u5df2\u6709\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\u3002 \u4f7f\u7528\u679a\u4e3e\u548c if-else \u5b9e\u73b0\u591a\u6001\uff0c\u96be\u4ee5\u6269\u5c55\uff0c\u8fd8\u8981\u4e00\u76f4\u53bb\u4fee\u6539\u539f\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u5c31\u8fdd\u80cc\u4e86 \u5f00\u95ed\u539f\u5219 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u5982\u679c\u6211\u4eec\u53ef\u4ee5\u201c\u6ce8\u5165\u201d\u4ee3\u7801\u5c31\u597d\u4e86\uff01\u80fd\u5426\u628a\u4e00\u6bb5\u201c\u4ee3\u7801\u201d\u4f5c\u4e3a generic_sum \u51fd\u6570\u7684\u53c2\u6570\u5462\uff1f \u4ee3\u7801\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u51fd\u6570\uff0c\u6ce8\u5165\u4ee3\u7801\u5c31\u662f\u6ce8\u5165\u51fd\u6570\u3002\u6211\u4eec\u5148\u5b9a\u4e49\u51fa\u4e09\u4e2a\u4e0d\u540c\u64cd\u4f5c\u5bf9\u5e94\u7684\u51fd\u6570\uff1a int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } \u7136\u540e\uff0c\u628a\u8fd9\u4e09\u4e2a\u5c0f\u51fd\u6570\uff0c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5927\u51fd\u6570 generic_sum \u7684\u53c2\u6570\u5c31\u884c\uff01 int generic_sum(std::vector const &v, auto op) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { // \u51fd\u6570\u4f5c\u8005\u65e0\u9700\u4e86\u89e3\u7528\u6237\u6307\u5b9a\u7684\u201c\u64cd\u4f5c\u201d\u5177\u4f53\u662f\u4ec0\u4e48 // \u53ea\u9700\u8981\u8c03\u7528\u8fd9\u4e00\u201c\u64cd\u4f5c\u201d\uff0c\u5f97\u5230\u7ed3\u679c\u5c31\u884c ret = op(ret, v[i]); } return ret; } int main() { std::vector a = {1, 2, 3, 4}; // \u7528\u6237\u65e0\u9700\u5173\u5fc3\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u4ec0\u4e48 // \u53ea\u9700\u968f\u5fc3\u6240\u6b32\u6307\u5b9a\u4ed6\u7684\u201c\u64cd\u4f5c\u201d\u4f5c\u4e3a\u53c2\u6570 generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u8d23\u4efb\u660e\u786e\u4e86\uff0c\u6211\u4eec\u6210\u529f\u628a\u4e00\u90e8\u5206\u7ec6\u8282\u4ece generic_sum \u4e2d\u8fdb\u4e00\u6b65\u62bd\u79bb\u3002 \u5e93\u4f5c\u8005 generic_sum \u4e0d\u5fc5\u4e86\u89e3 main \u7684\u64cd\u4f5c\u5177\u4f53\u662f\u4ec0\u4e48\uff0c\u4ed6\u53ea\u8d1f\u8d23\u5229\u7528\u8fd9\u4e2a\u64cd\u4f5c\u6c42\u201c\u548c\u201d\u3002 \u5e93\u7528\u6237 main \u4e0d\u5fc5\u4e86\u89e3 generic_sum \u5982\u4f55\u5b9e\u73b0\u64cd\u4f5c\u7d2f\u52a0\uff0c\u4ed6\u53ea\u7ba1\u6ce8\u5165\u201c\u5982\u4f55\u64cd\u4f5c\u201d\u7684\u4ee3\u7801\uff0c\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u3002 \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 int generic_sum(std::vector const &v, auto op) P } \u8fd9\u91cc\u7684\u53c2\u6570 op \u7c7b\u578b\u58f0\u660e\u4e3a auto\uff0c\u6548\u679c\u5c31\u662f\uff0cop \u8fd9\u4e2a\u53c2\u6570\u73b0\u5728\u80fd\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u4e86\uff08\u5305\u62ec\u51fd\u6570\uff01\uff09 int generic_sum(std::vector const &v, auto op) { ... } \u51c6\u786e\u7684\u8bf4\uff0c auto op \u53c2\u6570\u7684\u6548\u679c\u662f\u4f7f generic_sum \u53d8\u4e3a\u4e00\u4e2a \u6a21\u677f\u51fd\u6570 \uff0c\u5176\u4e2d op \u53c2\u6570\u53d8\u6210\u4e86\u6a21\u677f\u53c2\u6570\uff0c\u80fd\u591f\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u4e86\u3002\u800c\u5199\u660e\u7c7b\u578b\u7684\u53c2\u6570 std::vector const &v \u5c31\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u6548\u679c\uff0c\u5c31\u53ea\u80fd\u63a5\u53d7 vector \u800c\u5df2\u3002 \u5982\u679c\u4f60\u4e0d\u652f\u6301 C++20 \u7684\u8bdd\uff0c\u9700\u8981\u663e\u5f0f\u5199\u51fa template \uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff1a template int generic_sum(std::vector const &v, Op op) { ... } C++11\uff1aauto \u53ea\u80fd\u7528\u4e8e\u5b9a\u4e49\u53d8\u91cf\uff1bC++14\uff1a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u662f auto\uff1bC++17\uff1a\u6a21\u677f\u53c2\u6570\u4e5f\u53ef\u4ee5 auto\uff1bC++20\uff1a\u51fd\u6570\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f auto \u4e86\uff1b\uff08\u72c2\u60f3\uff09C++47\uff1aauto \u73b0\u5728\u662f C++47 \u7684\u552f\u4e00\u5173\u952e\u5b57\uff0c\u7528\u6237\u53ea\u9700\u4e0d\u65ad\u8f93\u5165 auto-auto-auto\uff0c\u7f16\u8bd1\u5668\u5185\u5efa\u4eba\u5de5\u667a\u80fd\u81ea\u52a8\u8bc6\u522b\u4f60\u7684\u610f\u56fe\u751f\u6210\u673a\u5668\u7801\u3002 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 \u5728\u8fc7\u53bb\u7684 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f \u4e2d\uff0c\u51fd\u6570\uff08\u4ee3\u7801\uff09\u548c\u5bf9\u8c61\uff08\u6570\u636e\uff09\u88ab \u5272\u88c2 \u5f00\u6765\uff0c\u4ed6\u4eec\u611a\u6627\u5730\u8ba4\u4e3a \u51fd\u6570\u4e0d\u662f\u5bf9\u8c61 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f \u5219\u8ba4\u4e3a\uff1a \u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u53d8\u91cf\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\uff01 Function lives matter! \u9762\u5411\u5bf9\u8c61\u5c31\u597d\u6bd4\u8ba1\u7b97\u673a\u7684\u201c\u54c8\u4f5b\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u548c\u6570\u636e\u5272\u88c2\uff0c\u4ee3\u7801\u53ea\u80fd\u5355\u65b9\u9762\u64cd\u4f5c\u6570\u636e\u3002\u51fd\u6570\u5f0f\u5c31\u597d\u6bd4\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u4e5f\u662f\u6570\u636e\u3002\u770b\u4f3c\u4f1a\u5bfc\u81f4\u4f4e\u6548\uff0c\u5b9e\u5219\u5927\u5927\u65b9\u4fbf\u4e86\u52a8\u6001\u52a0\u8f7d\u65b0\u7a0b\u5e8f\uff0c\u56e0\u800c\u73b0\u5728\u7684\u8ba1\u7b97\u673a\u57fa\u672c\u90fd\u91c7\u7528\u4e86\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\u3002 \u603b\u4e4b\uff0c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u88ab\u4eb2\u5207\u5730\u5c0a\u79f0\u4e3a \u51fd\u6570\u5bf9\u8c61 \u3002 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 C++98 \u65f6\u4ee3\uff0c\u4eba\u4eec\u8fd8\u9700\u8981\u5355\u72ec\u8dd1\u5230 main \u5916\u9762\uff0c\u4e13\u95e8\u5b9a\u4e49 add \u3001 mul \u3001 max \u51fd\u6570\u3002\u5f04\u5f97\u6574\u4e2a\u4ee3\u7801\u4e71\u54c4\u54c4\u7684\uff0c\u975e\u5e38\u9ebb\u70e6\u3002 int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } C++11 \u5f15\u5165\u4e86 Lambda \u8868\u8fbe\u5f0f \u8bed\u6cd5\uff0c\u5141\u8bb8\u4f60\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002 int main() { std::vector a = {1, 2, 3, 4}; auto add = [](int a, int b) { return a + b; }; auto mul = [](int a, int b) { return a * b; }; auto max = [](int a, int b) { return std::max(a, b); }; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u4e0d\u7528\u5f80 main \u5916\u9762\u585e\u5783\u573e\u4e86\uff0c\u4e00\u6e05\u723d\u3002 \u66f4\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u751a\u81f3\u4e0d\u7528\u5b9a\u4e49\u53d8\u91cf\uff0c\u76f4\u63a5\u628a Lambda \u8868\u8fbe\u5f0f\u5199\u5728 generic_sum \u7684\u53c2\u6570\u91cc\u5c31\u884c\u4e86\uff01 int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); // ***\u6539*** return 0; } \u4ee5\u4e0a\u5199\u6cd5\u90fd\u662f\u7b49\u4ef7\u7684\u3002 \u8981\u652f\u6301\u4e00\u4e2a\u65b0\u64cd\u4f5c\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5730\u65b9\uff1a\u5728\u8c03\u7528 generic_sum \u65f6\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002\u968f\u53eb\u968f\u5230\uff0c\u4e0d\u7528\u7ea0\u7ed3\u4e8e\u201c\u8d77\u540d\u5f3a\u8feb\u75c7\u201d\uff0c\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u5462\uff1f \u51c6\u786e\u7684\u8bf4\uff0cLambda \u521b\u5efa\u7684\u662f\u51fd\u6570\u5bf9\u8c61 (function object) \u6216\u79f0\u4eff\u51fd\u6570 (functor) \u800c\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u3002 \u5176\u5b9e C++98 \u65f6\u4ee3\u4eba\u4eec\u5c31\u5df2\u7ecf\u5927\u91cf\u5728\u7528 operator()() \u6a21\u62df\u51fd\u6570\u5bf9\u8c61\u4e86\uff0c\u8457\u540d\u7684\u7b2c\u4e09\u65b9\u5e93 Boost \u4e5f\u5c01\u88c5\u4e86\u5404\u79cd\u51fd\u6570\u5f0f\u5e38\u7528\u7684\u5bb9\u5668\u548c\u5de5\u5177\u3002C++11 \u624d\u7ec8\u4e8e\u628a \u51fd\u6570\u5bf9\u8c61 \u8fd9\u4e2a\u6982\u5ff5\u8f6c\u6b63\uff0c\u5e76\u5f15\u5165\u4e86\u66f4\u65b9\u4fbf\u7684 Lambda \u8bed\u6cd5\u7cd6\u3002 \u5373\u4f7f\u662f\u9762\u5411\u5bf9\u8c61\u7684\u5934\u53f7\u5b5d\u5b50 Java\uff0c\u4e5f\u5df2\u7ecf\u5f00\u59cb\u5f15\u5165\u51fd\u6570\u5f0f\u7684 Lambda \u8bed\u6cd5\u7cd6\uff0cC# \u7684 LINQ \u66f4\u662f\u660e\u76ee\u5f20\u80c6\u7684\u81f4\u656c map-reduce \u5168\u5bb6\u6876\uff0c\u751a\u81f3 C \u8bed\u8a00\u7528\u6237\u4e5f\u5f00\u59cb\u73a9\u5404\u79cd\u51fd\u6570\u6307\u9488\u56de\u8c03\u2026\u2026\u6ca1\u529e\u6cd5\uff0c\u51fd\u6570\u5f0f\u786e\u5b9e\u65b9\u4fbf\u5440\uff01 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u51fd\u6570\u5bf9\u8c61 op \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8ba9 generic_sum \u5185\u90e8\u53bb\u8c03\u7528\uff0c\u5c31\u50cf\u5f80 generic_sum \u4f53\u5185\u201c\u6ce8\u5165\u201d\u4e86\u4e00\u6bb5\u81ea\u5b9a\u4e49\u4ee3\u7801\u4e00\u6837\u3002 \u8fd9\u53ef\u4ee5\u8ba9 generic_sum \u5728\u4e0d\u4fee\u6539\u672c\u4f53\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u201c\u6ce8\u5165\u201d\u90e8\u5206\uff0c\u8f7b\u677e\u6269\u5c55\uff0c\u6ee1\u8db3 \u5f00\u95ed\u539f\u5219 \u3002 \u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u8fd9\u4f53\u73b0\u7684\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8981\u6c42\u7684 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u3002 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219: \u4e00\u4e2a\u5c01\u88c5\u597d\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u56db\u5927\u7f16\u7a0b\u8303\u5f0f\u90fd\u5404\u81ea\u53d1\u5c55\u51fa\u4e86 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u7684\u89e3\u51b3\u65b9\u6848\uff1a \u9762\u5411\u8fc7\u7a0b\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u6307\u9488 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u865a\u51fd\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u5bf9\u8c61 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u6a21\u677f\u5143\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u6a21\u677f\u53c2\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u540c\u6837\u662f\u628a\u62bd\u8c61\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u6837\u89e3\u51b3\u53ef\u6269\u5c55\u95ee\u9898\u3002 \u51fd\u6570\u6307\u9488\u8d34\u8fd1\u5e95\u5c42\u786c\u4ef6\uff0c\u865a\u51fd\u6570\u65b9\u4fbf\u6574\u5408\u591a\u4e2a\u63a5\u53e3\uff0c\u51fd\u6570\u5bf9\u8c61\u8f7b\u91cf\u7ea7\u3001\u968f\u5730\u53d6\u7528\uff0c\u6a21\u677f\u5143\u6709\u52a9\u9ad8\u6027\u80fd\u4f18\u5316\uff0c\u4e0d\u540c\u7684\u7f16\u7a0b\u8303\u5f0f\u6b8a\u9014\u540c\u5f52\u3002 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4f9d\u8d56\u6ce8\u5165\u539f\u5219\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u8026\u5408\u5ea6: \u6307\u7684\u662f\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u548c\u5176\u4ed6\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u8026\u5408\u5ea6\u8d8a\u4f4e\uff0c\u8d8a\u5bb9\u6613\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3001\u91cd\u6784\u3001\u590d\u7528\u548c\u6269\u5c55\u3002 \u9ad8\u8026\u5408\u5ea6\u7684\u5178\u578b\u662f\u201c\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab\u201d\u3002\u4f4e\u8026\u5408\u7684\u5178\u8303\u662f\u86af\u8693\uff0c\u56e0\u4e3a\u86af\u8693\u53ef\u4ee5\u5728\u4efb\u610f\u65ad\u9762\u5207\u5f00\uff0c\u8fd8\u80fd\u6d3b\u4e0b\u6765\uff0c\u770b\u6765\u86af\u8693\u7684\u8eab\u4f53\u8bbe\u8ba1\u975e\u5e38\u201c\u6a21\u5757\u5316\u201d\u5462\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u8f6f\u4ef6\u5e94\u5f53\u8ffd\u6c42\u4f4e\u8026\u5408\u5ea6\uff0c\u9002\u5ea6\u89e3\u8026\u7684\u8f6f\u4ef6\u80fd\u66f4\u5feb\u9002\u5e94\u9700\u6c42\u53d8\u5316\u3002\u4f46\u8fc7\u5ea6\u7684\u4f4e\u8026\u5408\u4e5f\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fc7\u4e8e\u5206\u6563\uff0c\u4e0d\u6613\u9605\u8bfb\u548c\u4fee\u6539\uff0c\u751a\u81f3\u53ef\u80fd\u8d77\u5230\u53cd\u6548\u679c\u3002 \u82e5\u4f60\u89e3\u8026\u540e\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u5316\u8981\u6539\u52a8\u7684\u5730\u65b9\u53d8\u5c11\u4e86\uff0c\u90a3\u5c31\u662f\u5408\u7406\u7684\u89e3\u8026\u3002\u82e5\u4f60\u8fc7\u5206\u89e3\u8026\uff0c\u4ee3\u7801\u4e1c\u4e00\u5757\u897f\u4e00\u5757\uff0c\u4ee5\u81f3\u4e8e\u9700\u6c42\u53d8\u5316\u65f6\u9700\u8981\u5230\u5904\u6539\uff0c\u6bd4\u4e0d\u89e3\u8026\u65f6\u6d6a\u8d39\u7684\u65f6\u95f4\u8fd8\u8981\u591a\uff0c\u90a3\u5c31\u662f\u89e3\u8026\u8fc7\u5ea6\u3002 \u5b8c\u5168\u96f6\u8026\u5408\u7684\u7a0b\u5e8f\u6bcf\u4e2a\u51fd\u6570\u4e92\u4e0d\u8054\u7cfb\uff0c\u5c31\u50cf\u628a\u86af\u8693\u62c6\u6563\u6210\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u7ec6\u80de\u4e00\u6837\u3002\u8fde\u521d\u59cb\u9700\u6c42\u201c\u6d3b\u7740\u201d\u90fd\u5b9e\u73b0\u4e0d\u4e86\uff0c\u8c08\u4f55\u9002\u5e94\u9700\u6c42\u53d8\u5316\uff1f\u6240\u4ee5\u89e3\u8026\u4e5f\u5207\u52ff\u77eb\u6789\u8fc7\u6b63\u3002 \u4e3a\u4e86\u907f\u514d\u89e3\u8026\u77eb\u6789\u8fc7\u6b63\uff0c\u4eba\u4eec\u53c8\u63d0\u51fa\u4e86\u5185\u805a\u7684\u6982\u5ff5\uff0c\u5e76\u89c4\u5b9a\u89e3\u8026\u7684\u524d\u63d0\u662f\uff1a\u4e0d\u803d\u8bef\u5185\u805a\u3002\u803d\u8bef\u5230\u5185\u805a\u7684\u89e3\u8026\uff0c\u5c31\u53ea\u4f1a\u8d77\u5230\u964d\u4f4e\u53ef\u7ef4\u62a4\u6027\u7684\u53cd\u6548\u679c\u4e86\u3002 \u5185\u805a: \u6307\u7684\u662f\u540c\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u5185\u90e8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u5185\u805a\u5ea6\u8d8a\u9ad8\uff0c\u529f\u80fd\u8d8a\u72ec\u7acb\uff0c\u8d8a\u65b9\u4fbf\u96c6\u4e2d\u7ef4\u62a4\u3002 \u4f8b\u5982\uff0c\u4eba\u7684\u5fc3\u810f\u4e13\u95e8\u8d1f\u8d23\u6cf5\u8840\uff0c\u809d\u810f\u53ea\u8d1f\u8d23\u89e3\u6bd2\uff0c\u8fd9\u5c31\u662f\u9ad8\u5185\u805a\u7684\u4eba\u4f53\u5668\u5b98\u3002\u82e5\u4eba\u7684\u5fc3\u810f\u8fd8\u8981\u517c\u804c\u89e3\u6bd2\uff0c\u809d\u810f\u8fd8\u517c\u804c\u6cf5\u8840\uff0c\u770b\u4f3c\u597d\u50cf\u662f\u589e\u52a0\u4e86\u201c\u4e07\u4e00\u5fc3\u810f\u574f\u6389\u201d\u7684\u5197\u4f59\u6027\uff0c\u5b9e\u9645\u4e0a\u628a\u201c\u6cf5\u8840\u201d\u8fd9\u4e00\u529f\u80fd\u62c6\u6563\u5230\u5404\u5730\uff0c\u65e0\u6cd5\u201c\u96c6\u4e2d\u529b\u91cf\u6cf5\u5927\u8840\u201d\u4e86\u3002 \u4eba\u7c7b\u7684\u5927\u8111\u548c CPU \u4e00\u6837\uff0c\u4e5f\u6709\u201c\u7f13\u5b58\u5c40\u57df\u6027 (cache-locality)\u201d\u7684\u9650\u5236\uff1a\u4e0d\u80fd\u540c\u65f6\u5728\u5f88\u591a\u4e2a\u4e3b\u9898\u4e4b\u95f4\u5feb\u901f\u5207\u6362\uff0c\u65e0\u8bba\u662f\u65f6\u95f4\u4e0a\u7684\u8fd8\u662f\u7a7a\u95f4\u4e0a\u7684\u5272\u88c2 (cache-miss)\uff0c\u90fd\u4f1a\u5e72\u6270\u7a0b\u5e8f\u5458\u601d\u7ef4\u7684\u8fde\u8d2f\u6027\uff0c\u4ece\u800c\u589e\u5927\u5fc3\u667a\u8d1f\u62c5\u3002 \u597d\u7684\u8f6f\u4ef6\u8981\u4fdd\u6301\u4f4e\u8026\u5408\uff0c\u540c\u65f6\u9ad8\u5185\u805a\u3002 \u5c31\u50cf\u201c\u6c11\u4e3b\u96c6\u4e2d\u5236\u201d\u4e00\u6837\uff0c\u65e2\u8981\u76d1\u7763\u9632\u6b62\u5927\u6743\u72ec\u63fd\uff0c\u53c8\u8981\u96c6\u4e2d\u529b\u91cf\u529e\u4e00\u4e2a\u4eba\u529e\u4e0d\u6210\u7684\u5927\u4e8b\u3002 \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u4f20\u7edf\u7684\u9762\u5411\u5bf9\u8c61\u540c\u6837\u53ef\u4ee5\u7528 \u865a\u51fd\u6570\u63a5\u53e3\u7c7b \u6a21\u62df \u51fd\u6570\u5bf9\u8c61 \u4e00\u6837\u7684\u529f\u80fd\uff0c\u53ea\u4e0d\u8fc7\u6ca1\u6709 lambda \u548c\u95ed\u5305\u7684\u8bed\u6cd5\u52a0\u6301\uff0c\u5199\u8d77\u6765\u975e\u5e38\u7e41\u7410\uff0c\u5c31\u548c\u5728 C \u8bed\u8a00\u91cc\u201c\u6a21\u62df\u201d\u9762\u5411\u5bf9\u8c61\u4e00\u6837\u3002 \u4e3a\u4e86\u8fd9\u4e48\u5c0f\u7684\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u5355\u72ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5c31\u50cf\u5988\u5988\u5f00\u4e00\u67b6\u201c\u7a7a\u4e2d\u6218\u8f66\u201d A380 \u53ea\u662f\u4e3a\u4e86\u63a5\u4f60\u653e\u5b66\u4e00\u6837\uff0c\u7b49\u4f60\u503c\u597d\u673a\u7684\u65f6\u95f4\u6211\u81ea\u5df1\u8d70\u90fd\u8d70\u5230\u4e86\u3002\u800c\u51fd\u6570\u5f0f\u4e2d\uff0c\u7528 lambda \u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e\u968f\u5730\u6293\u6765\u4e00\u53f0\u5171\u4eab\u5355\u8f66\u5f00\u8d70\u3002 struct OpBase { // \u9762\u5411\u5bf9\u8c61\uff1a\u9047\u4e8b\u4e0d\u51b3\u5148\u5b9a\u4e49\u63a5\u53e3\u2026\u2026 virtual int compute(int a, int b) = 0; virtual ~OpBase() = default; }; struct OpAdd : OpBase { int compute(int a, int b) override { return a + b; } }; struct OpMul : OpBase { int compute(int a, int b) override { return a * b; } }; struct OpMax : OpBase { int compute(int a, int b) override { return std::max(a, b); } }; int generic_sum(std::vector const &v, OpBase *op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op->compute(ret, v[i]); // \u5199\u8d77\u6765\u4e5f\u9ebb\u70e6\uff0c\u9700\u8981\u8c03\u7528\u4ed6\u7684\u6210\u5458\u51fd\u6570\uff0c\u6210\u5458\u51fd\u6570\u53c8\u8981\u8d77\u540d\u2026\u2026 } delete op; return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, new OpAdd()); generic_sum(a, new OpMul()); generic_sum(a, new OpMax()); return 0; } \u4e0d\u4ec5\u9700\u8981\u5b9a\u4e49\u4e00\u5806\u7c7b\uff0c\u63a5\u53e3\u7c7b\uff0c\u5b9e\u73b0\u7c7b\uff0c\u7ee7\u627f\u6765\u7ee7\u627f\u53bb\uff0c\u8fd8\u9700\u8981\u7ba1\u7406\u8ba8\u538c\u7684\u6307\u9488\uff0c\u4ee3\u7801\u91cf\u7ffb\u500d\uff0c\u6ca1\u4ec0\u4e48\u53ef\u8bfb\u6027\uff0c\u53c8\u5f71\u54cd\u8fd0\u884c\u6548\u7387\u3002 3 \u5e74 2 \u73ed\u5c0f\u5f6d\u540c\u5b66\uff0c\u4f60\u7684\u5988\u5988\u5f00\u7740 A380 \u6765\u63a5\u4f60\u4e86\u3002 \u800c\u73b0\u4ee3 C++ \u53ea\u9700 Lambda \u8bed\u6cd5\u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u723d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u521a\u521a\uff0c\u6211\u4eec\u7684\u5b9e\u73b0\u7528\u4e86 auto op \u505a\u53c2\u6570\uff0c\u8fd9\u7b49\u4ef7\u4e8e\u8ba9 generic_sum \u53d8\u6210\u4e00\u4e2a\u6a21\u677f\u51fd\u6570\u3002 int generic_sum(std::vector const &v, auto op); // \u4e0d\u652f\u6301 C++20 \u65f6\u7684\u66ff\u4ee3\u5199\u6cd5\uff1a template int generic_sum(std::vector const &v, Op op); \u8fd9\u610f\u5473\u7740\u6bcf\u5f53\u7528\u6237\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\uff08lambda\uff09\u65f6\uff0c generic_sum \u90fd\u4f1a\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u904d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u7f16\u8bd1\u540e\uff0c\u4f1a\u53d8\u6210\u7c7b\u4f3c\u4e8e\u8fd9\u6837\uff1a generic_sum(a); generic_sum(a); generic_sum(a); \u4f1a\u751f\u6210\u4e09\u4efd\u51fd\u6570\uff0c\u6bcf\u4e2a\u90fd\u662f\u72ec\u7acb\u7f16\u8bd1\u7684\uff1a int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = add(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = mul(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = max(ret, v[i]); } return ret; } \u8fd9\u5141\u8bb8\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e2a\u7248\u672c\u7684 generic_sum \u5355\u72ec\u505a\u4f18\u5316\uff0c\u91cf\u8eab\u5b9a\u5236\u6700\u4f18\u7684\u4ee3\u7801\u3002 \u4f8b\u5982 add \u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u56e0\u4e3a\u53ea\u5728 generic_sum \u4e2d\u4f7f\u7528\u4e86\uff0c\u4f1a\u88ab\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u5185\u8054\uff0c\u4e0d\u4f1a\u4ea7\u751f\u51fd\u6570\u8c03\u7528\u548c\u8df3\u8f6c\u7684\u6307\u4ee4\uff0c\u5404\u81ea\u4f18\u5316\u6210\u5355\u72ec\u4e00\u6761\u52a0\u6cd5 / \u4e58\u6cd5 / \u6700\u5927\u503c\u6307\u4ee4\u7b49\u3002 \u6bd4\u5982\uff0c\u7f16\u8bd1\u5668\u4f1a\u68c0\u6d4b\u5230 += \u53ef\u4ee5\u77e2\u91cf\u5316\uff0c\u4e8e\u662f\u7528 _mm_add_epi32 \u66ff\u4ee3\u4e86\u3002\u540c\u7406\uff0cmul \u5219\u7528 _mm_mullo_epi32 \u66ff\u4ee3\uff0cmax \u5219\u7528 _mm_max_epi32 \u66ff\u4ee3\u7b49\uff0c\u5404\u81ea\u5206\u522b\u751f\u6210\u4e86\u5404\u81ea\u7248\u672c\u6700\u4f18\u7684\u4ee3\u7801\u3002\u800c\u5982\u679c\u662f\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u4e0d\u4f1a\u751f\u6210\u4e09\u4efd\u91cf\u8eab\u5b9a\u505a\u7684\u5b9e\u4f8b\uff0c\u65e0\u6cd5\u77e2\u91cf\u5316\uff08\u6709\u4e00\u79cd\u4f8b\u5916\uff0c\u5c31\u662f\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u4e86 generic_sum \u4f3c\u4e4e\u53ea\u6709\u8fd9\u4e09\u79cd\u53ef\u80fd\u53c2\u6570\uff0c\u7136\u540e\u505a\u4e86 IPO \u4f18\u5316\uff0c\u4f46\u5e76\u4e0d\u5982\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u6837\u7a33\u5b9a\u5f3a\u5236\uff09\u3002 \u4e3a\u4e09\u79cd\u4e0d\u540c\u7684 op \u53c2\u6570\u5206\u522b\u5b9a\u505a\u4e09\u4efd\u3002\u867d\u7136\u589e\u52a0\u4e86\u7f16\u8bd1\u65f6\u95f4\uff0c\u81a8\u80c0\u4e86\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u4f53\u79ef\uff1b\u4f46\u751f\u6210\u7684\u673a\u5668\u7801\u662f\u5206\u522b\u9488\u5bf9\u6bcf\u79cd\u7279\u4f8b\u4e00\u5bf9\u4e00\u6df1\u5ea6\u4f18\u5316\u7684\uff0c\u66f4\u9ad8\u6548\u3002 \u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\uff08gemm\uff09\u7684\u6700\u4f18\u7b97\u6cd5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u77e9\u9635\u5927\u5c0f\u548c\u5f62\u72b6\u662f\u4e0d\u540c\u7684\u3002\u8457\u540d\u7684\u7ebf\u6027\u4ee3\u6570\u5e93 CUBLAS \u548c MKL \u4e2d\uff0c\u4f1a\u81ea\u52a8\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\u5f62\u72b6\uff0c\u9009\u53d6\u6700\u4f18\u7684\u7b97\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cCUBLAS \u5e93\u91cc\u5176\u5b9e\u5b58\u7740\u9002\u5408\u5404\u79cd\u77e9\u9635\u5927\u5c0f\u6392\u5217\u7ec4\u5408\u7684\u7b97\u6cd5\u4ee3\u7801\uff08\u4ee5 fatbin \u683c\u5f0f\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u4e2d\uff09\u3002\u5f53\u8c03\u7528\u77e9\u9635\u4e58\u6cd5\u65f6\uff0c\u81ea\u52a8\u67e5\u5230\u6700\u9002\u5408\u7684\u4e00\u7248\u6765\u8c03\u7528\u7ed9\u4f60\u3002\u7c7b\u4f3c gemm\uff0c\u8fd8\u6709 gemv\u3001spmv\u2026\u2026\u6240\u6709\u7684\u77e9\u9635\u8fd0\u7b97 API \u90fd\u7ecf\u5386\u4e86\u8fd9\u6837\u7684\u201c\u7f16\u8bd1\u671f\u201d\u66b4\u529b\u6392\u5217\u7ec4\u5408\uff0c\u53ea\u4e3a\u201c\u8fd0\u884c\u65f6\u201d\u91ca\u653e\u6700\u5927\u6027\u80fd\uff01\u8fd9\u4e5f\u5bfc\u81f4\u7f16\u8bd1\u597d\u7684 cublas.dll \u6587\u4ef6\u6765\u5230\u4e86\u6050\u6016\u7684 20 MB \u5de6\u53f3\uff0c\u800c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9ad8\u6548\u3002 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 Lambda \u51fd\u6570\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u533f\u540d\u7684\uff0c\u6bcf\u4e2a Lambda \u8868\u8fbe\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97 generic_sum \u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684 Lambda \u90fd\u4f1a\u5b9e\u4f8b\u5316\u4e00\u904d\u3002\u867d\u7136\u6709\u5229\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u4f46\u4e5f\u5f71\u54cd\u4e86\u7f16\u8bd1\u901f\u5ea6\u548c\u7075\u6d3b\u6027\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7 decltype(add) \u83b7\u53d6 add \u8fd9\u4e2a Lambda \u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4e5f\u53ea\u80fd\u901a\u8fc7 auto \u6765\u6355\u83b7 Lambda \u5bf9\u8c61\u4e3a\u53d8\u91cf\u3002 \u4e3a\u6b64\uff0c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::function \u5bb9\u5668\uff0c\u4ed6\u80fd\u5bb9\u7eb3\u4efb\u4f55\u51fd\u6570\u5bf9\u8c61\uff01\u65e0\u8bba\u662f\u533f\u540d\u7684 Lambda \u51fd\u6570\u5bf9\u8c61\uff0c\u8fd8\u662f\u666e\u666e\u901a\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u90fd\u80fd\u7eb3\u5165 std::function \u7684\u4f53\u5185\u3002 \u552f\u4e00\u7684\u4ee3\u4ef7\u662f\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u51fa\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002 \u4f8b\u5982\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e24\u4e2a int \uff0c std::function auto add_lambda = [](int a, int b) { // Lambda \u51fd\u6570\u5bf9\u8c61 return a + b; }; struct AddClass { int operator()(int a, int b) { // \u81ea\u5b9a\u4e49\u7c7b\u6a21\u62df\u51fd\u6570\u5bf9\u8c61 return a + b; } }; AddClass add_object; int add_regular_func(int a, int b) { // \u666e\u901a\u51fd\u6570 return a + b; } std::function add; // \u6240\u6709\u5e7f\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u7edf\u7edf\u63a5\u7eb3 add = add_lambda; // OK add = add_object; // OK add = add_regular_func; // OK int generic_sum(std::vector const &v, std::function op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op(ret, v[i]); // \u5199\u8d77\u6765\u548c\u6a21\u677f\u4f20\u53c2\u65f6\u4e00\u6837\u65e0\u611f } // \u65e0\u9700\u6307\u9488\uff0c\u65e0\u9700 delete\uff0cfunction \u80fd\u81ea\u52a8\u7ba1\u7406\u51fd\u6570\u5bf9\u8c61\u751f\u547d\u5468\u671f return ret; } \u5982\u679c\u8fd8\u60f3\u652f\u6301\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u770b std::function \u3002\u8fd9\u91cc std::any \u662f\u4e2a\u8d85\u7ea7\u4e07\u80fd\u5bb9\u5668\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u5bf9\u8c61\uff0c\u4ed6\u548c std::function \u4e00\u6837\u90fd\u91c7\u7528\u4e86\u201c\u7c7b\u578b\u64e6\u9664 (type-erasure)\u201d\u6280\u672f\uff0c\u7f3a\u70b9\u662f\u5fc5\u987b\u914d\u5408 std::any_cast \u624d\u80fd\u53d6\u51fa\u4f7f\u7528\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u8fdb\u9636\u4e13\u9898\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u4ed6\u4eec\u7684\u539f\u7406\uff0c\u5e76\u5e26\u4f60\u81ea\u5df1\u505a\u4e00\u4e2a\u64e6\u52a0\u6cd5\u7684\u7c7b\u578b\u64e6\u9664\u5bb9\u5668\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u80fd\u5728\u9759\u6001\u4e0e\u52a8\u6001\u4e4b\u95f4\u8f7b\u677e\u5207\u6362\uff0c \u9ad8\u6027\u80fd \u4e0e \u7075\u6d3b\u6027 \u4efb\u541b\u9009\u62e9\u3002 \u5728\u9700\u8981\u6027\u80fd\u7684 \u74f6\u9888\u4ee3\u7801 \u4e2d\u7528\u6a21\u677f\u4f20\u53c2\uff0c\u7f16\u8bd1\u671f\u9759\u6001\u5206\u53d1\uff0c\u591a\u6b21\u91cf\u8eab\u5b9a\u505a\uff0c\u63d0\u9ad8\u8fd0\u884c\u65f6\u6027\u80fd\u3002 \u74f6\u9888\u4ee3\u7801: \u5f80\u5f80\u4e00\u4e2a\u7a0b\u5e8f 80% \u7684\u65f6\u95f4\u82b1\u5728 20% \u7684\u4ee3\u7801\u4e0a\u3002\u8fd9 20% \u662f\u5728\u7a0b\u5e8f\u4e2d\u9891\u7e41\u6267\u884c\u7684\u3001\u8ba1\u7b97\u91cf\u5927\u7684\u3001\u6216\u8005\u8c03\u7528\u7279\u522b\u8017\u65f6\u7684\u51fd\u6570\u3002\u9488\u5bf9\u8fd9\u90e8\u5206\u74f6\u9888\u4ee3\u7801\u4f18\u5316\u5373\u53ef\uff0c\u800c\u5269\u4f59\u7684 80% \u6253\u9171\u6cb9\u4ee3\u7801\uff0c\u5927\u53ef\u4ee5\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u5199\u3002 \u5728\u6027\u80fd\u65e0\u5173\u7d27\u8981\u7684\u9876\u5c42\u4e1a\u52a1\u903b\u8f91\u4e2d\u7528 function \u5bb9\u5668\u4f20\u53c2\uff0c\u8fd0\u884c\u65f6\u52a8\u6001\u5206\u53d1\uff0c\u8282\u7701\u7f16\u8bd1\u4f53\u79ef\uff0c\u65b9\u4fbf\u6301\u4e45\u5b58\u50a8\uff0c\u7075\u6d3b\u6613\u7528\u3002 \u4f8b\u5982\u4e0a\u9762\u7684 generic_sum \u51fd\u6570\uff0c\u5982\u679c\u6211\u4eec\u7a81\u7136\u60f3\u8981\u9ad8\u6027\u80fd\u4e86\uff0c\u53ea\u9700\u628a std::function op \u8f7b\u8f7b\u6539\u4e3a auto op \u5c31\u8f7b\u677e\u5207\u6362\u5230\u9759\u6001\u5206\u53d1\u6a21\u5f0f\u4e86\u3002 \u800c\u865a\u51fd\u6570\u4e00\u65e6\u7528\u4e86\uff0c\u57fa\u672c\u5c31\u53ea\u80fd\u52a8\u6001\u5206\u53d1\u4e86\uff0c\u5373\u4f7f\u80fd\u88ab IPO \u4f18\u5316\u6389\uff0c\u865a\u8868\u6307\u9488\u4e5f\u6c38\u8fdc\u5360\u636e\u7740\u4e00\u4e2a 8 \u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u4e14\u6c38\u8fdc\u53ea\u80fd\u4ee5\u6307\u9488\u5f62\u5f0f\u4f20\u6765\u4f20\u53bb\u3002 \u4e00\u79cd\u9759\u6001\u5206\u53d1\u7248\u7684\u865a\u51fd\u6570\u66ff\u4ee3\u54c1\u662f CRTP\uff0c\u4ed6\u57fa\u4e8e\u6a21\u677f\u5143\u7f16\u7a0b\uff0c\u4f46\u4e0e\u865a\u51fd\u6570\u4e4b\u95f4\u5207\u6362\u56f0\u96be\uff0c\u4e0d\u50cf\u51fd\u6570\u5bf9\u8c61\u90a3\u4e48\u65e0\u611f\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u4e13\u9898\u8bfe\u4e2d\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u3002 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 mt_queue> task_queue; void thread1() { task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a11\"); }); task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a12\"); }); } void thread2() { while (true) { auto task = task_queue.pop(); task(); } } mt_queue \u662f\u5c0f\u5f6d\u8001\u5e08\u5c01\u88c5\u7684\u591a\u7ebf\u7a0b\u5b89\u5168\u7684\u6d88\u606f\u961f\u5217\uff0c\u5b9e\u73b0\u539f\u7406\u4f1a\u5728\u7a0d\u540e\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4e2d\u8be6\u7ec6\u8bb2\u89e3\u3002 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570 int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello(2, 3) \u7ed3\u679c\uff1a{}\", hello(2, 3)); fmt::println(\"main \u8c03\u7528 hello(2, 4) \u7ed3\u679c\uff1a{}\", hello(2, 4)); fmt::println(\"main \u8c03\u7528 hello(2, 5) \u7ed3\u679c\uff1a{}\", hello(2, 5)); return 0; } int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello2(3) \u7ed3\u679c\uff1a{}\", hello2(3)); fmt::println(\"main \u8c03\u7528 hello2(4) \u7ed3\u679c\uff1a{}\", hello2(4)); fmt::println(\"main \u8c03\u7528 hello2(5) \u7ed3\u679c\uff1a{}\", hello2(5)); return 0; }","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b"},{"location":"functional/#_1","text":"\u51fd\u6570\u5f0f\u7f16\u7a0b \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f \u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f \u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01 \u7528\u51fd\u6570\u5c01\u88c5 \u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408 \u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528 \u4e8c\u6b21\u5c01\u88c5 Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c \u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f \u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5 \u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a \u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6 \u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01 C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a \u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4 \u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1 \u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1 \u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217 \u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305 \u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389 bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b"},{"location":"functional/#_2","text":"int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum = {}\", s); return 0; } \u8fd9\u662f\u4e00\u4e2a\u8ba1\u7b97\u6570\u7ec4\u6c42\u548c\u7684\u7b80\u5355\u7a0b\u5e8f\u3002 \u4f46\u662f\uff0c\u4ed6\u53ea\u80fd\u8ba1\u7b97\u6570\u7ec4 a \u7684\u6c42\u548c\uff0c\u65e0\u6cd5\u590d\u7528\u3002 \u5982\u679c\u6211\u4eec\u6709\u53e6\u4e00\u4e2a\u6570\u7ec4 b \u4e5f\u9700\u8981\u6c42\u548c\u7684\u8bdd\uff0c\u5c31\u5f97\u628a\u6574\u4e2a\u6c42\u548c\u7684 for \u5faa\u73af\u91cd\u65b0\u5199\u4e00\u904d\uff1a int main() { std::vector a = {1, 2, 3, 4}; int s = 0; for (int i = 0; i < a.size(); i++) { s += a[i]; } fmt::println(\"sum of a = {}\", s); std::vector b = {5, 6, 7, 8}; s = 0; for (int i = 0; i < a.size(); i++) { s += b[i]; } fmt::println(\"sum of b = {}\", s); return 0; } \u8fd9\u5c31\u51fa\u73b0\u4e86\u7a0b\u5e8f\u8bbe\u8ba1\u7684\u5927\u5fcc\uff1a\u4ee3\u7801\u91cd\u590d\u3002 \u4f8b\u5982\uff0c\u4f60\u6709\u5439\u7a7a\u8c03\u7684\u9700\u6c42\uff0c\u548c\u5145\u624b\u673a\u7684\u9700\u6c42\u3002\u4f60\u4e3a\u4e86\u6ee1\u8db3\u8fd9\u4e24\u4e2a\u9700\u6c42\uff0c\u8d2d\u4e70\u4e86\u4e24\u53f0\u53d1\u7535\u673a\uff0c\u5206\u522b\u4e3a\u7a7a\u8c03\u548c\u624b\u673a\u4f9b\u7535\u3002\u7b2c\u4e8c\u5929\uff0c\u4f60\u53c8\u4ea7\u751f\u4e86\u73a9\u7535\u8111\u9700\u6c42\uff0c\u4e8e\u662f\u4f60\u53c8\u8d2d\u4e70\u4e00\u53f0\u53d1\u7535\u673a\uff0c\u4e13\u4e3a\u7535\u8111\u4f9b\u7535\u2026\u2026\u771f\u662f\u6d6a\u8d39\uff01 \u91cd\u590d\u7684\u4ee3\u7801\u4e0d\u4ec5\u5f71\u54cd\u4ee3\u7801\u7684 \u53ef\u8bfb\u6027 \uff0c\u4e5f\u589e\u52a0\u4e86 \u7ef4\u62a4 \u4ee3\u7801\u7684\u6210\u672c\u3002 \u770b\u8d77\u6765\u4e71\u7cdf\u7cdf\u7684\uff0c\u4fe1\u606f\u5bc6\u5ea6\u4f4e\uff0c\u8ba9\u4eba\u4e00\u773c\u770b\u4e0d\u51fa\u4ee3\u7801\u5728\u5e72\u4ec0\u4e48\u7684\u529f\u80fd \u5f88\u5bb9\u6613\u5199\u9519\uff0c\u770b\u8d70\u773c\uff0c\u96be\u8c03\u8bd5 \u590d\u5236\u7c98\u8d34\u8fc7\u7a0b\u4e2d\uff0c\u5bb9\u6613\u6f0f\u6539\uff0c\u6bd4\u5982\u8fd9\u91cc\u7684 s += b[i] \u53ef\u80fd\u5199\u6210 s += a[i] \u800c\u81ea\u5df1\u4e0d\u53d1\u73b0 \u6539\u8d77\u6765\u4e0d\u65b9\u4fbf\uff0c\u5f53\u6211\u4eec\u7684\u9700\u6c42\u53d8\u66f4\u65f6\uff0c\u9700\u8981\u591a\u5904\u4fee\u6539\uff0c\u6bd4\u5982\u5f53\u6211\u9700\u8981\u6539\u4e3a\u8ba1\u7b97\u4e58\u79ef\u65f6\uff0c\u9700\u8981\u628a\u4e24\u4e2a\u5730\u65b9\u90fd\u6539\u6210 s *= \u6539\u4e86\u4ee5\u540e\u53ef\u80fd\u6f0f\u6539\u4e00\u90e8\u5206\uff0c\u7559\u4e0b Bug \u9690\u60a3 \u654f\u6377\u5f00\u53d1\u9700\u8981\u53cd\u590d\u4fee\u6539\u4ee3\u7801\uff0c\u6bd4\u5982\u4f60\u6b63\u5728\u8c03\u8bd5 += \u548c -= \u7684\u533a\u522b\uff0c\u770b\u7ed3\u679c\u53d8\u5316\uff0c\u5982\u679c\u4e00\u6b21\u5207\u6362\u9700\u8981\u6539\u591a\u5904\uff0c\u5c31\u5f71\u54cd\u4e86\u8c03\u8bd5\u901f\u5ea6","title":"\u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\uff1f"},{"location":"functional/#_3","text":"\u5982\u679c\u4f60\u8fd8\u662f\u559c\u6b22\u201c\u4e00\u672c\u9053\u201d\u5199\u6cd5\u7684\u8bdd\uff0c\u4e0d\u59a8\u60f3\u60f3\u770b\uff0c\u5b8c\u5168\u4e0d\u7528\u4efb\u4f55\u6807\u51c6\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u7684\u51fd\u6570\u548c\u7c7b\uff0c\u628a fmt::println \u548c std::vector \u8fd9\u4e9b\u51fd\u6570\u5168\u90e8\u62c6\u89e3\u6210\u4e00\u4e2a\u4e2a\u7cfb\u7edf\u8c03\u7528\u3002\u90a3\u8fd9\u6574\u4e2a\u7a0b\u5e8f\u4f1a\u6709\u591a\u96be\u5199\uff1f int main() { #ifdef _WIN32 int *a = (int *)VirtualAlloc(NULL, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE); #else int *a = (int *)mmap(NULL, 4 * sizeof(int), PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); #endif a[0] = 1; a[1] = 2; a[2] = 3; a[3] = 4; int s = 0; for (int i = 0; i < 4; i++) { s += a[i]; } char buffer[64]; buffer[0] = 's'; buffer[1] = 'u'; buffer[2] = 'm'; buffer[3] = ' '; buffer[4] = '='; buffer[5] = ' '; // \u4f8b\u5982\uff0c\u5982\u679c\u8981\u4fee\u6539\u6b64\u5904\u7684\u63d0\u793a\u6587\u672c\uff0c\u751a\u81f3\u9700\u8981\u4fee\u6539\u540e\u9762\u7684 len \u53d8\u91cf... int len = 6; int x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif int *b = (int *)a; b[0] = 4; b[1] = 5; b[2] = 6; b[3] = 7; int s = 0; for (int i = 0; i < 4; i++) { s += b[i]; } len = 6; x = s; do { buffer[len++] = '0' + x % 10; x /= 10; } while (x); buffer[len++] = '\\n'; #ifdef _WIN32 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buffer, len, NULL, NULL); #else write(1, buffer, len); #endif #ifdef _WIN32 VirtualFree(a, 0, MEM_RELEASE); #else munmap(a); #endif return 0; } \u4e0d\u4ec5\u5b8c\u5168\u6ca1\u6709\u53ef\u8bfb\u6027\u3001\u53ef\u7ef4\u62a4\u6027\uff0c\u751a\u81f3\u90fd\u6ca1\u6709\u53ef\u79fb\u690d\u6027\u3002 \u9664\u975e\u4f60\u53ea\u5199\u5e94\u4ed8\u5bfc\u5e08\u7684\u201c\u4e00\u6b21\u6027\u201d\u7a0b\u5e8f\uff0c\u4e00\u65e6\u8981\u5b9e\u73b0\u590d\u6742\u7684\u4e1a\u52a1\u9700\u6c42\uff0c\u4e0d\u53ef\u907f\u514d\u7684\u8981\u81ea\u5df1\u5c01\u88c5\u51fd\u6570\u6216\u7c7b\u3002\u7f51\u4e0a\u6240\u6709\u9f13\u5439\u201c\u4e0d\u5c01\u88c5\u201d\u201c\u8bbe\u8ba1\u6a21\u5f0f\u662f\u9762\u5b50\u5de5\u7a0b\u201d\u7684\u53cd\u667a\u8a00\u8bba\uff0c\u90fd\u662f\u6ca1\u6709\u505a\u8fc7\u5927\u578b\u9879\u76ee\u7684\u3002","title":"\u72c2\u60f3\uff1a\u6ca1\u6709\u51fd\u6570\u7684\u4e16\u754c\uff1f"},{"location":"functional/#_4","text":"\u5f88\u591a\u8bbe\u8ba1\u6a21\u5f0f\u6559\u6750\u7247\u9762\u5f3a\u8c03 \u53ef\u8bfb\u6027 \uff0c\u4eff\u4f5b\u8bbe\u8ba1\u6a21\u5f0f\u5c31\u662f\u4e3a\u4e86\u201c\u4f18\u96c5\u201d\u201c\u9ad8\u5927\u4e0a\u201d\u201c\u7f8e\u5b66\u201d\uff1f\u4f7f\u5f97\u5f88\u591a\u4eba\u8ba4\u4e3a\uff0c\u201c\u6211\u8fd9\u4e2a\u662f\u81ea\u5df1\u7684\u9879\u76ee\uff0c\u4e0d\u7528\u7f8e\u5316\u7ed9\u9886\u5bfc\u770b\u201d\u800c\u62d2\u7edd\u8bbe\u8ba1\u6a21\u5f0f\u3002\u5b9e\u9645\u4e0a\u8bbe\u8ba1\u6a21\u5f0f\u7684\u4e3b\u8981\u4ef7\u503c\u5728\u4e8e \u65b9\u4fbf\u540e\u7eed\u4fee\u6539 *\uff01 \u4f8b\u5982 B \u7ad9\u4ee5\u524d\u53ea\u652f\u6301\u4e0a\u4f20\u666e\u901a\u89c6\u9891\uff0c\u73b0\u5728\u53d4\u53d4\u7a81\u7136\u63d0\u51fa\uff1a\u8981\u652f\u6301\u4e92\u52a8\u89c6\u9891\uff0c\u5145\u7535\u89c6\u9891\uff0c\u89c6\u9891\u5408\u96c6\uff0c\u8fd8\u5e9f\u9664\u4e86\u89c6\u9891\u5206 p\uff0c\u8fd8\u8981\u652f\u6301\u4e0a\u4f20\u77ed\u89c6\u9891\uff0c\u7ad6\u5c4f\u5f00\u5173\u7b49\u2026\u2026\u6bcf\u4e00\u4e2a\u53d4\u53d4\u7684\u8981\u6c42\uff0c\u90fd\u9700\u8981\u5927\u91cf\u7a0b\u5e8f\u5458\u4fee\u6539\u4ee3\u7801\uff0c\u65e0\u8bba\u6d89\u53ca\u524d\u7aef\u8fd8\u662f\u540e\u7aef\u3002 \u4e0e\u5efa\u7b51\u3001\u7ed8\u753b\u7b49\u9886\u57df\u4e0d\u540c\uff0c\u4e00\u6b21\u4ea4\u4ed8\u5b8c\u6bd5\u5c31\u53ef\u4ee5\u51e0\u4e4e\u6c38\u4e45\u4f7f\u7528\u3002\u800c\u8f6f\u4ef6\u5f00\u53d1\u662f\u4e00\u4e2a\u6301\u7eed\u7684\u8fc7\u7a0b\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u66f4\uff0c\u90fd\u5bfc\u81f4\u4ee3\u7801\u9700\u8981\u4fee\u6539\u3002\u5f00\u53d1\u4eba\u5458\u51e0\u4e4e\u9700\u8981\u4e00\u76f4\u56f4\u7ed5\u7740\u8f6f\u4ef6\u4ee3\u7801\uff0c\u4e0d\u65ad\u7684\u4fee\u6539\u3002\u8c03\u67e5\u8868\u660e\uff0c\u7a0b\u5e8f\u5458 90% \u7684\u65f6\u95f4\u82b1\u5728 \u6539\u4ee3\u7801 \u4e0a\uff0c \u5199\u4ee3\u7801 \u53ea\u5360 10%\u3002 \u8f6f\u4ef6\u5c31\u50cf\u751f\u7269\uff0c\u8981\u4e0d\u65ad\u8fdb\u5316\uff0c\u8f6f\u4ef6\u4e0d\u66f4\u65b0\u4e0d\u7ef4\u62a4\u4e86\u7b49\u4e8e\u6b7b\u3002\u5982\u679c\u4e00\u4e2a\u8f6f\u4ef6\u9010\u6e10\u53d8\u5f97\u81c3\u80bf\u96be\u4ee5\u4fee\u6539\uff0c\u65e0\u6cd5\u9002\u5e94\u65b0\u9700\u6c42\uff0c\u90a3\u4ed6\u5c31\u50cf\u5df2\u7ecf\u5931\u53bb\u8fdb\u5316\u80fd\u529b\u7684\u751f\u7269\u79cd\u7fa4\uff0c\u5982\u300a\u4e09\u4f53\u300b\u4e16\u754c\u89c2\u4e2d\u201c\u5b89\u987f\u201d\u5230\u6fb3\u5927\u5229\u4e9a\u4fdd\u7559\u533a\u91cc\u201c\u7edd\u80b2\u201d\u7684\u4eba\u7c7b\uff0c\u88ab\u6dd8\u6c70\u53ea\u662f\u65f6\u95f4\u95ee\u9898\u3002 \u5982\u679c\u6211\u4eec\u80fd\u5728 \u5199\u4ee3\u7801 \u9636\u6bb5\uff0c\u5c31\u628a\u7a0b\u5e8f\u51c6\u5907\u5f97 \u6613\u4e8e\u540e\u7eed\u4fee\u6539 \uff0c\u90a3\u5c31\u53ef\u4ee5\u5728\u540e\u7eed 90% \u7684 \u6539\u4ee3\u7801 \u9636\u6bb5\u7701\u4e0b\u65e0\u6570\u65f6\u95f4\u3002 \u5982\u4f55\u8ba9\u4ee3\u7801\u6613\u4e8e\u4fee\u6539\uff1f\u524d\u4eba\u603b\u7ed3\u51fa\u4e00\u7cfb\u5217\u5e38\u7528\u7684\u5199\u6cd5\uff0c\u8fd9\u7c7b\u5199\u6cd5\u6709\u52a9\u4e8e\u8ba9\u540e\u7eed\u4fee\u6539\u66f4\u5bb9\u6613\uff0c\u5404\u81ea\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u573a\u5408\uff0c\u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u3002 \u63d0\u5347\u53ef\u7ef4\u62a4\u6027\u6700\u57fa\u7840\u7684\u4e00\u70b9\uff0c\u5c31\u662f\u907f\u514d\u91cd\u590d\uff01 \u5f53\u4f60\u6709\u5f88\u591a\u5730\u65b9\u51fa\u73b0\u91cd\u590d\u7684\u4ee3\u7801\u65f6\uff0c\u4e00\u65e6\u9700\u8981\u6d89\u53ca\u4fee\u6539\u8fd9\u90e8\u5206\u903b\u8f91\u65f6\uff0c\u5c31\u9700\u8981\u5230\u6bcf\u4e00\u4e2a\u51fa\u73b0\u4e86\u8fd9\u4e2a\u903b\u8f91\u7684\u4ee3\u7801\u4e2d\uff0c\u53bb\u9010\u4e00\u4fee\u6539\u3002 \u4f8b\u5982\u4f60\u7684\u540d\u5b57\uff0c\u5728\u51fa\u751f\u8bc1\uff0c\u8eab\u4efd\u8bc1\uff0c\u5b66\u751f\u8bc1\uff0c\u6bd5\u4e1a\u8bc1\uff0c\u623f\u4ea7\u8bc1\uff0c\u9a7e\u9a76\u8bc1\uff0c\u5404\u79cd\u5730\u65b9\u90fd\u51fa\u73b0\u4e86\u3002\u90a3\u4e48\u4f60\u8981\u6539\u540d\u7684\u8bdd\uff0c\u6240\u6709\u8fd9\u4e9b\u8bc1\u4ef6\u90fd\u9700\u8981\u91cd\u65b0\u5370\u5237\uff01\u5982\u679c\u80fd\u628a\u4ed6\u4eec\u5408\u5e76\u6210\u4e00\u4e2a\u201c\u7edf\u4e00\u8bc1\u201d\uff0c\u90a3\u4e48\u53ea\u9700\u8981\u4fee\u6539\u201c\u7edf\u4e00\u8bc1\u201d\u4e0a\u7684\u540d\u5b57\u5c31\u884c\u4e86\u3002 \u4e0d\u8fc7\uff0c\u73b0\u5b9e\u4e2d\u5e76\u6ca1\u6709\u9891\u7e41\u6539\u540d\u5b57\u7684\u9700\u6c42\uff0c\u8fd9\u8bf4\u660e\uff1a \u5bf9\u4e8e\u4e0d\u5e38\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u53ef\u4ee5\u5bb9\u5fcd\u4e00\u5b9a\u7684\u91cd\u590d\u3002 \u8d8a\u662f\u672a\u6765\u6709\u53ef\u80fd\u4fee\u6539\u7684\uff0c\u5c31\u8d8a\u9700\u8981\u8bbe\u8ba1\u6a21\u5f0f\u964d\u91cd\uff01 \u4f8b\u5982\u6570\u5b66\u5e38\u6570 PI = 3.1415926535897\uff0c\u8fd9\u8f88\u5b50\u90fd\u4e0d\u53ef\u80fd\u51fa\u73b0\u4fee\u6539\u7684\u9700\u6c42\uff0c\u90a3\u5199\u6b7b\u4e5f\u6ca1\u5173\u7cfb\u3002\u5982\u679c\u8981\u628a PI \u5b9a\u4e49\u6210\u5b8f\uff0c\u53ea\u662f\u51fa\u4e8e\u201c\u8bb0\u4e0d\u4f4f\u201d\u201c\u5199\u8d77\u6765\u592a\u957f\u4e86\u201d\u201c\u590d\u5236\u7c98\u8d34\u9ebb\u70e6\u201d\u3002\u6240\u4ee5\u5bf9\u4e8e PI \u8fd9\u79cd\u4e0d\u4f1a\u4fee\u6539\u7684\u4e1c\u897f\uff0c\u964d\u91cd\u53ea\u662f\u589e\u52a0 \u53ef\u8bfb\u6027 \uff0c\u800c\u4e0d\u662f \u53ef\u4fee\u6539\u6027 \u3002 \u4f46\u662f\uff0c\u4e0d\u8981\u60f3\u5f53\u7136\uff01\u9700\u6c42\u7684\u5343\u53d8\u4e07\u5316\u603b\u662f\u8d85\u51fa\u4f60\u7684\u60f3\u8c61\u3002 \u4f8b\u5982\u4f60\u505a\u4e86\u4e00\u4e2a\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u6e38\u620f\uff0c\u9700\u8981\u7528\u5230\u91cd\u529b\u52a0\u901f\u5ea6 g = 9.8\uff0c\u4f60\u60f3\u5f53\u7136\u8ba4\u4e3a g \u4ee5\u540e\u4e0d\u53ef\u80fd\u4fee\u6539\u3002\u8001\u677f\u4e5f\u4fe1\u8a93\u65e6\u65e6\u5411\u4f60\u4fdd\u8bc1\uff1a\u201c\u6ca1\u4e8b\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u4e0d\u4f1a\u6539\u53d8\u3002\u201d\u4f60\u5c31\u5199\u6b7b\u5728\u4ee3\u7801\u91cc\u4e86\u3002 \u6ca1\u60f3\u5230\uff0c\u201c\u6124\u6012\u7684\u5c0f\u9e1f\u201d\u8001\u677f\u7a81\u7136\u8981\u6c42\u4f60\u52a0\u5165\u201c\u6708\u7403\u7ae0\u201d\u5173\u5361\uff0c\u5728\u8fd9\u4e9b\u5173\u5361\u4e2d\uff0c\u91cd\u529b\u52a0\u901f\u5ea6\u662f g = 1.6\u3002 \u5982\u679c\u4f60\u4e00\u5f00\u59cb\u5c31\u5df2\u7ecf\u628a g \u63d0\u53d6\u51fa\u6765\uff0c\u5b9a\u4e49\u4e3a\u5e38\u91cf\uff1a struct Level { const double g = 9.8; void physics_sim() { bird.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f pig.v = g * t; // \u5047\u88c5\u8fd9\u91cc\u662f\u7269\u7406\u4eff\u771f\u7a0b\u5e8f } }; \u90a3\u4e48\u8981\u652f\u6301\u6708\u7403\u5173\u5361\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5c31\u53ef\u4ee5\u4e86\u3002 struct Level { double g; Level(Chapter chapter) { if (chapter == ChapterMoon) { g = 1.6; } else { g = 9.8; } } void physics_sim() { bird.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g pig.v = g * t; // \u65e0\u9700\u4efb\u4f55\u4fee\u6539\uff0c\u81ea\u52a8\u9002\u5e94\u4e86\u65b0\u7684\u975e\u5e38\u6570 g } }; \u5c0f\u5f6d\u8001\u5e08\u4e4b\u524d\u505a zeno \u65f6\uff0c\u8be2\u95ee\u8981\u4e0d\u8981\u628a\u6e32\u67d3\u7ba1\u7ebf\u8282\u70b9\u5316\uff0c\u65b9\u4fbf\u7528\u6237\u52a8\u6001\u7f16\u7a0b\uff1f\u5f20\u7329\u7329\u5c31\u662f\u4fe1\u8a93\u65e6\u65e6\u9053\uff1a\u201c\u6e32\u67d3\u662f\u4e00\u4e2a\u9ad8\u5ea6\u6210\u719f\u9886\u57df\uff0c\u4e0d\u4f1a\u6709\u591a\u5c11\u4fee\u6539\u9700\u6c42\u7684\u3002\u201d\u5c0f\u5f6d\u8001\u5e08\u9042\u5199\u6b7b\u4e86\u6e32\u67d3\u7ba1\u7ebf\uff0c\u4e13\u4e3a\u6027\u80fd\u6781\u5ea6\u4f18\u5316\uff0c\u51e0\u4e2a\u6708\u540e\uff0c\u5f20\u7329\u7329\u7f9e\u7b54\u7b54\u627e\u5230\u5c0f\u5f6d\u8001\u5e08\uff1a\u201c\u5c0f\u5f6d\u8001\u5e08\uff0c\u90a3\u4e2a\uff0c\u6e32\u67d3\uff0c\u80fd\u4e0d\u80fd\u6539\u6210\u8282\u70b9\u554a\u2026\u2026\u201d\u3002\u8fd9\u4e2a\u6545\u4e8b\u544a\u8bc9\u6211\u4eec\uff0c\u7532\u65b9\u7684\u4fe1\u8a93\u65e6\u65e6\u653e\u7684\u4e00\u4e2a\u5c41\u90fd\u4e0d\u80fd\u4fe1\u3002","title":"\u8bbe\u8ba1\u6a21\u5f0f\u8ffd\u6c42\u7684\u662f\u201c\u53ef\u6539\u201d\u800c\u4e0d\u662f\u201c\u53ef\u8bfb\u201d\uff01"},{"location":"functional/#_5","text":"\u51fd\u6570\u5c31\u662f\u6765\u5e2e\u4f60\u89e3\u51b3\u4ee3\u7801\u91cd\u590d\u95ee\u9898\u7684\uff01\u8981\u9886\uff1a \u628a\u5171\u540c\u7684\u90e8\u5206\u63d0\u53d6\u51fa\u6765\uff0c\u628a\u4e0d\u540c\u7684\u90e8\u5206\u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\u3002 void sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } fmt::println(\"sum of v = {}\", s); } int main() { std::vector a = {1, 2, 3, 4}; sum(a); std::vector b = {5, 6, 7, 8}; sum(b); return 0; } \u8fd9\u6837 main \u51fd\u6570\u91cc\u5c31\u53ef\u4ee5\u53ea\u5173\u5fc3\u8981\u6c42\u548c\u7684\u6570\u7ec4\uff0c\u800c\u4e0d\u7528\u5173\u5fc3\u6c42\u548c\u5177\u4f53\u662f\u5982\u4f55\u5b9e\u73b0\u7684\u4e86\u3002\u4e8b\u540e\u6211\u4eec\u53ef\u4ee5\u968f\u65f6\u628a sum \u7684\u5185\u5bb9\u5077\u5077\u6362\u6389\uff0c\u6362\u6210\u5e76\u884c\u7684\u7b97\u6cd5\uff0cmain \u4e5f\u4e0d\u7528\u77e5\u9053\u3002\u8fd9\u5c31\u662f \u5c01\u88c5 \uff0c\u53ef\u4ee5\u628a\u91cd\u590d\u7684\u516c\u5171\u90e8\u5206\u62bd\u53d6\u51fa\u6765\uff0c\u65b9\u4fbf\u4ee5\u540e\u4fee\u6539\u4ee3\u7801\u3002 sum \u51fd\u6570\u76f8\u5f53\u4e8e\uff0c\u5f53\u9700\u8981\u5439\u7a7a\u8c03\u65f6\uff0c\u63d2\u4e0a\u7a7a\u8c03\u63d2\u5ea7\u3002\u5f53\u9700\u8981\u7ed9\u624b\u673a\u5145\u7535\u65f6\uff0c\u63d2\u4e0a\u624b\u673a\u5145\u7535\u5668\u3002\u4f60\u4e0d\u9700\u8981\u5173\u5fc3\u63d2\u5ea7\u91cc\u7684\u7535\u54ea\u91cc\u6765\uff0c\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4f1a\u66ff\u4f60\u60f3\u529e\u6cd5\u89e3\u51b3\uff0c\u60f3\u529e\u6cd5\u4f18\u5316\uff0c\u60f3\u529e\u6cd5\u5347\u7ea7\u5230\u7eff\u8272\u80fd\u6e90\u3002\u4f60\u53ea\u9700\u8981\u5439\u7740\u7a7a\u8c03\u7ed9\u4f60\u6b63\u5728\u5f00\u53d1\u7684\u624b\u673a App \u4f18\u5316\u5c31\u884c\u4e86\uff0c\u5927\u5927\u51cf\u8f7b\u7a0b\u5e8f\u5458\u5fc3\u667a\u8d1f\u62c5\u3002","title":"\u7528\u51fd\u6570\u5c01\u88c5"},{"location":"functional/#_6","text":"\u4f46\u662f\uff01\u8fd9\u6bb5\u4ee3\u7801\u4ecd\u7136\u6709\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u628a sum \u6c42\u548c\u7684\u7ed3\u679c\uff0c\u76f4\u63a5\u5728 sum \u91cc\u6253\u5370\u4e86\u51fa\u6765\u3002sum \u91cc\u5199\u6b7b\u4e86\uff0c\u6c42\u5b8c\u548c\u4e4b\u540e\u53ea\u80fd\u76f4\u63a5\u6253\u5370\uff0c\u8c03\u7528\u8005 main \u6839\u672c\u65e0\u6cd5\u63a7\u5236\u3002 \u8fd9\u662f\u4e00\u79cd\u9519\u8bef\u7684\u5c01\u88c5\uff0c\u6216\u8005\u8bf4\uff0c\u5c01\u88c5\u8fc7\u5934\u4e86\u3002 \u4f60\u628a\u624b\u673a\u5145\u7535\u5668 (fmt::println) \u710a\u6b7b\u5728\u4e86\u63d2\u5ea7 (sum) \u4e0a\uff0c\u73b0\u5728\u8fd9\u4e2a\u63d2\u5ea7\u53ea\u80fd\u7ed9\u624b\u673a\u5145\u7535 (\u7528\u4e8e\u76f4\u63a5\u6253\u5370) \u4e86\uff0c\u4e0d\u80fd\u7ed9\u7b14\u8bb0\u672c\u7535\u8111\u5145\u7535 (\u6c42\u548c\u7ed3\u679c\u4e0d\u76f4\u63a5\u7528\u4e8e\u6253\u5370) \u4e86\uff01\u5c3d\u7ba1\u901a\u8fc7\u66f4\u6362\u5145\u7535\u7ebf (\u53c2\u6570 v)\uff0c\u8fd8\u53ef\u4ee5\u652f\u6301\u652f\u6301\u5b89\u5353 (a) \u548c\u82f9\u679c (b) \u4e24\u79cd\u624b\u673a\u7684\u5145\u7535\uff0c\u4f46\u8fd9\u6837\u710a\u6b7b\u7684\u63d2\u5ea7\u5df2\u7ecf\u548c\u7b14\u8bb0\u672c\u7535\u8111\u65e0\u7f18\u4e86\u3002","title":"\u8981\u5c01\u88c5\uff0c\u4f46\u4e0d\u8981\u8026\u5408"},{"location":"functional/#_7","text":"\u5f88\u660e\u663e\uff0c\u201c\u6253\u5370\u201d\u548c\u201c\u6c42\u548c\u201d\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u64cd\u4f5c\uff0c\u4e0d\u5e94\u8be5\u710a\u6b7b\u5728\u4e00\u5757\u3002 sum \u51fd\u6570\u7684\u672c\u804c\u5de5\u4f5c\u662f\u201c\u6570\u7ec4\u6c42\u548c\u201d\uff0c\u4e0d\u5e94\u8be5\u9644\u8d60\u6253\u5370\u529f\u80fd\u3002 sum \u8ba1\u7b97\u51fa\u6c42\u548c\u7ed3\u679c\u540e\uff0c\u76f4\u63a5 return \u5373\u53ef\u3002 \u5982\u4f55\u5904\u7406\u8fd9\u4e2a\u7ed3\u679c\uff0c\u662f\u8c03\u7528\u8005 main \u7684\u4e8b\uff0c\u6b63\u5982\u201c\u56fd\u5bb6\u7535\u7f51\u201d\u4e0d\u4f1a\u7ba1\u4f60\u7528\u4ed6\u63d0\u4f9b\u7684\u7535\u6765\u5439\u7a7a\u8c03\u8fd8\u662f\u73a9\u6e38\u620f\u4e00\u6837\uff0c\u53ea\u8981\u4e0d\u59a8\u788d\u5230\u5176\u4ed6\u5c45\u6c11\u7684\u6b63\u5e38\u7528\u7535\u3002 int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum of a = {}\", sum(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"sum of b = {}\", sum(b)); return 0; } \u8fd9\u5c31\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8bf4\u7684 \u804c\u8d23\u5355\u4e00\u539f\u5219 \u3002","title":"\u6bcf\u4e2a\u51fd\u6570\u5e94\u8be5\u804c\u8d23\u5355\u4e00\uff0c\u522b\u4e00\u5fc3\u591a\u7528"},{"location":"functional/#_8","text":"\u5047\u8bbe\u6211\u4eec\u8981\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c\u53ef\u4ee5\u518d\u5b9a\u4e49\u4e2a\u51fd\u6570 average\uff0c\u4ed6\u53ef\u4ee5\u57fa\u4e8e sum \u5b9e\u73b0\uff1a int sum(std::vector const &v) { int s = 0; for (int i = 0; i < v.size(); i++) { s += v[i]; } return s; } double average(std::vector const &v) { return (double)sum(v) / v.size(); } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"average of a = {}\", average(a)); std::vector b = {5, 6, 7, 8}; fmt::println(\"average of b = {}\", average(b)); return 0; } \u8fdb\u4e00\u6b65\u5c01\u88c5\u4e00\u4e2a\u6253\u5370\u6570\u7ec4\u6240\u6709\u7edf\u8ba1\u5b66\u4fe1\u606f\u7684\u51fd\u6570\uff1a void print_statistics(std::vector const &v) { if (v.empty()) { fmt::println(\"this is empty...\"); } else { fmt::println(\"sum: {}\", sum(v)); fmt::println(\"average: {}\", average(v)); fmt::println(\"min: {}\", min(v)); fmt::println(\"max: {}\", max(v)); } } int main() { std::vector a = {1, 2, 3, 4}; print_statistics(a); std::vector b = {5, 6, 7, 8}; print_statistics(b); return 0; } \u66b4\u9732 API \u65f6\uff0c\u8981\u540c\u65f6\u63d0\u4f9b\u5e95\u5c42\u7684 API \u548c\u9ad8\u5c42\u5c01\u88c5\u7684 API\u3002\u7528\u6237\u5982\u679c\u60f3\u8981\u63a7\u5236\u66f4\u591a\u7ec6\u8282\u53ef\u4ee5\u8c03\u7528\u5e95\u5c42 API\uff0c\u60f3\u8981\u7701\u4e8b\u7684\u7528\u6237\u53ef\u4ee5\u8c03\u7528\u9ad8\u5c42\u5c01\u88c5\u597d\u7684 API\u3002 \u9ad8\u5c42\u5c01\u88c5 API \u5e94\u5f53\u53ef\u4ee5\u5b8c\u5168\u901a\u8fc7\u8c03\u7528\u5e95\u5c42 API \u5b9e\u73b0\uff0c\u63d0\u4f9b\u9ad8\u5c42 API \u53ea\u662f\u65b9\u4fbf\u521d\u7ea7\u7528\u6237\u4f7f\u7528\u548c\u7406\u89e3\u3002 \u4f8b\u5982 libcurl \u5c31\u63d0\u4f9b\u4e86 curl_easy \u548c curl_multi \u4e24\u5957 API\u3002 - `curl_multi` \u63d0\u4f9b\u4e86\u8d85\u8be6\u7ec6\u7684\u53c2\u6570\uff0c\u628a\u6bcf\u4e2a\u64cd\u4f5c\u5206\u62c6\u6210\u591a\u6b65\uff0c\u65b9\u4fbf\u7528\u6237\u63d2\u624b\u7ec6\u8282\uff0c\u6ee1\u8db3\u9ad8\u7ea7\u7528\u6237\u7684\u5b9a\u5236\u5316\u9700\u6c42\uff0c\u4f46\u592a\u8fc7\u590d\u6742\uff0c\u96be\u4ee5\u5b66\u4e60\u3002 - `curl_easy` \u662f\u5bf9 `curl_multi` \u7684\u518d\u5c01\u88c5\uff0c\u63d0\u4f9b\u4e86\u66f4\u7b80\u5355\u7684 API\uff0c\u4f46\u662f\u5bf9\u5177\u4f53\u7ec6\u8282\u5c31\u96be\u4ee5\u64cd\u63a7\u4e86\uff0c\u9002\u5408\u521d\u5b66\u8005\u4e0a\u624b\u3002","title":"\u4e8c\u6b21\u5c01\u88c5"},{"location":"functional/#linus-3-24","text":"Linux \u5185\u6838\u4e3a\u4ec0\u4e48\u575a\u6301\u4f7f\u7528 TAB=8 \u4e3a\u4ee3\u7801\u98ce\u683c\uff1f TODO\uff1a\u8fd8\u5728\u5199","title":"Linus \u7684\u6700\u4f73\u5b9e\u8df5\uff1a\u6bcf\u4e2a\u51fd\u6570\u4e0d\u8981\u8d85\u8fc7 3 \u5c42\u5d4c\u5957\uff0c\u51fd\u6570\u4f53\u4e0d\u8981\u8d85\u8fc7 24 \u884c"},{"location":"functional/#_9","text":"\u4f60\u4ea7\u751f\u4e86\u4e24\u4e2a\u9700\u6c42\uff0c\u5206\u522b\u5c01\u88c5\u4e86\u4e24\u4e2a\u51fd\u6570\uff1a sum \u6c42\u6240\u6709\u5143\u7d20\u7684\u548c product \u6c42\u6240\u6709\u5143\u7d20\u7684\u79ef int sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret += v[i]; } return ret; } int product(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { ret *= v[i]; } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", sum(a)); fmt::println(\"product: {}\", product(a)); return 0; } \u6ce8\u610f\u5230 sum \u548c product \u7684\u5185\u5bb9\u51e0\u4e4e\u5982\u51fa\u4e00\u8f99\uff0c\u552f\u4e00\u7684\u533a\u522b\u5728\u4e8e\uff1a sum \u7684\u5faa\u73af\u4f53\u4e3a += \uff1b product \u7684\u5faa\u73af\u4f53\u4e3a *= \u3002 \u8fd9\u79cd\u51fd\u6570\u4f53\u5185\u6709\u90e8\u5206\u4ee3\u7801\u91cd\u590d\uff0c\u4f46\u53c8\u6709\u7279\u5b9a\u90e8\u5206\u4e0d\u540c\uff0c\u96be\u4ee5\u62bd\u79bb\u3002 \u8be5\u600e\u4e48\u590d\u7528\u8fd9\u91cd\u590d\u7684\u90e8\u5206\u4ee3\u7801\u5462\uff1f \u6211\u4eec\u8981\u628a sum \u548c product \u5408\u5e76\u6210\u4e00\u4e2a\u51fd\u6570 generic_sum \u3002\u7136\u540e\u901a\u8fc7\u51fd\u6570\u53c2\u6570\uff0c\u628a\u5dee\u5f02\u90e8\u5206\uff080\u3001 += \uff09\u201c\u6ce8\u5165\u201d\u5230\u4e24\u4e2a\u51fd\u6570\u539f\u672c\u4e0d\u540c\u5730\u65b9\u3002","title":"\u4e3a\u4ec0\u4e48\u9700\u8981\u51fd\u6570\u5f0f\uff1f"},{"location":"functional/#_10","text":"\u5982\u4f55\u8868\u793a\u6211\u8fd9\u4e2a\u51fd\u6570\u662f\u8981\u505a\u6c42\u548c += \u8fd8\u662f\u6c42\u79ef *= \uff1f \u8ba9\u6211\u4eec\u5b9a\u4e49\u679a\u4e3e\uff1a enum Mode { ADD, // \u6c42\u548c\u64cd\u4f5c MUL, // \u6c42\u79ef\u64cd\u4f5c }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { // \u51fd\u6570\u5185\u5224\u65ad\u679a\u4e3e\uff0c\u51b3\u5b9a\u8981\u505a\u4ec0\u4e48\u64cd\u4f5c ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; fmt::println(\"sum: {}\", generic_sum(a, ADD)); // \u7528\u6237\u6307\u5b9a\u4ed6\u60f3\u8981\u7684\u64cd\u4f5c fmt::println(\"product: {}\", generic_sum(a, MUL)); return 0; } \u7136\u800c\uff0c\u5982\u679c\u7528\u6237\u73b0\u5728\u60f3\u8981\u6c42\u6570\u7ec4\u7684 \u6700\u5927\u503c \u5462\uff1f \u679a\u4e3e\u4e2d\u8fd8\u6ca1\u6709\u5b9e\u73b0\u6700\u5927\u503c\u7684\u64cd\u4f5c\u2026\u2026\u8981\u652f\u6301\uff0c\u5c31\u5f97\u624b\u5fd9\u811a\u4e71\u5730\u53bb\u4fee\u6539 generic_sum \u51fd\u6570\u548c Mode \u679a\u4e3e\u539f\u672c\u7684\u5b9a\u4e49\uff0c\u771f\u9ebb\u70e6\uff01 enum Mode { ADD, MUL, MAX, // ***\u6539*** }; int generic_sum(std::vector const &v, Mode mode) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { if (mode == ADD) { ret += v[i]; } else if (mode == MUL) { ret *= v[i]; } else if (mode == MAX) { // ***\u6539*** ret = std::max(ret, v[i]); // ***\u6539*** } } return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, MAX); // ***\u6539*** return 0; } \u6211\u7528 // ***\u6539*** \u6307\u793a\u4e86\u6240\u6709\u9700\u8981\u6539\u52a8\u7684\u5730\u65b9\u3002 \u4e3a\u4e86\u589e\u52a0\u4e00\u4e2a\u6c42\u6700\u5927\u503c\u7684\u64cd\u4f5c\uff0c\u5c31\u9700\u8981\u4e09\u5904\u5206\u6563\u5728\u5404\u5730\u7684\u6539\u52a8\uff01 \u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u5bb9\u6613\u6284\u6f0f\uff0c\u6284\u9519\uff0c\u6bd4\u5982 MAX \u4e0d\u5c0f\u5fc3\u6253\u9519\u6210 MUL \u4e86\uff0c\u81ea\u5df1\u5374\u6ca1\u53d1\u73b0\uff0c\u7559\u4e0b BUG \u9690\u60a3\u3002 \u8fd9\u6837\u5199\u4ee3\u7801\u7684\u65b9\u5f0f\uff0c\u5fc3\u667a\u8d1f\u62c5\u6781\u5927\uff0c\u6574\u5929\u5c31\u63d0\u5fc3\u540a\u80c6\u7740\u4e1c\u4e00\u5757\uff0c\u897f\u4e00\u5757\u7684\u6563\u88c5\u4ee3\u7801\uff0c\u62c5\u5fc3\u7740\u6709\u6ca1\u6709\u54ea\u4e2a\u5730\u65b9\u5199\u9519\u5199\u6f0f\uff0c\u4e25\u91cd\u59a8\u788d\u4e86\u5f00\u53d1\u6548\u7387\u3002 \u5e76\u4e14\u5199\u51fa\u6765\u7684\u4ee3\u7801\u4e5f\u4e0d\u80fd\u9002\u5e94\u9700\u6c42\u7684\u53d8\u5316\uff1a\u5047\u5982\u6211\u9700\u8981\u652f\u6301 MIN \u5462\uff1f\u53c8\u5f97\u6539\u4e09\u4e2a\u5730\u65b9\uff01\u8fd9\u8fdd\u80cc\u4e86\u8bbe\u8ba1\u6a21\u5f0f\u7684 \u5f00\u95ed\u539f\u5219 \u3002 \u5f00\u95ed\u539f\u5219: \u5bf9\u6269\u5c55\u5f00\u653e\uff0c\u5bf9\u4fee\u6539\u5c01\u95ed\u3002\u6307\u7684\u662f\u8f6f\u4ef6\u5728\u9002\u5e94\u9700\u6c42\u53d8\u5316\u65f6\uff0c\u5e94\u5c3d\u91cf\u901a\u8fc7 \u6269\u5c55\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\uff0c\u800c\u4e0d\u662f\u901a\u8fc7 \u4fee\u6539\u5df2\u6709\u4ee3\u7801 \u6765\u5b9e\u73b0\u53d8\u5316\u3002 \u4f7f\u7528\u679a\u4e3e\u548c if-else \u5b9e\u73b0\u591a\u6001\uff0c\u96be\u4ee5\u6269\u5c55\uff0c\u8fd8\u8981\u4e00\u76f4\u53bb\u4fee\u6539\u539f\u51fd\u6570\u7684\u5e95\u5c42\u5b9e\u73b0\uff0c\u5c31\u8fdd\u80cc\u4e86 \u5f00\u95ed\u539f\u5219 \u3002","title":"\u679a\u4e3e\u7684\u7cdf\u7cd5\u7528\u6cd5"},{"location":"functional/#_11","text":"\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u201c\u6ce8\u5165\u201d\u4ee3\u7801\u5c31\u597d\u4e86\uff01\u80fd\u5426\u628a\u4e00\u6bb5\u201c\u4ee3\u7801\u201d\u4f5c\u4e3a generic_sum \u51fd\u6570\u7684\u53c2\u6570\u5462\uff1f \u4ee3\u7801\uff0c\u5b9e\u9645\u4e0a\u5c31\u662f\u51fd\u6570\uff0c\u6ce8\u5165\u4ee3\u7801\u5c31\u662f\u6ce8\u5165\u51fd\u6570\u3002\u6211\u4eec\u5148\u5b9a\u4e49\u51fa\u4e09\u4e2a\u4e0d\u540c\u64cd\u4f5c\u5bf9\u5e94\u7684\u51fd\u6570\uff1a int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } \u7136\u540e\uff0c\u628a\u8fd9\u4e09\u4e2a\u5c0f\u51fd\u6570\uff0c\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u5927\u51fd\u6570 generic_sum \u7684\u53c2\u6570\u5c31\u884c\uff01 int generic_sum(std::vector const &v, auto op) { int ret = v[0]; for (int i = 1; i < v.size(); i++) { // \u51fd\u6570\u4f5c\u8005\u65e0\u9700\u4e86\u89e3\u7528\u6237\u6307\u5b9a\u7684\u201c\u64cd\u4f5c\u201d\u5177\u4f53\u662f\u4ec0\u4e48 // \u53ea\u9700\u8981\u8c03\u7528\u8fd9\u4e00\u201c\u64cd\u4f5c\u201d\uff0c\u5f97\u5230\u7ed3\u679c\u5c31\u884c ret = op(ret, v[i]); } return ret; } int main() { std::vector a = {1, 2, 3, 4}; // \u7528\u6237\u65e0\u9700\u5173\u5fc3\u51fd\u6570\u7684\u5177\u4f53\u5b9e\u73b0\u662f\u4ec0\u4e48 // \u53ea\u9700\u968f\u5fc3\u6240\u6b32\u6307\u5b9a\u4ed6\u7684\u201c\u64cd\u4f5c\u201d\u4f5c\u4e3a\u53c2\u6570 generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u8d23\u4efb\u660e\u786e\u4e86\uff0c\u6211\u4eec\u6210\u529f\u628a\u4e00\u90e8\u5206\u7ec6\u8282\u4ece generic_sum \u4e2d\u8fdb\u4e00\u6b65\u62bd\u79bb\u3002 \u5e93\u4f5c\u8005 generic_sum \u4e0d\u5fc5\u4e86\u89e3 main \u7684\u64cd\u4f5c\u5177\u4f53\u662f\u4ec0\u4e48\uff0c\u4ed6\u53ea\u8d1f\u8d23\u5229\u7528\u8fd9\u4e2a\u64cd\u4f5c\u6c42\u201c\u548c\u201d\u3002 \u5e93\u7528\u6237 main \u4e0d\u5fc5\u4e86\u89e3 generic_sum \u5982\u4f55\u5b9e\u73b0\u64cd\u4f5c\u7d2f\u52a0\uff0c\u4ed6\u53ea\u7ba1\u6ce8\u5165\u201c\u5982\u4f55\u64cd\u4f5c\u201d\u7684\u4ee3\u7801\uff0c\u4ee5\u51fd\u6570\u7684\u5f62\u5f0f\u3002","title":"\u51fd\u6570\u5f0f\u7f16\u7a0b\u5149\u8363\u6551\u573a"},{"location":"functional/#c20-auto","text":"int generic_sum(std::vector const &v, auto op) P } \u8fd9\u91cc\u7684\u53c2\u6570 op \u7c7b\u578b\u58f0\u660e\u4e3a auto\uff0c\u6548\u679c\u5c31\u662f\uff0cop \u8fd9\u4e2a\u53c2\u6570\u73b0\u5728\u80fd\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u7684\u5bf9\u8c61\u4e86\uff08\u5305\u62ec\u51fd\u6570\uff01\uff09 int generic_sum(std::vector const &v, auto op) { ... } \u51c6\u786e\u7684\u8bf4\uff0c auto op \u53c2\u6570\u7684\u6548\u679c\u662f\u4f7f generic_sum \u53d8\u4e3a\u4e00\u4e2a \u6a21\u677f\u51fd\u6570 \uff0c\u5176\u4e2d op \u53c2\u6570\u53d8\u6210\u4e86\u6a21\u677f\u53c2\u6570\uff0c\u80fd\u591f\u63a5\u53d7\u4efb\u610f\u7c7b\u578b\u4e86\u3002\u800c\u5199\u660e\u7c7b\u578b\u7684\u53c2\u6570 std::vector const &v \u5c31\u6ca1\u6709\u4efb\u4f55\u989d\u5916\u6548\u679c\uff0c\u5c31\u53ea\u80fd\u63a5\u53d7 vector \u800c\u5df2\u3002 \u5982\u679c\u4f60\u4e0d\u652f\u6301 C++20 \u7684\u8bdd\uff0c\u9700\u8981\u663e\u5f0f\u5199\u51fa template \uff0c\u624d\u80fd\u5b9e\u73b0\u540c\u6837\u7684\u6548\u679c\uff1a template int generic_sum(std::vector const &v, Op op) { ... } C++11\uff1aauto \u53ea\u80fd\u7528\u4e8e\u5b9a\u4e49\u53d8\u91cf\uff1bC++14\uff1a\u51fd\u6570\u8fd4\u56de\u7c7b\u578b\u53ef\u4ee5\u662f auto\uff1bC++17\uff1a\u6a21\u677f\u53c2\u6570\u4e5f\u53ef\u4ee5 auto\uff1bC++20\uff1a\u51fd\u6570\u53c2\u6570\u4e5f\u53ef\u4ee5\u662f auto \u4e86\uff1b\uff08\u72c2\u60f3\uff09C++47\uff1aauto \u73b0\u5728\u662f C++47 \u7684\u552f\u4e00\u5173\u952e\u5b57\uff0c\u7528\u6237\u53ea\u9700\u4e0d\u65ad\u8f93\u5165 auto-auto-auto\uff0c\u7f16\u8bd1\u5668\u5185\u5efa\u4eba\u5de5\u667a\u80fd\u81ea\u52a8\u8bc6\u522b\u4f60\u7684\u610f\u56fe\u751f\u6210\u673a\u5668\u7801\u3002","title":"\u6211\u7528\u4e86 C++20 \u7684\u51fd\u6570\u53c2\u6570 auto \u8bed\u6cd5\u7cd6"},{"location":"functional/#_12","text":"\u5728\u8fc7\u53bb\u7684 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f \u4e2d\uff0c\u51fd\u6570\uff08\u4ee3\u7801\uff09\u548c\u5bf9\u8c61\uff08\u6570\u636e\uff09\u88ab \u5272\u88c2 \u5f00\u6765\uff0c\u4ed6\u4eec\u611a\u6627\u5730\u8ba4\u4e3a \u51fd\u6570\u4e0d\u662f\u5bf9\u8c61 \u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f \u5219\u8ba4\u4e3a\uff1a \u51fd\u6570\u4e5f\u662f\u4e00\u79cd\u53d8\u91cf\uff0c\u51fd\u6570\u53ef\u4ee5\u4f5c\u4e3a\u53e6\u4e00\u4e2a\u51fd\u6570\u7684\u53c2\u6570\uff01 Function lives matter! \u9762\u5411\u5bf9\u8c61\u5c31\u597d\u6bd4\u8ba1\u7b97\u673a\u7684\u201c\u54c8\u4f5b\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u548c\u6570\u636e\u5272\u88c2\uff0c\u4ee3\u7801\u53ea\u80fd\u5355\u65b9\u9762\u64cd\u4f5c\u6570\u636e\u3002\u51fd\u6570\u5f0f\u5c31\u597d\u6bd4\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\uff0c\u4ee3\u7801\u4e5f\u662f\u6570\u636e\u3002\u770b\u4f3c\u4f1a\u5bfc\u81f4\u4f4e\u6548\uff0c\u5b9e\u5219\u5927\u5927\u65b9\u4fbf\u4e86\u52a8\u6001\u52a0\u8f7d\u65b0\u7a0b\u5e8f\uff0c\u56e0\u800c\u73b0\u5728\u7684\u8ba1\u7b97\u673a\u57fa\u672c\u90fd\u91c7\u7528\u4e86\u201c\u51af\u8bfa\u4f9d\u66fc\u67b6\u6784\u201d\u3002 \u603b\u4e4b\uff0c\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff0c\u88ab\u4eb2\u5207\u5730\u5c0a\u79f0\u4e3a \u51fd\u6570\u5bf9\u8c61 \u3002","title":"\u51fd\u6570\u4e5f\u662f\u5bf9\u8c61\uff01"},{"location":"functional/#c11-lambda","text":"C++98 \u65f6\u4ee3\uff0c\u4eba\u4eec\u8fd8\u9700\u8981\u5355\u72ec\u8dd1\u5230 main \u5916\u9762\uff0c\u4e13\u95e8\u5b9a\u4e49 add \u3001 mul \u3001 max \u51fd\u6570\u3002\u5f04\u5f97\u6574\u4e2a\u4ee3\u7801\u4e71\u54c4\u54c4\u7684\uff0c\u975e\u5e38\u9ebb\u70e6\u3002 int add(int a, int b) { return a + b; } int mul(int a, int b) { return a * b; } int max(int a, int b) { return std::max(a, b); } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } C++11 \u5f15\u5165\u4e86 Lambda \u8868\u8fbe\u5f0f \u8bed\u6cd5\uff0c\u5141\u8bb8\u4f60\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002 int main() { std::vector a = {1, 2, 3, 4}; auto add = [](int a, int b) { return a + b; }; auto mul = [](int a, int b) { return a * b; }; auto max = [](int a, int b) { return std::max(a, b); }; generic_sum(a, add); generic_sum(a, product); generic_sum(a, max); return 0; } \u4e0d\u7528\u5f80 main \u5916\u9762\u585e\u5783\u573e\u4e86\uff0c\u4e00\u6e05\u723d\u3002 \u66f4\u8fdb\u4e00\u6b65\uff0c\u6211\u4eec\u751a\u81f3\u4e0d\u7528\u5b9a\u4e49\u53d8\u91cf\uff0c\u76f4\u63a5\u628a Lambda \u8868\u8fbe\u5f0f\u5199\u5728 generic_sum \u7684\u53c2\u6570\u91cc\u5c31\u884c\u4e86\uff01 int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); // ***\u6539*** return 0; } \u4ee5\u4e0a\u5199\u6cd5\u90fd\u662f\u7b49\u4ef7\u7684\u3002 \u8981\u652f\u6301\u4e00\u4e2a\u65b0\u64cd\u4f5c\uff0c\u53ea\u9700\u4fee\u6539\u4e00\u5904\u5730\u65b9\uff1a\u5728\u8c03\u7528 generic_sum \u65f6\u5c31\u5730\u521b\u5efa\u4e00\u4e2a\u51fd\u6570\u3002\u968f\u53eb\u968f\u5230\uff0c\u4e0d\u7528\u7ea0\u7ed3\u4e8e\u201c\u8d77\u540d\u5f3a\u8feb\u75c7\u201d\uff0c\u662f\u4e0d\u662f\u5f88\u65b9\u4fbf\u5462\uff1f \u51c6\u786e\u7684\u8bf4\uff0cLambda \u521b\u5efa\u7684\u662f\u51fd\u6570\u5bf9\u8c61 (function object) \u6216\u79f0\u4eff\u51fd\u6570 (functor) \u800c\u4e0d\u662f\u4f20\u7edf\u610f\u4e49\u4e0a\u7684\u51fd\u6570\u3002 \u5176\u5b9e C++98 \u65f6\u4ee3\u4eba\u4eec\u5c31\u5df2\u7ecf\u5927\u91cf\u5728\u7528 operator()() \u6a21\u62df\u51fd\u6570\u5bf9\u8c61\u4e86\uff0c\u8457\u540d\u7684\u7b2c\u4e09\u65b9\u5e93 Boost \u4e5f\u5c01\u88c5\u4e86\u5404\u79cd\u51fd\u6570\u5f0f\u5e38\u7528\u7684\u5bb9\u5668\u548c\u5de5\u5177\u3002C++11 \u624d\u7ec8\u4e8e\u628a \u51fd\u6570\u5bf9\u8c61 \u8fd9\u4e2a\u6982\u5ff5\u8f6c\u6b63\uff0c\u5e76\u5f15\u5165\u4e86\u66f4\u65b9\u4fbf\u7684 Lambda \u8bed\u6cd5\u7cd6\u3002 \u5373\u4f7f\u662f\u9762\u5411\u5bf9\u8c61\u7684\u5934\u53f7\u5b5d\u5b50 Java\uff0c\u4e5f\u5df2\u7ecf\u5f00\u59cb\u5f15\u5165\u51fd\u6570\u5f0f\u7684 Lambda \u8bed\u6cd5\u7cd6\uff0cC# \u7684 LINQ \u66f4\u662f\u660e\u76ee\u5f20\u80c6\u7684\u81f4\u656c map-reduce \u5168\u5bb6\u6876\uff0c\u751a\u81f3 C \u8bed\u8a00\u7528\u6237\u4e5f\u5f00\u59cb\u73a9\u5404\u79cd\u51fd\u6570\u6307\u9488\u56de\u8c03\u2026\u2026\u6ca1\u529e\u6cd5\uff0c\u51fd\u6570\u5f0f\u786e\u5b9e\u65b9\u4fbf\u5440\uff01","title":"C++11 \u5f15\u5165 Lambda \u8bed\u6cd5\u7cd6"},{"location":"functional/#_13","text":"\u51fd\u6570\u5bf9\u8c61 op \u4f5c\u4e3a\u53c2\u6570\u4f20\u5165\uff0c\u8ba9 generic_sum \u5185\u90e8\u53bb\u8c03\u7528\uff0c\u5c31\u50cf\u5f80 generic_sum \u4f53\u5185\u201c\u6ce8\u5165\u201d\u4e86\u4e00\u6bb5\u81ea\u5b9a\u4e49\u4ee3\u7801\u4e00\u6837\u3002 \u8fd9\u53ef\u4ee5\u8ba9 generic_sum \u5728\u4e0d\u4fee\u6539\u672c\u4f53\u7684\u60c5\u51b5\u4e0b\uff0c\u901a\u8fc7\u4fee\u6539\u201c\u6ce8\u5165\u201d\u90e8\u5206\uff0c\u8f7b\u677e\u6269\u5c55\uff0c\u6ee1\u8db3 \u5f00\u95ed\u539f\u5219 \u3002 \u66f4\u51c6\u786e\u7684\u8bf4\uff0c\u8fd9\u4f53\u73b0\u7684\u662f\u8bbe\u8ba1\u6a21\u5f0f\u6240\u8981\u6c42\u7684 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u3002 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219: \u4e00\u4e2a\u5c01\u88c5\u597d\u7684\u51fd\u6570\u6216\u7c7b\uff0c\u5e94\u8be5\u5c3d\u91cf\u4f9d\u8d56\u4e8e\u62bd\u8c61\u63a5\u53e3\uff0c\u800c\u4e0d\u662f\u4f9d\u8d56\u4e8e\u5177\u4f53\u5b9e\u73b0\u3002\u8fd9\u53ef\u4ee5\u63d0\u9ad8\u7a0b\u5e8f\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u56db\u5927\u7f16\u7a0b\u8303\u5f0f\u90fd\u5404\u81ea\u53d1\u5c55\u51fa\u4e86 \u4f9d\u8d56\u6ce8\u5165\u539f\u5219 \u7684\u89e3\u51b3\u65b9\u6848\uff1a \u9762\u5411\u8fc7\u7a0b\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u6307\u9488 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u9762\u5411\u5bf9\u8c61\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u865a\u51fd\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u51fd\u6570\u5bf9\u8c61 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u6a21\u677f\u5143\u7f16\u7a0b\u8303\u5f0f\u4e2d\uff0c \u6a21\u677f\u53c2\u6570 \u5c31\u662f\u90a3\u4e2a\u62bd\u8c61\u63a5\u53e3\u3002 \u540c\u6837\u662f\u628a\u62bd\u8c61\u63a5\u53e3\u4f5c\u4e3a\u53c2\u6570\uff0c\u540c\u6837\u89e3\u51b3\u53ef\u6269\u5c55\u95ee\u9898\u3002 \u51fd\u6570\u6307\u9488\u8d34\u8fd1\u5e95\u5c42\u786c\u4ef6\uff0c\u865a\u51fd\u6570\u65b9\u4fbf\u6574\u5408\u591a\u4e2a\u63a5\u53e3\uff0c\u51fd\u6570\u5bf9\u8c61\u8f7b\u91cf\u7ea7\u3001\u968f\u5730\u53d6\u7528\uff0c\u6a21\u677f\u5143\u6709\u52a9\u9ad8\u6027\u80fd\u4f18\u5316\uff0c\u4e0d\u540c\u7684\u7f16\u7a0b\u8303\u5f0f\u6b8a\u9014\u540c\u5f52\u3002","title":"\u4f9d\u8d56\u6ce8\u5165\u539f\u5219"},{"location":"functional/#_14","text":"\u4f9d\u8d56\u6ce8\u5165\u539f\u5219\u53ef\u4ee5\u51cf\u5c11\u4ee3\u7801\u4e4b\u95f4\u7684\u8026\u5408\u5ea6\uff0c\u5927\u5927\u63d0\u9ad8\u4ee3\u7801\u7684\u7075\u6d3b\u6027\u548c\u53ef\u6269\u5c55\u6027\u3002 \u8026\u5408\u5ea6: \u6307\u7684\u662f\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u548c\u5176\u4ed6\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u8026\u5408\u5ea6\u8d8a\u4f4e\uff0c\u8d8a\u5bb9\u6613\u8fdb\u884c\u5355\u5143\u6d4b\u8bd5\u3001\u91cd\u6784\u3001\u590d\u7528\u548c\u6269\u5c55\u3002 \u9ad8\u8026\u5408\u5ea6\u7684\u5178\u578b\u662f\u201c\u7275\u4e00\u53d1\u800c\u52a8\u5168\u8eab\u201d\u3002\u4f4e\u8026\u5408\u7684\u5178\u8303\u662f\u86af\u8693\uff0c\u56e0\u4e3a\u86af\u8693\u53ef\u4ee5\u5728\u4efb\u610f\u65ad\u9762\u5207\u5f00\uff0c\u8fd8\u80fd\u6d3b\u4e0b\u6765\uff0c\u770b\u6765\u86af\u8693\u7684\u8eab\u4f53\u8bbe\u8ba1\u975e\u5e38\u201c\u6a21\u5757\u5316\u201d\u5462\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u8f6f\u4ef6\u5e94\u5f53\u8ffd\u6c42\u4f4e\u8026\u5408\u5ea6\uff0c\u9002\u5ea6\u89e3\u8026\u7684\u8f6f\u4ef6\u80fd\u66f4\u5feb\u9002\u5e94\u9700\u6c42\u53d8\u5316\u3002\u4f46\u8fc7\u5ea6\u7684\u4f4e\u8026\u5408\u4e5f\u4f1a\u5bfc\u81f4\u4ee3\u7801\u8fc7\u4e8e\u5206\u6563\uff0c\u4e0d\u6613\u9605\u8bfb\u548c\u4fee\u6539\uff0c\u751a\u81f3\u53ef\u80fd\u8d77\u5230\u53cd\u6548\u679c\u3002 \u82e5\u4f60\u89e3\u8026\u540e\uff0c\u6bcf\u6b21\u9700\u6c42\u53d8\u5316\u8981\u6539\u52a8\u7684\u5730\u65b9\u53d8\u5c11\u4e86\uff0c\u90a3\u5c31\u662f\u5408\u7406\u7684\u89e3\u8026\u3002\u82e5\u4f60\u8fc7\u5206\u89e3\u8026\uff0c\u4ee3\u7801\u4e1c\u4e00\u5757\u897f\u4e00\u5757\uff0c\u4ee5\u81f3\u4e8e\u9700\u6c42\u53d8\u5316\u65f6\u9700\u8981\u5230\u5904\u6539\uff0c\u6bd4\u4e0d\u89e3\u8026\u65f6\u6d6a\u8d39\u7684\u65f6\u95f4\u8fd8\u8981\u591a\uff0c\u90a3\u5c31\u662f\u89e3\u8026\u8fc7\u5ea6\u3002 \u5b8c\u5168\u96f6\u8026\u5408\u7684\u7a0b\u5e8f\u6bcf\u4e2a\u51fd\u6570\u4e92\u4e0d\u8054\u7cfb\uff0c\u5c31\u50cf\u628a\u86af\u8693\u62c6\u6563\u6210\u4e00\u4e2a\u4e2a\u72ec\u7acb\u7684\u7ec6\u80de\u4e00\u6837\u3002\u8fde\u521d\u59cb\u9700\u6c42\u201c\u6d3b\u7740\u201d\u90fd\u5b9e\u73b0\u4e0d\u4e86\uff0c\u8c08\u4f55\u9002\u5e94\u9700\u6c42\u53d8\u5316\uff1f\u6240\u4ee5\u89e3\u8026\u4e5f\u5207\u52ff\u77eb\u6789\u8fc7\u6b63\u3002 \u4e3a\u4e86\u907f\u514d\u89e3\u8026\u77eb\u6789\u8fc7\u6b63\uff0c\u4eba\u4eec\u53c8\u63d0\u51fa\u4e86\u5185\u805a\u7684\u6982\u5ff5\uff0c\u5e76\u89c4\u5b9a\u89e3\u8026\u7684\u524d\u63d0\u662f\uff1a\u4e0d\u803d\u8bef\u5185\u805a\u3002\u803d\u8bef\u5230\u5185\u805a\u7684\u89e3\u8026\uff0c\u5c31\u53ea\u4f1a\u8d77\u5230\u964d\u4f4e\u53ef\u7ef4\u62a4\u6027\u7684\u53cd\u6548\u679c\u4e86\u3002 \u5185\u805a: \u6307\u7684\u662f\u540c\u4e00\u4e2a\u6a21\u5757\u3001\u7c7b\u3001\u51fd\u6570\u5185\u90e8\u5404\u4e2a\u5143\u7d20\u4e4b\u95f4\u7684\u5173\u8054\u7a0b\u5ea6\u3002\u5185\u805a\u5ea6\u8d8a\u9ad8\uff0c\u529f\u80fd\u8d8a\u72ec\u7acb\uff0c\u8d8a\u65b9\u4fbf\u96c6\u4e2d\u7ef4\u62a4\u3002 \u4f8b\u5982\uff0c\u4eba\u7684\u5fc3\u810f\u4e13\u95e8\u8d1f\u8d23\u6cf5\u8840\uff0c\u809d\u810f\u53ea\u8d1f\u8d23\u89e3\u6bd2\uff0c\u8fd9\u5c31\u662f\u9ad8\u5185\u805a\u7684\u4eba\u4f53\u5668\u5b98\u3002\u82e5\u4eba\u7684\u5fc3\u810f\u8fd8\u8981\u517c\u804c\u89e3\u6bd2\uff0c\u809d\u810f\u8fd8\u517c\u804c\u6cf5\u8840\uff0c\u770b\u4f3c\u597d\u50cf\u662f\u589e\u52a0\u4e86\u201c\u4e07\u4e00\u5fc3\u810f\u574f\u6389\u201d\u7684\u5197\u4f59\u6027\uff0c\u5b9e\u9645\u4e0a\u628a\u201c\u6cf5\u8840\u201d\u8fd9\u4e00\u529f\u80fd\u62c6\u6563\u5230\u5404\u5730\uff0c\u65e0\u6cd5\u201c\u96c6\u4e2d\u529b\u91cf\u6cf5\u5927\u8840\u201d\u4e86\u3002 \u4eba\u7c7b\u7684\u5927\u8111\u548c CPU \u4e00\u6837\uff0c\u4e5f\u6709\u201c\u7f13\u5b58\u5c40\u57df\u6027 (cache-locality)\u201d\u7684\u9650\u5236\uff1a\u4e0d\u80fd\u540c\u65f6\u5728\u5f88\u591a\u4e2a\u4e3b\u9898\u4e4b\u95f4\u5feb\u901f\u5207\u6362\uff0c\u65e0\u8bba\u662f\u65f6\u95f4\u4e0a\u7684\u8fd8\u662f\u7a7a\u95f4\u4e0a\u7684\u5272\u88c2 (cache-miss)\uff0c\u90fd\u4f1a\u5e72\u6270\u7a0b\u5e8f\u5458\u601d\u7ef4\u7684\u8fde\u8d2f\u6027\uff0c\u4ece\u800c\u589e\u5927\u5fc3\u667a\u8d1f\u62c5\u3002 \u597d\u7684\u8f6f\u4ef6\u8981\u4fdd\u6301\u4f4e\u8026\u5408\uff0c\u540c\u65f6\u9ad8\u5185\u805a\u3002 \u5c31\u50cf\u201c\u6c11\u4e3b\u96c6\u4e2d\u5236\u201d\u4e00\u6837\uff0c\u65e2\u8981\u76d1\u7763\u9632\u6b62\u5927\u6743\u72ec\u63fd\uff0c\u53c8\u8981\u96c6\u4e2d\u529b\u91cf\u529e\u4e00\u4e2a\u4eba\u529e\u4e0d\u6210\u7684\u5927\u4e8b\u3002","title":"\u4f4e\u8026\u5408\uff0c\u9ad8\u5185\u805a"},{"location":"functional/#_15","text":"\u4f20\u7edf\u7684\u9762\u5411\u5bf9\u8c61\u540c\u6837\u53ef\u4ee5\u7528 \u865a\u51fd\u6570\u63a5\u53e3\u7c7b \u6a21\u62df \u51fd\u6570\u5bf9\u8c61 \u4e00\u6837\u7684\u529f\u80fd\uff0c\u53ea\u4e0d\u8fc7\u6ca1\u6709 lambda \u548c\u95ed\u5305\u7684\u8bed\u6cd5\u52a0\u6301\uff0c\u5199\u8d77\u6765\u975e\u5e38\u7e41\u7410\uff0c\u5c31\u548c\u5728 C \u8bed\u8a00\u91cc\u201c\u6a21\u62df\u201d\u9762\u5411\u5bf9\u8c61\u4e00\u6837\u3002 \u4e3a\u4e86\u8fd9\u4e48\u5c0f\u7684\u4e00\u4e2a\u4ee3\u7801\u5757\uff0c\u5355\u72ec\u5b9a\u4e49\u4e00\u4e2a\u7c7b\uff0c\u5c31\u50cf\u5988\u5988\u5f00\u4e00\u67b6\u201c\u7a7a\u4e2d\u6218\u8f66\u201d A380 \u53ea\u662f\u4e3a\u4e86\u63a5\u4f60\u653e\u5b66\u4e00\u6837\uff0c\u7b49\u4f60\u503c\u597d\u673a\u7684\u65f6\u95f4\u6211\u81ea\u5df1\u8d70\u90fd\u8d70\u5230\u4e86\u3002\u800c\u51fd\u6570\u5f0f\u4e2d\uff0c\u7528 lambda \u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u76f8\u5f53\u4e8e\u968f\u5730\u6293\u6765\u4e00\u53f0\u5171\u4eab\u5355\u8f66\u5f00\u8d70\u3002 struct OpBase { // \u9762\u5411\u5bf9\u8c61\uff1a\u9047\u4e8b\u4e0d\u51b3\u5148\u5b9a\u4e49\u63a5\u53e3\u2026\u2026 virtual int compute(int a, int b) = 0; virtual ~OpBase() = default; }; struct OpAdd : OpBase { int compute(int a, int b) override { return a + b; } }; struct OpMul : OpBase { int compute(int a, int b) override { return a * b; } }; struct OpMax : OpBase { int compute(int a, int b) override { return std::max(a, b); } }; int generic_sum(std::vector const &v, OpBase *op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op->compute(ret, v[i]); // \u5199\u8d77\u6765\u4e5f\u9ebb\u70e6\uff0c\u9700\u8981\u8c03\u7528\u4ed6\u7684\u6210\u5458\u51fd\u6570\uff0c\u6210\u5458\u51fd\u6570\u53c8\u8981\u8d77\u540d\u2026\u2026 } delete op; return ret; } int main() { std::vector a = {1, 2, 3, 4}; generic_sum(a, new OpAdd()); generic_sum(a, new OpMul()); generic_sum(a, new OpMax()); return 0; } \u4e0d\u4ec5\u9700\u8981\u5b9a\u4e49\u4e00\u5806\u7c7b\uff0c\u63a5\u53e3\u7c7b\uff0c\u5b9e\u73b0\u7c7b\uff0c\u7ee7\u627f\u6765\u7ee7\u627f\u53bb\uff0c\u8fd8\u9700\u8981\u7ba1\u7406\u8ba8\u538c\u7684\u6307\u9488\uff0c\u4ee3\u7801\u91cf\u7ffb\u500d\uff0c\u6ca1\u4ec0\u4e48\u53ef\u8bfb\u6027\uff0c\u53c8\u5f71\u54cd\u8fd0\u884c\u6548\u7387\u3002 3 \u5e74 2 \u73ed\u5c0f\u5f6d\u540c\u5b66\uff0c\u4f60\u7684\u5988\u5988\u5f00\u7740 A380 \u6765\u63a5\u4f60\u4e86\u3002 \u800c\u73b0\u4ee3 C++ \u53ea\u9700 Lambda \u8bed\u6cd5\u5c31\u5730\u5b9a\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u723d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); });","title":"\u4e0e\u4f20\u7edf\u9762\u5411\u5bf9\u8c61\u7684\u5bf9\u6bd4"},{"location":"functional/#_16","text":"\u521a\u521a\uff0c\u6211\u4eec\u7684\u5b9e\u73b0\u7528\u4e86 auto op \u505a\u53c2\u6570\uff0c\u8fd9\u7b49\u4ef7\u4e8e\u8ba9 generic_sum \u53d8\u6210\u4e00\u4e2a\u6a21\u677f\u51fd\u6570\u3002 int generic_sum(std::vector const &v, auto op); // \u4e0d\u652f\u6301 C++20 \u65f6\u7684\u66ff\u4ee3\u5199\u6cd5\uff1a template int generic_sum(std::vector const &v, Op op); \u8fd9\u610f\u5473\u7740\u6bcf\u5f53\u7528\u6237\u6307\u5b9a\u4e00\u4e2a\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\uff08lambda\uff09\u65f6\uff0c generic_sum \u90fd\u4f1a\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u904d\u3002 generic_sum(a, [](int a, int b) { return a + b; }); generic_sum(a, [](int a, int b) { return a * b; }); generic_sum(a, [](int a, int b) { return std::max(a, b); }); \u7f16\u8bd1\u540e\uff0c\u4f1a\u53d8\u6210\u7c7b\u4f3c\u4e8e\u8fd9\u6837\uff1a generic_sum(a); generic_sum(a); generic_sum(a); \u4f1a\u751f\u6210\u4e09\u4efd\u51fd\u6570\uff0c\u6bcf\u4e2a\u90fd\u662f\u72ec\u7acb\u7f16\u8bd1\u7684\uff1a int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = add(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = mul(ret, v[i]); } return ret; } int generic_sum(std::vector const &v) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = max(ret, v[i]); } return ret; } \u8fd9\u5141\u8bb8\u7f16\u8bd1\u5668\u4e3a\u6bcf\u4e2a\u7248\u672c\u7684 generic_sum \u5355\u72ec\u505a\u4f18\u5316\uff0c\u91cf\u8eab\u5b9a\u5236\u6700\u4f18\u7684\u4ee3\u7801\u3002 \u4f8b\u5982 add \u8fd9\u4e2a\u51fd\u6570\u5bf9\u8c61\uff0c\u56e0\u4e3a\u53ea\u5728 generic_sum \u4e2d\u4f7f\u7528\u4e86\uff0c\u4f1a\u88ab\u88ab\u7f16\u8bd1\u5668\u81ea\u52a8\u5185\u8054\uff0c\u4e0d\u4f1a\u4ea7\u751f\u51fd\u6570\u8c03\u7528\u548c\u8df3\u8f6c\u7684\u6307\u4ee4\uff0c\u5404\u81ea\u4f18\u5316\u6210\u5355\u72ec\u4e00\u6761\u52a0\u6cd5 / \u4e58\u6cd5 / \u6700\u5927\u503c\u6307\u4ee4\u7b49\u3002 \u6bd4\u5982\uff0c\u7f16\u8bd1\u5668\u4f1a\u68c0\u6d4b\u5230 += \u53ef\u4ee5\u77e2\u91cf\u5316\uff0c\u4e8e\u662f\u7528 _mm_add_epi32 \u66ff\u4ee3\u4e86\u3002\u540c\u7406\uff0cmul \u5219\u7528 _mm_mullo_epi32 \u66ff\u4ee3\uff0cmax \u5219\u7528 _mm_max_epi32 \u66ff\u4ee3\u7b49\uff0c\u5404\u81ea\u5206\u522b\u751f\u6210\u4e86\u5404\u81ea\u7248\u672c\u6700\u4f18\u7684\u4ee3\u7801\u3002\u800c\u5982\u679c\u662f\u666e\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u4e0d\u4f1a\u751f\u6210\u4e09\u4efd\u91cf\u8eab\u5b9a\u505a\u7684\u5b9e\u4f8b\uff0c\u65e0\u6cd5\u77e2\u91cf\u5316\uff08\u6709\u4e00\u79cd\u4f8b\u5916\uff0c\u5c31\u662f\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u4e86 generic_sum \u4f3c\u4e4e\u53ea\u6709\u8fd9\u4e09\u79cd\u53ef\u80fd\u53c2\u6570\uff0c\u7136\u540e\u505a\u4e86 IPO \u4f18\u5316\uff0c\u4f46\u5e76\u4e0d\u5982\u6a21\u677f\u5b9e\u4f8b\u5316\u4e00\u6837\u7a33\u5b9a\u5f3a\u5236\uff09\u3002 \u4e3a\u4e09\u79cd\u4e0d\u540c\u7684 op \u53c2\u6570\u5206\u522b\u5b9a\u505a\u4e09\u4efd\u3002\u867d\u7136\u589e\u52a0\u4e86\u7f16\u8bd1\u65f6\u95f4\uff0c\u81a8\u80c0\u4e86\u751f\u6210\u7684\u4e8c\u8fdb\u5236\u4f53\u79ef\uff1b\u4f46\u751f\u6210\u7684\u673a\u5668\u7801\u662f\u5206\u522b\u9488\u5bf9\u6bcf\u79cd\u7279\u4f8b\u4e00\u5bf9\u4e00\u6df1\u5ea6\u4f18\u5316\u7684\uff0c\u66f4\u9ad8\u6548\u3002 \u4f8b\u5982\u77e9\u9635\u4e58\u6cd5\uff08gemm\uff09\u7684\u6700\u4f18\u7b97\u6cd5\uff0c\u5bf9\u4e8e\u4e0d\u540c\u7684\u77e9\u9635\u5927\u5c0f\u548c\u5f62\u72b6\u662f\u4e0d\u540c\u7684\u3002\u8457\u540d\u7684\u7ebf\u6027\u4ee3\u6570\u5e93 CUBLAS \u548c MKL \u4e2d\uff0c\u4f1a\u81ea\u52a8\u6839\u636e\u7528\u6237\u8f93\u5165\u7684\u77e9\u9635\u5f62\u72b6\uff0c\u9009\u53d6\u6700\u4f18\u7684\u7b97\u6cd5\u3002\u4e5f\u5c31\u662f\u8bf4\uff0cCUBLAS \u5e93\u91cc\u5176\u5b9e\u5b58\u7740\u9002\u5408\u5404\u79cd\u77e9\u9635\u5927\u5c0f\u6392\u5217\u7ec4\u5408\u7684\u7b97\u6cd5\u4ee3\u7801\uff08\u4ee5 fatbin \u683c\u5f0f\u5b58\u50a8\u5728\u4e8c\u8fdb\u5236\u4e2d\uff09\u3002\u5f53\u8c03\u7528\u77e9\u9635\u4e58\u6cd5\u65f6\uff0c\u81ea\u52a8\u67e5\u5230\u6700\u9002\u5408\u7684\u4e00\u7248\u6765\u8c03\u7528\u7ed9\u4f60\u3002\u7c7b\u4f3c gemm\uff0c\u8fd8\u6709 gemv\u3001spmv\u2026\u2026\u6240\u6709\u7684\u77e9\u9635\u8fd0\u7b97 API \u90fd\u7ecf\u5386\u4e86\u8fd9\u6837\u7684\u201c\u7f16\u8bd1\u671f\u201d\u66b4\u529b\u6392\u5217\u7ec4\u5408\uff0c\u53ea\u4e3a\u201c\u8fd0\u884c\u65f6\u201d\u91ca\u653e\u6700\u5927\u6027\u80fd\uff01\u8fd9\u4e5f\u5bfc\u81f4\u7f16\u8bd1\u597d\u7684 cublas.dll \u6587\u4ef6\u6765\u5230\u4e86\u6050\u6016\u7684 20 MB \u5de6\u53f3\uff0c\u800c\u6211\u4eec\u79f0\u4e4b\u4e3a\u9ad8\u6548\u3002","title":"\u51fd\u6570\u5bf9\u8c61\u5728\u6a21\u677f\u52a0\u6301\u4e0b\u9759\u6001\u5206\u53d1"},{"location":"functional/#function","text":"Lambda \u51fd\u6570\u5bf9\u8c61\u7684\u7c7b\u578b\u662f\u533f\u540d\u7684\uff0c\u6bcf\u4e2a Lambda \u8868\u8fbe\u5f0f\u90fd\u4f1a\u521b\u5efa\u4e00\u4e2a\u5168\u65b0\u7684\u51fd\u6570\u5bf9\u8c61\u7c7b\u578b\uff0c\u8fd9\u4f7f\u5f97 generic_sum \u5bf9\u4e8e\u6bcf\u4e2a\u4e0d\u540c\u7684 Lambda \u90fd\u4f1a\u5b9e\u4f8b\u5316\u4e00\u904d\u3002\u867d\u7136\u6709\u5229\u4e8e\u6027\u80fd\u4f18\u5316\uff0c\u4f46\u4e5f\u5f71\u54cd\u4e86\u7f16\u8bd1\u901f\u5ea6\u548c\u7075\u6d3b\u6027\u3002 \u901a\u5e38\uff0c\u6211\u4eec\u53ea\u80fd\u901a\u8fc7 decltype(add) \u83b7\u53d6 add \u8fd9\u4e2a Lambda \u5bf9\u8c61\u7684\u7c7b\u578b\u3002\u4e5f\u53ea\u80fd\u901a\u8fc7 auto \u6765\u6355\u83b7 Lambda \u5bf9\u8c61\u4e3a\u53d8\u91cf\u3002 \u4e3a\u6b64\uff0c\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::function \u5bb9\u5668\uff0c\u4ed6\u80fd\u5bb9\u7eb3\u4efb\u4f55\u51fd\u6570\u5bf9\u8c61\uff01\u65e0\u8bba\u662f\u533f\u540d\u7684 Lambda \u51fd\u6570\u5bf9\u8c61\uff0c\u8fd8\u662f\u666e\u666e\u901a\u901a\u7684\u51fd\u6570\u6307\u9488\uff0c\u90fd\u80fd\u7eb3\u5165 std::function \u7684\u4f53\u5185\u3002 \u552f\u4e00\u7684\u4ee3\u4ef7\u662f\uff0c\u4f60\u9700\u8981\u6307\u5b9a\u51fa\u6240\u6709\u53c2\u6570\u7684\u7c7b\u578b\uff0c\u548c\u8fd4\u56de\u503c\u7684\u7c7b\u578b\u3002 \u4f8b\u5982\u4e00\u4e2a\u53c2\u6570\u4e3a\u4e24\u4e2a int \uff0c std::function auto add_lambda = [](int a, int b) { // Lambda \u51fd\u6570\u5bf9\u8c61 return a + b; }; struct AddClass { int operator()(int a, int b) { // \u81ea\u5b9a\u4e49\u7c7b\u6a21\u62df\u51fd\u6570\u5bf9\u8c61 return a + b; } }; AddClass add_object; int add_regular_func(int a, int b) { // \u666e\u901a\u51fd\u6570 return a + b; } std::function add; // \u6240\u6709\u5e7f\u4e49\u51fd\u6570\u5bf9\u8c61\uff0c\u7edf\u7edf\u63a5\u7eb3 add = add_lambda; // OK add = add_object; // OK add = add_regular_func; // OK int generic_sum(std::vector const &v, std::function op) { int ret = v[0]; for (int i = 1; i < v.size(); ++i) { ret = op(ret, v[i]); // \u5199\u8d77\u6765\u548c\u6a21\u677f\u4f20\u53c2\u65f6\u4e00\u6837\u65e0\u611f } // \u65e0\u9700\u6307\u9488\uff0c\u65e0\u9700 delete\uff0cfunction \u80fd\u81ea\u52a8\u7ba1\u7406\u51fd\u6570\u5bf9\u8c61\u751f\u547d\u5468\u671f return ret; } \u5982\u679c\u8fd8\u60f3\u652f\u6301\u4efb\u610f\u7c7b\u578b\u7684\u53c2\u6570\u548c\u8fd4\u56de\u503c\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u8bd5\u8bd5\u770b std::function \u3002\u8fd9\u91cc std::any \u662f\u4e2a\u8d85\u7ea7\u4e07\u80fd\u5bb9\u5668\uff0c\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u4f55\u5bf9\u8c61\uff0c\u4ed6\u548c std::function \u4e00\u6837\u90fd\u91c7\u7528\u4e86\u201c\u7c7b\u578b\u64e6\u9664 (type-erasure)\u201d\u6280\u672f\uff0c\u7f3a\u70b9\u662f\u5fc5\u987b\u914d\u5408 std::any_cast \u624d\u80fd\u53d6\u51fa\u4f7f\u7528\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u8fdb\u9636\u4e13\u9898\u4e2d\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u4ed6\u4eec\u7684\u539f\u7406\uff0c\u5e76\u5e26\u4f60\u81ea\u5df1\u505a\u4e00\u4e2a\u64e6\u52a0\u6cd5\u7684\u7c7b\u578b\u64e6\u9664\u5bb9\u5668\u3002 \u51fd\u6570\u5f0f\u7f16\u7a0b\uff0c\u80fd\u5728\u9759\u6001\u4e0e\u52a8\u6001\u4e4b\u95f4\u8f7b\u677e\u5207\u6362\uff0c \u9ad8\u6027\u80fd \u4e0e \u7075\u6d3b\u6027 \u4efb\u541b\u9009\u62e9\u3002 \u5728\u9700\u8981\u6027\u80fd\u7684 \u74f6\u9888\u4ee3\u7801 \u4e2d\u7528\u6a21\u677f\u4f20\u53c2\uff0c\u7f16\u8bd1\u671f\u9759\u6001\u5206\u53d1\uff0c\u591a\u6b21\u91cf\u8eab\u5b9a\u505a\uff0c\u63d0\u9ad8\u8fd0\u884c\u65f6\u6027\u80fd\u3002 \u74f6\u9888\u4ee3\u7801: \u5f80\u5f80\u4e00\u4e2a\u7a0b\u5e8f 80% \u7684\u65f6\u95f4\u82b1\u5728 20% \u7684\u4ee3\u7801\u4e0a\u3002\u8fd9 20% \u662f\u5728\u7a0b\u5e8f\u4e2d\u9891\u7e41\u6267\u884c\u7684\u3001\u8ba1\u7b97\u91cf\u5927\u7684\u3001\u6216\u8005\u8c03\u7528\u7279\u522b\u8017\u65f6\u7684\u51fd\u6570\u3002\u9488\u5bf9\u8fd9\u90e8\u5206\u74f6\u9888\u4ee3\u7801\u4f18\u5316\u5373\u53ef\uff0c\u800c\u5269\u4f59\u7684 80% \u6253\u9171\u6cb9\u4ee3\u7801\uff0c\u5927\u53ef\u4ee5\u600e\u4e48\u65b9\u4fbf\u600e\u4e48\u5199\u3002 \u5728\u6027\u80fd\u65e0\u5173\u7d27\u8981\u7684\u9876\u5c42\u4e1a\u52a1\u903b\u8f91\u4e2d\u7528 function \u5bb9\u5668\u4f20\u53c2\uff0c\u8fd0\u884c\u65f6\u52a8\u6001\u5206\u53d1\uff0c\u8282\u7701\u7f16\u8bd1\u4f53\u79ef\uff0c\u65b9\u4fbf\u6301\u4e45\u5b58\u50a8\uff0c\u7075\u6d3b\u6613\u7528\u3002 \u4f8b\u5982\u4e0a\u9762\u7684 generic_sum \u51fd\u6570\uff0c\u5982\u679c\u6211\u4eec\u7a81\u7136\u60f3\u8981\u9ad8\u6027\u80fd\u4e86\uff0c\u53ea\u9700\u628a std::function op \u8f7b\u8f7b\u6539\u4e3a auto op \u5c31\u8f7b\u677e\u5207\u6362\u5230\u9759\u6001\u5206\u53d1\u6a21\u5f0f\u4e86\u3002 \u800c\u865a\u51fd\u6570\u4e00\u65e6\u7528\u4e86\uff0c\u57fa\u672c\u5c31\u53ea\u80fd\u52a8\u6001\u5206\u53d1\u4e86\uff0c\u5373\u4f7f\u80fd\u88ab IPO \u4f18\u5316\u6389\uff0c\u865a\u8868\u6307\u9488\u4e5f\u6c38\u8fdc\u5360\u636e\u7740\u4e00\u4e2a 8 \u5b57\u8282\u7684\u7a7a\u95f4\uff0c\u4e14\u6c38\u8fdc\u53ea\u80fd\u4ee5\u6307\u9488\u5f62\u5f0f\u4f20\u6765\u4f20\u53bb\u3002 \u4e00\u79cd\u9759\u6001\u5206\u53d1\u7248\u7684\u865a\u51fd\u6570\u66ff\u4ee3\u54c1\u662f CRTP\uff0c\u4ed6\u57fa\u4e8e\u6a21\u677f\u5143\u7f16\u7a0b\uff0c\u4f46\u4e0e\u865a\u51fd\u6570\u4e4b\u95f4\u5207\u6362\u56f0\u96be\uff0c\u4e0d\u50cf\u51fd\u6570\u5bf9\u8c61\u90a3\u4e48\u65e0\u611f\uff0c\u4e4b\u540e\u7684\u6a21\u677f\u5143\u4e13\u9898\u8bfe\u4e2d\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u3002","title":"\u51fd\u6570\u5bf9\u8c61\u4e5f\u53ef\u5728 function \u5bb9\u5668\u4e2d\u52a8\u6001\u5206\u53d1"},{"location":"functional/#_17","text":"mt_queue> task_queue; void thread1() { task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a11\"); }); task_queue.push([] { fmt::println(\"\u6b63\u5728\u6267\u884c\u4efb\u52a12\"); }); } void thread2() { while (true) { auto task = task_queue.pop(); task(); } } mt_queue \u662f\u5c0f\u5f6d\u8001\u5e08\u5c01\u88c5\u7684\u591a\u7ebf\u7a0b\u5b89\u5168\u7684\u6d88\u606f\u961f\u5217\uff0c\u5b9e\u73b0\u539f\u7406\u4f1a\u5728\u7a0d\u540e\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4e2d\u8be6\u7ec6\u8bb2\u89e3\u3002","title":"\u6848\u4f8b\uff1a\u51fd\u6570\u5bf9\u8c61\u7684\u52a8\u6001\u5206\u53d1\u7528\u4e8e\u591a\u7ebf\u7a0b\u4efb\u52a1\u961f\u5217"},{"location":"functional/#_18","text":"","title":"\u51fd\u6570\u5bf9\u8c61\u7684\u91cd\u8981\u673a\u5236\uff1a\u95ed\u5305"},{"location":"functional/#c","text":"","title":"\u51fd\u6570\u6307\u9488\u662f C \u8bed\u8a00\u964b\u4e60\uff0c\u6539\u6389"},{"location":"functional/#bind","text":"int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello(2, 3) \u7ed3\u679c\uff1a{}\", hello(2, 3)); fmt::println(\"main \u8c03\u7528 hello(2, 4) \u7ed3\u679c\uff1a{}\", hello(2, 4)); fmt::println(\"main \u8c03\u7528 hello(2, 5) \u7ed3\u679c\uff1a{}\", hello(2, 5)); return 0; } int hello(int x, int y) { fmt::println(\"hello({}, {})\", x, y); return x + y; } int main() { fmt::println(\"main \u8c03\u7528 hello2(3) \u7ed3\u679c\uff1a{}\", hello2(3)); fmt::println(\"main \u8c03\u7528 hello2(4) \u7ed3\u679c\uff1a{}\", hello2(4)); fmt::println(\"main \u8c03\u7528 hello2(5) \u7ed3\u679c\uff1a{}\", hello2(5)); return 0; }","title":"bind \u4e3a\u51fd\u6570\u5bf9\u8c61\u7ed1\u5b9a\u53c2\u6570"},{"location":"functions/","text":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u81ea\u5b9a\u4e49\u51fd\u6570 \u81ea\u5b9a\u4e49\u51fd\u6570 \u51fd\u6570\u53ef\u4ee5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u7c7b\u578b\u5199 void \u5373\u53ef\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7684\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u4ed6\u7684\u526f\u4f5c\u7528\uff08\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8f93\u51fa\u6587\u672c\u5230\u63a7\u5236\u53f0\uff0c\u4fee\u6539\u5f15\u7528\u53c2\u6570\u7b49\uff09\u3002 void compute() { return; } \u5bf9\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a void \uff09\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u7565 return \u4e0d\u5199\u3002 \u5bf9\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u5199 return \u8bed\u53e5\uff0c\u5426\u5219\u7a0b\u5e8f\u51fa\u9519\u3002 TODO\uff1a\u66f4\u591a\u4ecb\u7ecd\u51fd\u6570","title":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5)"},{"location":"functions/#_1","text":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5) \u81ea\u5b9a\u4e49\u51fd\u6570","title":"\u8ba4\u8bc6\u51fd\u6570 (\u672a\u5b8c\u5de5)"},{"location":"functions/#_2","text":"\u51fd\u6570\u53ef\u4ee5\u6ca1\u6709\u8fd4\u56de\u503c\uff0c\u53ea\u9700\u8981\u8fd4\u56de\u7c7b\u578b\u5199 void \u5373\u53ef\uff0c\u8fd9\u6837\u7684\u51fd\u6570\u8c03\u7528\u7684\u76ee\u7684\u53ea\u662f\u4e3a\u4e86\u4ed6\u7684\u526f\u4f5c\u7528\uff08\u5982\u4fee\u6539\u5168\u5c40\u53d8\u91cf\uff0c\u8f93\u51fa\u6587\u672c\u5230\u63a7\u5236\u53f0\uff0c\u4fee\u6539\u5f15\u7528\u53c2\u6570\u7b49\uff09\u3002 void compute() { return; } \u5bf9\u4e8e\u6ca1\u6709\u8fd4\u56de\u503c\uff08\u8fd4\u56de\u7c7b\u578b\u4e3a void \uff09\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7701\u7565 return \u4e0d\u5199\u3002 \u5bf9\u4e8e\u6709\u8fd4\u56de\u503c\u7684\u51fd\u6570\uff0c\u5fc5\u987b\u5199 return \u8bed\u53e5\uff0c\u5426\u5219\u7a0b\u5e8f\u51fa\u9519\u3002 TODO\uff1a\u66f4\u591a\u4ecb\u7ecd\u51fd\u6570","title":"\u81ea\u5b9a\u4e49\u51fd\u6570"},{"location":"helloworld/","text":"\u4f60\u597d\uff0c\u4e16\u754c \u4f60\u597d\uff0c\u4e16\u754c \u4ec0\u4e48\u662f\u51fd\u6570 \u4ece main \u51fd\u6570\u8bf4\u8d77 main \u51fd\u6570\u7684\u8fd4\u56de\u503c \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f \u6253\u5370\u4e00\u4e9b\u4fe1\u606f \u6ce8\u91ca \u4ec0\u4e48\u662f\u51fd\u6570 \u51fd\u6570: \u4e00\u6bb5\u7528 {} \u5305\u88f9\u7684\u4ee3\u7801\u5757\uff0c\u6709\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u540d\u5b57\u505a\u6807\u8bc6\u3002\u51fd\u6570\u53ef\u4ee5\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002\u51fd\u6570\u7684 {} \u4ee3\u7801\u5757\u5185\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u6bcf\u6b21\u8be5\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u6267\u884c\u3002 int compute() { return 42; } \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c compute \u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c int \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u6574\u6570\u3002 \u4e43\u53d6\u6574\u6570\u4e4b\u82f1\u6587\u201cinteger\u201d\u7684\u201cint\u201d\u800c\u5f97\u540d\uff08\u6a21\u4eff\u4faf\u6377\u8001\u5e08\u8bf4\u8bdd\uff09 \u800c {} \u5305\u88f9\u7684\u662f\u51fd\u6570\u4f53\uff0c\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u3002 \u6b64\u5904 return 42 \u5c31\u662f\u51fd\u6570\u4f53\u5185\u7684\u552f\u4e00\u4e00\u6761\u8bed\u53e5\uff0c\u8868\u793a\u51fd\u6570\u7acb\u5373\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd4\u56de 42\u3002 \u8fd4\u56de\u503c: \u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4f1a\u5411\u8c03\u7528\u8be5\u51fd\u6570\u7684\u8c03\u7528\u8005\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f return \u540e\u9762\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6b64\u5904 compute \u7684\u8fd4\u56de\u7c7b\u578b\u662f int \uff0c\u4e5f\u5c31\u662f\u8bf4 compute \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002 \u5173\u4e8e\u51fd\u6570\u7684\u53c2\u6570\uff0c\u6211\u4eec\u7a0d\u540e\u518d\u505a\u8bf4\u660e\u3002 \u4ece main \u51fd\u6570\u8bf4\u8d77 C++ \u7a0b\u5e8f\u901a\u5e38\u7531\u4e00\u7cfb\u5217\u51fd\u6570\u7ec4\u6210\uff0c\u5176\u4e2d\u5fc5\u987b\u6709\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002 main \u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a int main() { } \u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u8c03\u7528 main \u51fd\u6570\u3002 \u4e25\u683c\u6765\u8bf4\uff0c\u662f C++ \u8fd0\u884c\u65f6\u8c03\u7528\u4e86 main \u51fd\u6570\uff0c\u4f46\u76ee\u524d\u5148\u7406\u89e3\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\u4e86 main \u51fd\u6570\u201d\u4e5f\u65e0\u59a8\u3002 \u8981\u628a\u7a0b\u5e8f\u53d1\u5c55\u58ee\u5927\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9 main \u51fd\u6570\u7ee7\u7eed\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u6574\u4e2a\u7a0b\u5e8f\u7684\u903b\u8f91\uff08\u4e0d\u63a8\u8350\uff09\u3002 \u56e0\u6b64\uff0c main \u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u201c\u5b87\u5b99\u5927\u7206\u70b8\u201d\u3002 main \u51fd\u6570\u7684\u8fd4\u56de\u503c int main() { return 0; } return \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\uff0cmain \u51fd\u6570\u8fd4\u56de\uff0c\u5373\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u7ed3\u675f\u3002 main \u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u6574\u6570 ( int \u7c7b\u578b)\uff0c\u7528\u8fd9\u4e2a\u6574\u6570\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u793a\u7a0b\u5e8f\u9000\u51fa\u7684\u539f\u56e0\u3002 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\u5b8c\u6bd5\uff0c\u6b63\u5e38\u7ed3\u675f\u9000\u51fa\uff0c\u90a3\u5c31\u8bf7\u8fd4\u56de 0\u3002 \u8fd4\u56de\u4e00\u4e2a\u4e0d\u4e3a 0 \u7684\u6574\u6570\u53ef\u4ee5\u8868\u793a\u7a0b\u5e8f\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u662f\u56e0\u4e3a\u51fa\u9519\u4e86\u624d\u9000\u51fa\u7684\uff0c\u503c\u7684\u591a\u5c11\u53ef\u4ee5\u7528\u4e8e\u8868\u660e\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\u3002 \u64cd\u4f5c\u7cfb\u7edf\uff1a\u6211\u8c03\u7528\u4e86\u4f60\u8fd9\u4e2a\u7a0b\u5e8f\u7684 main \u51fd\u6570\uff0c\u6211\u597d\u5947\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u4e86\uff1f\u8ba9\u6211\u4eec\u7ea6\u5b9a\u597d\uff1a\u5982\u679c\u4f60\u8fd0\u8f6c\u6b63\u5e38\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de0\u8868\u793a\u6210\u529f\u54e6\uff01\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u4ee3\u7801\uff0c\u6bd4\u5982\u8fd4\u56de1\u8868\u793a\u65e0\u6743\u9650\uff0c2\u8868\u793a\u627e\u4e0d\u5230\u6587\u4ef6\u2026\u2026\u4e4b\u7c7b\u7684\u3002\u5f53\u7136\uff0c\u9519\u8bef\u4ee3\u7801\u90fd\u662f\u4e0d\u4e3a0\u7684\u3002 \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f TODO: \u4ecb\u7ecd\u63a7\u5236\u53f0 \u6253\u5370\u4e00\u4e9b\u4fe1\u606f int main() { std::println(\"Hello, World!\"); } \u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa Hello, World! \u3002 \u6ce8\u91ca int main() { // \u5c0f\u5f6d\u8001\u5e08\uff0c\u8bf7\u4f60\u5728\u8fd9\u91cc\u63d2\u5165\u7a0b\u5e8f\u7684\u903b\u8f91\u54e6\uff01 } \u8fd9\u91cc\u7684 // \u662f\u6ce8\u91ca\uff0c\u6ce8\u91ca\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u690d\u5165\u63cf\u8ff0\u6027\u7684\u6587\u672c\u3002\u6709\u65f6\u4e5f\u4f1a\u7528\u4e8e\u591a\u4eba\u534f\u4f5c\u9879\u76ee\u4e2d\u7a0b\u5e8f\u5458\u4e4b\u95f4\u4e92\u76f8\u6c9f\u901a\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u8bf6\u563f\u4f60\u770b\u4e0d\u89c1\u6211 } \u5728\u7f16\u8bd1\u5668\u770b\u6765\u5c31\u53ea\u662f\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); } (* \u7f16\u8bd1\u5668\u8138\u7ea2\u4e2d* ) C++ \u652f\u6301\u884c\u6ce8\u91ca // xx \u548c\u5757\u6ce8\u91ca /* xx */ \u4e24\u79cd\u8bed\u6cd5\u3002 int main() { // \u6211\u662f\u884c\u6ce8\u91ca /* \u6211\u662f\u5757\u6ce8\u91ca */ /* \u5757\u6ce8\u91ca \u53ef\u4ee5 \u6709 \u5f88\u591a\u884c */ std::println(/* \u5757\u6ce8\u91ca\u4e5f\u53ef\u4ee5\u5939\u5728\u4ee3\u7801\u4e2d\u95f4 */\"\u4f60\u597d\"); std::println(\"\u4e16\u754c\"); // \u884c\u6ce8\u91ca\u53ea\u80fd\u8ffd\u52a0\u5728\u4e00\u884c\u7684\u672b\u5c3e std::println(\"\u65e9\u5b89\"); } \u5728\u6211\u4eec\u4ee5\u540e\u7684\u6848\u4f8b\u4ee3\u7801\u4e2d\uff0c\u90fd\u4f1a\u50cf\u8fd9\u6837\u6ce8\u91ca\u8bf4\u660e\uff0c\u5145\u5f53 \u5c31\u5730\u8bb2\u89e3\u5458 \u7684\u6548\u679c\u3002\u53bb\u9664\u8fd9\u4e9b\u6ce8\u91ca\u5e76\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6dfb\u52a0\u6587\u5b57\u6ce8\u91ca\u53ea\u662f\u5c0f\u5f6d\u8001\u5e08\u4e3a\u4e86\u63d0\u9192\u4f60\u6bcf\u4e00\u884c\u7684\u4ee3\u7801\u4f5c\u7528\u3002","title":"\u4f60\u597d\uff0c\u4e16\u754c"},{"location":"helloworld/#_1","text":"\u4f60\u597d\uff0c\u4e16\u754c \u4ec0\u4e48\u662f\u51fd\u6570 \u4ece main \u51fd\u6570\u8bf4\u8d77 main \u51fd\u6570\u7684\u8fd4\u56de\u503c \u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f \u6253\u5370\u4e00\u4e9b\u4fe1\u606f \u6ce8\u91ca","title":"\u4f60\u597d\uff0c\u4e16\u754c"},{"location":"helloworld/#_2","text":"\u51fd\u6570: \u4e00\u6bb5\u7528 {} \u5305\u88f9\u7684\u4ee3\u7801\u5757\uff0c\u6709\u4e00\u4e2a\u72ec\u4e00\u65e0\u4e8c\u7684\u540d\u5b57\u505a\u6807\u8bc6\u3002\u51fd\u6570\u53ef\u4ee5\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002\u51fd\u6570\u53ef\u4ee5\u6709\u8fd4\u56de\u503c\u548c\u53c2\u6570\u3002\u51fd\u6570\u7684 {} \u4ee3\u7801\u5757\u5185\u7684\u7a0b\u5e8f\u4ee3\u7801\uff0c\u6bcf\u6b21\u8be5\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u90fd\u4f1a\u6267\u884c\u3002 int compute() { return 42; } \u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c compute \u5c31\u662f\u51fd\u6570\u7684\u540d\u5b57\uff0c int \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\u7c7b\u578b\u2014\u2014\u6574\u6570\u3002 \u4e43\u53d6\u6574\u6570\u4e4b\u82f1\u6587\u201cinteger\u201d\u7684\u201cint\u201d\u800c\u5f97\u540d\uff08\u6a21\u4eff\u4faf\u6377\u8001\u5e08\u8bf4\u8bdd\uff09 \u800c {} \u5305\u88f9\u7684\u662f\u51fd\u6570\u4f53\uff0c\u662f\u51fd\u6570\u88ab\u8c03\u7528\u65f6\u4f1a\u6267\u884c\u7684\u4ee3\u7801\u3002 \u6b64\u5904 return 42 \u5c31\u662f\u51fd\u6570\u4f53\u5185\u7684\u552f\u4e00\u4e00\u6761\u8bed\u53e5\uff0c\u8868\u793a\u51fd\u6570\u7acb\u5373\u6267\u884c\u5b8c\u6bd5\uff0c\u8fd4\u56de 42\u3002 \u8fd4\u56de\u503c: \u5f53\u4e00\u4e2a\u51fd\u6570\u6267\u884c\u5b8c\u6bd5\u65f6\uff0c\u4f1a\u5411\u8c03\u7528\u8be5\u51fd\u6570\u7684\u8c03\u7528\u8005\u8fd4\u56de\u4e00\u4e2a\u503c\uff0c\u8fd9\u4e2a\u503c\u5c31\u662f return \u540e\u9762\u7684\u8868\u8fbe\u5f0f\u7684\u503c\u3002\u8fd4\u56de\u503c\u53ef\u4ee5\u6709\u4e0d\u540c\u7684\u7c7b\u578b\uff0c\u6b64\u5904 compute \u7684\u8fd4\u56de\u7c7b\u578b\u662f int \uff0c\u4e5f\u5c31\u662f\u8bf4 compute \u9700\u8981\u8fd4\u56de\u4e00\u4e2a\u6574\u6570\u3002 \u5173\u4e8e\u51fd\u6570\u7684\u53c2\u6570\uff0c\u6211\u4eec\u7a0d\u540e\u518d\u505a\u8bf4\u660e\u3002","title":"\u4ec0\u4e48\u662f\u51fd\u6570"},{"location":"helloworld/#main","text":"C++ \u7a0b\u5e8f\u901a\u5e38\u7531\u4e00\u7cfb\u5217\u51fd\u6570\u7ec4\u6210\uff0c\u5176\u4e2d\u5fc5\u987b\u6709\u4e00\u4e2a\u540d\u4e3a main \u7684\u51fd\u6570\u4f5c\u4e3a\u7a0b\u5e8f\u7684\u5165\u53e3\u70b9\u3002 main \u51fd\u6570\u7684\u5b9a\u4e49\u5982\u4e0b\uff1a int main() { } \u7a0b\u5e8f\u542f\u52a8\u65f6\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u8c03\u7528 main \u51fd\u6570\u3002 \u4e25\u683c\u6765\u8bf4\uff0c\u662f C++ \u8fd0\u884c\u65f6\u8c03\u7528\u4e86 main \u51fd\u6570\uff0c\u4f46\u76ee\u524d\u5148\u7406\u89e3\u4e3a\u201c\u64cd\u4f5c\u7cfb\u7edf\u8c03\u7528\u4e86 main \u51fd\u6570\u201d\u4e5f\u65e0\u59a8\u3002 \u8981\u628a\u7a0b\u5e8f\u53d1\u5c55\u58ee\u5927\uff0c\u6211\u4eec\u53ef\u4ee5\u8ba9 main \u51fd\u6570\u7ee7\u7eed\u8c03\u7528\u5176\u4ed6\u51fd\u6570\uff0c\u4e5f\u53ef\u4ee5\u76f4\u63a5\u5728 main \u51fd\u6570\u4e2d\u7f16\u5199\u6574\u4e2a\u7a0b\u5e8f\u7684\u903b\u8f91\uff08\u4e0d\u63a8\u8350\uff09\u3002 \u56e0\u6b64\uff0c main \u53ef\u4ee5\u88ab\u770b\u4f5c\u662f\u201c\u5b87\u5b99\u5927\u7206\u70b8\u201d\u3002","title":"\u4ece main \u51fd\u6570\u8bf4\u8d77"},{"location":"helloworld/#main_1","text":"int main() { return 0; } return \u8868\u793a\u51fd\u6570\u7684\u8fd4\u56de\uff0cmain \u51fd\u6570\u8fd4\u56de\uff0c\u5373\u610f\u5473\u7740\u7a0b\u5e8f\u7684\u7ed3\u675f\u3002 main \u51fd\u6570\u603b\u662f\u8fd4\u56de\u4e00\u4e2a\u6574\u6570 ( int \u7c7b\u578b)\uff0c\u7528\u8fd9\u4e2a\u6574\u6570\u5411\u64cd\u4f5c\u7cfb\u7edf\u8868\u793a\u7a0b\u5e8f\u9000\u51fa\u7684\u539f\u56e0\u3002 \u5982\u679c\u7a0b\u5e8f\u6b63\u5e38\u6267\u884c\u5b8c\u6bd5\uff0c\u6b63\u5e38\u7ed3\u675f\u9000\u51fa\uff0c\u90a3\u5c31\u8bf7\u8fd4\u56de 0\u3002 \u8fd4\u56de\u4e00\u4e2a\u4e0d\u4e3a 0 \u7684\u6574\u6570\u53ef\u4ee5\u8868\u793a\u7a0b\u5e8f\u51fa\u73b0\u4e86\u5f02\u5e38\uff0c\u662f\u56e0\u4e3a\u51fa\u9519\u4e86\u624d\u9000\u51fa\u7684\uff0c\u503c\u7684\u591a\u5c11\u53ef\u4ee5\u7528\u4e8e\u8868\u660e\u9519\u8bef\u7684\u5177\u4f53\u539f\u56e0\u3002 \u64cd\u4f5c\u7cfb\u7edf\uff1a\u6211\u8c03\u7528\u4e86\u4f60\u8fd9\u4e2a\u7a0b\u5e8f\u7684 main \u51fd\u6570\uff0c\u6211\u597d\u5947\u7a0b\u5e8f\u662f\u5426\u6b63\u786e\u6267\u884c\u4e86\uff1f\u8ba9\u6211\u4eec\u7ea6\u5b9a\u597d\uff1a\u5982\u679c\u4f60\u8fd0\u8f6c\u6b63\u5e38\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de0\u8868\u793a\u6210\u529f\u54e6\uff01\u5982\u679c\u6709\u9519\u8bef\u7684\u8bdd\uff0c\u5c31\u8fd4\u56de\u4e00\u4e2a\u9519\u8bef\u4ee3\u7801\uff0c\u6bd4\u5982\u8fd4\u56de1\u8868\u793a\u65e0\u6743\u9650\uff0c2\u8868\u793a\u627e\u4e0d\u5230\u6587\u4ef6\u2026\u2026\u4e4b\u7c7b\u7684\u3002\u5f53\u7136\uff0c\u9519\u8bef\u4ee3\u7801\u90fd\u662f\u4e0d\u4e3a0\u7684\u3002","title":"main \u51fd\u6570\u7684\u8fd4\u56de\u503c"},{"location":"helloworld/#_3","text":"TODO: \u4ecb\u7ecd\u63a7\u5236\u53f0","title":"\u8fd9\u4e2a\u9ed1\u8272\u7684\u7a97\u53e3\u662f\uff1f"},{"location":"helloworld/#_4","text":"int main() { std::println(\"Hello, World!\"); } \u4ee5\u4e0a\u4ee3\u7801\u4f1a\u5728\u63a7\u5236\u53f0\u8f93\u51fa Hello, World! \u3002","title":"\u6253\u5370\u4e00\u4e9b\u4fe1\u606f"},{"location":"helloworld/#_5","text":"int main() { // \u5c0f\u5f6d\u8001\u5e08\uff0c\u8bf7\u4f60\u5728\u8fd9\u91cc\u63d2\u5165\u7a0b\u5e8f\u7684\u903b\u8f91\u54e6\uff01 } \u8fd9\u91cc\u7684 // \u662f\u6ce8\u91ca\uff0c\u6ce8\u91ca\u4f1a\u88ab\u7f16\u8bd1\u5668\u5ffd\u7565\uff0c\u901a\u5e38\u7528\u4e8e\u5728\u7a0b\u5e8f\u6e90\u7801\u4e2d\u690d\u5165\u63cf\u8ff0\u6027\u7684\u6587\u672c\u3002\u6709\u65f6\u4e5f\u4f1a\u7528\u4e8e\u591a\u4eba\u534f\u4f5c\u9879\u76ee\u4e2d\u7a0b\u5e8f\u5458\u4e4b\u95f4\u4e92\u76f8\u6c9f\u901a\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u7f16\u8bd1\u5668\u662f\u715e\u7b14 // \u8bf6\u563f\u4f60\u770b\u4e0d\u89c1\u6211 } \u5728\u7f16\u8bd1\u5668\u770b\u6765\u5c31\u53ea\u662f\uff1a int main() { std::println(\"\u7f16\u8bd1\u5668\u4f1f\u5927\uff0c\u65e0\u9700\u591a\u8a00\"); } (* \u7f16\u8bd1\u5668\u8138\u7ea2\u4e2d* ) C++ \u652f\u6301\u884c\u6ce8\u91ca // xx \u548c\u5757\u6ce8\u91ca /* xx */ \u4e24\u79cd\u8bed\u6cd5\u3002 int main() { // \u6211\u662f\u884c\u6ce8\u91ca /* \u6211\u662f\u5757\u6ce8\u91ca */ /* \u5757\u6ce8\u91ca \u53ef\u4ee5 \u6709 \u5f88\u591a\u884c */ std::println(/* \u5757\u6ce8\u91ca\u4e5f\u53ef\u4ee5\u5939\u5728\u4ee3\u7801\u4e2d\u95f4 */\"\u4f60\u597d\"); std::println(\"\u4e16\u754c\"); // \u884c\u6ce8\u91ca\u53ea\u80fd\u8ffd\u52a0\u5728\u4e00\u884c\u7684\u672b\u5c3e std::println(\"\u65e9\u5b89\"); } \u5728\u6211\u4eec\u4ee5\u540e\u7684\u6848\u4f8b\u4ee3\u7801\u4e2d\uff0c\u90fd\u4f1a\u50cf\u8fd9\u6837\u6ce8\u91ca\u8bf4\u660e\uff0c\u5145\u5f53 \u5c31\u5730\u8bb2\u89e3\u5458 \u7684\u6548\u679c\u3002\u53bb\u9664\u8fd9\u4e9b\u6ce8\u91ca\u5e76\u4e0d\u5f71\u54cd\u7a0b\u5e8f\u7684\u6b63\u5e38\u8fd0\u884c\uff0c\u6dfb\u52a0\u6587\u5b57\u6ce8\u91ca\u53ea\u662f\u5c0f\u5f6d\u8001\u5e08\u4e3a\u4e86\u63d0\u9192\u4f60\u6bcf\u4e00\u884c\u7684\u4ee3\u7801\u4f5c\u7528\u3002","title":"\u6ce8\u91ca"},{"location":"platform/","text":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 \u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC GCC Clang \u7f16\u8bd1\u5668\u9009\u9879 C++ \u6807\u51c6 \u4f18\u5316\u7b49\u7ea7 \u8c03\u8bd5\u4fe1\u606f \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 TODO \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u662f\u5c06\u6e90\u4ee3\u7801 ( .cpp ) \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7a0b\u5e8f ( .exe ) \u7684\u5de5\u5177\u3002 C++ \u662f \u7f16\u8bd1\u578b\u8bed\u8a00 \uff0c\u6e90\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u6267\u884c\u54e6\uff01\u521a\u5f00\u59cb\u5b66\u7f16\u7a0b\u7684\u5c0f\u5f6d\u8001\u5e08\u66fe\u7ecf\u628a\u7f51\u4e0a\u7684 \u201cHello, World\u201d \u4ee3\u7801\u62f7\u8d1d\u5230 .c \u6e90\u7801\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u628a\u540e\u7f00\u540d\u6539\u6210 .exe \uff0c\u53d1\u73b0\u8fd9\u6837\u6839\u672c\u6267\u884c\u4e0d\u4e86\u2026\u2026\u540e\u6765\u624d\u77e5\u9053\u9700\u8981\u901a\u8fc7\u4e00\u79cd\u53eb\u505a \u7f16\u8bd1\u5668 \u7f16\u8bd1 .c \u6587\u4ef6\uff0c\u624d\u80fd\u5f97\u5230\u8ba1\u7b97\u673a\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u7684 .exe \u6587\u4ef6\u3002 C++ \u6e90\u7801 .cpp \u662f\u5199\u7ed9\u4eba\u7c7b\u770b\u7684\uff01\u8ba1\u7b97\u673a\u5e76\u4e0d\u8ba4\u8bc6\uff0c\u8ba1\u7b97\u673a\u53ea\u8ba4\u8bc6\u4e8c\u8fdb\u5236\u7684\u673a\u5668\u7801\u3002\u8981\u628a C++ \u6e90\u7801\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u53ef\u4ee5\u6267\u884c\u7684\u673a\u5668\u7801\u3002 \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u6700\u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u6709\uff1aGCC\u3001Clang\u3001MSVC \u4fd7\u79f0\u201c\u5fa1\u4e09\u5bb6\u201d\u3002 \u8fd9\u4e9b\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u4e86\u5927\u90e8\u5206 C++20 \u6807\u51c6\u548c\u5c0f\u90e8\u5206 C++23 \u6807\u51c6\uff0c\u800c C++17 \u6807\u51c6\u90fd\u662f\u5b8c\u5168\u652f\u6301\u7684\u3002 \u6709\u4eba\u8bf4\u8fc7\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f GCC\u3002\u201d GCC \u4e3b\u8981\u53ea\u5728 Linux \u548c MacOS \u7b49 Unix \u7c7b\u7cfb\u7edf\u53ef\u7528\uff0c\u4e0d\u652f\u6301 Windows \u7cfb\u7edf\u3002\u4f46\u662f GCC \u6709\u7740\u5927\u91cf\u597d\u7528\u7684\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u5927\u540d\u9f0e\u9f0e\u7684 pbds \uff08\u57fa\u4e8e\u7b56\u7565\u7684\u6570\u636e\u7ed3\u6784\uff09\uff0c\u8fd8\u6709\u5404\u79cd __attribute__ \uff0c\u5404\u79cd __builtin_ \u7cfb\u5217\u51fd\u6570\u3002\u4e0d\u8fc7\u968f\u7740\u65b0\u6807\u51c6\u7684\u51fa\u53f0\uff0c\u5f88\u591a\u539f\u672c\u5c5e\u4e8e GCC \u7684\u529f\u80fd\u90fd\u6210\u4e86\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982 __attribute__((warn_unused)) \u53d8\u6210\u4e86\u6807\u51c6\u7684 [[nodiscard]] \uff0c __builtin_clz \u53d8\u6210\u4e86\u6807\u51c6\u7684 std::countl_zero \uff0c __VA_OPT__ \u540d\u5b57\u90fd\u6ca1\u53d8\u5c31\u8fdb\u4e86 C++20 \u6807\u51c6\u3002 PBDS \u53c8\u79f0 \u201c\u5e73\u677f\u7535\u89c6\u201d \u4e5f\u6709 MinGW \u8fd9\u6837\u7684\u9b54\u6539\u7248 GCC \u7f16\u8bd1\u5668\uff0c\u628a GCC \u79fb\u690d\u5230\u4e86 Windows \u7cfb\u7edf\u4e0a\uff0c\u540c\u65f6\u4e5f\u80fd\u7528 GCC \u7684\u4e00\u4e9b\u7279\u6027\u3002\u4e0d\u8fc7 MinGW \u6700\u8fd1\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u6700\u65b0\u7684 GCC Windows \u79fb\u690d\u7248\u7531 MinGW-w64 \u7ee7\u7eed\u7ef4\u62a4\u3002 Clang \u662f\u8de8\u5e73\u53f0\u7684\u7f16\u8bd1\u5668\uff0c\u652f\u6301\u5927\u591a\u6570\u4e3b\u6d41\u5e73\u53f0\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u754c\u7684\u5fa1\u4e09\u5bb6\uff1aLinux\u3001MacOS\u3001Windows\u3002Clang \u652f\u6301\u4e86\u5f88\u5927\u4e00\u90e8\u5206 GCC \u7279\u6027\u548c\u90e8\u5206 MSVC \u7279\u6027\u3002\u5176\u6240\u5c5e\u7684 LLVM \u9879\u76ee\u66f4\u662f\u7f16\u8bd1\u5668\u9886\u57df\u7684\u4e2d\u6d41\u7825\u67f1\uff0c\u4e0d\u4ec5\u652f\u6301 C\u3001C++\u3001Objective-C\u3001Fortran \u7b49\uff0cRust \u548c Swift \u7b49\u8bed\u8a00\u4e5f\u662f\u57fa\u4e8e LLVM \u540e\u7aef\u7f16\u8bd1\u7684\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709\u5f88\u591a\u663e\u5361\u5382\u5546\u7684 OpenGL \u9a71\u52a8\u4e5f\u662f\u57fa\u4e8e LLVM \u5b9e\u73b0\u7f16\u8bd1\u7684\u3002\u5e76\u4e14 Clang \u8eab\u517c\u6570\u804c\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u8bd1\uff0c\u8fd8\u652f\u6301\u9759\u6001\u5206\u6790\u3002\u8bb8\u591a IDE \u5e38\u89c1\u7684\u8bed\u8a00\u670d\u52a1\u534f\u8bae (LSP) \u5c31\u662f\u57fa\u4e8e Clang \u7684\u670d\u52a1\u7248\u2014\u2014\u2014\u2014Clangd \u5b9e\u73b0\u7684 (\u4f8b\u5982\u4f60\u53ef\u4ee5\u6309 Ctrl \u70b9\u51fb\uff0c\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u8fd9\u6837\u7684\u529f\u80fd\u5c31\u662f IDE \u901a\u8fc7\u8c03\u7528 Clangd \u7684 LSP \u63a5\u53e3\u5b9e\u73b0\uff09\u3002\u4e0d\u8fc7 Clang \u7684\u6027\u80fd\u4f18\u5316\u6bd4\u8f83\u6fc0\u8fdb\uff0c\u867d\u7136\u6709\u52a9\u4e8e\u6027\u80fd\u63d0\u5347\uff0c\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u72af\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u53ef\u80fd\u4f18\u5316\u51fa\u532a\u5937\u6240\u601d\u7684\u7ed3\u679c\uff0c\u5982\u679c\u4f60\u8981\u5b9e\u9a8c\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u662f\u6700\u64c5\u957f\u590d\u73b0\u7684\u3002\u4e14 Clang \u5bf9\u4e00\u4e9b C++ \u65b0\u6807\u51c6\u7279\u6027\u652f\u6301\u76f8\u5bf9\u8f83\u6162\uff0c\u6ca1\u6709 GCC \u548c MSVC \u90a3\u4e48\u4e0a\u5fc3\u3002 \u4f8b\u5982 C++20 \u65e9\u5df2\u5141\u8bb8 lambda \u8868\u8fbe\u5f0f\u6355\u83b7 structural-binding \u53d8\u91cf\uff0c\u800c Clang \u81f3\u4eca\u8fd8\u6ca1\u6709\u652f\u6301\uff0c\u5c3d\u7ba1 Clang \u5df2\u7ecf\u652f\u6301\u4e86\u5f88\u591a\u5176\u4ed6 C++20 \u7279\u6027\u3002 Apple Clang \u662f\u82f9\u679c\u516c\u53f8\u81ea\u5df1\u9b54\u6539\u7684 Clang \u7248\u672c\uff0c\u53ea\u5728 MacOS \u7cfb\u7edf\u4e0a\u53ef\u7528\uff0c\u652f\u6301 Objective-C \u548c Swift \u8bed\u8a00\u3002\u4f46\u662f\u7248\u672c\u8f83\u5b98\u65b9 Clang \u843d\u540e\u4e00\u4e9b\uff0c\u5f88\u591a\u65b0\u7279\u6027\u90fd\u6ca1\u6709\u8ddf\u8fdb\uff0c\u57fa\u672c\u4e0a\u53ea\u6709\u4e13\u95e8\u4f3a\u5019\u82f9\u679c\u7684\u5f00\u53d1\u8005\u4f1a\u7528\u3002 GCC \u548c Clang \u4e5f\u652f\u6301 Objective-C\u3002 MSVC \u662f Windows \u9650\u5b9a\u7684\u7f16\u8bd1\u5668\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a MSVC \u7279\u6709\u7684\u6269\u5c55\u3002\u4e5f\u6709\u4eba\u5728 Clang \u4e0a\u9b54\u6539\u51fa\u4e86 MSVC \u517c\u5bb9\u6a21\u5f0f\uff0c\u517c\u987e Clang \u7279\u6027\u7684\u540c\u65f6\uff0c\u652f\u6301\u4e86 MSVC \u7684\u4e00\u4e9b\u7279\u6027\uff08\u4f8b\u5982 __declspec \uff09\uff0c\u53ef\u4ee5\u7f16\u8bd1\u7528\u4e86 MSVC \u7279\u6027\u7684\u4ee3\u7801\uff0c\u5373 clang-cl \uff0c\u5728\u6700\u65b0\u7684 VS2022 IDE \u4e2d\u4e5f\u96c6\u6210\u4e86 clang-cl \u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cMSVC \u7684\u4f18\u5316\u80fd\u529b\u662f\u6bd4\u8f83\u5dee\u7684\uff0c\u6bd4 GCC \u548c Clang \u90fd\u5dee\uff0c\u4f8b\u5982 MSVC \u51e0\u4e4e\u603b\u662f\u5047\u5b9a\u6240\u6709\u6307\u9488 aliasing\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u9047\u5230\u5f88\u591a\u6307\u9488\u64cd\u4f5c\u7684\u5faa\u73af\u65f6\uff0c\u51e0\u4e4e\u6ca1\u6cd5\u505a\u5faa\u73af\u77e2\u91cf\u5316\u3002\u4f46\u662f\u4e5f\u4f7f\u5f97\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f Bug\uff0c\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u4e00\u4e9b\u53ea\u7528 MSVC \u7684\u4eba\u4e0d\u77e5\u9053\u67d0\u4e9b\u5199\u6cd5\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 Intel C++ compiler \u662f\u82f1\u7279\u5c14\u5f00\u53d1\u7684 C++ \u7f16\u8bd1\u5668\uff0c\u7531\u4e8e\u662f\u786c\u4ef6\u5382\u5546\u5f00\u53d1\u7684\uff0c\u7279\u522b\u64c5\u957f\u505a\u6027\u80fd\u4f18\u5316\u3002\u4f46\u7531\u4e8e\u66f4\u65b0\u8f83\u6162\uff0c\u57fa\u672c\u6ca1\u6709\u66f4\u4e0a\u65b0\u7279\u6027\uff0c\u4e5f\u6ca1\u4ec0\u4e48\u4eba\u5728\u7528\u4e86\u3002 \u6700\u8fd1\u4ed6\u4eec\u53c8\u51fa\u4e86\u4e2a Intel DPC++ compiler\uff0c\u652f\u6301\u6700\u65b0\u7684\u5e76\u884c\u7f16\u7a0b\u9886\u57df\u7279\u5b9a\u8bed\u8a00 SyCL\u3002 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC cl.exe /c main.cpp \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main.exe \u4e86\u3002 GCC g++ -c main.cpp -o main \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main \u4e86\u3002 Linux \u7cfb\u7edf\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u6ca1\u6709\u540e\u7f00\u540d\uff0c\u6240\u4ee5\u6ca1\u6709 .exe \u540e\u7f00\u3002 Clang Windows \u4e0a\uff1a clang++.exe -c main.cpp -o main.exe Linux / MacOS \u4e0a\uff1a clang++ -c main.cpp -o main \u7f16\u8bd1\u5668\u9009\u9879 \u7f16\u8bd1\u5668\u9009\u9879\u662f\u7528\u6765\u63a7\u5236\u7f16\u8bd1\u5668\u7684\u884c\u4e3a\u7684\u3002\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6709\u4e0d\u540c\u7684\u9009\u9879\uff0c\u8bed\u6cd5\u6709\u5fae\u5999\u7684\u4e0d\u540c\uff0c\u4f46\u5927\u81f4\u529f\u6548\u76f8\u540c\u3002 \u4f8b\u5982\u5f53\u6211\u4eec\u8bf4\u201c\u7f16\u8bd1\u8fd9\u4e2a\u6e90\u7801\u65f6\uff0c\u6211\u7528\u4e86 GCC \u7f16\u8bd1\u5668\uff0c -O3 \u548c -std=c++20 \u9009\u9879\u201d\uff0c\u8bf4\u7684\u5c31\u662f\u628a\u8fd9\u4e9b\u9009\u9879\u52a0\u5230\u4e86 g++ \u7684\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\uff1a g++ -O3 -std=c++20 -c main.cpp -o main \u5176\u4e2d Clang \u548c GCC \u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\u5f88\u5927\u4ea4\u96c6\u3002\u800c MSVC \u57fa\u672c\u81ea\u6210\u4e00\u6d3e\u3002 Clang \u548c GCC \u7684\u9009\u9879\u90fd\u662f -xxx \u7684\u5f62\u5f0f\uff0cMSVC \u7684\u9009\u9879\u662f /xxx \u7684\u5f62\u5f0f\u3002 \u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\uff1a C++ \u6807\u51c6 \u6307\u5b9a\u8981\u9009\u7528\u7684 C++ \u6807\u51c6\u3002 Clang \u548c GCC\uff1a -std=c++98 \u3001 -std=c++03 \u3001 -std=c++11 \u3001 -std=c++14 \u3001 -std=c++17 \u3001 -std=c++20 \u3001 -std=c++23 MSVC\uff1a /std:c++98 \u3001 /std:c++11 \u3001 /std:c++14 \u3001 /std:c++17 \u3001 /std:c++20 \u3001 /std:c++latest \u4f8b\u5982\u8981\u7f16\u8bd1\u4e00\u4e2a C++20 \u6e90\u7801\u6587\u4ef6\uff0c\u5206\u522b\u7528 GCC\u3001Clang\u3001MSVC\uff1a GCC\uff08Linux\uff09\uff1a g++ -std=c++20 -c main.cpp -o main Clang\uff08Linux\uff09\uff1a clang++ -std=c++20 -c main.cpp -o main MSVC\uff08Windows\uff09\uff1a cl.exe /std:c++20 /c main.cpp \u4f18\u5316\u7b49\u7ea7 Clang \u548c GCC\uff1a -O0 \u3001 -O1 \u3001 -O2 \u3001 -O3 \u3001 -Ofast \u3001 -Os \u3001 -Oz \u3001 -Og -O0 \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u7f16\u8bd1\u901f\u5ea6\u6700\u5feb\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u5f00\u53d1\u4eba\u5458\u5185\u90e8\u8c03\u8bd5\u9636\u6bb5\u3002 -O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\uff08\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u7684\u4e0d\u53ef\u62b5\u8fbe\u4ee3\u7801\uff09\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u90e8\u5206\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\uff0c\u7f16\u8bd1\u901f\u5ea6\u8f83\u5feb\uff0c\u6267\u884c\u901f\u5ea6\u4e5f\u6bd4 -O0 \u5feb\u3002\u4f46\u662f\u4f1a\u4e22\u5931\u51fd\u6570\u7684\u884c\u53f7\u4fe1\u606f\uff0c\u5f71\u54cd\u8bf8\u5982 gdb \u7b49\u8c03\u8bd5\uff0c\u5982\u9700\u5feb\u901f\u8c03\u8bd5\u53ef\u4ee5\u7528 -Og \u9009\u9879\u3002 -O2 \uff1a\u6bd4 -O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002 -O3 \uff1a\u6bd4 -O2 \u66f4\u6fc0\u8fdb\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u590d\u6742\u7684\u5faa\u73af\u7528 SIMD \u77e2\u91cf\u6307\u4ee4\u4f18\u5316\u52a0\u901f\uff0c\u628a\u4e00\u4e9b\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u6027\u80fd\u63d0\u5347\u5f88\u5927\uff0c\u4f46\u662f\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b Bug\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\u5219\u7edd\u4e0d\u4f1a\u6709\u95ee\u9898\uff0c\u5bf9\u81ea\u5df1\u7684\u4ee3\u7801\u8d28\u91cf\u6709\u81ea\u4fe1\u5c31\u53ef\u4ee5\u653e\u5fc3\u5f00\uff0c\u7f16\u8bd1\u901f\u5ea6\u4e5f\u4f1a\u5f88\u6162\uff0c\u4e00\u822c\u7528\u4e8e\u7a0b\u5e8f\u6700\u7ec8\u6210\u54c1\u53d1\u5e03\u9636\u6bb5\u3002 -Ofast \uff1a\u5728 -O3 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5bf9\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97\u8fdb\u884c\u66f4\u6df1\u5c42\u6b21\u7684\u4f18\u5316\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u6d6e\u70b9\u6570\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e0d\u6d89\u53ca\u5230 NaN \u548c Inf \u7684\u5904\u7406\uff0c\u90a3\u4e48 -Ofast \u4e0d\u4f1a\u6709\u592a\u5927\u7684\u95ee\u9898\uff0c\u4e00\u822c\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\u7684\u7ec8\u6781\u6027\u80fd\u4f18\u5316\u3002 -Os \uff1a\u5728 -O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u6bd4 -O0 \u3001 -O1 \u3001 -O2 \u90fd\u8981\u5c0f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Oz \uff1a\u5728 -Os \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u628a\u4ee3\u7801\u538b\u7f29\uff0c\u53ef\u80fd\u628a\u672c\u53ef\u4ee5\u4e00\u6761\u5927\u6307\u4ee4\u5b8c\u6210\u7684\u4efb\u52a1\u4e5f\u62c6\u6210\u591a\u6761\u5c0f\u6307\u4ee4\uff0c\u4e3a\u4e86\u5c3a\u5bf8\u5b8c\u5168\u6027\u80fd\uff0c\u5927\u5e45\u51cf\u5c11\u4e86\u51fd\u6570\u5185\u8054\u7684\u673a\u4f1a\uff0c\u6709\u65f6\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Og \uff1a\u5728 -O0 \u7684\u57fa\u7840\u4e0a\uff0c\u5c3d\u53ef\u80fd\u4fdd\u7559\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\uff0c\u4e0d\u505a\u7834\u574f\u51fd\u6570\u884c\u53f7\u7b49\u4fe1\u606f\u7684\u4f18\u5316\uff0c\u5efa\u8bae\u914d\u5408\u4ea7\u751f\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\u7684 -g \u9009\u9879\u4f7f\u7528\u3002\u4f46\u8fd8\u662f\u4f1a\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u4f18\u5316\uff0c\u6bd4 -O0 \u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002\u4f46 -Og \u7684\u6240\u6709\u4f18\u5316\u90fd\u4e0d\u4f1a\u6d89\u53ca\u5230\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u8c03\u8bd5\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u4f46\u662f\u7531\u4e8e\u63d2\u5165\u4e86\u8c03\u8bd5\u4fe1\u606f\uff0c\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u53d8\u5f97\u5f88\u5927\uff0c\u4e00\u822c\u5728\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002 MSVC\uff1a /Od \u3001 /O1 \u3001 /O2 \u3001 /Ox \u3001 /Ob1 \u3001 /Ob2 \u3001 /Os /Od \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\u9636\u6bb5\u3002 /O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\u3002 /O2 \uff1a\u6bd4 /O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u4f1a\u5c1d\u8bd5\u628a\u4e00\u4e9b\u5faa\u73af\u77e2\u91cf\u5316\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ox \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u4f46\u662f\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ob1 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\u3002 /Ob2 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\uff0c\u4f46\u662f\u4f1a\u6269\u5927\u5185\u8054\u8303\u56f4\uff0c\u4e00\u822c\u6bd4 /Ob1 \u66f4\u5feb\uff0c\u4f46\u662f\u4e5f\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u3002 /Os \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 \u8c03\u8bd5\u4fe1\u606f Clang \u548c GCC\uff1a -g \u3001 -g0 \u3001 -g1 \u3001 -g2 \u3001 -g3 MSVC\uff1a /Z7 \u3001 /Zi \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f Clang \u548c GCC\uff1a -Dmacro=value MSVC\uff1a /Dmacro=value \u4f8b\u5982\uff1a \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 libstdc++ \u662f GCC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e GCC \u662f Linux \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libstdc++ \u4e5f\u662f Linux \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u770b\u5230\u4ed6\u7684\u6e90\u7801\uff1ahttps://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3 libc++ \u662f Clang \u5b98\u65b9\u7f16\u5199\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e Clang \u662f MacOS \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libc++ \u4e5f\u662f MacOS \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002libc++ \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u65e9\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\u3002\u9879\u76ee\u7684\u5f00\u6e90\u5730\u5740\u662f\uff1ahttps://github.com/llvm/llvm-project/tree/main/libcxx MSVC STL \u662f MSVC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e MSVC \u662f Windows \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 MSVC STL \u4e5f\u662f Windows \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002MSVC STL \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u665a\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\uff0c\u4f46\u662f\u73b0\u5728\u4ed6\u5df2\u7ecf\u5b8c\u5168\u652f\u6301 C++20\uff0c\u5e76\u4e14\u4e5f\u5b8c\u5168\u5f00\u6e90\u4e86\uff1ahttps://github.com/microsoft/STL \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6807\u51c6\u5e93\u548c\u7f16\u8bd1\u5668\u5e76\u4e0d\u662f\u7ed1\u5b9a\u7684\uff0c\u4f8b\u5982 Clang \u53ef\u4ee5\u7528 libstdc++ \u6216 MSVC STL\uff0cGCC \u4e5f\u53ef\u4ee5\u88ab\u914d\u7f6e\u4f7f\u7528 libc++\u3002 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0cClang \u9ed8\u8ba4\u7528\u7684\u5c31\u662f libstdc++\u3002\u9700\u8981\u4e3a Clang \u6307\u5b9a -stdlib=libc++ \u9009\u9879\uff0c\u624d\u80fd\u4f7f\u7528\u3002 \u725b\u5934\u4eba\u7b11\u8bdd\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f libstdc++\u3002\u56e0\u4e3a\u5373\u4f7f\u4ed6\u7684\u7f16\u8bd1\u5668\u662f Clang\uff0c\u4ed6\u7528\u7684\u5927\u6982\u7387\u4f9d\u7136\u662f libstdc++\u3002\u201d \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO","title":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9"},{"location":"platform/#_1","text":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9 IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01 \u7f16\u8bd1\u5668\u662f\uff1f \u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6 \u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801 MSVC GCC Clang \u7f16\u8bd1\u5668\u9009\u9879 C++ \u6807\u51c6 \u4f18\u5316\u7b49\u7ea7 \u8c03\u8bd5\u4fe1\u606f \u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93 \u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84 \u5b9a\u4e49\u5b8f \u8b66\u544a\u5f00\u5173 \u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6 \u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f TODO","title":"\u5f00\u53d1\u73af\u5883\u4e0e\u5e73\u53f0\u9009\u62e9"},{"location":"platform/#ide","text":"TODO","title":"IDE \u4e0d\u662f\u7f16\u8bd1\u5668\uff01"},{"location":"platform/#_2","text":"\u7f16\u8bd1\u5668\u662f\u5c06\u6e90\u4ee3\u7801 ( .cpp ) \u7f16\u8bd1\u6210\u53ef\u6267\u884c\u7a0b\u5e8f ( .exe ) \u7684\u5de5\u5177\u3002 C++ \u662f \u7f16\u8bd1\u578b\u8bed\u8a00 \uff0c\u6e90\u4ee3\u7801\u4e0d\u80fd\u76f4\u63a5\u6267\u884c\u54e6\uff01\u521a\u5f00\u59cb\u5b66\u7f16\u7a0b\u7684\u5c0f\u5f6d\u8001\u5e08\u66fe\u7ecf\u628a\u7f51\u4e0a\u7684 \u201cHello, World\u201d \u4ee3\u7801\u62f7\u8d1d\u5230 .c \u6e90\u7801\u6587\u4ef6\u4e2d\uff0c\u7136\u540e\u628a\u540e\u7f00\u540d\u6539\u6210 .exe \uff0c\u53d1\u73b0\u8fd9\u6837\u6839\u672c\u6267\u884c\u4e0d\u4e86\u2026\u2026\u540e\u6765\u624d\u77e5\u9053\u9700\u8981\u901a\u8fc7\u4e00\u79cd\u53eb\u505a \u7f16\u8bd1\u5668 \u7f16\u8bd1 .c \u6587\u4ef6\uff0c\u624d\u80fd\u5f97\u5230\u8ba1\u7b97\u673a\u53ef\u4ee5\u76f4\u63a5\u6267\u884c\u7684 .exe \u6587\u4ef6\u3002 C++ \u6e90\u7801 .cpp \u662f\u5199\u7ed9\u4eba\u7c7b\u770b\u7684\uff01\u8ba1\u7b97\u673a\u5e76\u4e0d\u8ba4\u8bc6\uff0c\u8ba1\u7b97\u673a\u53ea\u8ba4\u8bc6\u4e8c\u8fdb\u5236\u7684\u673a\u5668\u7801\u3002\u8981\u628a C++ \u6e90\u7801\u8f6c\u6362\u4e3a\u8ba1\u7b97\u673a\u53ef\u4ee5\u6267\u884c\u7684\u673a\u5668\u7801\u3002","title":"\u7f16\u8bd1\u5668\u662f\uff1f"},{"location":"platform/#_3","text":"\u6700\u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u6709\uff1aGCC\u3001Clang\u3001MSVC \u4fd7\u79f0\u201c\u5fa1\u4e09\u5bb6\u201d\u3002 \u8fd9\u4e9b\u7f16\u8bd1\u5668\u90fd\u652f\u6301\u4e86\u5927\u90e8\u5206 C++20 \u6807\u51c6\u548c\u5c0f\u90e8\u5206 C++23 \u6807\u51c6\uff0c\u800c C++17 \u6807\u51c6\u90fd\u662f\u5b8c\u5168\u652f\u6301\u7684\u3002 \u6709\u4eba\u8bf4\u8fc7\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f GCC\u3002\u201d GCC \u4e3b\u8981\u53ea\u5728 Linux \u548c MacOS \u7b49 Unix \u7c7b\u7cfb\u7edf\u53ef\u7528\uff0c\u4e0d\u652f\u6301 Windows \u7cfb\u7edf\u3002\u4f46\u662f GCC \u6709\u7740\u5927\u91cf\u597d\u7528\u7684\u6269\u5c55\u529f\u80fd\uff0c\u4f8b\u5982\u5927\u540d\u9f0e\u9f0e\u7684 pbds \uff08\u57fa\u4e8e\u7b56\u7565\u7684\u6570\u636e\u7ed3\u6784\uff09\uff0c\u8fd8\u6709\u5404\u79cd __attribute__ \uff0c\u5404\u79cd __builtin_ \u7cfb\u5217\u51fd\u6570\u3002\u4e0d\u8fc7\u968f\u7740\u65b0\u6807\u51c6\u7684\u51fa\u53f0\uff0c\u5f88\u591a\u539f\u672c\u5c5e\u4e8e GCC \u7684\u529f\u80fd\u90fd\u6210\u4e86\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f8b\u5982 __attribute__((warn_unused)) \u53d8\u6210\u4e86\u6807\u51c6\u7684 [[nodiscard]] \uff0c __builtin_clz \u53d8\u6210\u4e86\u6807\u51c6\u7684 std::countl_zero \uff0c __VA_OPT__ \u540d\u5b57\u90fd\u6ca1\u53d8\u5c31\u8fdb\u4e86 C++20 \u6807\u51c6\u3002 PBDS \u53c8\u79f0 \u201c\u5e73\u677f\u7535\u89c6\u201d \u4e5f\u6709 MinGW \u8fd9\u6837\u7684\u9b54\u6539\u7248 GCC \u7f16\u8bd1\u5668\uff0c\u628a GCC \u79fb\u690d\u5230\u4e86 Windows \u7cfb\u7edf\u4e0a\uff0c\u540c\u65f6\u4e5f\u80fd\u7528 GCC \u7684\u4e00\u4e9b\u7279\u6027\u3002\u4e0d\u8fc7 MinGW \u6700\u8fd1\u5df2\u7ecf\u505c\u6b62\u66f4\u65b0\uff0c\u6700\u65b0\u7684 GCC Windows \u79fb\u690d\u7248\u7531 MinGW-w64 \u7ee7\u7eed\u7ef4\u62a4\u3002 Clang \u662f\u8de8\u5e73\u53f0\u7684\u7f16\u8bd1\u5668\uff0c\u652f\u6301\u5927\u591a\u6570\u4e3b\u6d41\u5e73\u53f0\uff0c\u5305\u62ec\u64cd\u4f5c\u7cfb\u7edf\u754c\u7684\u5fa1\u4e09\u5bb6\uff1aLinux\u3001MacOS\u3001Windows\u3002Clang \u652f\u6301\u4e86\u5f88\u5927\u4e00\u90e8\u5206 GCC \u7279\u6027\u548c\u90e8\u5206 MSVC \u7279\u6027\u3002\u5176\u6240\u5c5e\u7684 LLVM \u9879\u76ee\u66f4\u662f\u7f16\u8bd1\u5668\u9886\u57df\u7684\u4e2d\u6d41\u7825\u67f1\uff0c\u4e0d\u4ec5\u652f\u6301 C\u3001C++\u3001Objective-C\u3001Fortran \u7b49\uff0cRust \u548c Swift \u7b49\u8bed\u8a00\u4e5f\u662f\u57fa\u4e8e LLVM \u540e\u7aef\u7f16\u8bd1\u7684\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709\u5f88\u591a\u663e\u5361\u5382\u5546\u7684 OpenGL \u9a71\u52a8\u4e5f\u662f\u57fa\u4e8e LLVM \u5b9e\u73b0\u7f16\u8bd1\u7684\u3002\u5e76\u4e14 Clang \u8eab\u517c\u6570\u804c\uff0c\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u8bd1\uff0c\u8fd8\u652f\u6301\u9759\u6001\u5206\u6790\u3002\u8bb8\u591a IDE \u5e38\u89c1\u7684\u8bed\u8a00\u670d\u52a1\u534f\u8bae (LSP) \u5c31\u662f\u57fa\u4e8e Clang \u7684\u670d\u52a1\u7248\u2014\u2014\u2014\u2014Clangd \u5b9e\u73b0\u7684 (\u4f8b\u5982\u4f60\u53ef\u4ee5\u6309 Ctrl \u70b9\u51fb\uff0c\u8df3\u8f6c\u5230\u51fd\u6570\u5b9a\u4e49\uff0c\u8fd9\u6837\u7684\u529f\u80fd\u5c31\u662f IDE \u901a\u8fc7\u8c03\u7528 Clangd \u7684 LSP \u63a5\u53e3\u5b9e\u73b0\uff09\u3002\u4e0d\u8fc7 Clang \u7684\u6027\u80fd\u4f18\u5316\u6bd4\u8f83\u6fc0\u8fdb\uff0c\u867d\u7136\u6709\u52a9\u4e8e\u6027\u80fd\u63d0\u5347\uff0c\u5982\u679c\u4f60\u4e0d\u5c0f\u5fc3\u72af\u4e86\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u53ef\u80fd\u4f18\u5316\u51fa\u532a\u5937\u6240\u601d\u7684\u7ed3\u679c\uff0c\u5982\u679c\u4f60\u8981\u5b9e\u9a8c\u672a\u5b9a\u4e49\u884c\u4e3a\uff0cClang \u662f\u6700\u64c5\u957f\u590d\u73b0\u7684\u3002\u4e14 Clang \u5bf9\u4e00\u4e9b C++ \u65b0\u6807\u51c6\u7279\u6027\u652f\u6301\u76f8\u5bf9\u8f83\u6162\uff0c\u6ca1\u6709 GCC \u548c MSVC \u90a3\u4e48\u4e0a\u5fc3\u3002 \u4f8b\u5982 C++20 \u65e9\u5df2\u5141\u8bb8 lambda \u8868\u8fbe\u5f0f\u6355\u83b7 structural-binding \u53d8\u91cf\uff0c\u800c Clang \u81f3\u4eca\u8fd8\u6ca1\u6709\u652f\u6301\uff0c\u5c3d\u7ba1 Clang \u5df2\u7ecf\u652f\u6301\u4e86\u5f88\u591a\u5176\u4ed6 C++20 \u7279\u6027\u3002 Apple Clang \u662f\u82f9\u679c\u516c\u53f8\u81ea\u5df1\u9b54\u6539\u7684 Clang \u7248\u672c\uff0c\u53ea\u5728 MacOS \u7cfb\u7edf\u4e0a\u53ef\u7528\uff0c\u652f\u6301 Objective-C \u548c Swift \u8bed\u8a00\u3002\u4f46\u662f\u7248\u672c\u8f83\u5b98\u65b9 Clang \u843d\u540e\u4e00\u4e9b\uff0c\u5f88\u591a\u65b0\u7279\u6027\u90fd\u6ca1\u6709\u8ddf\u8fdb\uff0c\u57fa\u672c\u4e0a\u53ea\u6709\u4e13\u95e8\u4f3a\u5019\u82f9\u679c\u7684\u5f00\u53d1\u8005\u4f1a\u7528\u3002 GCC \u548c Clang \u4e5f\u652f\u6301 Objective-C\u3002 MSVC \u662f Windows \u9650\u5b9a\u7684\u7f16\u8bd1\u5668\uff0c\u63d0\u4f9b\u4e86\u5f88\u591a MSVC \u7279\u6709\u7684\u6269\u5c55\u3002\u4e5f\u6709\u4eba\u5728 Clang \u4e0a\u9b54\u6539\u51fa\u4e86 MSVC \u517c\u5bb9\u6a21\u5f0f\uff0c\u517c\u987e Clang \u7279\u6027\u7684\u540c\u65f6\uff0c\u652f\u6301\u4e86 MSVC \u7684\u4e00\u4e9b\u7279\u6027\uff08\u4f8b\u5982 __declspec \uff09\uff0c\u53ef\u4ee5\u7f16\u8bd1\u7528\u4e86 MSVC \u7279\u6027\u7684\u4ee3\u7801\uff0c\u5373 clang-cl \uff0c\u5728\u6700\u65b0\u7684 VS2022 IDE \u4e2d\u4e5f\u96c6\u6210\u4e86 clang-cl \u3002\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0cMSVC \u7684\u4f18\u5316\u80fd\u529b\u662f\u6bd4\u8f83\u5dee\u7684\uff0c\u6bd4 GCC \u548c Clang \u90fd\u5dee\uff0c\u4f8b\u5982 MSVC \u51e0\u4e4e\u603b\u662f\u5047\u5b9a\u6240\u6709\u6307\u9488 aliasing\uff0c\u8fd9\u610f\u5473\u7740\u5f53\u9047\u5230\u5f88\u591a\u6307\u9488\u64cd\u4f5c\u7684\u5faa\u73af\u65f6\uff0c\u51e0\u4e4e\u6ca1\u6cd5\u505a\u5faa\u73af\u77e2\u91cf\u5316\u3002\u4f46\u662f\u4e5f\u4f7f\u5f97\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f Bug\uff0c\u53e6\u4e00\u65b9\u9762\uff0c\u8fd9\u4e5f\u5bfc\u81f4\u4e00\u4e9b\u53ea\u7528 MSVC \u7684\u4eba\u4e0d\u77e5\u9053\u67d0\u4e9b\u5199\u6cd5\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\u3002 Intel C++ compiler \u662f\u82f1\u7279\u5c14\u5f00\u53d1\u7684 C++ \u7f16\u8bd1\u5668\uff0c\u7531\u4e8e\u662f\u786c\u4ef6\u5382\u5546\u5f00\u53d1\u7684\uff0c\u7279\u522b\u64c5\u957f\u505a\u6027\u80fd\u4f18\u5316\u3002\u4f46\u7531\u4e8e\u66f4\u65b0\u8f83\u6162\uff0c\u57fa\u672c\u6ca1\u6709\u66f4\u4e0a\u65b0\u7279\u6027\uff0c\u4e5f\u6ca1\u4ec0\u4e48\u4eba\u5728\u7528\u4e86\u3002 \u6700\u8fd1\u4ed6\u4eec\u53c8\u51fa\u4e86\u4e2a Intel DPC++ compiler\uff0c\u652f\u6301\u6700\u65b0\u7684\u5e76\u884c\u7f16\u7a0b\u9886\u57df\u7279\u5b9a\u8bed\u8a00 SyCL\u3002","title":"\u7f16\u8bd1\u5668\u5fa1\u4e09\u5bb6"},{"location":"platform/#_4","text":"","title":"\u4f7f\u7528\u7f16\u8bd1\u5668\u7f16\u8bd1\u6e90\u7801"},{"location":"platform/#msvc","text":"cl.exe /c main.cpp \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main.exe \u4e86\u3002","title":"MSVC"},{"location":"platform/#gcc","text":"g++ -c main.cpp -o main \u8fd9\u6837\u5c31\u53ef\u4ee5\u5f97\u5230\u53ef\u6267\u884c\u6587\u4ef6 main \u4e86\u3002 Linux \u7cfb\u7edf\u7684\u53ef\u6267\u884c\u6587\u4ef6\u5e76\u6ca1\u6709\u540e\u7f00\u540d\uff0c\u6240\u4ee5\u6ca1\u6709 .exe \u540e\u7f00\u3002","title":"GCC"},{"location":"platform/#clang","text":"Windows \u4e0a\uff1a clang++.exe -c main.cpp -o main.exe Linux / MacOS \u4e0a\uff1a clang++ -c main.cpp -o main","title":"Clang"},{"location":"platform/#_5","text":"\u7f16\u8bd1\u5668\u9009\u9879\u662f\u7528\u6765\u63a7\u5236\u7f16\u8bd1\u5668\u7684\u884c\u4e3a\u7684\u3002\u4e0d\u540c\u7684\u7f16\u8bd1\u5668\u6709\u4e0d\u540c\u7684\u9009\u9879\uff0c\u8bed\u6cd5\u6709\u5fae\u5999\u7684\u4e0d\u540c\uff0c\u4f46\u5927\u81f4\u529f\u6548\u76f8\u540c\u3002 \u4f8b\u5982\u5f53\u6211\u4eec\u8bf4\u201c\u7f16\u8bd1\u8fd9\u4e2a\u6e90\u7801\u65f6\uff0c\u6211\u7528\u4e86 GCC \u7f16\u8bd1\u5668\uff0c -O3 \u548c -std=c++20 \u9009\u9879\u201d\uff0c\u8bf4\u7684\u5c31\u662f\u628a\u8fd9\u4e9b\u9009\u9879\u52a0\u5230\u4e86 g++ \u7684\u547d\u4ee4\u884c\u53c2\u6570\u4e2d\uff1a g++ -O3 -std=c++20 -c main.cpp -o main \u5176\u4e2d Clang \u548c GCC \u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\u5f88\u5927\u4ea4\u96c6\u3002\u800c MSVC \u57fa\u672c\u81ea\u6210\u4e00\u6d3e\u3002 Clang \u548c GCC \u7684\u9009\u9879\u90fd\u662f -xxx \u7684\u5f62\u5f0f\uff0cMSVC \u7684\u9009\u9879\u662f /xxx \u7684\u5f62\u5f0f\u3002 \u5e38\u89c1\u7684\u7f16\u8bd1\u5668\u9009\u9879\u6709\uff1a","title":"\u7f16\u8bd1\u5668\u9009\u9879"},{"location":"platform/#c","text":"\u6307\u5b9a\u8981\u9009\u7528\u7684 C++ \u6807\u51c6\u3002 Clang \u548c GCC\uff1a -std=c++98 \u3001 -std=c++03 \u3001 -std=c++11 \u3001 -std=c++14 \u3001 -std=c++17 \u3001 -std=c++20 \u3001 -std=c++23 MSVC\uff1a /std:c++98 \u3001 /std:c++11 \u3001 /std:c++14 \u3001 /std:c++17 \u3001 /std:c++20 \u3001 /std:c++latest \u4f8b\u5982\u8981\u7f16\u8bd1\u4e00\u4e2a C++20 \u6e90\u7801\u6587\u4ef6\uff0c\u5206\u522b\u7528 GCC\u3001Clang\u3001MSVC\uff1a GCC\uff08Linux\uff09\uff1a g++ -std=c++20 -c main.cpp -o main Clang\uff08Linux\uff09\uff1a clang++ -std=c++20 -c main.cpp -o main MSVC\uff08Windows\uff09\uff1a cl.exe /std:c++20 /c main.cpp","title":"C++ \u6807\u51c6"},{"location":"platform/#_6","text":"Clang \u548c GCC\uff1a -O0 \u3001 -O1 \u3001 -O2 \u3001 -O3 \u3001 -Ofast \u3001 -Os \u3001 -Oz \u3001 -Og -O0 \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u7f16\u8bd1\u901f\u5ea6\u6700\u5feb\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u5f00\u53d1\u4eba\u5458\u5185\u90e8\u8c03\u8bd5\u9636\u6bb5\u3002 -O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\uff08\u7f16\u8bd1\u5668\u68c0\u6d4b\u5230\u7684\u4e0d\u53ef\u62b5\u8fbe\u4ee3\u7801\uff09\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u90e8\u5206\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\uff0c\u7f16\u8bd1\u901f\u5ea6\u8f83\u5feb\uff0c\u6267\u884c\u901f\u5ea6\u4e5f\u6bd4 -O0 \u5feb\u3002\u4f46\u662f\u4f1a\u4e22\u5931\u51fd\u6570\u7684\u884c\u53f7\u4fe1\u606f\uff0c\u5f71\u54cd\u8bf8\u5982 gdb \u7b49\u8c03\u8bd5\uff0c\u5982\u9700\u5feb\u901f\u8c03\u8bd5\u53ef\u4ee5\u7528 -Og \u9009\u9879\u3002 -O2 \uff1a\u6bd4 -O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\uff0c\u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002 -O3 \uff1a\u6bd4 -O2 \u66f4\u6fc0\u8fdb\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u590d\u6742\u7684\u5faa\u73af\u7528 SIMD \u77e2\u91cf\u6307\u4ee4\u4f18\u5316\u52a0\u901f\uff0c\u628a\u4e00\u4e9b\u590d\u6742\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u6027\u80fd\u63d0\u5347\u5f88\u5927\uff0c\u4f46\u662f\u5982\u679c\u4f60\u7684\u7a0b\u5e8f\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b Bug\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u6ca1\u6709\u672a\u5b9a\u4e49\u884c\u4e3a\u5219\u7edd\u4e0d\u4f1a\u6709\u95ee\u9898\uff0c\u5bf9\u81ea\u5df1\u7684\u4ee3\u7801\u8d28\u91cf\u6709\u81ea\u4fe1\u5c31\u53ef\u4ee5\u653e\u5fc3\u5f00\uff0c\u7f16\u8bd1\u901f\u5ea6\u4e5f\u4f1a\u5f88\u6162\uff0c\u4e00\u822c\u7528\u4e8e\u7a0b\u5e8f\u6700\u7ec8\u6210\u54c1\u53d1\u5e03\u9636\u6bb5\u3002 -Ofast \uff1a\u5728 -O3 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u5bf9\u6d6e\u70b9\u6570\u7684\u8fd0\u7b97\u8fdb\u884c\u66f4\u6df1\u5c42\u6b21\u7684\u4f18\u5316\uff0c\u4f46\u662f\u53ef\u80fd\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u6d6e\u70b9\u6570\u8ba1\u7b97\u7ed3\u679c\u4e0d\u51c6\u786e\u3002\u5982\u679c\u4f60\u7684\u4ee3\u7801\u4e0d\u6d89\u53ca\u5230 NaN \u548c Inf \u7684\u5904\u7406\uff0c\u90a3\u4e48 -Ofast \u4e0d\u4f1a\u6709\u592a\u5927\u7684\u95ee\u9898\uff0c\u4e00\u822c\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u9886\u57df\u7684\u7ec8\u6781\u6027\u80fd\u4f18\u5316\u3002 -Os \uff1a\u5728 -O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u6bd4 -O0 \u3001 -O1 \u3001 -O2 \u90fd\u8981\u5c0f\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Oz \uff1a\u5728 -Os \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u628a\u4ee3\u7801\u538b\u7f29\uff0c\u53ef\u80fd\u628a\u672c\u53ef\u4ee5\u4e00\u6761\u5927\u6307\u4ee4\u5b8c\u6210\u7684\u4efb\u52a1\u4e5f\u62c6\u6210\u591a\u6761\u5c0f\u6307\u4ee4\uff0c\u4e3a\u4e86\u5c3a\u5bf8\u5b8c\u5168\u6027\u80fd\uff0c\u5927\u5e45\u51cf\u5c11\u4e86\u51fd\u6570\u5185\u8054\u7684\u673a\u4f1a\uff0c\u6709\u65f6\u7528\u4e8e\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002 -Og \uff1a\u5728 -O0 \u7684\u57fa\u7840\u4e0a\uff0c\u5c3d\u53ef\u80fd\u4fdd\u7559\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\uff0c\u4e0d\u505a\u7834\u574f\u51fd\u6570\u884c\u53f7\u7b49\u4fe1\u606f\u7684\u4f18\u5316\uff0c\u5efa\u8bae\u914d\u5408\u4ea7\u751f\u66f4\u591a\u8c03\u8bd5\u4fe1\u606f\u7684 -g \u9009\u9879\u4f7f\u7528\u3002\u4f46\u8fd8\u662f\u4f1a\u505a\u4e00\u4e9b\u7b80\u5355\u7684\u4f18\u5316\uff0c\u6bd4 -O0 \u6267\u884c\u901f\u5ea6\u66f4\u5feb\u3002\u4f46 -Og \u7684\u6240\u6709\u4f18\u5316\u90fd\u4e0d\u4f1a\u6d89\u53ca\u5230\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u56e0\u6b64\u975e\u5e38\u9002\u5408\u8c03\u8bd5\u672a\u5b9a\u4e49\u884c\u4e3a\u3002\u4f46\u662f\u7531\u4e8e\u63d2\u5165\u4e86\u8c03\u8bd5\u4fe1\u606f\uff0c\u6700\u7ec8\u7684\u53ef\u6267\u884c\u6587\u4ef6\u4f1a\u53d8\u5f97\u5f88\u5927\uff0c\u4e00\u822c\u5728\u5f00\u53d1\u4eba\u5458\u8c03\u8bd5\u65f6\u4f7f\u7528\u3002 MSVC\uff1a /Od \u3001 /O1 \u3001 /O2 \u3001 /Ox \u3001 /Ob1 \u3001 /Ob2 \u3001 /Os /Od \uff1a\u4e0d\u8fdb\u884c\u4efb\u4f55\u4f18\u5316\uff0c\u5fe0\u5b9e\u590d\u523b\u4f60\u5199\u7684\u4ee3\u7801\uff0c\u672a\u5b9a\u4e49\u884c\u4e3a\u4e0d\u5bb9\u6613\u4ea7\u751f\u8be1\u5f02\u7684\u7ed3\u679c\uff0c\u4e00\u822c\u7528\u4e8e\u8c03\u8bd5\u9636\u6bb5\u3002 /O1 \uff1a\u6700\u57fa\u672c\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6b7b\u4ee3\u7801\u5220\u9664\uff0c\u53bb\u6389\u6ca1\u6709\u7528\u7684\u53d8\u91cf\uff0c\u628a\u53d8\u91cf\u7528\u5bc4\u5b58\u5668\u4ee3\u66ff\u7b49\u3002 /O2 \uff1a\u6bd4 /O1 \u66f4\u5f3a\u7684\u4f18\u5316\uff0c\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\uff0c\u628a\u4e00\u4e9b\u51fd\u6570\u5185\u8054\uff0c\u51cf\u5c11\u51fd\u6570\u8c03\u7528\uff0c\u8fd8\u4f1a\u5c1d\u8bd5\u628a\u4e00\u4e9b\u5faa\u73af\u77e2\u91cf\u5316\uff0c\u628a\u4e00\u4e9b\u7b80\u5355\u7684\u6570\u7ec4\u64cd\u4f5c\u7528\u66f4\u5feb\u7684\u6307\u4ee4\u66ff\u4ee3\u7b49\u3002\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ox \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u8fdb\u4e00\u6b65\u4f18\u5316\uff0c\u4f46\u662f\u4e0d\u4f1a\u5bfc\u81f4\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u4e00\u822c\u7528\u4e8e\u53d1\u5e03\u9636\u6bb5\u3002 /Ob1 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\u3002 /Ob2 \uff1a\u542f\u7528\u51fd\u6570\u5185\u8054\uff0c\u4f46\u662f\u4f1a\u6269\u5927\u5185\u8054\u8303\u56f4\uff0c\u4e00\u822c\u6bd4 /Ob1 \u66f4\u5feb\uff0c\u4f46\u662f\u4e5f\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u3002 /Os \uff1a\u5728 /O2 \u7684\u57fa\u7840\u4e0a\uff0c\u4e13\u95e8\u4f18\u5316\u4ee3\u7801\u5927\u5c0f\uff0c\u6027\u80fd\u88ab\u5f53\u4f5c\u6b21\u8981\u9700\u6c42\uff0c\u4f46\u662f\u4f1a\u7981\u6b62\u4f1a\u5bfc\u81f4\u53ef\u6267\u884c\u6587\u4ef6\u53d8\u5927\u7684\u4f18\u5316\u3002\u4f1a\u628a\u4e00\u4e9b\u5faa\u73af\u5c55\u5f00\u3001\u5185\u8054\u7b49\u4f18\u5316\u5173\u95ed\uff0c\u628a\u4e00\u4e9b\u4ee3\u7801\u7528\u66f4\u5c0f\u7684\u6307\u4ee4\u5b9e\u73b0\uff0c\u5c3d\u53ef\u80fd\u51cf\u5c0f\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5c3a\u5bf8\uff0c\u901a\u5e38\u7528\u4e8e\u9700\u8981\u8282\u7701\u5185\u5b58\u7684\u5d4c\u5165\u5f0f\u7cfb\u7edf\u5f00\u53d1\u3002","title":"\u4f18\u5316\u7b49\u7ea7"},{"location":"platform/#_7","text":"Clang \u548c GCC\uff1a -g \u3001 -g0 \u3001 -g1 \u3001 -g2 \u3001 -g3 MSVC\uff1a /Z7 \u3001 /Zi","title":"\u8c03\u8bd5\u4fe1\u606f"},{"location":"platform/#_8","text":"","title":"\u5934\u6587\u4ef6\u641c\u7d22\u8def\u5f84"},{"location":"platform/#_9","text":"","title":"\u6307\u5b9a\u8981\u94fe\u63a5\u7684\u5e93"},{"location":"platform/#_10","text":"","title":"\u5e93\u6587\u4ef6\u641c\u7d22\u8def\u5f84"},{"location":"platform/#_11","text":"Clang \u548c GCC\uff1a -Dmacro=value MSVC\uff1a /Dmacro=value \u4f8b\u5982\uff1a","title":"\u5b9a\u4e49\u5b8f"},{"location":"platform/#_12","text":"","title":"\u8b66\u544a\u5f00\u5173"},{"location":"platform/#_13","text":"libstdc++ \u662f GCC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e GCC \u662f Linux \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libstdc++ \u4e5f\u662f Linux \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002\u4f60\u53ef\u4ee5\u5728\u8fd9\u91cc\u770b\u5230\u4ed6\u7684\u6e90\u7801\uff1ahttps://github.com/gcc-mirror/gcc/tree/master/libstdc%2B%2B-v3 libc++ \u662f Clang \u5b98\u65b9\u7f16\u5199\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e Clang \u662f MacOS \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 libc++ \u4e5f\u662f MacOS \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002libc++ \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u65e9\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\u3002\u9879\u76ee\u7684\u5f00\u6e90\u5730\u5740\u662f\uff1ahttps://github.com/llvm/llvm-project/tree/main/libcxx MSVC STL \u662f MSVC \u5b98\u65b9\u7684 C++ \u6807\u51c6\u5e93\u5b9e\u73b0\uff0c\u7531\u4e8e MSVC \u662f Windows \u7cfb\u7edf\u7684\u4e3b\u6d41\u7f16\u8bd1\u5668\uff0c\u6240\u4ee5 MSVC STL \u4e5f\u662f Windows \u4e0a\u6700\u5e38\u7528\u7684\u6807\u51c6\u5e93\u3002MSVC STL \u4e5f\u662f C++ \u6807\u51c6\u5e93\u4e2d\u6700\u665a\u5b9e\u73b0 C++11 \u6807\u51c6\u7684\uff0c\u4f46\u662f\u73b0\u5728\u4ed6\u5df2\u7ecf\u5b8c\u5168\u652f\u6301 C++20\uff0c\u5e76\u4e14\u4e5f\u5b8c\u5168\u5f00\u6e90\u4e86\uff1ahttps://github.com/microsoft/STL \u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u6807\u51c6\u5e93\u548c\u7f16\u8bd1\u5668\u5e76\u4e0d\u662f\u7ed1\u5b9a\u7684\uff0c\u4f8b\u5982 Clang \u53ef\u4ee5\u7528 libstdc++ \u6216 MSVC STL\uff0cGCC \u4e5f\u53ef\u4ee5\u88ab\u914d\u7f6e\u4f7f\u7528 libc++\u3002 \u5728 Linux \u7cfb\u7edf\u4e2d\uff0cClang \u9ed8\u8ba4\u7528\u7684\u5c31\u662f libstdc++\u3002\u9700\u8981\u4e3a Clang \u6307\u5b9a -stdlib=libc++ \u9009\u9879\uff0c\u624d\u80fd\u4f7f\u7528\u3002 \u725b\u5934\u4eba\u7b11\u8bdd\uff1a\u201c\u5982\u679c\u4f60\u4e0d\u77e5\u9053\u4e00\u4e2a\u4eba\u662f\u7528\u7684\u4ec0\u4e48\u6807\u51c6\u5e93\uff0c\u90a3\u4e48\u4f60\u53ef\u4ee5\u731c\u4ed6\u7528\u7684\u662f libstdc++\u3002\u56e0\u4e3a\u5373\u4f7f\u4ed6\u7684\u7f16\u8bd1\u5668\u662f Clang\uff0c\u4ed6\u7528\u7684\u5927\u6982\u7387\u4f9d\u7136\u662f libstdc++\u3002\u201d","title":"\u6807\u51c6\u5e93\u5fa1\u4e09\u5bb6"},{"location":"platform/#_14","text":"TODO","title":"\u6807\u51c6\u5e93\u7684\u8c03\u8bd5\u6a21\u5f0f"},{"location":"unicode/","text":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u96c6 ASCII Latin-1 Unicode \u603b\u7ed3 \u5b57\u7b26\u7f16\u7801 UTF-32 UTF-8 \u517c\u5bb9 ASCII \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d UTF-16 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 BOM \u6807\u8bb0 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 UTF-8 \u9635\u8425 UTF-16 \u9635\u8425 UTF-32 \u9635\u8425 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale UTF \u4e4b\u95f4\u4e92\u8f6c GBK \u548c UTF \u4e92\u8f6c UTF \u548c ANSI \u4e92\u8f6c \u5927\u603b\u7ed3 GBK \u548c Shift-JIS \u4e92\u8f6c \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u66f4\u591a\u529f\u80fd\uff1f\uff01 Windows \u7528\u6237\uff1aMultiByteToWideChar MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b Linux \u7528\u6237\uff1aiconv iconv \u547d\u4ee4\u884c\u5de5\u5177 \u672c\u5730\u5316 (locale) \u533a\u5206\u5b57\u7b26\u7c7b\u578b \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u5bbd\u5b57\u7b26\u6d41 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring wchar_t \u7cfb\u5217\u51fd\u6570 std::wcout \u7684\u4f7f\u7528 std::wfstream \u7684\u4f7f\u7528 C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 Windows \u4e13\u9898 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 TCHAR \u6d41\u6d3e UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 Qt QString QTextCodec from/toLocal8Bits/Utf8/Latin1/Ascii \u5b57\u7b26\u4e32\u5e38\u91cf QTextStream Python 3 str Rust &str \u548c String Java String COW \u5b57\u7b26\u4e32 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme \u6b63\u89c4\u5316 \u96f6\u5bbd\u7a7a\u683c \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u7684\u5999\u7528 Base64 \u9632\u4e71\u7801 \u5b57\u7b26\u96c6 \u8ba1\u7b97\u673a\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5b57\u7b26\uff0c\u800c\u662f\u7528\u6570\u5b57\u6765\u4ee3\u66ff\uff0c\u8fd9\u5c31\u662f\u5b57\u7b26\u96c6\uff0c\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\u3002 ASCII ASCII \u4e3a\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u7ec4\u3001\u6807\u70b9\u7b26\u53f7\u7b49 128 \u4e2a\u5b57\u7b26\uff0c\u6bcf\u4e2a\u90fd\u7528\u4e00\u4e2a 0 \u5230 127 \u8303\u56f4\u5185\u7684\u6570\u5b57\u5bf9\u5e94\u3002 \u5982\u679c\u4f60\u60f3\u8981\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c31\u5728\u8fd9\u4e2a\u8868\u91cc\u5bfb\u627e\u5230\u76f8\u5e94\u7684\u6570\u5b57\u7f16\u53f7\uff0c\u7136\u540e\u5b58\u8fd9\u4e2a\u7f16\u53f7\u5373\u53ef\u3002 \u4f8b\u5982\u4e0b\u9762\u7684\u4e00\u4e32\u6570\u5b57\uff1a 80 101 110 103 \u5728 ASCII \u8868\u4e2d\u67e5\u627e\uff0c\u53d1\u73b0\u8fd9\u4e9b\u6570\u5b57\u5206\u522b\u5bf9\u5e94 P \u3001 e \u3001 n \u3001 g \u56db\u4e2a\u5b57\u6bcd\uff0c\u8fde\u8d77\u6765\u5c31\u8fd8\u539f\u5f97\u5230\u4e86\u539f\u672c\u7684\u5b57\u7b26\u4e32\u201cPeng\u201d\u3002 Latin-1 Latin-1 \u6269\u5145\u4e86 ASCII \u5b57\u7b26\u96c6\uff0c\u4fdd\u6301 ASCII \u539f\u6709 0 \u5230 127 \u7684\u90e8\u5206\u6620\u5c04\u4e0d\u53d8\uff0c\u989d\u5916\u8ffd\u52a0\u4e86 128 \u5230 255 \u7684\u6620\u5c04\u5173\u7cfb\u3002\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a EASCII\uff08\u6269\u5c55 ASCII\uff09\u3002 Unicode Unicode \u5b57\u7b26\u96c6\u4e3a\u5168\u4e16\u754c\u7684\u6240\u6709\u5b57\u7b26\u90fd\u5bf9\u5e94\u4e86\u4e00\u4e2a\u6574\u6570\u3002 \u5b57\u7b26 \u7f16\u53f7 \u6211 25105 \u6212 25106 \u6213 25107 \u6214 25108 \u6215 25109 \u6216 25110 \u6217 25111 \u6218 25112 \u6219 25113 \u621a 25114 \u51fa\u4e8e\u5386\u53f2\u517c\u5bb9\u6027\u8003\u8651\uff0cUnicode \u5728 0 \u5230 256 \u533a\u95f4\u5185\u7684\u6620\u5c04\u548c ASCII\u3001Latin-1 \u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002 \u5b57\u7b26 \u7f16\u53f7 P 80 e 101 n 110 g 103 Unicode \u7ecf\u8fc7\u4e86\u8bb8\u591a\u7248\u672c\u7684\u53d1\u5c55\uff0c\u65e9\u671f\u7684 Unicode \u53ea\u6536\u5f55\u4e86 65536 (0x10000) \u4e2a\u5b57\u7b26\uff0c\u540e\u6765\u6269\u5145\u5230\u4e86 1114112 (0x110000) \u4e2a\u5b57\u7b26\u3002 \u603b\u4e4b\uff0c\u73b0\u5728 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u867d\u7136\u5360\u7528\u4e86 1114112 \u8fd9\u591a\u683c\u7801\u70b9\u7a7a\u95f4\uff0c\u4e0d\u8fc7\u5176\u4e2d\u5f88\u591a\u90fd\u662f\u7a7a\u53f7\uff0c\u7559\u5f85\u672a\u6765\u6269\u5145\u4f7f\u7528\u3002 Unicode \u5b57\u7b26\u6620\u5c04\u8868\u53ef\u4ee5\u5728\u7f51\u4e0a\u627e\u5230\uff1a https://symbl.cc/en/unicode-table/ https://www.compart.com/en/unicode/ \u603b\u7ed3 \u5b57\u7b26\u96c6: \u4ece\u5b57\u7b26\u5230\u6574\u6570\u7684\u4e00\u4e00\u6620\u5c04\u3002 ASCII: \u53ea\u6536\u5f55\u4e86\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 Latin-1: \u5728 ASCII \u57fa\u7840\u4e0a\u8ffd\u52a0\u4e86\u6ce8\u97f3\u5b57\u6bcd\uff0c\u6ee1\u8db3\u6b27\u6d32\u7528\u6237\u9700\u8981\u3002 Unicode: \u6536\u5f55\u4e86\u5168\u4e16\u754c\u6240\u6709\u6587\u5b57\u548c\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 \u8ba1\u7b97\u673a\u5b58\u50a8\u5b57\u7b26\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5b58\u50a8\u4e86\u90a3\u4e2a\u5bf9\u5e94\u7684\u6574\u6570\u3002 \u8fd9\u4e9b\u6574\u6570\u5c31\u88ab\u79f0\u4e3a \u7801\u70b9 (code point) \uff0c\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002 \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u5458\u901a\u5e38\u559c\u6b22\u7528\u5341\u516d\u8fdb\u5236\u4e66\u5199\u6570\u5b57\uff1a \u5b57\u7b26 \u7f16\u53f7 \u6211 0x6211 \u6212 0x6212 \u6213 0x6213 \u6214 0x6214 \u6215 0x6215 \u6216 0x6216 \u6217 0x6217 \u6218 0x6218 \u6219 0x6219 \u621a 0x621A \u4f8b\u5982\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\uff0c\u5728 Unicode \u8868\u4e2d\u7f16\u53f7\u4e3a 0x6211\u3002\u4e8e\u662f\u5f53\u8ba1\u7b97\u673a\u9700\u8981\u8868\u793a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\u65f6\uff0c\u5c31\u7528 0x6211 \u8fd9\u4e2a\u6574\u6570\u4ee3\u66ff\u3002 \u5982\u679c\u8981\u8868\u793a\u591a\u4e2a\u5b57\u7b26\uff0c\u90a3\u5c31\u7528\u4e00\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u5427\uff01 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5904\u7406\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a 0x6211 0x7231 0x30EDE 0x30EDE 0x9762 0x21 \u8fd9\u4e00\u4e32\u6570\u5b57\u4ee3\u66ff\u3002 \u5b57\u7b26\u7f16\u7801 Unicode \u53ea\u662f\u6307\u5b9a\u4e86\u6574\u6570\uff0c\u6ca1\u6709\u89c4\u5b9a\u6574\u6570\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5b58\u5728\u3002 \u5b57\u7b26\u7f16\u7801: \u5c06\u5b57\u7b26\u7684\u6574\u6570\u7f16\u53f7\u5e8f\u5217\u5316\u4e3a\u8ba1\u7b97\u673a\u53ef\u76f4\u63a5\u5b58\u50a8\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u5b9e\u9645\u5b58\u5728\u7684\u6574\u6570\u7c7b\u578b\u3002 Unicode \u5b57\u7b26\u53ef\u4ee5\u9009\u7528\u4ee5\u4e0b\u8fd9\u4e9b\u5b57\u7b26\u7f16\u7801\u6765\u5e8f\u5217\u5316\uff1a UTF-32: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u4e2a uint32_t \u6574\u6570\u5b58\u50a8\u3002 UTF-16: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 2 \u4e2a uint16_t \u6574\u6570\u5b58\u50a8\u3002 UTF-8: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u5b58\u50a8\u3002 \u7ffb\u8bd1\u51fa\u6765\u7684\u8fd9\u4e9b\u5c0f\u6574\u6570\u53eb \u7801\u4f4d (code unit) \u3002\u4f8b\u5982\u5bf9\u4e8e UTF-8 \u800c\u8a00\uff0c\u6bcf\u4e2a uint8_t \u5c31\u662f\u4ed6\u7684\u7801\u4f4d\u3002 UTF-32 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u6700\u5927\u503c 0x10FFFF \u6709 21 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0cC \u8bed\u8a00\u4e2d uint32_t \u80fd\u5bb9\u7eb3 32 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0c\u6240\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u7528 uint32_t \u6570\u7ec4\u6765\u4e00\u4e2a\u4e2a\u5bb9\u7eb3 Unicode \u5b57\u7b26\u7801\u70b9\u3002\u867d\u7136\u6d6a\u8d39\u4e86 11 \u4f4d\uff0c\u4f46\u81f3\u5c11\u6240\u6709 Unicode \u5b57\u7b26\u90fd\u80fd\u5b89\u5168\u5bb9\u7eb3\u3002 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a std::vector s = { 0x00006211, // \u6211 0x00007231, // \u7231 0x00030EDE, // \ud883\udede 0x00030EDE, // \ud883\udede 0x00009762, // \u9762 0x00000021, // ! }; \u8fd9\u4e2a\u6570\u7ec4\u8868\u793a\u3002 UTF-32 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u56fa\u5b9a\u5bf9\u5e94\u4e00\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-32 \u662f \u5b9a\u957f\u7f16\u7801 \u3002\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5c31\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002 \u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u64cd\u4f5c\u3002 \u65e0\u8bba\u5bf9\u6570\u7ec4\u5982\u4f55\u5207\u7247\uff0c\u90fd\u4e0d\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u7834\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u53cd\u8f6c\uff0c\u4e0d\u4f1a\u4ea7\u751f\u7834\u574f\u5b57\u7b26\u7684\u95ee\u9898\u3002 \u7f3a\u70b9\u662f\uff1a \u6d6a\u8d39\u5b58\u50a8\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\uff0c\u59cb\u7ec8\u91c7\u7528 UTF-32 \u5f62\u5f0f\u5904\u7406\u6587\u5b57\u3002 UTF-32 \u4e5f\u88ab\u79f0\u4e3a UCS-4\uff0c\u4ed6\u4fe9\u662f\u540c\u4e49\u8bcd\u3002 UTF-8 UTF-32 \u867d\u7136\u65b9\u4fbf\u4e86\u6587\u5b57\u5904\u7406\uff0c\u7136\u800c\uff0c\u5374\u6d6a\u8d39\u4e86\u5927\u91cf\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e0d\u5229\u4e8e\u6587\u5b57\u5b58\u50a8\uff01\u4e00\u4e2a\u5b57\u7b26\uff0c\u65e0\u8bba\u4ed6\u662f\u5e38\u7528\u8fd8\u662f\u4e0d\u5e38\u7528\uff0c\u90fd\u8981\u9738\u5360 4 \u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\u3002 Unicode \u7f16\u7801\u5b57\u7b26\u65f6\uff0c\u7279\u610f\u628a\u5e38\u7528\u7684\u5b57\u7b26\u9760\u524d\u6392\u5217\u4e86\u3002 \u4e16\u754c\u4e0a\u5e38\u7528\u8bed\u8a00\u6587\u5b57\u90fd\u88ab\u523b\u610f\u7f16\u7801\u5728\u4e86 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u8d85\u8fc7 0x10000 \u7684\u57fa\u672c\u90fd\u662f\u4e0d\u5e38\u7528\u7684\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\uff0c\u5f88\u591a\u90fd\u662f\u5df2\u7ecf\u65e0\u4eba\u4f7f\u7528\u7684\u53e4\u4ee3\u6587\u5b57\u548c\u751f\u50fb\u5b57\uff0c\u4f8b\u5982\u201c\ud883\udede\u201d\u3002\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8fd9\u4e9b\u5076\u5c14\u4f7f\u7528\u7684\u7f55\u89c1\u6587\u5b57\uff0c\u5c31\u8981\u6c42\u6240\u6709\u6587\u5b57\u90fd\u7528\u540c\u6837\u7684 4 \u5b57\u8282\u5bbd\u5ea6\u5b58\u50a8\uff0c\u5b9e\u5728\u662f\u6709\u70b9\u6d6a\u8d39\u3002 \u5728 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u540c\u6837\u6709\u6309\u7167\u5e38\u7528\u5ea6\u6392\u5e8f\uff1a 0 \u5230 0x7F \u662f\uff08\u6b27\u7f8e\u7528\u6237\uff09\u6700\u5e38\u7528\u7684\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u534a\u89d2\u6807\u70b9\u3002 0x80 \u5230 0x7FF \u662f\u8868\u97f3\u6587\u5b57\u533a\uff0c\u5e38\u7528\u7684\u6ce8\u97f3\u5b57\u6bcd\u3001\u62c9\u4e01\u5b57\u6bcd\u3001\u5e0c\u814a\u5b57\u6bcd\u3001\u897f\u91cc\u5c14\u5b57\u6bcd\u3001\u5e0c\u4f2f\u6765\u5b57\u6bcd\u7b49\u3002 0x800 \u5230 0xFFFF \u662f\u8868\u610f\u6587\u5b57\uff0c\u7b80\u7e41\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u6cf0\u6587\u3001\u9a6c\u6765\u6587\u3001\u963f\u62c9\u4f2f\u6587\u7b49\u3002 0x10000 \u5230 0x10FFFF \u662f\u4e0d\u5e38\u7528\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\u3002 UTF-8 \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u538b\u7f29\u95ee\u9898\u800c\u8bde\u751f\u7684\u3002 UTF-8 \u628a\u4e00\u4e2a\u7801\u70b9\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u7801\u4f4d\uff0c\u4e00\u4e2a\u7801\u4f4d\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u8868\u793a\u3002 0 \u5230 0x7F \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 1 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 3 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002 \u5e8f\u5217\u5316\u89c4\u5219\u5982\u4e0b\uff1a \u517c\u5bb9 ASCII \u5bf9\u4e8e 0 \u5230 0x7F \u7684\u5b57\u7b26\uff0c\u8fd9\u4e2a\u8303\u56f4\u7684\u5b57\u7b26\u9700\u8981 7 \u4f4d\u5b58\u50a8\u3002 \u6211\u4eec\u9009\u62e9\u76f4\u63a5\u5b58\u50a8\u5176\u503c\u3002 \u4f8b\u5982 \u2018P\u2019 \u4f1a\u88ab\u76f4\u63a5\u5b58\u50a8\u5176 Unicode \u503c\u7684 80\uff080x50\uff09\uff1a 01010000 \u7531\u4e8e Unicode \u5728 0 \u5230 0x7F \u8303\u56f4\u5185\u4e0e ASCII \u8868\u76f8\u540c\uff0c\u800c UTF-8 \u53c8\u628a 0 \u5230 0x7F \u7684\u503c\u76f4\u63a5\u5b58\u50a8\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u517c\u5bb9 ASCII\u3002\u8fd9\u4f7f\u5f97\u539f\u672c\u8bbe\u8ba1\u4e8e\u5904\u7406 ASCII \u7684 C \u8bed\u8a00\u51fd\u6570\uff0c\u4f8b\u5982 strlen\u3001strcat\u3001sprintf \u7b49\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u65e0\u7f1d\u5207\u6362\u5230 UTF-8\u3002\u53cd\u4e4b\u4ea6\u7136\uff0c\u4efb\u4f55\u8bbe\u8ba1\u7528\u4e8e UTF-8 \u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5b8c\u5168\u63a5\u53d7 ASCII \u683c\u5f0f\u7684\u8f93\u5165\u6587\u672c\u3002 \u4f46\u90e8\u5206\u6d89\u53ca\u5b57\u7b26\u957f\u5ea6\u7684\u51fd\u6570\u4f1a\u6709\u4e9b\u8bb8\u4e0d\u517c\u5bb9\uff0c\u4f8b\u5982 strlen \u6c42\u51fa\u7684\u957f\u5ea6\u4f1a\u53d8\u6210\u5b57\u8282\u7684\u6570\u91cf\u800c\u4e0d\u662f\u5b57\u7b26\u7684\u6570\u91cf\u4e86\uff0c\u4f8b\u5982 strlen(\"\u6211\u4eec\") \u4f1a\u5f97\u5230 6 \u800c\u4e0d\u662f 2\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6784\u9020\u5c31\u50cf\u4e00\u5217\u5c0f\u706b\u8f66\u4e00\u6837\uff0c\u4e0d\u540c\u8303\u56f4\u5185\u7684\u7801\u4f4d\u4f1a\u88ab\u7f16\u7801\u6210\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8f66\uff0c\u4f46\u4ed6\u4eec\u90fd\u6709\u4e00\u4e2a\u8f66\u5934\u3002 \u6839\u636e\u706b\u8f66\u5934\u7684\u201c\u7b49\u7ea7\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u65ad\u51fa\u540e\u9762\u62c9\u7740\u51e0\u8282\u8f66\u53a2\u3002 \u706b\u8f66\u5934\u662f\u4ec0\u4e48\u7b49\u7ea7\u7531\u4ed6\u7684\u4e8c\u8fdb\u5236\u524d\u7f00\u51b3\u5b9a\uff1a \u5982\u679c\u662f 0 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u662f\u5355\u72ec\u4e00\u53f0\u706b\u8f66\u5934\uff0c\u540e\u9762\u6ca1\u6709\u8f66\u53a2\u4e86\uff0c\u8fd9\u8868\u793a\u8f66\u5934\u91cc\u9762\u76f4\u63a5\u88c5\u7740 0 \u5230 0x7F \u8303\u56f4\u7684\u666e\u901a ASCII \u5b57\u7b26\u3002 \u5982\u679c\u662f 110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e00\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u6b27\u6d32\u5b57\u7b26\u3002 \u5982\u679c\u662f 1110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e24\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u4e16\u754c\u5e38\u7528\u5b57\u7b26\u3002 \u5982\u679c\u662f 11110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e09\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u751f\u50fb\u5b57\u7b26\u3002 \u5982\u679c\u662f 10 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u8fd9\u662f\u4e00\u8282\u8f66\u53a2\uff0c\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u8282\u5355\u72ec\u7684\u8f66\u53a2\u5728\u524d\u9762\u65e0\u5934\u9a7e\u9a76\uff0c\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\u3002 \u5c0f\u670b\u53cb\u7528\u5c0f\u53f7\u5217\u8f66\u88c5\uff0c\u5927\u670b\u53cb\u7528\u5927\u53f7\u5217\u8f66\u88c5\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u4e32\u4e8c\u8fdb\u5236\uff1a 11100110 10000010 10000001 \u9996\u5148\uff0c\u770b\u5230\u7b2c\u4e00\u4e2a\u5b57\u8282\uff0c\u662f 1110 \u5f00\u5934\u7684\u4e09\u7ea7\u8f66\u5934\uff01\u8bf4\u660e\u540e\u9762\u8fd8\u6709\u4e24\u8282\u8f66\u53a2\u662f\u5c5e\u4e8e\u4ed6\u7684\u3002\u706b\u8f66\u5934\u4e2d 4 \u4f4d\u7528\u4e8e\u8868\u793a\u8f66\u5934\u7b49\u7ea7\u4e86\uff0c\u5269\u4e0b\u8fd8\u6709 4 \u4f4d\u7528\u4e8e\u88c5\u4e58\u5ba2\u3002 \u8f66\u53a2\u4e5f\u6709\u56fa\u5b9a\u7684\u524d\u7f00\uff0c\u6240\u6709\u7684\u8f66\u53a2\u90fd\u5fc5\u987b\u662f 10 \u5f00\u5934\u7684\u3002\u53bb\u9664\u8fd9\u5f00\u5934\u7684 2 \u4f4d\uff0c\u5269\u4e0b\u7684 6 \u4f4d\u5c31\u662f\u4e58\u5ba2\u3002 \u5bf9\u4e8e\u8fd9\u79cd\u4e09\u7ea7\u5217\u8f66\uff0c4 + 6 + 6 \u603b\u5171 16 \u4f4d\u4e8c\u8fdb\u5236\uff0c\u521a\u597d\u53ef\u4ee5\u88c5\u5f97\u4e0b 0xFFFF \u5185\u7684\u4e58\u5ba2\u3002 0110 000010 000001 \u7f16\u7801\u65f6\u5219\u662f\u53cd\u8fc7\u6765\u3002 \u4e58\u5ba2\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e09\u7247\uff0c\u4f8b\u5982\u5bf9\u4e8e\u201c\u6211\u201d\u8fd9\u4e2a\u4e58\u5ba2\uff0c\u201c\u6211\u201d\u7684\u7801\u70b9\u662f 0x6211\uff0c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u662f\uff1a 110010000010001 \u628a\u4e58\u5ba2\u5207\u5206\u6210\u9ad8 4 \u4f4d\u3001\u4e2d 6 \u4f4d\u548c\u4f4e 6 \u4f4d\uff1a 1100 100000 10001 \u52a0\u4e0a 1110 \u3001 10 \u548c 10 \u524d\u7f00\u540e\uff0c\u5f62\u6210\u4e00\u5217\u706b\u8f66\uff1a 11100110 10000010 10000001 \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u628a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\uff0c\u7f16\u7801\u6210\u4e86\u4e09\u8282\u5217\u8f66\uff0c\u585e\u8fdb\u5b57\u8282\u6d41\u7684\u7f51\u7edc\u96a7\u9053\u91cc\u4e86\u3002 \u603b\u7ed3\uff1a \u524d\u7f00\u662f 0 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 7 \u540d\u3002 \u524d\u7f00\u662f 10 \u7684\u662f\u8f66\u53a2\uff1a\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002 \u524d\u7f00\u662f 110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 5 \u540d + 1 \u8282\u8f66\u53a2\u8f7d\u5ba2 6 \u540d = \u5171 11 \u540d\u3002 \u524d\u7f00\u662f 1110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 4 \u540d + 2 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 16 \u540d\u3002 \u524d\u7f00\u662f 11110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 3 \u540d + 3 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 21 \u540d\u3002 \u9ad8\u7ea7\u8f66\u5934\u88c5\u4e86\u9632\u5f39\u94a2\u677f\uff0c\u8f7d\u5ba2\u7a7a\u95f4\u53d8\u5c11\uff0c\u53ea\u597d\u5300\u5230\u540e\u9762\u7684\u8f66\u53a2\u3002 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u5982\u679c\u53d1\u73b0 10 \u5f00\u5934\u7684\u72ec\u7acb\u8f66\u53a2\uff0c\u5c31\u8bf4\u660e\u51fa\u95ee\u9898\u4e86\uff0c\u53ef\u80fd\u662f\u706b\u8f66\u88ab\u9519\u8bef\u62e6\u8170\u622a\u65ad\uff0c\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u88ab\u9519\u8bef\u5730\u53cd\u8f6c\u3002\u56e0\u4e3a 10 \u53ea\u53ef\u80fd\u662f\u706b\u8f66\u8f66\u53a2\uff0c\u4e0d\u53ef\u80fd\u51fa\u73b0\u5728\u706b\u8f66\u5934\u90e8\u3002\u6b64\u65f6\u89e3\u7801\u5668\u5e94\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u66ff\u6362\u3002 10000010 10000001 \u5728\u7f51\u7edc\u6536\u53d1\u5305\u65f6\uff0c\u5982\u679c\u4f60\u4e0d\u59a5\u5584\u5904\u7406 TCP \u7c98\u5305\u95ee\u9898\uff0c\u5c31\u53ef\u80fd\u706b\u8f66\u5934\u8fdb\u53bb\u4e86\uff0c\u706b\u8f66\u5c3e\u5df4\u8fd8\u9732\u5728\u96a7\u9053\u5916\u9762\uff0c\u4e00\u6bb5\u5b8c\u6574\u7684\u5217\u8f66\u88ab\u5207\u65ad\uff0c\u5bfc\u81f4 UTF-8 \u89e3\u8bfb\u7684\u65f6\u5019\u51fa\u9519\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u8bbe\u7acb\u4e00\u4e2a\u72b6\u6001\u673a\u6765\u89e3\u7801 UTF-8\u3002C \u8bed\u8a00\u7684 mbstate_t \u5c31\u662f\u8fd9\u79cd\u72b6\u6001\u673a\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\uff0c\u5374\u53d1\u73b0\u91cc\u9762\u88c5\u7740 0x394 (\u201c\u0394\u201d)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u5c31\u80fd\u88c5\u4e0b\u7684\u6b27\u6d32\u5b57\u7b26\uff0c\u5374\u7528\u4e86\u4e09\u7ea7\u706b\u8f66\u5934\u88c5\uff0c\u8bf4\u660e\u88c5\u7bb1\u90a3\u8fb9\u7684\u4eba\u5077\u61d2\u6ee5\u7528\u8d44\u6e90\u4e86\uff01\u8fd9\u79cd\u60c5\u51b5\u4e0b UTF-8 \u89e3\u7801\u5668\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u8981\u4fdd\u8bc1\u7f16\u7801\u7684\u552f\u4e00\u6027\uff0c0x394 \u662f 0x7F \u5230 0x7FF \u8303\u56f4\u7684\uff0c\u5c31\u5e94\u8be5\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u88c5\u3002 \u4ee5\u53ca\uff0c\u5982\u679c\u53d1\u73b0 11111 \u5f00\u5934\u7684\u4e94\u7ea7\u706b\u8f66\u5934\uff0c\u4e5f\u8981\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u6700\u591a\u53ea\u652f\u6301\u56db\u7ea7\u706b\u8f66\u5934\u3002 \u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u56db\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u7684\u5b57\u7b26\u8303\u56f4\u8d85\u8fc7\u4e86 0x10FFFF\uff0c\u8fd9\u8d85\u51fa\u4e86 Unicode \u7684\u8303\u56f4\uff0c\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\u3002\u5982\u679c\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u53d1\u73b0\u5b57\u7b26\u8303\u56f4\u5904\u5728\u4fdd\u7559\u533a 0xD800 \u5230 0xDFFF \u5185\uff0c\u8fd9\u662f Unicode \u627f\u8bfa\u6c38\u4e0d\u52a0\u5165\u5b57\u7b26\u7684\u533a\u95f4\uff08\u7a0d\u540e\u8bb2\u89e3 UTF-16 \u65f6\u4f1a\u89e3\u91ca\u4e3a\u4ec0\u4e48\uff09\uff0c\u4e5f\u8981\u62a5\u9519\u3002\u603b\u4e4b Unicode \u7801\u70b9\u7684\u5408\u6cd5\u8303\u56f4\u662f 0x0 \u5230 0xD7FF\uff0c0xE000 \u5230 0x10FFFF\u3002 \u603b\u4e4b\uff0cUTF-8 \u5177\u6709\u4e00\u5b9a\u7684\u5197\u4f59\u548c\u81ea\u7ea0\u9519\u80fd\u529b\uff0c\u5982\u679c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5dee\u9519\uff0c\u53ef\u80fd\u4f1a\u7206\u51fa\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u3002\u8fd9\u4e2a\u7279\u6b8a\u5b57\u7b26\u662f Unicode \u5b98\u65b9\u89c4\u5b9a\u7684\uff0c\u7801\u70b9\u4e3a 0xFFFD\uff0c\u51fa\u73b0\u4ed6\u5c31\u610f\u5473\u7740 UTF-8 \u89e3\u7801\u5931\u8d25\u4e86\u3002 \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u4ee5 UTF-8 \u683c\u5f0f\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff1a std::vector s = { 0xE6, 0x88, 0x91, // \u6211\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xE7, 0x88, 0xB1, // \u7231\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xE9, 0x9D, 0xA2, // \u9762\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0x21, // !\uff0c\u8fd9\u662f\u4e2a ASCII \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u76f4\u63a5\u7528\u5355\u4e2a\u706b\u8f66\u5934\u88c5 }; UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u662f\u4e00\u79cd \u53d8\u957f\u7f16\u7801 \u3002\u53d8\u957f\u7f16\u7801\u7684\u7f3a\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e0d\u4e00\u5b9a\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u56e0\u6b64\uff0c\u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\u3002 \u6570\u7ec4\u7684\u5355\u4e2a\u5143\u7d20\u7d22\u5f15\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u53d6\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u3002 \u5bf9\u6570\u7ec4\u7684\u5207\u7247\uff0c\u53ef\u80fd\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u5207\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u4e0d\u4e00\u5b9a\u80fd\u628a\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\uff0c\u56e0\u4e3a\u53ef\u80fd\u4e0d\u614e\u628a\u4e00\u4e2a\u5b57\u7b26\u7684\u591a\u4e2a\u7801\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u5b57\u7b26\u7834\u574f\u3002 \u4f18\u70b9\u662f\uff1a \u8282\u7ea6\u5b58\u50a8\u7a7a\u95f4\u3002 \u6211\u4eec\u63a8\u8350\u53ea\u5728\u7f51\u7edc\u901a\u4fe1\u3001\u786c\u76d8\u5b58\u50a8\u65f6\uff0c\u91c7\u7528 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u6587\u5b57\u3002 \u603b\u4e4b\uff0cUTF-8 \u9002\u5408\u5b58\u50a8\uff0cUTF-32 \u9002\u5408\u5904\u7406\u3002 \u6211\u4eec\u5efa\u8bae\u8ba1\u7b97\u673a\u4ece\u786c\u76d8\u6216\u7f51\u7edc\u4e2d\u8bfb\u51fa UTF-8 \u5b57\u7b26\u4e32\u540e\uff0c\u7acb\u5373\u5c06\u5176\u8f6c\u6362\u4e3a UTF-32\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u6587\u5b57\u5904\u7406\u3002\u5f53\u9700\u8981\u5199\u5165\u786c\u76d8\u6216\u7f51\u7edc\u65f6\uff0c\u518d\u8f6c\u6362\u56de UTF-8\uff0c\u907f\u514d\u786c\u76d8\u5bb9\u91cf\u548c\u7f51\u7edc\u5e26\u5bbd\u7684\u6d6a\u8d39\u3002 \u8ba1\u7b97\u673a\u9700\u8981\u5916\u7801\u548c\u5185\u7801\u4e24\u79cd\uff1a \u5916\u7801=\u786c\u76d8\u4e2d\u7684\u6587\u672c=UTF-32 \u5185\u7801=\u5185\u5b58\u4e2d\u7684\u6587\u672c=UTF-8 UTF-16 UTF-16 \u7684\u7b56\u7565\u662f\uff1a\u65e2\u7136\u5927\u591a\u6570\u5e38\u7528\u5b57\u7b26\u7684\u7801\u70b9\u90fd\u5728 0x0 \u5230 0xFFFF \u5185\uff0c\u7528 uint32_t \u6765\u5b58\u50a8\u4e5f\u592a\u6d6a\u8d39\u4e86\u3002\u4ed6\u7684\u65b9\u6848\u5982\u4e0b\uff1a \u5bf9\u4e8e 0x0 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5c31\u7528\u4e00\u4e2a uint16_t \u76f4\u63a5\u5b58\u3002 \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u53cd\u6b63\u4e0d\u5e38\u89c1\uff0c\u5c31\u62c6\u6210\u4e24\u4e2a uint16_t \u5b58\u3002\u8fd9\u4e2a\u62c6\u7684\u65b9\u6848\u5f88\u6709\u8bb2\u7a76\uff0c\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u62c6\uff0c\u7531\u4e8e\u89e3\u7801\u65f6\u6536\u5230\u7684\u662f\u4e2a\u6ca1\u5934\u6ca1\u5c3e\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u65e0\u6cd5\u5206\u8fa8\u8fd9\u5230\u5e95\u662f\u4e24\u4e2a uint16_t \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u8fd8\u662f\u4e00\u4e2a uint16_t \u7684\u666e\u901a\u5b57\u7b26\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u628a\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u201c\ud883\udede\u201d\uff0c0x30EDE\u3002\u62c6\u6210\u4e24\u4e2a uint16_t \uff0c\u5f97\u5230 0x3 \u548c 0x0EDE\u3002\u5982\u679c\u76f4\u63a5\u5b58\u50a8\u8fd9\u4e24\u4e2a uint16_t \uff1a 0x0003 0x0EDE \u4e4b\u540e\u89e3\u7801\u65f6\uff0c\u5148\u8bfb\u5230 0x0003\uff0c\u8fd8\u4f1a\u4ee5\u4e3a\u4ed6\u662f\u5355\u72ec\u7684\u4e00\u4e2a uint16_t \uff0c\u8868\u793a 3 \u53f7\u5b57\u7b26\u201c\u201d\u3002\u540e\u9762\u7684 0x0EDE \u5c31\u53d8\u6210\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 0x0EDE\uff0c\u53d8\u6210\u4e86 0x0EDE \u53f7\u5b57\u7b26 \u201c\u0ede\u201d\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u201c\ud883\udede\u201d\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u6beb\u4e0d\u76f8\u5e72\u7684\u5b57\u7b26\uff0c\u201c\u0ede\u201d\u4e86\u3002 \u4e3a\u4e86\u907f\u514d\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u4e24\u4e2a uint16_t \u9700\u8981\u91c7\u7528\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\u4ee5\u793a\u533a\u5206\u3002\u8ba9\u89e3\u7801\u5668\u4e00\u770b\u5230\uff0c\u5c31\u80fd\u786e\u5b9a\u8fd9\u4e24\u4e2a uint16_t \u9700\u8981\u7ec4\u88c5\u6210\u540c\u4e00\u4e2a\u5b57\u7b26\u3002 \u8fd9\u5c31\u7528\u5230\u4e86\u4e00\u4e2a\u201c\u6f0f\u6d1e\u201d\uff1aUnicode \u5e76\u6ca1\u6709\u628a\u7801\u70b9\u5206\u914d\u7684\u6ee1\u6ee1\u5f53\u5f53\uff0c\u6216\u8bb8\u662f\u51fa\u4e8e\u5148\u89c1\u4e4b\u660e\uff0c\u5728 0xD800 \u5230 0xDFFF \u4e4b\u95f4\u9884\u7559\u4e86\u4e00\u5927\u6bb5\u7a7a\u53f7\uff1a UTF-16 \u5c31\u662f\u5229\u7528\u4e86\u8fd9\u4e00\u6bb5\u7a7a\u95f4\uff0c\u4ed6\u89c4\u5b9a\uff1a0xD800 \u5230 0xDFFF \u4e4b\u95f4\u7684\u7801\u70b9\u5c06\u6c38\u8fdc\u4e0d\u7528\u6765\u8868\u793a\u5b57\u7b26\uff0c\u800c\u662f\u4f5c\u4e3a \u4ee3\u7406\u5bf9 (surrogate-pair) \u3002\u5176\u4e2d 0xD800 \u5230 0xDBFF \u662f \u9ad8\u4f4d\u4ee3\u7406 (high surrogate) \uff0c0xDC00 \u5230 0xDFFF \u662f \u4f4e\u4f4d\u4ee3\u7406 (low surrogate) \u3002\u9ad8\u4ee3\u7406\u5728\u524d\uff0c\u4f4e\u4ee3\u7406\u5728\u540e\u3002 \u4e00\u4e2a\u8d85\u8fc7 0xFFFF \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f1a\u88ab\u62c6\u6210\u4e24\u6bb5\uff0c\u4e00\u6bb5\u653e\u5728\u9ad8\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u6bb5\u653e\u5728\u4f4e\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u524d\u4e00\u540e\u653e\u5165 uint16_t \u5e8f\u5217\u4e2d\u3002 \u642d\u8f7d\u8d85\u5bbd\u8d85\u9650\u8d27\u7269\u7684\u8f66\u8f86\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e24\u6bb5\u518d\u8fdb\u5165\u96a7\u9053\u3002 \u5177\u4f53\u62c6\u5206\u65b9\u6cd5\u5982\u4e0b\uff1a \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7801\u70b9\uff0c\u9996\u5148\u5c06\u5176\u503c\u51cf\u53bb 0x10000\uff0c\u53d8\u6210\u4e00\u4e2a\u8303\u56f4 0x0 \u5230 0xFFFFF \u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u8fd9\u80fd\u4fdd\u8bc1\u4ed6\u4eec\u53ea\u9700 20 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u53ef\u8868\u793a\u3002 \u4f8b\u5982\u201c\ud883\udede\u201d\u5bf9\u5e94\u7684\u7801\u70b9 0x30EDE\uff0c\u51cf\u53bb\u540e\u5c31\u53d8\u6210 0x20EDE\u3002 \u7136\u540e\uff0c\u5199\u51fa 0x20EDE \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a 00100000111011011110 \u603b\u5171 20 \u4f4d\uff0c\u6211\u4eec\u5c06\u5176\u62c6\u6210\u9ad8\u4f4e\u5404 10 \u4f4d\uff1a 0010000011 1011011110 \u5404\u81ea\u5199\u51fa\u76f8\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6570\uff1a 0x083 0x2DE \u56e0\u4e3a\u6700\u591a\u53ea\u6709 10 \u4f4d\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4f1a\u5728 0 \u5230 0x3FF \u7684\u8303\u56f4\u5185\u3002 \u800c 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u9884\u7559\u7684\u7a7a\u95f4\uff0c\u521a\u597d\u53ef\u4ee5\u5206\u522b\u5bb9\u7eb3 0x400 \u4e2a\u6570\uff01 \u6240\u4ee5\uff0c\u6211\u4eec\u5c06\u62c6\u5206\u51fa\u6765\u7684\u4e24\u4e2a 10 \u4f4d\u6570\uff0c\u5206\u522b\u52a0\u4e0a 0xD800 \u548c 0xDC00\uff1a 0xD800+0x083=0xD883 0xDC00+0x2DE=0xDFDE \u8fd9\u4e24\u4e2a\u6570\uff0c\u5fc5\u5b9a\u662f 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u6570\u3002\u800c\u8fd9\u4e24\u4e2a\u8303\u56f4\u90fd\u662f Unicode \u59d4\u5458\u4f1a\u9884\u7559\u7684\u4ee3\u7406\u5bf9\u533a\u95f4\uff0c\u7edd\u5bf9\u6ca1\u6709\u666e\u901a\u5b57\u7b26\u3002\u6240\u4ee5\uff0c\u751f\u6210\u7684\u4e24\u4e2a\u4ee3\u7406\u5bf9\u4e0d\u4f1a\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u53ef\u4ee5\u653e\u5fc3\u653e\u8fdb uint16_t \u6570\u7ec4\uff0c\u89e3\u7801\u5668\u5982\u679c\u68c0\u6d4b\u5230\u4ee3\u7406\u5bf9\uff0c\u5c31\u8bf4\u660e\u662f\u4e24\u8282\u8f66\u53a2\uff0c\u53ef\u4ee5\u653e\u5fc3\u8fde\u7eed\u8bfb\u53d6\u4e24\u4e2a uint16_t \u3002 \u6240\u4ee5\uff0c 0xD883 0xDFDE \u5c31\u662f\u201c\ud883\udede\u201d\u7528 UTF-16 \u7f16\u7801\u540e\u7684\u7ed3\u679c\u3002 \u4ee3\u7406\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\uff0c\u5f53\u89e3\u7801\u5668\u68c0\u6d4b\u5230\u4e00\u4e2a 0xD800 \u5230 0xDBFF \u8303\u56f4\u5185\u7684\u9ad8\u4ee3\u7406\u65f6\uff0c\u5c31\u9884\u793a\u7740\u8fd8\u9700\u8981\u518d\u8bfb\u53d6\u4e00\u4e2a\u4f4e\u4ee3\u7406\uff0c\u624d\u80fd\u62fc\u63a5\u6210\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u3002 \u5982\u679c\u63a5\u4e0b\u6765\u8bfb\u5230\u7684\u4e0d\u662f 0xDC00 \u5230 0xDFFF \u8303\u56f4\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u800c\u662f\u666e\u901a\u5b57\u7b26\u7684\u8bdd\uff0c\u90a3\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u53ef\u80fd\u662f\u4e2d\u95f4\u88ab\u4eba\u4e22\u5305\u4e86\uff0c\u9700\u8981\u62a5\u9519\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u9876\u66ff\u3002 \u53e6\u5916\uff0c\u5982\u679c\u8bfb\u5230\u4e86\u4e00\u4e2a\u5355\u72ec\u5b58\u5728\u7684 0xD800 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u90a3\u4e5f\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u56e0\u4e3a\u4ee3\u7406\u5b57\u7b26\u53ea\u6709\u6210\u5bf9\u51fa\u73b0\u624d\u6709\u610f\u4e49\uff0c\u4f4e\u4ee3\u7406\u5b57\u7b26\u4e0d\u53ef\u80fd\u5355\u72ec\u5728\u5f00\u5934\u51fa\u73b0\u3002 \u53ef\u89c1\uff0cUTF-16 \u548c UTF-8 \u4e00\u6837\uff0c\u90fd\u662f\u201c\u5c0f\u706b\u8f66\u201d\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0cUTF-16 \u540c\u6837\u4e5f\u6709\u7740\u7c7b\u4f3c\u4e8e UTF-8 \u7684\u6297\u5e72\u6270\u673a\u5236\u3002 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 \u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u591a\u5b57\u8282\u7684\u6574\u6570\u7c7b\u578b\uff08\u5982 uint16_t \u548c uint32_t \uff09\u9700\u8981\u88ab\u62c6\u6210\u591a\u4e2a\u5b57\u8282\u6765\u5b58\u50a8\u3002\u62c6\u5f00\u540e\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u6309\u4ec0\u4e48\u987a\u5e8f\u5b58\u5165\u5185\u5b58\uff1f\u4e0d\u540c\u7684\u786c\u4ef6\u67b6\u6784\u4ea7\u751f\u4e86\u4e89\u6267\uff1a \u5927\u7aef\u6d3e (bit endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u4e5f\u5c31\u662f\u5927\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5c0f\u7aef\u6d3e (little endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u4e5f\u5c31\u662f\u5c0f\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u8ba1\u7b97\u673a\u7535\u8def\u7684\u8ba1\u7b97\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x78 0x56 0x34 0x12 \u4f8b\u5982\uff0cIntel \u7684 x86 \u67b6\u6784\u548c ARM \u516c\u53f8\u7684 ARM \u67b6\u6784\u90fd\u662f\u5c0f\u7aef\u6d3e\uff0c\u800c Motorola \u516c\u53f8\u7684 68k \u67b6\u6784\u548c Sun \u516c\u53f8\u7684 SPARC \u67b6\u6784\u90fd\u662f\u5927\u7aef\u6d3e\u3002 \u8fd9\u5176\u5b9e\u662f\u5f88\u65e0\u804a\u7684\u4e89\u6267\uff0c\u4e3a\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u6539\u53d8\u8ba1\u7b97\u673a\u7684\u8bbe\u8ba1\u6beb\u65e0\u9053\u7406\uff0c\u6bd5\u7adf\u4e16\u754c\u4e0a\u4e5f\u6709\u4ece\u53f3\u5f80\u5de6\u4e66\u5199\u7684\u6587\u5b57\u548c\u4ece\u4e0a\u5f80\u4e0b\u4e66\u5199\u7684\u6587\u5b57\uff0c\u751a\u81f3\u6709\u5de6\u53f3\u6765\u56de\u4e66\u5199\u7684\u6587\u5b57\u2026\u2026\u5982\u679c\u8981\u4f3a\u5019\u4eba\u7c7b\uff0c\u4f60\u600e\u4e48\u4e0d\u6539\u6210\u5341\u8fdb\u5236\u5462\uff1f\u603b\u4e4b\uff0c\u6211\u8ba4\u4e3a\u5c0f\u7aef\u624d\u662f\u6700\u9002\u5408\u8ba1\u7b97\u673a\u7684\uff0c\u5e02\u9762\u4e0a\u5927\u591a\u6570\u4e3b\u6d41\u786c\u4ef6\u90fd\u662f\u5c0f\u7aef\u67b6\u6784\u3002 \u5728\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u53d1\u6d88\u606f\u548c\u6536\u6d88\u606f\u7684\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u67b6\u6784\uff0c\u5982\u679c\u53d1\u6d88\u606f\u7684\u662f\u5c0f\u7aef\u67b6\u6784\uff0c\u6536\u6d88\u606f\u7684\u662f\u5927\u7aef\u67b6\u6784\uff0c\u90a3\u4e48\u53d1\u51fa\u53bb\u7684\u662f 0x12345678\uff0c\u6536\u5230\u7684\u5c31\u4f1a\u53d8\u6210 0x78563421 \u4e86\u3002 \u56e0\u6b64\u4e92\u8054\u7f51\u4e00\u822c\u89c4\u5b9a\uff0c\u6240\u6709\u591a\u5b57\u8282\u7684\u6570\u636e\u5728\u7f51\u7edc\u5305\u4e2d\u7edf\u4e00\u91c7\u7528\u5927\u7aef\u3002\u5bf9\u4e8e\u5927\u7aef\u67b6\u6784\uff0c\u4ed6\u4eec\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u5bf9\u4e8e\u5c0f\u7aef\u67b6\u6784\uff0c\u5728\u53d1\u5305\u524d\u9700\u8981\u628a\u81ea\u5df1\u7684\u5c0f\u7aef\u6570\u636e\u505a\u5b57\u8282\u5e8f\u53cd\u8f6c\uff0c\u53d8\u6210\u5927\u7aef\u7684\u4ee5\u540e\uff0c\u518d\u53d1\u9001\u3002\u4e4b\u540e\u7684\u7f51\u7edc\u4e13\u9898\u8bfe\u4e2d\u6211\u4eec\u4f1a\u8be6\u89e3\u8fd9\u4e00\u5757\u3002 \u57fa\u4e8e\u5b57\u8282\u7801\u7684\u865a\u62df\u673a\u8bed\u8a00\u901a\u5e38\u4f1a\u89c4\u5b9a\u4e00\u4e2a\u5b57\u8282\u5e8f\uff1a\u50cf Java \u8fd9\u79cd\u9762\u5411\u4e92\u8054\u7f51\u8bed\u8a00\uff0c\u7d22\u6027\u4e5f\u89c4\u5b9a\u4e86\u7edf\u4e00\u91c7\u7528\u5927\u7aef\uff0c\u65e0\u8bba JVM \u8fd0\u884c\u5728\u5927\u7aef\u673a\u5668\u8fd8\u662f\u5c0f\u7aef\u673a\u5668\u4e0a\u3002\u8fd9\u4f7f\u5f97\u4ed6\u4e0e\u4e92\u8054\u7f51\u901a\u4fe1\u6bd4\u8f83\u65b9\u4fbf\uff0c\u800c\u5728 x86 \u548c ARM \u67b6\u6784\u4e0a\uff0c\u4e0e\u672c\u5730\u53ea\u63a5\u53d7\u5c0f\u7aef\u6570\u636e\u7684 API\uff0c\u4f8b\u5982 OpenGL\uff0c\u6c9f\u901a\u8f83\u4e3a\u56f0\u96be\uff0c\u9700\u8981\u505a\u989d\u5916\u7684\u5b57\u8282\u5e8f\u8f6c\u6362\u3002\u800c C# \u4e3b\u6253\u6e38\u620f\u4e1a\u52a1\uff08\u4f8b\u5982 Unity\uff09\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\uff0c\u6240\u4ee5\u89c4\u5b9a\u5168\u90e8\u91c7\u7528\u5c0f\u7aef\u3002\u4f5c\u4e3a\u5e95\u5c42\u7f16\u7a0b\u8bed\u8a00\u7684 C++ \u5219\u662f\u5165\u4e61\u968f\u4fd7\uff0c\u4f60\u7684\u786c\u4ef6\u662f\u4ec0\u4e48\u7aef\uff0c\u4ed6\u5c31\u662f\u4ec0\u4e48\u7aef\uff0c\u4e0d\u4e3b\u52a8\u505a\u4efb\u4f55\u989d\u5916\u7684\u8f6c\u6362\u3002 UTF-16 \u548c UTF-32 \u7684\u7801\u4f4d\u90fd\u662f\u591a\u5b57\u8282\u7684\uff0c\u4e5f\u4f1a\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u3002\u4f8b\u5982\uff0cUTF-16 \u4e2d\u7684 uint16_t \u5e8f\u5217\uff1a 0x1234 0x5678 \u5728\u5927\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5728\u5c0f\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x34 0x12 0x78 0x56 \u8fd9\u6837\u4e00\u6765\uff0cUTF-16 \u548c UTF-32 \u7684\u5b57\u8282\u6d41\uff0c\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u987a\u5e8f\u3002\u8fd9\u7ed9\u8de8\u5e73\u53f0\u7684\u6587\u672c\u5904\u7406\u5e26\u6765\u4e86\u9ebb\u70e6\u3002 \u6240\u4ee5\u5f53\u4f60\u9700\u8981\u628a UTF-16 \u5b58\u5165\u786c\u76d8\u548c\u5728\u7f51\u7edc\u53d1\u9001\u65f6\uff0c\u8fd8\u9700\u8981\u989d\u5916\u6307\u660e\u4f60\u7528\u7684\u662f\u5927\u7aef\u7684 UTF-16 \u8fd8\u662f\u5c0f\u7aef\u7684 UTF-16\u3002 \u56e0\u6b64 UTF-16 \u548c UTF-32 \u8fdb\u4e00\u6b65\u5206\u88c2\u4e3a\uff1a UTF-16LE\uff1a\u5c0f\u7aef\u7684 UTF-16 UTF-16BE\uff1a\u5927\u7aef\u7684 UTF-16 UTF-32LE\uff1a\u5c0f\u7aef\u7684 UTF-32 UTF-32BE\uff1a\u5927\u7aef\u7684 UTF-32 \u5982\u679c\u53ea\u5728\u5185\u5b58\u7684 wchar_t \u4e2d\u4f7f\u7528\u5c31\u4e0d\u7528\u533a\u5206\uff0c\u9ed8\u8ba4\u8ddf\u968f\u5f53\u524d\u673a\u5668\u7684\u5927\u5c0f\u7aef\u3002\u6240\u4ee5 UTF-16 \u548c UTF-32 \u901a\u5e38\u53ea\u4f1a\u51fa\u73b0\u5728\u5185\u5b58\u4e2d\u7528\u4e8e\u5feb\u901f\u5904\u7406\u548c\u8ba1\u7b97\uff0c\u5f88\u5c11\u7528\u5728\u5b58\u50a8\u548c\u901a\u4fe1\u4e2d\u3002 UTF-8 \u662f\u57fa\u4e8e\u5355\u5b57\u8282\u7684\u7801\u4f4d\uff0c\u706b\u8f66\u5934\u7684\u987a\u5e8f\u4e5f\u6709\u4e25\u683c\u89c4\u5b9a\uff0c\u706b\u8f66\u5934\u603b\u662f\u5728\u6700\u524d\uff0c\u6839\u672c\u4e0d\u53d7\u5b57\u8282\u5e8f\u5927\u5c0f\u7aef\u5f71\u54cd\uff0c\u4e5f\u5c31\u6ca1\u6709\u5f71\u54cd\u3002 \u7531\u4e8e\u538b\u7f29\u7387\u4f4e\uff0c\u53c8\u5b58\u5728\u5927\u5c0f\u7aef\u5b57\u8282\u5e8f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u800c\u4e92\u8054\u7f51\u6570\u636e\u9700\u8981\u4fdd\u8bc1\u76f8\u540c\u7684\u5927\u5c0f\u7aef\uff0c\u5728\u6536\u53d1\u5305\u65f6\u9700\u8981\u989d\u5916\u8f6c\u6362\uff0c\u56e0\u800c\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u7f51\u7edc\u3002\u800c UTF-8 \u7684\u5b58\u50a8\u5355\u4f4d\u662f\u5b57\u8282\uff0c\u5929\u751f\u6ca1\u6709\u5927\u5c0f\u7aef\u56f0\u6270\u3002\u66f4\u5999\u7684\u662f\uff0c\u4ed6\u4e14\u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u800c\u4e92\u8054\u7f51\u53c8\u662f\u53e4\u8463\u4e2d\u95f4\u4ef6\u6700\u591a\u7684\u5730\u65b9\u2026\u2026 \u603b\u4e4b\uff0c\u5b8c\u5168\u57fa\u4e8e\u5b57\u8282\u7684 UTF-8 \u662f\u6700\u9002\u5408\u7f51\u7edc\u901a\u4fe1\u548c\u786c\u76d8\u5b58\u50a8\u7684\u6587\u672c\u7f16\u7801\u683c\u5f0f\uff0c\u800c UTF-32 \u662f\u6700\u9002\u5408\u5728\u5185\u5b58\u4e2d\u5904\u7406\u7684\u683c\u5f0f\u3002 BOM \u6807\u8bb0 0xFEFF \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u4e0d\u53ef\u89c1\u5b57\u7b26\u201c\ufeff\u201d\uff0c\u8fd9\u662f\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002 \u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u7b26\u52a0\u5728\u6587\u672c\u6587\u4ef6\u7684\u5934\u90e8\uff0c\u544a\u8bc9\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u7528\u4ec0\u4e48\u7f16\u7801\u7684\u3002 \u5982\u679c\u662f UTF-16 \u548c UTF-32\uff0c\u56e0\u4e3a 0xFEFF \u4e0d\u5bf9\u79f0\uff0c\u4ed6\u8fd8\u80fd\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\u3002\u56e0\u6b64 0xFEFF \u88ab\u79f0\u4e3a\u5b57\u8282\u5e8f\u6807\u5fd7\uff08Byte-order-mark\uff0cBOM\uff09\u3002 \u5982\u679c\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u90a3\u4e48\u4ed6\u7167\u5e38\u8bfb\u51fa 0xFEFF\uff0c\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u5728\u6587\u672c\u4e2d\u4e0d\u663e\u793a\uff0c\u4e0d\u5f71\u54cd\u89c6\u89c9\u7ed3\u679c\u3002 \u4e00\u4e9b\u8001\u7684\u7f16\u8bd1\u5668\uff08\u8fdc\u53e4 MinGW\uff0c\u73b0\u5728\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u4f1a\u628a\u5e26\u6709 BOM \u7684 UTF-8 \u7684 .cpp \u6e90\u7801\u6587\u4ef6\uff0c\u5f53\u4f5c\u5934\u90e8\u5e26\u6709\u9519\u8bef\u5b57\u7b26\u7684\u4e71\u7801\u6587\u4ef6\uff0c\u4ece\u800c\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u7684\u8bb0\u4e8b\u672c\u4fdd\u5b58\u4e3a UTF-8 \u65f6\uff0c\u603b\u662f\u4f1a\u52a0\u4e0a BOM\u3002\u5982\u679c\u8bb0\u4e8b\u672c\u53d1\u73b0\u4e00\u4e2a\u6587\u4ef6\u6ca1\u6709 BOM\uff0c\u4f1a\u5f53\u4f5c ANSI\uff08GBK\uff09\u6765\u8bfb\u53d6\u3002 0xFEFF \u5728\u4e0d\u540c\u7684\u7f16\u7801\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1a UTF-8\uff1a 0xEF 0xBB 0xBF \uff0c\u4ed6\u4f1a\u5360\u7528 3 \u5b57\u8282\uff0c\u800c\u4e14\u4e0d\u4f1a\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\uff0c\u56e0\u4e3a UTF-8 \u662f\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u7684\u3002 UTF-16\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE \u3002 UTF-32\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0x00 0x00 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE 0x00 0x00 \u3002 \u56e0\u6b64\uff0c\u5728\u6587\u672c\u5934\u90e8\u52a0\u4e0a BOM \u6709\u52a9\u4e8e\u8f6f\u4ef6\u63a8\u6d4b\u8be5\u6587\u4ef6\u662f\u4ec0\u4e48\u7f16\u7801\u7684\uff08\u5982\u679c\u90a3\u8f6f\u4ef6\u652f\u6301\u89e3\u6790 BOM \u7684\u8bdd\uff09\u3002 \u4f8b\u5982 Windows \u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\u90fd\u88ab\u9ed8\u8ba4\u5047\u5b9a\u4e3a ANSI\uff08GBK\uff09\u7f16\u7801\uff0c\u5982\u679c\u4f60\u8981\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u4e3a UTF-8 \u7f16\u7801\uff0c\u5c31\u9700\u8981\u52a0\u4e0a BOM \u6807\u5fd7\u3002\u5f53 MSVC \u8bfb\u53d6\u65f6\uff0c\u770b\u5230\u5f00\u5934\u662f 0xEF 0xBB 0xBF \uff0c\u5c31\u660e\u767d\u8fd9\u662f\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u6587\u4ef6\u3002\u8fd9\u6837\uff0cMSVC \u5c31\u80fd\u6b63\u786e\u5730\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4e86\u3002\u5982\u679c MSVC \u6ca1\u770b\u5230 BOM\uff0c\u4f1a\u9ed8\u8ba4\u4ee5\u4e3a\u662f ANSI\uff08GBK\uff09\u7f16\u7801\u7684\uff0c\u4ece\u800c\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4f1a\u4e71\u7801\u3002\u5f00\u542f /utf-8 \u9009\u9879\u4e5f\u80fd\u8ba9 MSVC \u628a\u6ca1\u6709 BOM \u7684\u6e90\u7801\u6587\u4ef6\u5f53\u4f5c UTF-8 \u6765\u89e3\u6790\uff0c\u9002\u5408\u8de8\u5e73\u53f0\u5b9d\u5b9d\u4f53\u8d28\u3002 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u7c7b\u578b \u5927\u5c0f \u7f16\u7801 \u5b57\u9762\u91cf Linux char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e $LC_ALL \u201chello\u201d Windows char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e \u201chello\u201d Linux wchar_t 4 \u5b57\u8282 UTF-32 L\u201dhello\u201d Windows wchar_t 2 \u5b57\u8282 UTF-16 L\u201dhello\u201d char8_t 1 \u5b57\u8282 UTF-8 u8\u201dhello\u201d char16_t 2 \u5b57\u8282 UTF-16 u\u201dhello\u201d char32_t 4 \u5b57\u8282 UTF-32 U\u201dhello\u201d \u7531\u6b64\u53ef\u89c1\uff0c char \u548c wchar_t \u662f\u4e0d\u8de8\u5e73\u53f0\u7684\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f GBK\u3002\u5bf9\u4e8e\u7f8e\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\u3002 \u5bf9\u4e8e Linux \u7528\u6237\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e13\u95e8\u4fee\u6539\u8fc7\uff0c $LC_ALL \u9ed8\u8ba4\u662f en_US.UTF-8 \u6216 C.UTF-8 \u3002 \u8fd9\u5e26\u6765\u4e86\u5de8\u5927\u7684\u6df7\u6dc6\uff01\u5f88\u591a\u7f8e\u56fd\u7a0b\u5e8f\u5458\u6f5c\u610f\u8bc6\u91cc\u4f1a\u60f3\u5f53\u7136\u5730\u628a char \u5f53\u4f5c UTF-8 \u6765\u7528\u3002\u5f88\u591a\u5f00\u6e90\u9879\u76ee\uff0c\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u5f88\u591a\u56fd\u4eba\u505a\u7684\u9879\u76ee\uff0c\u90fd\u88ab\u8fd9\u79cd\u201c\u60f3\u5f53\u7136\u201d\u4f20\u67d3\u4e86\u3002 \u597d\u6d88\u606f\u662f\u65e0\u8bba\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u662f\u4ec0\u4e48\uff0c\u80af\u5b9a\u517c\u5bb9 ASCII\u3002\u4f8b\u5982 GBK \u548c UTF-8 \u90fd\u517c\u5bb9 ASCII\uff0c\u5426\u5219\u5c31\u548c\u6240\u6709\u7684 C \u8bed\u8a00\u7ecf\u5178\u51fd\u6570\u5982 strlen \uff0c\u6362\u884c\u7b26 '\\n' \uff0c\u8def\u5f84\u5206\u9694\u7b26 '/' \u548c '\\\\' \u51b2\u7a81\u4e86\u3002 wchar_t \u5c31\u597d\u4e00\u4e9b\uff0c\u867d\u7136\u5728 Windows \u7cfb\u7edf\u4e0a\u662f\u7cdf\u7cd5\u7684 UTF-16\uff0c\u4f46\u81f3\u5c11\u7a33\u5b9a\u4e86\uff0c\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u800c\u968f\u610f\u6539\u53d8\uff0c\u53ea\u8981\u4f60\u4e0d\u6253\u7b97\u8de8\u5e73\u53f0\uff0c wchar_t \u5c31\u662f Windows \u7a0b\u5e8f\u7684\u6807\u914d\u3002 \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u7684\u706b\u8f66\u5934\u548c\u8f66\u53a2\uff0c\u90fd\u662f 1 \u5f00\u5934\u7684\uff0c\u800c ASCII \u7684\u5355\u4f53\u706b\u8f66\u5934\u6c38\u8fdc\u662f 0 \u5f00\u5934\u3002\u8fd9\u5f88\u91cd\u8981\uff0c\u4e0d\u4ec5\u706b\u8f66\u5934\u9700\u8981\u548c ASCII \u533a\u5206\u5f00\u6765\uff0c\u8f66\u53a2\u4e5f\u9700\u8981\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff1a std::u32string path = \"\u4e00\u4e2a\u8001\u4f2f.txt\"; \u201c\u4e00\u4e2a\u8001\u4f2f\u201d \u8f6c\u6362\u4e3a Unicode \u7801\u70b9\u5206\u522b\u662f\uff1a 0x4E00 0x4E2A 0x8001 0x4F2F \u5982\u679c\u8ba9\u4ed6\u4eec\u539f\u5c01\u4e0d\u52a8\u76f4\u63a5\u5b58\u50a8\u8fdb char \u6570\u7ec4\u91cc\uff1a 0x4E 0x00 0x4E 0x2A 0x80 0x01 0x4F 0x2F \u5c31\u51fa\u95ee\u9898\u4e86\uff01\u9996\u5148\uff0c\u8fd9\u91cc 0x4E00 \u7684 0x00 \u90e8\u5206\uff0c\u4f1a\u88ab C \u8bed\u8a00\u5f53\u4f5c\u662f\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u62ff\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u53bb\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684 open \u51fd\u6570\uff0c\u4ed6\u4f1a\u4ee5\u4e3a\u4f60\u5728\u6253\u5f00 0x4E \u5355\u4e2a\u5b57\u7b26\u7684\u6587\u4ef6\u540d\uff0c\u4e5f\u5c31\u662f \"N\" \u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c0x2F \u5bf9\u5e94\u7684 ASCII \u5b57\u7b26\u662f '/' \uff0c\u662f\u8def\u5f84\u5206\u9694\u7b26\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4ee5\u4e3a\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u5b50\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6 \"N\\x00N*\\x80\\x01O/.txt\" \uff0c\u6587\u4ef6\u5939\u540d\u5b57\u53eb \"N\\x00N*\\x80\\x01O\" \u800c\u6587\u4ef6\u53eb \".txt\" \u3002 \u4e3a\u4e86\u80fd\u8ba9\u9488\u5bf9 ASCII \u8bbe\u8ba1\u7684\u64cd\u4f5c\u7cfb\u7edf API \u652f\u6301\u4e2d\u6587\u6587\u4ef6\u540d\uff0c\u5c31\u53ea\u80fd\u7ed5\u5f00\u6240\u6709 0x7F \u4ee5\u4e0b\u7684\u503c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 UTF-8 \u5bf9\u8f66\u53a2\u4e5f\u5168\u90e8\u62ac\u9ad8\u5230 0x80 \u4ee5\u4e0a\uff0c\u907f\u514d\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u614e\u628a\u8f66\u53a2\u5f53\u4f5c\u662f '/' \u6216 '\\0' \u3002 UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u7531\u4e8e\u5de8\u5927\u7684\u60ef\u6027\uff0c\u5f88\u591a\u4eba\u90fd\u60f3\u5f53\u7136\u7684\u628a std::string \u5f53\u4f5c UTF-8 \u6765\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6253\u5370\uff0c\u5e38\u89c4\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u662f\u6ca1\u95ee\u9898\u7684\u3002 \u5b57\u7b26\u4e32\u64cd\u4f5c\u6709\u4e0b\u9762\u8fd9\u51e0\u79cd\uff0c\u5f97\u76ca\u4e8e UTF-8 \u4f18\u79c0\u7684\u5e8f\u5217\u5316\u6d89\u53ca\u548c\u5197\u4f59\u6297\u5e72\u6270\u673a\u5236\uff0c\u7edd\u5927\u591a\u6570 ASCII \u652f\u6301\u7684\u64cd\u4f5c\uff0cUTF-8 \u5b57\u7b26\u4e32\u90fd\u80fd\u8f7b\u677e\u80dc\u4efb\uff0c\u552f\u72ec\u5176\u4e2d \u6d89\u53ca\u201c\u7d22\u5f15\u201d\u548c\u201c\u957f\u5ea6\u201d\u7684 \u4e00\u90e8\u5206\u64cd\u4f5c\u4e0d\u884c\u3002\u8fd9\u662f\u7531\u4e8e\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff0c\u5982\u679c\u9700\u8981\u505a\u201c\u7d22\u5f15\u201d\u7c7b\u64cd\u4f5c\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u8f6c\u6362\u6210\u5b9a\u957f\u7684 UTF-32 \u7f16\u7801\u3002 \u64cd\u4f5c UTF-8 UTF-32 GBK \u6c42\u5b57\u7b26\u4e32\u957f\u5ea6 \u00d7 \u221a \u00d7 \u5224\u65ad\u76f8\u7b49 \u221a \u221a \u221a \u5b57\u5178\u5e8f\u7684\u5927\u5c0f\u6bd4\u8f83 \u221a \u221a \u00d7 \u5b57\u7b26\u4e32\u62fc\u63a5 \u221a \u221a \u221a \u641c\u7d22\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u641c\u7d22\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u4e0b\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u904d\u5386\u6240\u6709\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u5b50\u5b57\u7b26\u4e32\u5207\u7247 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u7247 \u00d7 \u221a \u00d7 \u67e5\u627e\u5e76\u66ff\u6362\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u67e5\u627e\u5e76\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u5220\u9664\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u4e3a\u4ec0\u4e48\uff1f\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5b9e\u9a8c\uff1a std::string s = \"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \uff08\u4f7f\u7528 /utf-8 \u7f16\u8bd1\uff09\u8fd0\u884c\u540e\uff0c\u4f1a\u5f97\u5230 6\u3002 \u56e0\u4e3a std::string \u7684 size() \u8fd4\u56de\u7684\u662f char \u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5728 UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u975e ASCII \u7684\u5b57\u7b26\u4f1a\u88ab\u7f16\u7801\u4e3a\u591a\u4e2a char \uff0c\u5bf9\u4e8e\u4e2d\u6587\u800c\u8a00\uff0c\u4e2d\u6587\u90fd\u5728 0x2E80 \u5230 0x9FFF \u8303\u56f4\u5185\uff0c\u5c5e\u4e8e\u4e09\u7ea7\u5217\u8f66\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e2a\u6c49\u5b57\u4f1a\u88ab\u7f16\u7801\u6210 3 \u4e2a char \u3002 char \u662f\u5b57\u8282\uff08\u7801\u4f4d\uff09\u800c\u4e0d\u662f\u771f\u6b63\u7684\u5b57\u7b26\uff08\u7801\u70b9\uff09\u3002\u771f\u6b63\u7684 Unicode \u5b57\u7b26\u5e94\u8be5\u662f char32_t \u7c7b\u578b\u7684\u3002\u8c03\u7528 std::string \u7684 size() \u6216\u8005 strlen \u5f97\u5230\u7684\u53ea\u662f\u201c\u5b57\u8282\u6570\u91cf\u201d\u3002 \u800c UTF-32 \u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\uff08\u7801\u70b9\uff09\u90fd\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684 char32_t \uff08\u7801\u4f4d\uff09\uff0c size() \u5c31\u662f\u771f\u6b63\u7684\u201c\u5b57\u7b26\u6570\u91cf\u201d\uff0c\u8fd9\u5c31\u662f\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 std::u32string s = U\"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \u5982\u679c\u4f60\u7684\u64cd\u4f5c\u53ea\u6d89\u53ca\u5b57\u7b26\u4e32\u67e5\u62fc\u63a5\u4e0e\u67e5\u627e\uff0c\u90a3\u5c31\u53ef\u4ee5\u7528 UTF-8\u3002\u5982\u679c\u5927\u91cf\u6d89\u53ca\u7d22\u5f15\uff0c\u5207\u7247\uff0c\u5355\u4e2a\u5b57\u7b26\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u5fc5\u987b\u7528 UTF-32\uff08\u5426\u5219\u4e00\u9047\u5230\u6c49\u5b57\u5c31\u4f1a\u51fa\u9519\uff09\u3002 std::vector slogan = { \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\", \"\u5168\u4e16\u754c\u7a0b\u5e8f\u5458\u5927\u56e2\u7ed3\u4e07\u5c81\", }; std::string joined; for (auto const &s: slogan) { joined += s; // \u53ea\u662f\u62fc\u63a5\u800c\u5df2\uff0cUTF-8 \u6ca1\u95ee\u9898 } UTF-8 \u6309\u7d22\u5f15\u5207\u7247\u7684\u51fa\u9519\u6848\u4f8b\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-8 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u8282\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\ufffd\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-32 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u7b26\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u53ea\u6709\u5f53\u7d22\u5f15\u6765\u81ea find \u7684\u7ed3\u679c\u65f6\uff0cUTF-8 \u5b57\u7b26\u4e32\u7684\u5207\u7247\u624d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(\"\u516c\"); // pos = 12 fmt::println(\"UTF-8 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u8282\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(U'\u516c'); // pos = 4 fmt::println(\"UTF-32 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u7b26\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u6ce8\u610f\u5230\u8fd9\u91cc UTF-8 \u7684 \"\u516c\" \u9700\u8981\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\u3002 UTF-8 \u65e0\u6cd5\u53d6\u51fa\u5355\u4e2a\u975e ASCII \u5b57\u7b26\uff0c\u5bf9\u4e8e\u5355\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ecd\u7136\u53ea\u80fd\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u8fbe\uff08\u7531\u591a\u4e2a\u5b57\u8282\u7ec4\u6210\uff09\u3002 std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-8 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u8282\uff1a{}\", s[0]); // \u53ef\u80fd\u4f1a\u6253\u5370 \u2018\u00e5\u2019 (0xE5)\uff0c\u56e0\u4e3a\u201c\u5c0f\u201d\u7684 UTF-8 \u7f16\u7801\u662f 0xE5 0xB0 0x8F // \u4e5f\u53ef\u80fd\u662f\u4e71\u7801\u201c\ufffd\u201d\uff0c\u53d6\u51b3\u4e8e\u7ec8\u7aef\u7406\u89e3\u7684\u7f16\u7801\u683c\u5f0f std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-32 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1a{}\", s[0]); // \u4f1a\u6253\u5370 \u2018\u5c0f\u2019 UTF-8 \u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u4e5f\u4f1a\u51fa\u95ee\u9898\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u4e71\u7801 std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u628a\u6309\u5b57\u7b26\u6b63\u5e38\u53cd\u8f6c\uff0c\u5f97\u5230 \u201c\u5c81\u4e07\u8bfe\u5f00\u516c\u5e08\u8001\u5f6d\u5c0f\u201d \u603b\u7ed3\uff1aUTF-8 \u53ea\u80fd\u62fc\u63a5\u3001\u67e5\u627e\u3001\u6253\u5370\u3002\u4e0d\u80fd\u7d22\u5f15\u3001\u5207\u7247\u3001\u53cd\u8f6c\u3002 \u6309\u7d22\u5f15\u5207\u7247\u4e0d\u884c\uff0c\u4f46\u5982\u679c\u7d22\u5f15\u662f find \u51fa\u6765\u7684\u5c31\u6ca1\u95ee\u9898\u3002 \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5728 Windows \u5b98\u65b9\u7684\u8bf4\u8f9e\u4e2d\uff0c\u6709\u201cUnicode \u7f16\u7801\u201d\u548c\u201cANSI \u7f16\u7801\u201d\u7684\u8bf4\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528 Windows \u81ea\u5e26\u7684\u8bb0\u4e8b\u672c\u7a0b\u5e8f\uff0c\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u9009\u5355\uff1a \u7ffb\u8bd1\u4e00\u4e0b\uff1a \u201cANSI\u201d\u6307\u7684\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u8bbe\u7f6e\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\u3002 \u6240\u8c13\u201cUnicode\u201d\u5176\u5b9e\u6307\u7684\u662f UTF-16\u3002 \u6240\u8c13\u201cUnicode big endian\u201d\u6307\u7684\u662f\u5927\u7aef UTF-16\u3002 \u201cUTF-8\u201d\u6307\u7684\u662f UTF-8 with BOM \u800c\u4e0d\u662f\u6b63\u5e38\u7684 UTF-8\u3002 \u5b9e\u9645\u4e0a Unicode \u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u53ea\u662f\u628a\u5b57\u7b26\u6620\u5c04\u5230\u6574\u6570\uff0c\u66f4\u6ca1\u6709\u4ec0\u4e48\u5927\u7aef\u5c0f\u7aef\uff0cUTF-16 \u624d\u662f\u7f16\u7801\u683c\u5f0f\u3002 \u800c ANSI \u672c\u6765\u5e94\u8be5\u662f ASCII \u7684\u610f\u601d\uff0c char \u672c\u6765\u5c31\u53ea\u652f\u6301 ASCII\u3002 \u4f46\u7531\u4e8e\u5f53\u65f6\u5404\u56fd\u8feb\u5207\u9700\u8981\u652f\u6301\u81ea\u5df1\u672c\u56fd\u7684\u6587\u5b57\uff0c\u5c31\u5728\u517c\u5bb9 ASCII \u7684\u57fa\u7840\u4e0a\uff0c\u53d1\u5c55\u51fa\u4e86\u81ea\u5df1\u7684\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\u3002\u8fd9\u4e9b\u5f53\u5730\u7279\u4f9b\u7684\u5b57\u7b26\u96c6\u91cc\u53ea\u5305\u542b\u4e86\u672c\u56fd\u6587\u5b57\uff0c\u6240\u6709\u8fd9\u4e9b\u5404\u56fd\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u90fd\u548c UTF-8 \u7c7b\u4f3c\uff0c\u91c7\u7528\u706b\u8f66\u5934\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0c\u5bf9 0 \u5f00\u5934\u7684 ASCII \u90e8\u5206\u4e5f\u90fd\u662f\u517c\u5bb9\u3002\u6240\u4ee5 Windows \u7d22\u6027\u628a ANSI \u5f53\u4f5c\u201c\u5404\u56fd\u672c\u5730\u6587\u5b57\u7f16\u7801\u201d\u7684\u7b80\u79f0\u4e86\u3002\u4f46\u540e\u6765\u4e92\u8054\u7f51\u7684\u51fa\u73b0\uff0c\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4fe1\u606f\u4ea4\u6362\u56f0\u96be\u3002 \u4f8b\u5982\u4f60\u5728\u73a9\u4e00\u4e9b\u65e5\u672c\u7684 galgame \u65f6\uff0c\u4f1a\u53d1\u73b0\u91cc\u9762\u6587\u5b57\u5168\u90e8\u4e71\u7801\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u5728\u5404\u4e2a\u5730\u533a\u53d1\u884c\u7684\u662f\u201c\u7279\u4f9b\u7248\u201d\uff1a\u5728\u4e2d\u56fd\u5927\u9646\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 GBK \u5b57\u7b26\u96c6\uff0c\u5728\u65e5\u672c\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 Shift-JIS \u5b57\u7b26\u96c6\u3002\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u5185\u90e8\u5b58\u50a8\u7684\u662f Shift-JIS \u7684\u90a3\u4e9b\u201c\u6574\u6570\u201d\u3002\u8fd9\u5bfc\u81f4\u65e5\u672c\u7684 galgame \u5728\u4e2d\u56fd\u5927\u9646\u7279\u4f9b\u7684 Windows \u4e2d\uff0c\u628a Shift-JIS \u7684\u201c\u6574\u6570\u201d\u7528 GBK \u7684\u8868\u6765\u89e3\u8bfb\u4e86\uff0c\u4ece\u800c\u4e71\u7801\uff08GBK \u91cc\u7684\u65e5\u6587\u533a\u57df\u5e76\u6ca1\u6709\u548c Shift-JIS \u91cd\u53e0\uff09\u3002\u9700\u8981\u7528 Locale Emulator \u628a Shift-JIS \u7ffb\u8bd1\u6210 Unicode \u8bfb\u7ed9 Windows \u542c\u3002\u5982\u679c\u65e5\u672c\u7a0b\u5e8f\u5458\u4ece\u4e00\u5f00\u59cb\u5c31\u7edf\u4e00\u7528 Unicode \u6765\u5b58\u50a8\uff0c\u4e2d\u56fd\u533a\u73a9\u5bb6\u7684 Windows \u4e5f\u7edf\u4e00\u7528 Unicode \u89e3\u6790\uff0c\u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cUnicode \u7ec4\u7ec7\u51fa\u73b0\u4e86\uff0c\u4ed6\u7684\u4f7f\u547d\u5c31\u662f\u7edf\u4e00\u5168\u4e16\u754c\u7684\u5b57\u7b26\u96c6\uff0c\u4fdd\u8bc1\u5168\u4e16\u754c\u6240\u6709\u7684\u6587\u5b57\u90fd\u80fd\u5728\u5168\u4e16\u754c\u6240\u6709\u7684\u8ba1\u7b97\u673a\u4e0a\u663e\u793a\u51fa\u6765\u3002\u9996\u5148\u521b\u529e\u4e86 Unicode \u5b57\u7b26\u96c6\uff0c\u7136\u540e\u89c4\u5b9a\u4e86 UTF-8\u3001UTF-16\u3001UTF-32 \u4e09\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u6700\u7ec8 UTF-8 \u6210\u4e3a\u5916\u7801\u7684\u4e3b\u6d41\uff0cUTF-32 \u6210\u4e3a\u5185\u7801\u7684\u4e3b\u6d41\u3002 \u63a5\u4e0b\u6765\u4e3a\u4e86\u65b9\u4fbf\u8bb0\u5fc6\uff0c\u6211\u4eec\u7d22\u6027\u5c31\u987a\u7740\u5fae\u8f6f\u7684\u8fd9\u4e2a\u8bf4\u6cd5\uff1a \u7ba1 char \u53eb ANSI\uff1a\u968f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002 \u7ba1 wchar_t \u53eb Unicode\uff1a\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u5728 Linux \u4e0a\u662f UTF-32\u3002 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5fae\u8f6f\u7ba1 UTF-16 \u53eb Unicode \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u95ee\u9898\uff1a \u56e0\u4e3a\u5f53\u5e74 Unicode 5.0 \u7684\u65f6\u5019\u53ea\u6709 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff0c16 \u4f4d\u5c31\u88c5\u5f97\u4e0b\uff0c\u6240\u4ee5\u5f53\u65f6 UTF-16 \u8fd8\u662f\u4e00\u4e2a \u5b9a\u957f\u7f16\u7801 \u3002\u5fae\u8f6f\u4e8e\u662f\u51b3\u5b9a\u628a wchar_t \u5b9a\u4e49\u6210 2 \u5b57\u8282\uff0c\u5e76\u5728 NT \u5185\u6838\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528\u90fd\u5347\u7ea7\u6210\u4e86\u57fa\u4e8e wchar_t \u5b57\u7b26\u4e32\u7684 \u201cW \u7cfb\u201d API\u3002 \u6bd4\u5c14\u76d6\u5b50\u5f53\u65f6\u4ee5\u4e3a\u8fd9\u6837 UTF-16 \u5b9a\u957f\u5185\u7801\u5c31\u4e00\u52b3\u6c38\u9038\u4e86\uff0c\u5e76\u53f7\u53ec\u6240\u6709\u7a0b\u5e8f\u90fd\u6539\u7528 UTF-16 \u505a\u5185\u7801\uff0c\u522b\u7528 \u201cA \u7cfb\u201d API \u4e86\u3002 \u8d77\u521d\uff0c\u6240\u6709\u4eba\u90fd\u4ee5\u4e3a UTF-16 \u5c31\u662f\u6700\u7ec8\u7b54\u6848\u3002 \u6ca1\u60f3\u5230\u540e\u6765 Unicode \u59d4\u5458\u4f1a\u201c\u80cc\u523a\u201d\u4e86\u6bd4\u5c14\u76d6\u5b50\uff01\u5077\u5077\u628a\u8303\u56f4\u66f4\u65b0\u5230\u4e86 0x10FFFF\uff0c\u7a81\u7834\u4e86 16 \u4f4d\u6574\u6570\u7684\u5bb9\u91cf\u3002\u539f\u6765\u7684 UTF-16 \u5df2\u7ecf\u5bb9\u7eb3\u4e0d\u4e0b\uff0c\u53ea\u597d\u5229\u7528\u4e4b\u524d\u9884\u7559\u7684 0xD800 \u5230 0xDFFF \u7a7a\u53f7\u533a\u95f4\u4e11\u964b\u5730\u5b9e\u73b0\u4e86\u53d8\u957f\u7f16\u7801\u3002 \u76f4\u5230 UTF-16 \u4e00\u591c\u4e4b\u95f4\u6210\u4e86\u4e11\u964b\u7684 \u53d8\u957f\u7f16\u7801 \u3002 \u95f9\u4e86\u534a\u5929\uff0cWindows \u8d39\u5fc3\u8d39\u529b\u66ff Unicode \u59d4\u5458\u4f1a\u597d\u4e0d\u5bb9\u6613\u63a8\u5e7f\u7684 wchar_t \uff0c\u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801 \u7684\u597d\u5904\u3002\u53ef \u201cW \u7cfb\u201d API \u5374\u53c8\u710a\u6b7b\u5728\u4e86 NT \u5185\u6838\u6700\u5e95\u5c42\uff0c\u53cd\u590d\u6765\u5751\u7b2c\u4e00\u6b21\u7528 Windows \u7f16\u7a0b\u7684\u521d\u5b66\u8005\u3002 \u6bd4\u5c14\u76d6\u5b50\uff1a\u4f60\u8fd9\u6837\u663e\u5f97\u6211\u5f88\u5c0f\u4e11\u8bf6\uff1f \u9664 Windows \u5916\uff0cJava \u4e5f\u662f\u201cUTF-16 \u80cc\u523a\u201d\u7684\u53d7\u5bb3\u8005\uff0c\u4ed6\u4eec\u60f3\u5f53\u7136\u7684\u628a char \u5b9a\u4e49\u4e3a UTF-16\uff0c\u4ee5\u4e3a\u8fd9\u5c31\u662f\u672a\u6765\u6c38\u4e45\u7684\u5b9a\u957f\u5185\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u2026\u2026 \u76f4\u5230 Unicode \u52a0\u5165\u4e86 0x10FFFF\uff0cJava \u4e0d\u5f97\u4e0d\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e2a Character \u4f5c\u4e3a UTF-32 \u5b57\u7b26\uff0c\u8fd8\u5f04\u4e2a char \u5230 Character \u7684\u8f6c\u6362\uff0c\u597d\u4e0d\u5c34\u5c2c\uff01 Linux \u6210\u7acb\u4e8e 1991 \u5e74\uff0c\u5f53\u65f6 Unicode \u4e5f\u624d\u521a\u521a\u51fa\u73b0\u3002Unicode \u5ba3\u5e03\u52a0\u5165 0x10FFFF \u540e\uff0cLinux \u624d\u5f00\u59cb\u5f15\u5165\u652f\u6301 Unicode\u3002\u5728\u77e5\u9053\u4e86 Unicode \u5305\u542b 0x10FFFF \u540e\uff0c\u4ed6\u4eec\u4e00\u5f00\u59cb\u5c31\u628a wchar_t \u5b9a\u4e49\u6210 4 \u5b57\u8282\uff0c\u9003\u8fc7\u4e86 UTF-16 \u7684\u80cc\u523a\u3002 \u540e\u6765\u65b0\u51fa\u7684\u8bed\u8a00\uff0c\u5982 Python 3\u3001Go\u3001Rust\u3001Swift\u3001Kotlin\uff0c\u628a\u5b57\u7b26\u94a6\u5b9a\u4e3a UTF-32 \u4e86\u3002\u4ed6\u4eec\u53ea\u6709\u5728\u8c03\u7528 Windows API \u65f6\uff0c\u624d\u4f1a\u4e34\u65f6\u8f6c\u6362\u4e3a UTF-16 \u6765\u8c03\u7528\uff0c\u9664\u6b64\u4e4b\u5916\u518d\u65e0 UTF-16 \u51fa\u73b0\u3002 \u8bb8\u591a\u7cdf\u7cd5\u7684\u535a\u5ba2\u58f0\u79f0\uff1a\u662f\u56e0\u4e3a\u201cUTF-16 \u6700\u6709\u5229\u4e8e\u4e2d\u6587\u538b\u7f29\u201d\uff0c\u6240\u4ee5 Java \u548c Windows \u624d\u91c7\u7528\u7684\uff1f\u7136\u800c\u5c31\u6211\u4e86\u89e3\u5230\u7684\u5b9e\u9645\u60c5\u51b5\u662f\u56e0\u4e3a\u4ed6\u4eec\u9519\u8bef\u7684\u4ee5\u4e3a 0xFFFF \u662f Unicode \u7684\u4e0a\u9650\u624d\u9519\u8bef\u91c7\u7528\u4e86\uff0c\u4e0d\u7136\u4e3a\u4ec0\u4e48\u540e\u6765\u7684\u65b0\u8bed\u8a00\u90fd\u91c7\u7528\u4e86 UTF-32 \u5185\u7801 + UTF-8 \u5916\u7801\u7684\u7ec4\u5408\uff1f\u800c\u4e14\u5728\u5916\u7801\u4e2d\u91c7\u7528 UTF-8 \u6216 UTF-16 \u538b\u7f29\u786e\u5b9e\u6ca1\u95ee\u9898\uff0c\u4f46\u662f Java \u548c Windows \u7684\u5931\u8bef\u5728\u4e8e\u628a UTF-16 \u5f53\u4f5c\u5185\u7801\u4e86\uff01\u5185\u7801\u5c31\u7406\u5e94\u662f\u5b9a\u957f\u7f16\u7801\u7684\u624d\u65b9\u4fbf\uff0c\u5982\u679c\u4f60\u6709\u4e0d\u540c\u60f3\u6cd5\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002 \u603b\u4e4b\uff0cUTF-16 \u662f\u7cdf\u7c95\uff0c\u4f46\u4ed6\u662f Windows \u552f\u4e00\u5b8c\u6574\u652f\u6301\u7684 Unicode \u63a5\u53e3\u3002\u4e0d\u5efa\u8bae\u8f6f\u4ef6\u5185\u90e8\u7528 UTF-16 \u5b58\u50a8\u6587\u5b57\uff0c\u4f60\u53ef\u4ee5\u7528\u66f4\u7d27\u51d1\u7684 UTF-8 \u6216\u66f4\u65b9\u4fbf\u5207\u7247\u7684 UTF-32\uff0c\u53ea\u9700\u5728\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u524d\u4e34\u65f6\u8f6c\u6362\u6210 UTF-16 \u5c31\u884c\u3002 \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u5fc5\u987b\u6307\u51fa\uff1a\u5728 std::string \u4e2d\u88c5 UTF-8 \u5e76\u4e0d\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728 std::u8string \u91cc\u540c\u6837\u53ef\u4ee5\u88c5 GBK\u3002\u8fd9\u5c31\u597d\u6bd4\u4e00\u4e2a\u540d\u53eb Age \u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b9e\u9645\u5374\u88c5\u7740\u6027\u522b\u4e00\u6837\u3002 enum Age { // \u9519\u8bef\u793a\u8303 Male, Female, Custom, }; // \u9664\u4e86\u8ff7\u60d1\u540c\u4e8b\u5916\uff0c\u628a\u5e74\u9f84\u548c\u6027\u522b\u7684\u7c7b\u578b\u6df7\u7528\u6ca1\u6709\u597d\u5904 void registerStudent(Age age, Age sex); \u533a\u5206\u7c7b\u578b\u53ea\u662f\u5927\u591a\u6570\u4eba\u8bbe\u8ba1\u63a5\u53e3\u7684\u89c4\u8303\uff0c\u53ea\u662f\u65b9\u4fbf\u4f60\u901a\u8fc7\u770b\u51fd\u6570\u63a5\u53e3\u4e00\u773c\u533a\u5206\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u7684\u662f\u4ec0\u4e48\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u6ca1\u6709\u5f3a\u5236\u6027\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e00\u770b\u5c31\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u9700\u8981\u7684\u662f\u4ec0\u4e48\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 void thisFuncAcceptsANSI(std::string msg); void thisFuncAcceptsUTF8(std::u8string msg); void thisFuncAcceptsUTF16(std::u16string msg); void thisFuncAcceptsUnicode(std::wstring msg); void thisFuncAcceptsUTF32(std::u32string msg); \u6ca1\u6709 char8_t \u4e4b\u524d\uff0c\u7528\u7c7b\u578b\u522b\u540d\u540c\u6837\u53ef\u4ee5\u8d77\u5230\u5dee\u4e0d\u591a\u7684\u8bf4\u660e\u6548\u679c\uff08\u7f3a\u70b9\u662f\u65e0\u6cd5\u91cd\u8f7d\uff09\uff1a using ANSIString = std::string; using UTF8String = std::string; using UTF16String = std::vector; void thisFuncAcceptsANSI(ANSIString msg); void thisFuncAcceptsUTF8(UTF8String msg); void thisFuncAcceptsUTF16(UTF16String msg); \u4e4b\u6240\u4ee5\u6211\u4f1a\u8bf4\uff0c std::string \u5e94\u8be5\u88c5 ANSI \u5b57\u7b26\u4e32\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u6807\u51c6\u5e93\u5b98\u65b9\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5047\u5b9a std::string \u7c7b\u578b\u662f ANSI \u7f16\u7801\u683c\u5f0f\uff08GBK\uff09\u3002\u5e76\u4e0d\u662f\u8bf4\uff0c\u4f60\u4e0d\u80fd\u7528 std::string \u5b58\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u7684\u5185\u5bb9\u3002 \u5982\u679c\u4f60\u5c31\u662f\u60f3\u7528 std::string \u88c5 UTF-8 \u4e5f\u53ef\u4ee5\uff0c\u53ea\u4e0d\u8fc7\u4f60\u8981\u6ce8\u610f\u5728\u4f20\u5165\u6240\u6709\u4f7f\u7528\u4e86\u6587\u4ef6\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5982 fopen \uff0c std::ifstream \u7684\u6784\u9020\u51fd\u6570\u524d\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u8f6c\u6362\uff0c\u8f6c\u6210 GBK \u7684 std::string \u6216 UTF-16 \u7684 std::wstring \u540e\uff0c\u624d\u80fd\u4f7f\u7528\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u5982\u679c\u4f60\u59cb\u7ec8\u7528 std::u8string \u88c5 UTF-8\uff0c\u90a3\u4e48\u5f53\u4f60\u628a\u5b83\u8f93\u5165\u4e00\u4e2a\u63a5\u53d7 ANSI \u7684\u666e\u901a std::string \u53c2\u6570\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef\uff0c\u5f3a\u8feb\u4f60\u91cd\u65b0\u6e05\u9192\uff0c\u6216\u662f\u5f3a\u8feb\u4f60\u4f7f\u7528\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\uff0c\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u8fd9\u4e2a\u8f6c\u6362\u51fd\u6570\u7684\u5199\u6cd5\u3002 \u4f8b\u5982\u5f53\u4f60\u4f7f\u7528 std::cout << u8string \u65f6\u4f1a\u62a5\u9519\uff0c\u8feb\u4f7f\u4f60\u6539\u4e3a std::cout << u8toansi(u8string) \u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff0c\u4ece\u800c\u907f\u514d\u4e86\u628a UTF-8 \u7684\u5b57\u7b26\u4e32\u6253\u5370\u5230\u4e86\u53ea\u652f\u6301 GBK \u7684\u63a7\u5236\u53f0\u4e0a\u3002 \u5176\u4e2d\u8f6c\u6362\u51fd\u6570\u7b7e\u540d\u4e3a std::string u8toansi(std::u8string s) \uff0c\u5f88\u53ef\u60dc\uff0c\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u51fd\u6570\uff0c\u76f4\u5230 C++26 \u524d\uff0c\u6807\u51c6\u5e93\u5bf9\u5b57\u7b26\u7f16\u7801\u652f\u6301\u4e00\u76f4\u5f88\u5dee\uff0c\u4f60\u4e0d\u5f97\u4e0d\u81ea\u5df1\u5b9e\u73b0\u6216\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u3002 \u603b\u4e4b\uff0c char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u5411\u8c03\u7528\u8005\u6697\u793a\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5982\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a thisFuncAcceptUTF8(std::u8string msg); \u5982\u679c\u8c03\u7528\u8005\u559c\u6b22\u7528 std::string \u88c5 UTF-8 \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\uff1a std::string msg; // \u8c03\u7528\u8005\u786e\u4fe1\uff0c\u8fd9\u4e2a msg \u867d\u7136\u662f `std::string`\uff0c\u4f46\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f UTF-8 // \u90a3\u4e48\u4ed6\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e3a u8string\uff0c\u6765\u8bc1\u660e\u81ea\u5df1\u5934\u8111\u6e05\u9192 thisFuncAcceptUTF8(std::u8string((char8_t *)msg.data(), msg.size())); \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 C++ \u5b98\u65b9\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e24\u79cd\u5b57\u7b26\u96c6\u3002\u4e00\u79cd\u662f \u6e90\u7801\u5b57\u7b26\u96c6 (source charset) \uff0c\u4e00\u79cd\u662f \u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u3002 \u8fd9\u771f\u662f\u7cdf\u7cd5\u7684\u672f\u8bed\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u8fd9\u4e2a\u540d\u5b57\u5177\u6709\u8bef\u5bfc\u6027\uff0c\u4ed6\u548c\u8fd0\u884c\u65f6\u6839\u672c\u6ca1\u6709\u5173\u7cfb\uff0c\u660e\u660e\u662f\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\u3002\u6240\u4ee5\u5c0f\u5f6d\u8001\u5e08\u66ff\u4ed6\u6539\u4e2a\u540d\u5b57\uff0c\u5b9e\u9645\u5e94\u8be5\u53eb\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u96c6\u201d\u3002 \u800c\u4e14\u4ed6\u4eec\u53eb\u5b57\u7b26\u96c6\u4e5f\u4e0d\u5408\u7406\uff0c\u5e94\u8be5\u53eb\u5b57\u7b26\u7f16\u7801\u624d\u5bf9\uff0cUTF-8 \u548c UTF-16 \u90fd\u662f Unicode \u5b57\u7b26\u96c6\u7684\u4e24\u79cd\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u4ed6\u4eec\u660e\u663e\u662f\u4e0d\u540c\u7684\u3002 \u7136\u540e\uff0c\u518d\u5f15\u5165\u4e00\u4e2a\u771f\u6b63\u7684\uff0c\u8fd0\u884c\u65f6\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u8f6f\u4ef6\u5ba2\u6237\u7535\u8111\u7684\u533a\u57df\u8bbe\u7f6e\u3002 \u6700\u7ec8\uff0c\u7ecf\u8fc7\u5c0f\u5f6d\u8001\u5e08\u6539\u826f\u7684\u672f\u8bed\u5982\u4e0b\uff1a \u6e90\u7801\u5b57\u7b26\u7f16\u7801: .cpp \u6e90\u7801\u6587\u4ef6\u65f6\u7528\u7684\u5b57\u7b26\u7f16\u7801\u3002\u4f8b\u5982\u7a0b\u5e8f\u5458\u7528\u8bb0\u4e8b\u672c\u4fdd\u5b58 .cpp \u6e90\u7801\u6587\u4ef6\u65f6\uff0c\u9009\u62e9 \u201cUTF-8\u201d \u4fdd\u5b58\u5c31\u662f UTF-8\uff0c\u9009\u62e9 \u201cANSI\u201d \u4fdd\u5b58\u5c31\u662f GBK\u3002 \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f char \u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u5b57\u7b26\u7f16\u7801\u3002\u9ed8\u8ba4\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff08\u5f00\u53d1\u8005\uff09\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f\u6211\u4eec\u7684\u7a0b\u5e8f\u5728\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fd0\u884c\u65f6\uff0c\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf API \u7684 const char * \u671f\u671b\u63a5\u53d7\u600e\u6837\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002\u9ed8\u8ba4\u662f\u5ba2\u6237\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd9\u4e09\u4e2a\u53ef\u4ee5\u5404\u6709\u4e0d\u540c\u3002 \u5176\u4e2d \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801 \u548c \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801 \u7684\u4e0d\u5339\u914d\uff0c\u662f Windows \u8f6f\u4ef6\u51fa\u73b0\u4e71\u7801\u7684\u4e3b\u8981\u539f\u56e0\u3002 \u800c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u53ea\u4e8b\u5173\u4f60\u5982\u4f55\u4fdd\u5b58\u6e90\u7801\uff0c\u53ea\u662f\u8ba9\u7f16\u8bd1\u5668\u80fd\u591f\u6210\u529f\u8bfb\u53d6\u4f60\u7684\u6e90\u7801\uff0c\u5bf9\u8fd0\u884c\u65f6\u7684\u4e71\u7801\u95ee\u9898\u6ca1\u6709\u5f71\u54cd\u3002\u7f16\u8bd1\u5668\u8bfb\u5b8c\u6e90\u7801\u540e\uff0c\u8981\u5728\u5e38\u91cf\u533a\u751f\u6210\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u8fd8\u662f\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u7684\u3002 \u4f8b\u5982\u4e4b\u524d\u8bf4\u7684\u65e5\u672c galgame \u5728\u4e2d\u56fd\u7535\u8111\u4e0a\u6253\u5f00\u7206\u51fa\u4e71\u7801\uff0c\u5c31\u662f\u56e0\u4e3a\u662f\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u4e86 galgame\uff08\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u4e3a Shift-JIS\uff09\uff0c\u5728\u4e2d\u56fd\u5ba2\u6237\u7535\u8111\u4e0a\u6253\u5f00\uff08\u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801\u4e3a GBK\uff09\u5bfc\u81f4\u7684\u3002 \u65e5\u672c\u7a0b\u5e8f\u5458\u4f7f\u7528\u4ec0\u4e48\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u6839\u672c\u65e0\u5173\u7d27\u8981\u2026\u2026\u54ea\u6015\u4ed6\u4eec\u4f7f\u7528\u4e86 UTF-8 \u4fdd\u5b58\u6e90\u7801\uff0cMSVC \u7f16\u8bd1\u65f6\u4ecd\u7136\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a Shift-JIS \u7f16\u7801\u7684\u5b57\u9762\u91cf\u6765\u5b58\u50a8\u5728\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5e38\u91cf\u533a\u4e2d\u3002 \u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a\u8fd9\u4e9b\u82b1\u91cc\u80e1\u54e8\u73a9\u5e94\u7edf\u4e00\u4e3a UTF-8\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u8f6c\u6362\u6765\u8f6c\u6362\u53bb\u4e86\uff1f\u8fd8\u4e0d\u662f\u56e0\u4e3a\u5386\u53f2\u9057\u7559\uff0c\u4e00\u4e9b\u52b3\u4fdd\u7a0b\u5e8f\u5458\u4e0d\u80af\u628a\u4ed6\u4eec GBK \u7f16\u7801\u7684\u6e90\u7801\u6539\u6210 UTF-8 \u4fdd\u5b58\uff0c\u800c\u4e14\uff0cWindows \u4e5f\u5b8c\u5168\u4e0d\u63d0\u4f9b\u57fa\u4e8e UTF-8 \u7684\u8de8\u5e73\u53f0 API\uff08\u53ea\u63d0\u4f9b GBK \u548c UTF-16 \u4e24\u79cd\uff0c\u5c31\u662f\u4e0d\u7ed9 UTF-8 \u7684\uff0c\u975e\u5e38\u6076\u5fc3\u4eba\uff09\u3002\u6240\u4ee5 MSVC \u81f3\u4eca\u4ecd\u7136\u9ed8\u8ba4\u662f GBK \u7f16\u7801\u7684\uff08\u66f4\u51c6\u786e\u7684\u8bf4\u662f ANSI\uff0c\u8ddf\u968f\u4f60\u7cfb\u7edf\u7684\u533a\u57df\u8bbe\u7f6e\u800c\u53d8\uff0c\u5728\u4e2d\u56fd\u5c31 GBK\uff0c\u5728\u7f8e\u56fd\u5c31 UTF-8\uff0c\u5728\u6b27\u6d32\u5c31 Latin-1\uff0c\u975e\u5e38\u7684\u53cc\u6807\uff09\u3002\u5fae\u8f6f\u5404\u79cd\u626f\u76ae\u6548\u7387\u4f4e\u4e0b\uff0cAPI \u5f04\u4e86\u4e00\u5957\u53c8\u4e00\u5957\u4e92\u76f8\u6781\u9650\u62c9\u626f\uff0c\u800c\u6211\u4eec Linux \u548c GCC \u65e9\u5df2\u9ed8\u8ba4\u5c31\u662f UTF-8\u2026\u2026 \u6211\u7406\u89e3\u4f60\u73b0\u5728\u5927\u8111\u5e72\u70e7\u7684\u5fc3\u60c5\u3002\u4f3a\u5019\u8fd9\u4e9b\u5386\u53f2\u7b54\u8fa9\u5f88\u590d\u6742\uff0c\u4e5f\u5f88\u65e0\u804a\uff0c\u6beb\u65e0\u610f\u4e49\uff01\u53ea\u662f\u4e3a\u4e86\u64e6\u53cd Unicode \u52b3\u4fdd\u7684\u5c41\u80a1\u3002 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a \u5bf9\u4e8e\u8de8\u5e73\u53f0\u8f6f\u4ef6\u6765\u8bf4\uff0c\u6211\u63a8\u8350\u5927\u5bb6\u628a\u4e09\u4e2a\u5168\u90e8\u8bbe\u4e3a UTF-8\uff01\uff08\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4e3b\u8981\u662f\u4f3a\u5019 MSVC\uff09 Linux + GCC \u7528\u6237\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u4f60\u4eec\u6240\u6709\u5b57\u7b26\u96c6\u9ed8\u8ba4\u7684\u8bbe\u5b9a\u5c31\u662f UTF-8\u3002 Windows + MSVC \u7528\u6237\u8bf7\u5f00\u542f /utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\uff0c\u73b0\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u90fd\u662f UTF-8 \u4e86\u3002 Windows + MinGW \u7528\u6237\u8bf7\u5f00\u542f -finput-charset=utf-8 \u548c -fexec-charset=utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\u3002 \u6240\u6709\u6e90\u7801\u6587\u4ef6\u7edf\u4e00\u4ee5 UTF-8 \u7f16\u7801\u4fdd\u5b58\uff0c\u4e14\u5c3d\u91cf\u5728\u6700\u524d\u9762\u52a0\u4e0a 0xFEFF \u8fd9\u4e2a BOM \u6807\u8bb0\uff0c\u9632\u6b62 MSVC \u8111\u62bd\u5f53\u4f5c GBK \u6765\u8bfb\u53d6\u3002 \u5728 main \u51fd\u6570\u524d\uff0c\u52a0\u4e24\u884c\uff1a // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 return 0; } \u8fd9\u6837\u4e00\u5957\u6253\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u6587\u672c\u7f16\u8f91\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u7801\uff0c\u4f60\u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u8bfb\u53d6\u6e90\u7801\uff0c\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6b63\u786e\u7684\u628a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8def\u5f84\u8f6c\u4e3a UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u3002 \u5728 CMake \u4e2d\uff0c\u53ea\u5bf9 MSVC \u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a if (MSVC) target_compile_options(\u4f60\u7684\u7a0b\u5e8f PRIVATE /utf-8) else() \u4e5f\u53ef\u4ee5\u5728\u6700\u524d\u9762 add_compile_options \uff0c\u5b9e\u73b0\u5bf9\u6240\u6709\u4e4b\u540e\u5b9a\u4e49\u7684\u7a0b\u5e8f\u5168\u5c40\u542f\u7528\u8be5\u9009\u9879\u3002 \u5728\u6211\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u90fd\u4f1a\u8fd9\u6837\u5f00\u542f\uff0c\u89e3\u51b3 MSVC \u4e0d\u8de8\u5e73\u53f0\u7684\u95ee\u9898\uff1a if (MSVC) add_compile_options(/Zc:preprocessor /utf-8 /DNOMINMAX /D_USE_MATH_DEFINES /EHsc /bigobj) else() if (WIN32) add_compile_options(-finput-charset=utf-8 -fexec-charset=utf-8) endif() add_compile_options(-Wall -Wextra -Werror=return-type) endif() add_executable(\u4f60\u7684\u7a0b\u5e8f \u4f60\u7684\u6587\u4ef6.cpp) # \u81ea\u52a8\u7ee7\u627f\u4e86\u4e0a\u9762\u6240\u6709\u7684\u7f16\u8bd1\u5668\u9009\u9879 .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 Windows \u5b98\u65b9\u63d0\u4f9b\u7684\u771f\u6b63 API \u662f _wfopen \u3002 fopen \u53ea\u662f\u4ed6\u4eec\u63d0\u4f9b\u7684\u201cPOSIX \u517c\u5bb9\u5c42\u201d \u5305\u88c5\uff0c\u5176\u4f1a\u628a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u901a\u8fc7 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u540e\uff0c\u8f6c\u53d1\u7ed9 _wfopen \u3002 \u51fa\u4e8e\u8de8\u5e73\u53f0\u7684\u8981\u6c42\uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 _wfopen \u8fd9\u79cd\u5176\u4ed6\u5e73\u53f0\u6ca1\u6709\u7684\u51fd\u6570\uff0c\u4e5f\u4e0d\u60f3\u7528\u90a3\u8fde 2 \u5b57\u8282 4 \u5b57\u8282\u90fd\u98d8\u5ffd\u4e0d\u5b9a\u7684 wchar_t \uff0c\u66f4\u4e0d\u60f3\u8ba9 std::string \u5b58\u6839\u672c\u4e0d\u80fd\u8de8\u5e73\u53f0\u7684 GBK\u3002 \u53ea\u8981\u8ba9 fopen \u7684 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u51fd\u6570\u66ff\u6362\u6210 \u201cUTF-8 \u5230 UTF-16\u201d \u5c31\u884c\u4e86\u3002\u8fc7\u53bb\uff0c\u6211\u4eec\u65e0\u6cd5\u66ff\u6362\uff0c\u6700\u65b0\u7684 Windows \u5728\u4e00\u6b21\u66f4\u65b0\u4e2d\uff0c\u652f\u6301\u4e86 \".utf-8\" locale \u8fd9\u4e00\u9ed1\u79d1\u6280\uff0c\u4e13\u95e8\u6ee1\u8db3\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u3002 // \u9ed8\u8ba4 locale fopen(\"\u4f60\u597d.txt\") == _wfopen(gbk_to_utf16(\"\u4f60\u597d.txt\")); // \u8bbe\u7f6e\u4e86 utf-8 locale \u540e fopen(\"\u4f60\u597d.txt\") == _wfopen(utf8_to_utf16(\"\u4f60\u597d.txt\")); \u82e5\u4e0d\u8bbe\u7f6e setlocale(LC_ALL, \".utf-8\") \uff0c\u5219 fopen \u548c ifstream \u9ed8\u8ba4\u4f1a\u628a\u4f60\u63d0\u4f9b\u7684 const char * \u6587\u4ef6\u8def\u5f84\uff0c\u5f53\u4f5c GBK \u7f16\u7801\u7684\uff0c\u800c\u6211\u4eec\u8bbe\u7f6e\u4e86 /utf-8 \u6216 -fexec-charset=utf-8 \u540e\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7f16\u7801\u5df2\u7ecf\u662f UTF-8 \u4e86\uff0c\u8fd9\u6837 UTF-8 \u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u8f93\u5165\u8fdb\u671f\u671b const char * \u7684 fopen \u53c2\u6570\uff0c\u5c31\u4f1a\u51fa\u4e71\u7801\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c .utf-8 locale \u53ea\u662f\u5f71\u54cd\u4e86\u6807\u51c6\u5e93\uff01\u5e76\u4e0d\u6539\u53d8\u7cfb\u7edf API\u3002 \u76f4\u63a5\u8c03\u7528\u7cfb\u7edf API \u65f6\uff0cA \u7cfb API \u4ecd\u7136\u6709\u95ee\u9898\u3002 MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u63d0\u793a\", MB_OK); // \u4e0d\u884c\uff0c.utf-8 \u53ea\u662f\u8ba9\u6807\u51c6\u5e93\u53d8\u6210 UTF-8 \u63a5\u53e3\u4e86\uff0cA \u7cfb Windows API \u4ecd\u7136\u662f GBK MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u63d0\u793a\", MB_OK); // \u6ca1\u95ee\u9898\uff01\u7528 UTF-16 \u7684 wchar_t \u5b57\u9762\u91cf\u6765\u8c03\u7528 W \u63a5\u53e3\u603b\u662f\u6ca1\u95ee\u9898\u7684 \u8fd8\u662f\u9700\u8981\u6211\u4eec\u624b\u52a8\u8f6c\u6362 UTF-8 \u5230 UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u2026\u2026\u4f46\u662f\u53cd\u6b63\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u5f88\u5c11\u9700\u8981\u76f4\u63a5\u8c03\u7528 Windows API\uff0c\u90fd\u662f\u901a\u8fc7\u901a\u7528\u7684 C/C++ \u6807\u51c6\u5e93\uff0c\u56e0\u6b64 .utf-8 locale \u53ef\u80fd\u662f\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u60f3\u8fdb\u519b UTF-8 \u7684\u6700\u4f73\u9009\u62e9\u3002 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e \u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\uff0c\u90fd\u662f\u9488\u5bf9 char \u7684\uff0c\u53ea\u6709 char \u88ab\u6545\u610f\u9488\u5bf9\u4e86\uff0c\u5b58\u5728\u5b57\u7b26\u7f16\u7801\u4e0d\u7edf\u4e00\u7684\u95ee\u9898\u3002 \u5982\u679c\u5168\u90e8\u7528 wchar_t \u7684\u8bdd\uff0c\u867d\u7136\u5728 Linux \u4e0a\u662f UTF-32\uff0c\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u4e0d\u7edf\u4e00\u4e86\u3002\u4f46\u81f3\u5c11\u5728\u540c\u4e00\u4e2a Windows \u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u90fd\u662f\u7edf\u4e00\u7684 UTF-16\u3002 \u6240\u4ee5\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u662f\u5168\u9762\u91c7\u7528 wchar_t \u548c std::wstring \uff0c\u8fd9\u6837\u65e0\u8bba\u4f60\u7684\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\u5982\u4f55\uff0c\u90fd\u5bf9 wchar_t \u548c\u57fa\u4e8e const wchar_t * \u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002 C \u8bed\u8a00\u6807\u51c6\u6ca1\u6709 _wfopen \uff0c\u4f46\u662f std::ifstream \u6709\u57fa\u4e8e std::wstring \u7684\u6784\u9020\u51fd\u6570\uff0c\u5c31 C++ \u6807\u51c6\u5e93\u6765\u770b std::wstring \u7684\u652f\u6301\u8fd8\u662f\u5f88\u4e30\u5bcc\u7684\uff0c\u57fa\u672c std::string \u6709\u7684 std::wstring \u90fd\u6709\uff0c\u4f8b\u5982 std::to_string \u548c std::to_wstring \uff0c std::cout \u548c std::wcout \u3002\u672c\u7ae0\u8282\u6700\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5bbd\u5b57\u7b26\u6d41\u7684\u7528\u6cd5\u3002 \u7f3a\u70b9\u662f\uff0c\u9996\u5148\u6bcf\u6b21\u90fd\u9700\u8981\u5199 L\"\u4f60\u597d\" \u8fd9\u4e2a L \u524d\u7f00\u5f88\u9ebb\u70e6\uff0c\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u4e14\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u90fd\u5728\u7528 std::string \uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b std::wstring \u7684 API\u3002 \u4f8b\u5982 openvdb \u7684\u6587\u4ef6\u5199\u5165\u51fd\u6570\uff1a void openvdb::io::File::write(std::string const &filename); \u8fd9\u6837\u5c31\u5f88\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f60\u5185\u90e8\u5168\u662f UTF-16 \u7684 std::wstring \u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u8c03\u7528\u7b2c\u4e09\u65b9\u5e93\u524d\u5c31\u9700\u8981\u8f6c\u6210 GBK \u7684 std::string \u3002\u53ef\u4ee5\u7528 boost::locale::conv::to_utf \u8fd9\u4e2a\u51fd\u6570\u8f6c\u6362\uff0c\u4f46\u4e5f\u5f88\u9ebb\u70e6\uff0c\u800c\u4e14\u5982\u679c std::wstring \u542b\u6709 GBK \u8303\u56f4\u4e4b\u5916\u7684 \u201c\ud883\udede\u201d\uff0cGBK \u65e0\u6cd5\u8868\u793a\uff0c\u53c8\u4f1a\u6709\u7f16\u7801\u5931\u8d25\u7684\u95ee\u9898\u3002 \u8fd8\u6709 stbi_load \u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u662f\u53ea\u63d0\u4f9b\u4e86 const char * \u7684\u63a5\u53e3\uff0c\u591a\u4e86\u53bb\u4e86\u3002 setlocale(LC_ALL, \".utf-8\") \u7684\u597d\u5904\u662f\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5168\u81ea\u52a8\u90fd\u4ece GBK \u65e0\u7f1d\u5207\u6362\u5230 UTF-8\uff0c\u800c\u4e0d\u7528\u5bf9\u4ed6\u4eec\u7684\u6e90\u7801\u505a\u4efb\u4f55\u66f4\u6539\u3002\u56e0\u4e3a\u4ed6\u4eec\u5185\u90e8\u90fd\u662f\u8c03\u7528\u7684 fopen \u548c ifstream \u3002 u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u4e2d\u56fd\u533a Windows\uff0cMSVC\uff0c\u7f16\u8bd1\u9009\u9879\uff1a /std:c++17 std::string s = \"\u4f60\u597d\"; hexdump(s); // C4 E3 BA C3 (GBK) std::string s = u8\"\u4f60\u597d\"; hexdump(s); // E4 BD A0 E5 A5 BD (UTF-8) u8 \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u7f16\u7801\u5b58\u50a8\u3002\u65e0\u8bba\u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u662f\u4e0d\u662f UTF-8\u3002 \u7f16\u8bd1\u5668\u4fdd\u8bc1\u4f1a\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u8f6c\u6362\u4e3a UTF-8 \u7f16\u7801\u7684 char \u5b57\u8282\u5e8f\u5217\uff0c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u533a\u3002 \u8fd9\u5bf9\u4e8e\u5df2\u7ecf\u8bbe\u7f6e\u4e86 /utf-8 \u9009\u9879\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u5df2\u7ecf\u4fdd\u8bc1\u662f UTF-8 \u7684\u6211\u4eec\u6765\u8bf4\u6beb\u65e0\u4f5c\u7528\u3002\u53ea\u662f\u5bf9\u4e8e\u4e0d\u7528 /utf-8 \u7684\u540c\u5b66\uff0c\u4ed6\u4eec\u60f3\u8981\u4e34\u65f6\u521b\u5efa\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5c31\u53ef\u4ee5\u7528 u8 \u524d\u7f00\u3002 \u5728 C++17 \u548c\u4e4b\u524d\uff0c u8\"\u4f60\u597d\" \u4ea7\u751f\u7684\u662f const char [] \u7c7b\u578b\u7684\u5e38\u91cf\u3002 \u5728 C++20 \u4e2d\uff0c\u5f15\u5165\u4e86 char8_t \u3002\u7136\u540e\uff0c\u4ed6\u4eec\u89c4\u5b9a\uff0c u8\"\u4f60\u597d\" \u73b0\u5728\u4ea7\u751f\u7684\u662f const char8_t [] \u7c7b\u578b\u7684\u5e38\u91cf\u4e86\u3002 \u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u4ee5\u524d\u4f60\u5199\u7684\uff1a std::string s = u8\"\u4f60\u597d\"; \u73b0\u5728\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u56e0\u4e3a const char8_t [] \u65e0\u6cd5\u7528\u4e8e\u6784\u9020\u53ea\u652f\u6301 const char [] \u7684 std::string \u3002 \u597d\u5728 C++23 \u53c8\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u5141\u8bb8 const char8_t [] \u9690\u5f0f\u8f6c\u6362\u4e3a const char [] \uff0cC++17 \u4e4b\u524d\u7684\u8fd9\u79cd\u4ee3\u7801\u53c8\u80fd\u6b63\u5e38\u901a\u8fc7\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u60f3\u5feb\u4e50\u5730\u7528 u8 \u5b57\u9762\u91cf\uff0c\u8981\u4e48 C++17\uff0c\u8981\u4e48 C++23\uff0c\u8df3\u8fc7 C++20 \u6bd4\u8f83\u597d\u3002 \u4f60\u53ef\u4ee5\u770b\u5230\uff0cC++ \u7248\u672c\u7684\u66f4\u65b0\u5e76\u4e0d\u662f 100% \u5b8c\u5168\u5411\u524d\u517c\u5bb9\u7684\uff0c\u6709\u65f6\u4e5f\u4f1a\u6709\u7834\u574f\u6027\u7684\u53d8\u66f4\uff0c\u4f46\u6bd4\u8f83\u5c11\uff0c\u5e73\u65f6\u611f\u89c9\u4e0d\u5230\u3002\u6bd4\u5982 C++11 \u4e4b\u524d auto \u5c31\u6709\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u540e\u6765\u51b3\u5b9a\u8fd9\u4e2a\u529f\u80fd\u6ca1\u4ec0\u4e48\u7528\uff0c\u5c31\u628a auto \u6539\u6210\u53e6\u4e00\u4e2a\u610f\u601d\u4e86\u3002 \u9664\u4e86 u8 \u4ee5\u5916\uff0c\u8fd8\u6709\u8fd9\u4e9b\uff1a \u524d\u7f00 \u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u201c\u4f60\u597d\u201d \u8fd0\u884c\u5b57\u7b26\u96c6 (\u9ed8\u8ba4\u662f\u5f00\u53d1\u8005\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u800c\u4e0d\u662f\u5ba2\u6237\u7684) const char [] L\u201d\u4f60\u597d\u201d Windows \u4e0a UTF-16\uff1bLinux \u4e0a UTF-32 const wchar_t [] u8\u201d\u4f60\u597d\u201d UTF-8 const char8_t [] u\u201d\u4f60\u597d\u201d UTF-16 const char16_t [] U\u201d\u4f60\u597d\u201d UTF-32 const char32_t [] \u53ea\u4e0d\u8fc7\u662f\u5f00\u53d1\u8005\u548c\u5ba2\u6237\u5f80\u5f80\u5904\u4e8e\u540c\u4e00\u4e2a\u5730\u533a\uff0c\u6240\u4ee5 \"\u4f60\u597d\" \u770b\u8d77\u6765\u597d\u50cf\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165\u5230 std::cout \u4e2d\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\u4ed6\u53ea\u662f\u5f00\u53d1\u8005\u7535\u8111\u7684 ANSI\uff0c\u800c\u4e0d\u662f\u5ba2\u6237\u7535\u8111\u7684 ANSI\uff0c\u5982\u679c\u76f4\u63a5\u62ff\u6765\u6253\u5370\uff0c\u4f1a\u5bfc\u81f4\u4ee5 \"\" \u5e38\u91cf\u5f62\u5f0f\u5199\u6b7b\u7684\u5b57\u7b26\u4e32\u4f1a\u5728\u5ba2\u6237\u7535\u8111\u4e0a\u51fa\u73b0\u4e71\u7801\u3002\u9664\u975e\u8fd9\u4e2a\u5b57\u7b26\u4e32\u53ea\u5305\u542b ASCII\uff0c\u56e0\u4e3a\u6240\u6709 ANSI \u90fd\u517c\u5bb9 ASCII\uff0c\u624d\u6070\u597d\u907f\u514d\u4e86\u4e71\u7801\u3002 \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 \u628a\u5b57\u7b26\u4e32\u5f53\u4f5c\u7eaf\u7cb9\u7684\u201c\u5b57\u8282\u6d41\u201d\uff0c\u65e0\u89c6\u5b57\u7b26\u7f16\u7801\u3002\u6216\u8005\u8bf4\uff0c\u4f60\u4ece\u7cfb\u7edf\u8f93\u5165\u8fdb\u6765\u7684\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u6211\u5c31\u5b58\u50a8\u7684\u4ec0\u4e48\u7f16\u7801\u3002\u5bf9\u4e8e Unicode \u5219\u91c7\u53d6\u5b8c\u5168\u6446\u70c2\u7684\u6001\u5ea6\uff0c\u5b8c\u5168\u65e0\u89c6 Unicode \u7684\u5b58\u5728\u3002 \u9002\u7528\u573a\u666f\uff1a\u901a\u5e38\u4e0e\u6587\u5b57\u5904\u7406\u9886\u57df\u65e0\u5173\u7684\u8f6f\u4ef6\u4f1a\u91c7\u53d6\u8fd9\u79cd\u65b9\u6848\u3002 \u4f18\u70b9\uff1a\u65b9\u4fbf\uff0c\u4e14\u5185\u90e8\u5bf9\u5b57\u7b26\u4e32\u65e0\u4efb\u4f55\u8f6c\u6362\u548c\u5224\u65ad\uff0c\u6548\u7387\u6700\u9ad8\u3002 \u7f3a\u70b9\uff1a\u5728\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u8bfb\u5199\u5e26\u6709\u4e2d\u6587\u7684\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u4f1a\u9971\u53d7\u4e71\u7801\u548c\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u56f0\u6270\u3002 \u65b9\u6cd5\uff1a\u5b8c\u5168\u4f7f\u7528 const char * \u548c std::string \u3002 \u4ee3\u8868\u4f5c\uff1aLinux \u6587\u4ef6\u7cfb\u7edf ext4\u3001Lua \u7f16\u7a0b\u8bed\u8a00\u3001\u73b0\u4ee3 Python \u4e2d\u7684 bytes \u7c7b\u578b\u3001HTTP \u7684 ? \u53c2\u6570\u3001\u65e9\u671f FAT32 \u6587\u4ef6\u7cfb\u7edf\u7b49\u3002 \u8fd9\u7c7b\u8f6f\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u521d\u5b66\u8005\u5199\u6cd5\uff0c\u5982\u679c\u4f60\u4ece\u672a\u60f3\u8fc7\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u4ece\u4e0d\u4e86\u89e3 wchar_t \u3001 char32_t \u4e4b\u95f4\u7684\u6218\u4e89\uff0c\u53ea\u77e5\u9053 char \uff0c\u90a3\u4e48\u4f60\u5df2\u7ecf\u81ea\u52a8\u5728\u6b64\u9635\u8425\u91cc\u3002 \u6709\u4eba\u8bf4 Linux \u6587\u4ef6\u7cfb\u7edf\u662f UTF-8\uff1f\u5e76\u4e0d\u662f\uff01Linux \u6587\u4ef6\u7cfb\u7edf\u6839\u672c\u4e0d\u4f1a\u68c0\u9a8c\u4f60\u7684\u6587\u4ef6\u540d\u662f\u4e0d\u662f\u5408\u6cd5\u7684 UTF-8\uff0c\u53ea\u4e0d\u8fc7\u662f\u56e0\u4e3a\u4f60\u8bbe\u5b9a\u4e86 export LC_ALL=zh_CN.UTF-8 \uff0c\u8fd9\u4f1a\u4f7f\u6240\u6709\u7a0b\u5e8f\uff08\u5305\u62ec\u7ec8\u7aef\u6a21\u62df\u5668\uff09\u5047\u5b9a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5185\u5bb9\u90fd\u6309 UTF-8 \u7f16\u7801\uff0c\u4ece\u800c\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u5404\u7c7b API \u65f6\uff08\u5982 open\u3001write\uff09\u90fd\u4f1a\u4f7f\u7528 UTF-8 \u7f16\u7801\u7684 const char * \u8f93\u5165\uff0c\u5728 Linux \u7cfb\u7edf API \u770b\u6765\uff0c\u6240\u8c13\u201c\u6587\u4ef6\u540d\u201d\u53ea\u662f\u7eaf\u7cb9\u7684\u5b57\u8282\u6d41\uff0c\u53ea\u8981\u4fdd\u8bc1\u4e0d\u5305\u542b '/' \u548c '\\0' \uff0c\u65e0\u8bba\u4f60\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4ed6\u90fd\u4e0d\u5728\u4e4e\u3002\u800c\u6240\u6709\u7684 locale \u90fd\u517c\u5bb9 ASCII\uff0c\u6240\u4ee5\u7edd\u4e0d\u4f1a\u51fa\u73b0\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57\u7f16\u7801\u540e\u4ea7\u751f '/' \u7684\u60c5\u51b5\uff08\u4f8b\u5982 GB2312 \u4f1a\u628a\u4e00\u4e2a\u4e2d\u6587\u7f16\u7801\u6210\u4e24\u4e2a 0x80 \u5230 0xFF \u533a\u95f4\u7684\u5b57\u8282\uff0c\u548c ASCII \u7684\u8303\u56f4\u6ca1\u6709\u91cd\u53e0\uff0c\u66f4\u4e0d\u53ef\u80fd\u51fa\u73b0 '/' \uff09\uff0c\u5373\u4f7f\u6362\u6210 export LC_ALL=zh_CN.GB2312 \uff0cLinux \u6587\u4ef6\u7cfb\u7edf\u4e00\u6837\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u8bfb\u53d6\u4f60\u4e4b\u524d\u4ee5 UTF-8 \u5199\u5165\u7684\u6587\u4ef6\u4f1a\u53d8\u6210\u4e71\u7801\u800c\u5df2\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a\u7684 Windows \u800c\u8a00\uff0c\u4ed6\u7684\u6240\u6709 A \u51fd\u6570\u53ea\u652f\u6301 GBK \u7f16\u7801\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60 Lua \u4e2d\u628a\u5b57\u7b26\u4e32\u201c\u5f53\u4f5c\u201d UTF-8 \u6765\u7528\u3002\u90a3\u4e48\u5f53\u4f60\u5728\u8c03\u7528 Lua \u7684 io.open \u524d\uff0c\u9700\u8981\u5148\u505a\u4e00\u4e2a UTF-8 \u5230 GBK \u7684\u8f6c\u6362\uff0c\u8fd9\u8fd8\u4f1a\u5bfc\u81f4\u4e22\u5931\u90e8\u5206\u4e0d\u5728 GBK \u5185\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u5305\u542b Emoji\uff0c\u90a3\u5c31\u4f1a\u53d8\u6210 ??? \u4e71\u7801\u3002\u800c\u4f7f\u7528 W \u51fd\u6570\u7684 UTF-16 \u5c31\u4e0d\u4f1a\uff0c\u56e0\u4e3a UTF-16 \u80fd\u5bb9\u7eb3\u5b8c\u6574\u7684 Unicode \u6620\u5c04\u3002\u800c\u5b8c\u5168\u6446\u70c2\u7684 Lua\uff0c\u5176 io.open \u53ea\u662f\u4f7f\u7528 C \u8bed\u8a00\u5e93\u51fd\u6570 fopen \uff0c fopen \u53c8\u662f\u57fa\u4e8e Windows \u7684 A \u7cfb\u5217\u51fd\u6570\uff0cLua \u53c8\u6ca1\u6709\u63d0\u4f9b\u5bf9 Windows C \u8fd0\u884c\u65f6\u5e93\u7279\u6709\u7684 _wfopen \u51fd\u6570\u7684\u5c01\u88c5\uff0c\u4ece\u800c\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6253\u5f00\u4e00\u4e2a\u5e26\u6709 Emoji \u7684\u6587\u4ef6\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 ANSI \u9635\u8425\uff0c\u4f60\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0cchar \u6ee1\u5929\u98de\u6446\u70c2\u3002 UTF-8 \u9635\u8425 \u652f\u6301 Unicode\uff0c\u5b57\u7b26\u4e32\u7edf\u4e00\u4ee5 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u3001\u5904\u7406\u548c\u4f20\u8f93\u3002 \u5e94\u7528\u573a\u666f\uff1a\u5e38\u89c1\u4e8e\u6587\u5b57\u5904\u7406\u9700\u6c42\u4e0d\u5927\uff0c\u4f46\u6709\u5f3a\u70c8\u7684\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u7279\u522b\u662f\u4e92\u8054\u7f51\u65b9\u9762\u7684\u8f6f\u4ef6\u3002\u4ed6\u4eec\u901a\u5e38\u53ea\u7528\u5230\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3001\u67e5\u627e\u3001\u5207\u7247\u901a\u5e38\u4e5f\u53ea\u662f\u5728\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u6587\u4ef6\u5206\u9694\u7b26 '/' \uff09\u3002\u4e5f\u975e\u5e38\u9002\u5408\u4e3b\u8981\u9762\u5bf9\u7684\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0cUTF-8 \u662f\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u6700\u9ad8\u7684\uff0c\u6240\u4ee5\u4e5f\u5e7f\u6cdb\u7528\u4e8e\u7f16\u8bd1\u5668\u3001\u6570\u636e\u5e93\u4e4b\u7c7b\u7684\u573a\u666f\u3002\u540c\u65f6\u56e0\u4e3a UTF-8 \u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u4f7f\u5f97\u4ed6\u80fd\u8f7b\u6613\u9002\u914d\u8fdc\u53e4\u7684 C \u8bed\u8a00\u7a0b\u5e8f\u548c\u5e93\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-8 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8de8\u5e73\u53f0\uff0c\u5728\u7f51\u7edc\u4f20\u8f93\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u7801\uff0cUTF-8 \u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u7f16\u7801\u683c\u5f0f\uff0c\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684 UTF-8 \u8f6f\u4ef6\u53ef\u4ee5\u968f\u610f\u5171\u4eab\u6587\u672c\u6570\u636e\u3002\u517c\u5bb9 ASCII\uff0c\u65b9\u4fbf\u590d\u7528\u73b0\u6709\u5e93\u548c\u751f\u6001\u3002\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u9ad8\uff0c\u5bf9\u4e2d\u6587\u6587\u672c\u4e5f\u4e0d\u7b97\u592a\u5dee\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e\u5e95\u5c42 API \u5747\u91c7\u7528 UTF-16 \u7684 Windows \u7cfb\u7edf\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u635f\u5931\u3002\u4e14\u5b57\u7b26\u4e32\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4f1a\u53d8\u6210 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002 \u4ee3\u8868\u4f5c\uff1aRust \u8bed\u8a00\u3001Go \u8bed\u8a00\u3001CMake \u6784\u5efa\u7cfb\u7edf\u3001Julia \u8bed\u8a00\u7b49\u3002 \u5728 C++ \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 u8\"\u4f60\u597d\" \u521b\u5efa\u4e00\u4e2a\u4fdd\u8bc1\u5185\u90e8\u662f UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u7c7b\u578b\u4e3a char8_t [] \u3002 \u5982\u679c\u7528\u65e0\u524d\u7f00\u7684 \"\u4f60\u597d\" \u521b\u5efa\uff0c\u5219 MSVC \u9ed8\u8ba4\u4f1a\u4ee5\u7f16\u8bd1\u8005\u6240\u5728\u7cfb\u7edf\u7684\u201c\u533a\u57df\u8bbe\u7f6e (locale)\u201d \u4f5c\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u7f16\u7801\u683c\u5f0f\uff08\u800c\u4e0d\u662f\u8fd0\u884c\u8005\u7684\u533a\u57df\u8bbe\u7f6e\uff01\uff09\uff0c\u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8ba9 MSVC \u7f16\u8bd1\u5668\u9ed8\u8ba4\u91c7\u7528 UTF-8 \u7f16\u7801\uff0c\u5373\u8ba9 \"\u4f60\u597d\" \u548c u8\"\u4f60\u597d\" \u4e00\u6837\u91c7\u7528 UTF-8\u3002\u800c GCC \u9ed8\u8ba4\u5c31\u662f UTF-8\uff0c\u9664\u975e\u624b\u52a8\u6307\u5b9a -fexec-charset=GBK \u6765\u5207\u6362\u5230 GBK\u3002\u7a0d\u540e\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u7f16\u8bd1\u5668\u7684\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002 \u5047\u8bbe\u4f60\u901a\u8fc7 /utf-8 \u6216 -fexec-charset=utf-8 \u641e\u5b9a\u4e86\u7f16\u8bd1\u671f\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u3002\u63a5\u4e0b\u6765\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6587\u4ef6\u7cfb\u7edf\u3002 Linux \u6587\u4ef6\u7cfb\u7edf\u5185\u90e8\uff0c\u5747\u4f7f\u7528 8 \u4f4d\u7c7b\u578b char \u5b58\u50a8\uff0c\u5c06\u6587\u4ef6\u540d\u5f53\u4f5c\u5e73\u51e1\u7684\u5b57\u8282\u6d41\uff0c\u4e0d\u4f1a\u505a\u4efb\u4f55\u8f6c\u6362\u3002\u56e0\u6b64\u4f60\u7528 UTF-8 \u521b\u5efa\u548c\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5176\u4ed6\u4f7f\u7528 UTF-8 \u533a\u57df\u8bbe\u7f6e\u7684\u8f6f\u4ef6\u90fd\u53ef\u4ee5\u7167\u5e38\u6253\u5f00\uff0c\u4e0d\u4f1a\u6709\u4e71\u7801\u95ee\u9898\u3002 \u5176\u5b9e Windows \u4e0a\u4ee5 GBK \u7f16\u7801\u7684\u538b\u7f29\u6587\u4ef6\u6216\u6587\u672c\u6587\u4ef6\uff0c\u62f7\u8d1d\u5230 Linux \u4e0a\u6253\u5f00\u51fa\u73b0\u4e71\u7801\u95ee\u9898\uff0c\u5c31\u662f\u56e0\u4e3a Linux \u7684\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u90fd\u662f UTF-8 \u7684\u3002\u5b9e\u9645\u4e0a\u5982\u679c\u628a\u4f60\u7684\u6587\u4ef6\u62f7\u7ed9\u4e00\u4e2a\u7f8e\u56fd\u7684 Windows \u7528\u6237\uff0c\u4ed6\u4e5f\u4f1a\u770b\u5230\u4e71\u7801\uff0c\u56e0\u4e3a\u7f8e\u56fd\u5927\u533a\u7684 Windows \u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\uff0c\u800c\u4e2d\u56fd\u5927\u533a\u7684\u662f GBK\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8bb2\u5230\u89e3\u51b3\u65b9\u6848\u3002 \u800c Windows \u7684 NTFS \u6587\u4ef6\u7cfb\u7edf\uff0c\u91c7\u7528 16 \u4f4d\u7684 wchar_t \u5b58\u50a8\uff0cWindows \u7684\u6240\u6709 API\uff0c\u4e5f\u90fd\u662f\u57fa\u4e8e wchar_t \u7684\uff0cWindows \u5185\u6838\u5185\u90e8\u4e5f\u90fd\u7528 wchar_t \u50a8\u5b58\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u53ea\u6709\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6d41\u4f1a\u7528 char \u5b58\u50a8\u3002\u8fd9\u7c7b\u57fa\u4e8e wchar_t \u7684\u7cfb\u7edf API \u90fd\u6709\u4e00\u4e2a W \u540e\u7f00\uff0c\u4f8b\u5982\uff1a MessageBoxW(NULL, L\"\u4f60\u597d\", L\"\u6807\u9898\", MB_OK); \u8fd9\u4e2a MessageBoxW \u51fd\u6570\uff0c\u53ea\u63a5\u53d7 const wchar_t * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002 L\"\u4f60\u597d\" \u662f\u4e00\u4e2a wchar_t [] \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b83\u7684\u5185\u90e8\u7f16\u7801\u7c7b\u578b\u56fa\u5b9a\u662f UTF-16\uff0c\u4e0d\u4f1a\u968f\u7740\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002\u4e4b\u540e\u7684\u4e00\u8282\u4e2d\u4f1a\u8be6\u7ec6\u8bb2\u89e3 W \u548c A \u51fd\u6570\u7684\u95ee\u9898\u3002 \u867d\u7136\u4e5f\u6709\u63d0\u4f9b A \u540e\u7f00\u7684\u7cfb\u5217\u51fd\u6570\uff0c\u4ed6\u4eec\u548c W \u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u662f\u63a5\u53d7 const char * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u95ee\u9898\u5728\u4e8e\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u90fd\u5fc5\u987b\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f GBK \u7f16\u7801\uff01\u800c\u4e14\u65e0\u6cd5\u4fee\u6539\u3002 \u5f53\u8c03\u7528 A \u7cfb\u51fd\u6570\u65f6\uff0c\u4ed6\u4eec\u5185\u90e8\u4f1a\u628a GBK \u7f16\u7801\u8f6c\u6362\u4e3a UTF-16 \u7f16\u7801\uff0c\u7136\u540e\u8c03\u7528 Windows \u5185\u6838\u3002 \u8fd9\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\uff0c\u800c\u6240\u6709\u7684 C/C++ \u6807\u51c6\u5e93\u90fd\u662f\u57fa\u4e8e A \u51fd\u6570\u7684\uff01\u5982\u679c\u4f60\u7528 const char * \u5b57\u7b26\u4e32\u8c03\u7528 C \u6807\u51c6\u5e93\uff0c\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86 A \u51fd\u6570\u3002\u800c A \u51fd\u6570\u53ea\u63a5\u53d7 GBK\uff0c\u4f46\u4f60\u5374\u8f93\u5165\u4e86 UTF-8\uff01\u4ece\u800c UTF-8 \u4e2d\u6240\u6709\u9664 ASCII \u4ee5\u5916\u7684\uff0c\u5404\u79cd\u4e2d\u6587\u5b57\u7b26\u3001Emoji \u90fd\u4f1a\u53d8\u6210\u4e71\u7801\u3002 \u4f8b\u5982 fopen \u51fd\u6570\uff0c\u53ea\u6709 fopen(const char *path, const char *mode) \u8fd9\u4e00\u4e2a\u57fa\u4e8e char \u7684\u7248\u672c\uff0c\u91cc\u9762\u4e5f\u662f\u76f4\u63a5\u8c03\u7528\u7684 A \u51fd\u6570\uff0c\u5b8c\u5168\u4e0d\u7ed9\u6211\u9009\u62e9\u7684\u7a7a\u95f4\u3002\u867d\u7136 Windows \u4e5f\u63d0\u4f9b\u4e86 _wfopen(const wchar_t *path, const wchar_t *mode) \uff0c\u4f46\u90a3\u65e2\u4e0d\u662f POSIX \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u4e0d\u662f C \u8bed\u8a00\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u7528\u8fd9\u6837\u7684\u51fd\u6570\u5c31\u610f\u5473\u7740\u65e0\u6cd5\u8de8\u5e73\u53f0\u3002 Windows \u5b98\u65b9\u8ba4\u4e3a\uff1a W \u51fd\u6570\u624d\u662f\u771f\u6b63\u7684 API\uff0c A \u51fd\u6570\u53ea\u662f\u5e94\u4ed8\u4e0d\u542c\u8bdd\u7684\u5b9d\u5b9d\u3002\u53ef\u4f60\u5c31\u6ca1\u53d1\u73b0\u4f60\u81ea\u5df1\u7684 C/C++ \u6807\u51c6\u5e93\u4e5f\u5168\u90e8\u5728\u8c03\u7528\u7684 A \u51fd\u6570\u4e48\uff1f \u603b\u4e4b\uff0c A \u51fd\u6570\u662f\u6b8b\u5e9f\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u7528 W \u51fd\u6570\uff0c\u5c3d\u7ba1 UTF-16 \u662f\u5386\u53f2\u503a\uff0c\u4f46\u6211\u4eec\u522b\u65e0\u9009\u62e9\uff0c W \u51fd\u6570\u662f\u552f\u4e00\u80fd\u652f\u6301\u5b8c\u6574 Unicode \u5b57\u7b26\u8f93\u5165\u7684\u65b9\u5f0f\u3002 // \u5047\u8bbe\u8fd9\u6bb5 C++ \u4ee3\u7801\u4f7f\u7528 /utf-8 \u9009\u9879\u7f16\u8bd1\uff1a std::ifstream f(\"\u4f60\u597d.txt\"); // \u627e\u4e0d\u5230\u6587\u4ef6\uff0c\u5373\u4f7f\u201c\u4f60\u597d.txt\u201d\u5b58\u5728 std::ofstream f(\"\u4f60\u597d.txt\"); // \u4f1a\u521b\u5efa\u4e00\u4e2a\u4e71\u7801\u6587\u4ef6 \u6b63\u786e\u7684\u505a\u6cd5\u662f\u91c7\u7528 std::filesystem::u8path \u8fd9\u4e2a\u51fd\u6570\u505a UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\uff1a // C++17\uff0c\u9700\u8981\u7528 u8path \u8fd9\u4e2a\u51fd\u6570\u6784\u9020 path \u5bf9\u8c61\uff1a std::ifstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); // C++20 \u5f15\u5165 char8_t\uff0c\u533a\u5206\u4e8e\u666e\u901a char\uff0cpath \u7c7b\u4e5f\u6709\u4e86\u9488\u5bf9 const char8_t * \u7684\u6784\u9020\u51fd\u6570\u91cd\u8f7d\uff1a std::ifstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::filesystem::path \u7c7b\u7684 c_str() \u5728 Windows \u4e0a\u8fd4\u56de const wchar_t * \uff0c\u5728 Linux \u4e0a\u8fd4\u56de const char * \u3002\u8fd9\u5f88\u5408\u7406\uff0c\u56e0\u4e3a Windows \u6587\u4ef6\u7cfb\u7edf\u786e\u5b9e\u4ee5 wchar_t \u5b58\u50a8\u8def\u5f84\u540d\uff0c\u800c Linux \u6587\u4ef6\u7cfb\u7edf\u5b8c\u5168\u7528 char \u3002 \u6bcf\u6b21\u9700\u8981\u52a0 std::filesystem::u8path \u4e5f\u633a\u9ebb\u70e6\u7684\uff0c\u5bb9\u6613\u5fd8\u8bb0\uff0c\u4e00\u5fd8\u8bb0\u5c31\u65e0\u6cd5\u8bbf\u95ee\u4e2d\u6587\u76ee\u5f55\u3002 \u5f88\u591a\u8f6f\u4ef6\u5728 Windows \u4e0a\u65e0\u6cd5\u652f\u6301\u4e2d\u6587\u8def\u5f84\u540d\uff0c\u5c31\u662f\u56e0\u4e3a\u4ed6\u4eec\u4e60\u60ef\u4e86 Linux \u6216 MacOS \u7684\u5168 UTF-8 \u73af\u5883\uff0c\u5bf9\u6587\u4ef6\u8def\u5f84\u6ca1\u6709\u4efb\u4f55\u8f6c\u6362\u3002\u800c Windows \u5e95\u5c42\u5168\u662f UTF-16\uff0c\u6839\u672c\u6ca1\u6709\u63d0\u4f9b UTF-8 \u7684 API\uff0c\u4f60 UTF-8 \u53ea\u80fd\u8f6c\u6362\u6210 UTF-16 \u624d\u80fd\u907f\u514d\u4e2d\u6587\u4e71\u7801\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u6b7b\u6d3b\u4e0d\u80af\u63a5\u53d7\u660e\u6446\u7740\u5df2\u7ecf\u662f\u56fd\u9645\u901a\u7528\u6807\u51c6\u7684 UTF-8\uff0cA \u51fd\u6570\u7684\u7f16\u7801\u8fde\u5f53\u524d\u8fdb\u7a0b\u5207\u6362\u7684\u65b9\u6cd5\u90fd\u4e0d\u7ed9\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u5e94\u8be5\u7531 Windows \u5168\u8d23\u627f\u62c5\u3002 \u597d\u6d88\u606f\u662f\uff0c\u6700\u8fd1 MSVC \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6848\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\u5f00\u5934\uff0c\u52a0\u4e0a setlocale(LC_ALL, \".utf-8\") \u5c31\u53ef\u4ee5\u8ba9 C \u548c C++ \u6807\u51c6\u5e93\u8fdb\u5165 UTF-8 \u6a21\u5f0f\uff1a\u4e0d\u518d\u8c03\u7528 A \u7cfb\u51fd\u6570\u64cd\u4f5c\u6587\u4ef6\uff0c\u800c\u662f\u4f1a\u628a\u6587\u4ef6\u540d\u4ece UTF-8 \u8f6c\u6362\u6210 UTF-16 \u540e\u518d\u8c03\u7528\u771f\u6b63\u7a33\u5b9a\u7684 W \u7cfb\u51fd\u6570\u3002 setlocale(LC_ALL, \".utf-8\"); // \u53ea\u9700\u8981\u8fd9\u4e00\u884c FILE *fp = fopen(u8\"\u4f60\u597d.txt\", \"r\"); // \u53ef\u4ee5\u4e86 std::ifstream fin(u8\"\u4f60\u597d.txt\"); // \u53ef\u4ee5\u4e86 setlocale(LC_ALL, \".utf-8\"); \u53ea\u662f\u628a C \u6807\u51c6\u5e93\u7684 const char * \u53c2\u6570\u53d8\u6210\u4e86\u63a5\u53d7 UTF-8\uff0c\u5e76\u4e0d\u4f1a\u8ba9\u7cfb\u7edf\u7684 A \u51fd\u6570\u4e5f\u53d8\u6210 UTF-8 \u54e6\uff0c\u8c03\u7528\u672c\u5730 API \u65f6\u4ecd\u9700 UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-8 \u9635\u8425\uff0c\u5f00\u542f /utf-8 \uff0c\u7a0b\u5e8f\u5f00\u5934\u5199 setlocale(LC_ALL, \".utf-8\") \u3002Linux \u7528\u6237\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u3002 \u770b\u770b\u5404\u5927\u8f6f\u4ef6\u7ad9\u5728 UTF-8 \u9635\u8425\u7684\u7406\u7531\uff1a CMake\uff1a\u4f5c\u4e3a\u8de8\u5e73\u53f0\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u4e3a\u4e86\u8ba9\u9879\u76ee\u7684 CMakeLists.txt \u80fd\u8de8\u5e73\u53f0\u5171\u7528\u800c\u4e0d\u5fc5\u91cd\u5199\uff0c\u4ed6\u7406\u6240\u5f53\u7136\u5730\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\uff1a\u6240\u6709 CMakeLists.txt \u90fd\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u4e66\u5199\uff0c\u4e14\u7edf\u4e00\u4f7f\u7528\u6b63\u659c\u6760 '/' \u8def\u5f84\u5206\u9694\u7b26\u3002 CMake \u4f1a\u81ea\u52a8\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u5c06 UTF-8 \u5b57\u7b26\u4e32\u8f6c\u6362\u6210 UTF-16 \u540e\uff0c\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u5728 Linux \u7cfb\u7edf\u4e0a\u5219\u4e0d\u505a\u8f6c\u6362\u3002\u5728 Windows \u7cfb\u7edf\u4e0a\u8fd8\u4f1a\u81ea\u52a8\u628a\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u6b63\u659c\u6760 '/' \u8f6c\u6362\u6210 Windows \u4e13\u5c5e\u7684\u53cd\u659c\u6760 '\\\\' \uff0c\u65e0\u9700\u7528\u6237\u64cd\u5fc3\u3002 \u5c0f\u5f6d\u8001\u5e08\u81ea\u4e3b\u7814\u53d1\u7684 Zeno \u8282\u70b9\u4eff\u771f\u8f6f\u4ef6\uff1a\u7531\u4e8e\u4fdd\u5b58\u7684\u9879\u76ee\u5de5\u7a0b\u6587\u4ef6\u9700\u8981\u5728 Linux \u548c Windows \u5e73\u53f0\u4e0a\u4e92\u901a\uff0c\u4e0d\u80fd\u91c7\u7528 Windows \u5404\u81ea\u4e3a\u653f\u7684 GBK \u683c\u5f0f\uff0c\u4e14\u5de5\u7a0b\u6587\u4ef6\u5185\u5bb9\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\u4e2d\u3002 Rust \u548c Go\uff1a\u4e25\u683c\u533a\u5206\u201c\u5b57\u7b26 (32 \u4f4d)\u201d\u548c\u201c\u5b57\u8282 (8 \u4f4d)\u201d\u7684\u6982\u5ff5\u3002\u5728\u5b57\u7b26\u4e32\u7c7b\u578b\u4e2d\u5b58\u50a8\u5b57\u8282\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u4ee5\u5b57\u8282\u65b9\u5f0f\u8bfb\u53d6\u6216\u4ee5\u5b57\u7b26\u65b9\u5f0f\u8bfb\u53d6\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-8 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5904\u7406\u5207\u7247\u548c\u7d22\u5f15\u65f6\u4e0d\u65b9\u4fbf\u3002 \u7f16\u7a0b\u8bed\u8a00 \u5b57\u7b26\u7c7b\u578b (32 \u4f4d) \u5b57\u8282\u7c7b\u578b (8 \u4f4d) Rust char u8 Go rune byte Julia Char UInt8 \u4e3a\u6b64\uff0c\u8fd9\u4e9b\u8bed\u8a00\u90fd\u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e24\u5957 API\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u8282\u7d22\u5f15\u3002\u6309\u5b57\u7b26\u7d22\u5f15\u65f6\uff0c\u4f1a\u4ece\u5934\u5f00\u59cb\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\uff0c\u76f4\u5230\u89e3\u6790\u5230\u60f3\u8981\u7684\u5b57\u7b26\u4e3a\u6b62\uff0c\u590d\u6742\u5ea6 O(N) O(N) \u3002\u6309\u5b57\u8282\u7d22\u5f15\u65f6\uff0c\u76f4\u63a5\u8df3\u5230\u6307\u5b9a\u5b57\u8282\uff0c\u65e0\u9700\u89e3\u6790\uff0c\u590d\u6742\u5ea6 O(1) O(1) \u3002 let s = \"\u4f60\u597d\"; // \u6309\u5b57\u7b26\u904d\u5386 for c in s.chars() { // c: char println!(\"{}\", c); } // \u6309\u5b57\u8282\u904d\u5386 for b in s.bytes() { // b: u8 println!(\"{:02x}\", b); } \u5728 C++ \u4e2d\uff0c\u82e5\u8981\u91c7\u7528\u8fd9\u79cd UTF-8 \u65b9\u6848\uff0c\u53ef\u4ee5\u4f7f\u7528 utfcpp \u8fd9\u4e2a\u5e93\uff1a https://github.com/nemtrif/utfcpp \u7a0d\u540e\u6211\u4eec\u4f1a\u4ee5\u6848\u4f8b\u8be6\u7ec6\u6f14\u793a\u8fd9\u4e2a\u5e93\u7684\u7528\u6cd5\uff0c\u4e5f\u4f1a\u5c1d\u8bd5\u81ea\u5df1\u624b\u6413\u3002 \u65b9\u6cd51\uff1a\u4f7f\u7528 utf8to32 \u4e00\u6b21\u6027\u5b8c\u6210\u8f6c\u6362\uff0c\u7528\u5b8c\u540e\u518d\u8f6c\u56de\u53bb\u3002 std::string s = \"\u4f60\u597d\"; std::u32string u32 = utf8::utf8to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\u574f'; s = utf8::utf32to8(u32); fmt::println(\"{}\", s); // \u4f60\u574f \u65b9\u6cd52\uff1a utfcpp \u4e5f\u5c01\u88c5\u4e86\u4e00\u4e2a utf8::iterator \u8fed\u4ee3\u5668\u9002\u914d\u5668\uff0c\u6548\u679c\u7c7b\u4f3c\u4e8e Rust \u7684 .chars() \uff0c\u53ef\u4ee5\u5b57\u7b26\u800c\u4e0d\u662f\u5b57\u8282\u904d\u5386\u5b57\u7b26\u4e32\u5bb9\u5668\u3002 char s[] = \"\u4f60\u597d\"; utf8::unchecked::iterator bit(s); utf8::unchecked::iterator eit(s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u5b89\u5168\uff08\u5e26\u8fb9\u754c\u68c0\u6d4b\uff09\u7684\u7248\u672c char s[] = \"\u4f60\u597d\"; utf8::iterator bit(s, s, s + strlen(s)); utf8::iterator eit(s + strlen(s), s, s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u57fa\u4e8e std::string \u7684\u7248\u672c std::string s = \"\u4f60\u597d\"; utf8::iterator bit(s.begin(), s.begin(), s.end()); utf8::iterator eit(s.end(), s.begin(), s.end()); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } \u7531\u4e8e\u8fed\u4ee3\u5668\u63a5\u53e3\u590d\u6742\u96be\u61c2\uff0c\u5efa\u8bae\u5148\u5c01\u88c5\u6210\u5e26\u6709 begin() \u548c end() \u7684 range \u5bf9\u8c61\uff0c\u65b9\u4fbf\u4f7f\u7528 C++17 range-based loop \u8bed\u6cd5\u76f4\u89c2\u904d\u5386\uff1a template struct Utf8Range { utf8::iterator bit; utf8::iterator eit; template Utf8Range(T &&t) : bit(std::begin(t), std::begin(t), std::end(t)) , eit(std::end(t), std::begin(t), std::end(t)) {} auto begin() const { return bit; } auto end() const { return eit; } }; template Utf8Range(T &&t) -> Utf8Range; // \u4ee5\u4e0b\u662f\u65b0\u7c7b\u7684\u4f7f\u7528\u65b9\u6cd5 std::string s = \"\u4f60\u597d\"; for (char32_t c : Utf8Range(s)) { fmt::println(\"U+{:04X}\", c); } UTF-16 \u9635\u8425 \u652f\u6301 Unicode \u8fc7\u65e9\uff0c\u8bef\u4ee5\u4e3a 0xFFFF \u5c31\u662f Unicode \u7684\u4e0a\u9650\u3002 \u4e00\u5f00\u59cb\uff0c\u4eba\u4eec\u9519\u8bef\u5730\u628a UTF-16 \u5f53\u6210\u6c38\u8fdc\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e71\u7801\u95ee\u9898\uff0c\u6240\u4ee5\u90a3\u6bb5\u65f6\u671f\u7684\u8f6f\u4ef6\u90fd\u5927\u4e3e\u4f7f\u7528 UTF-16 \u4f5c\u4e3a\u5185\u7801\u3002\u6ca1\u60f3\u5230\u540e\u6765 Unicode \u53c8\u5f15\u5165 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u800c\u73b0\u6709\u7684\u5df2\u7ecf\u91c7\u7528\u4e86 16 \u4f4d\u5185\u7801\u7684\u8f6f\u4ef6\u53c8\u5df2\u7ecf\u65e0\u6cd5\u6839\u9664\uff0c\u53ea\u597d\u4f7f\u7528\u201c\u4ee3\u7406\u5bf9\u201d\u673a\u5236\uff0c\u589e\u91cf\u66f4\u65b0\u4fee\u590d\u4e86\u73b0\u6709\u7684 16 \u4f4d\u5185\u7801\u8f6f\u4ef6\u3002UTF-16 \u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u597d\u5904\uff0c\u7559\u4e0b\u5386\u53f2\u503a\u3002 \u4e8b\u5b9e\u4e0a\uff0cUnicode \u5df2\u7ecf\u65e0\u6cd5\u7ee7\u7eed\u6269\u5bb9\u7a81\u7834 0x10FFFF\uff0c\u5c31\u662f\u56e0\u4e3a\u53cc uint16_t \u7f16\u7801\u7684\u4ee3\u7406\u5bf9\u6700\u591a\u53ea\u80fd\u5bb9\u7eb3\u989d\u5916 0x100000 \u4e2a\u5b57\u7b26\u7684\u7a7a\u95f4\u3002\u672c\u6765 UTF-8 \u4e00\u5f00\u59cb\u7684\u8349\u6848\u662f\u6253\u7b97\u6700\u591a\u652f\u6301 8 \u8282\u5217\u8f66\uff0c\u5b8c\u5168\u5bb9\u7eb3\u9ad8\u8fbe 0x7FFFFFFF \u8303\u56f4\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u8ba9 Windows \u8fd8\u80fd\u7ee7\u7eed\u7528\uff0cUnicode \u624d\u88ab\u8feb\u6b62\u6b65 0x10FFFF\uff0cUTF-8 \u4e5f\u7ec8\u7ed3\u4e8e 4 \u8282\u5217\u8f66\u3002 \u5e94\u7528\u573a\u666f\uff1a\u901a\u5e38\u8ba4\u4e3a\uff0cUTF-16 \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff0c\u65b0\u8f6f\u4ef6\u4e0d\u5e94\u8be5\u518d\u4f7f\u7528 UTF-16\u3002\u53ea\u6709\u5728\u548c\u8fd9\u4e9b\u7cdf\u7c95\u8f6f\u4ef6\u7684 API \u6253\u4ea4\u9053\u65f6\uff0c\u624d\u5fc5\u987b\u8f6c\u6362\u4e3a UTF-16\u3002\u4f46\u4e5f\u6709\u4eba\u6307\u51fa\uff1aUTF-16 \u662f\u7eaf\u4e2d\u6587\u538b\u7f29\u7387\u6700\u9ad8\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6240\u4ee5 UTF-16 \u8fd8\u6bd4\u8f83\u9002\u5408\u7eaf\u4e2d\u6587\u6216\u4ee5\u4e2d\u6587\u5185\u5bb9\u4e3a\u4e3b\u7684\u6587\u672c\u6570\u636e\u538b\u7f29\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-16 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8c03\u7528 Windows \u7cfb\u7edf API \u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u76f4\u63a5\u5c31\u80fd\u8c03\u7528\uff0c\u6700\u9002\u5408 Windows \u672c\u5730\u5f00\u53d1\uff0c\u975e\u8de8\u5e73\u53f0\u3002\u4e14\u5bf9\u7eaf\u4e2d\u6587\u5185\u5bb9\u53ef\u6bd4 UTF-8 \u989d\u5916\u8282\u7701 33% \u7a7a\u95f4\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e Windows \u4ee5\u5916\u7684\u7cfb\u7edf\u5c31\u9700\u8981\u8f6c\u6362\u56de UTF-8\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u5f00\u9500\u3002\u4e14\u5982\u679c\u5b58\u50a8\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u7eaf\u82f1\u6587\uff0c\u5982 XML \u4ee3\u7801\u7b49\uff0c\u5185\u5b58\u5360\u7528\u4f1a\u6bd4 UTF-8 \u7ffb\u500d\u3002\u800c\u4e14 UTF-16 \u4ecd\u7136\u662f\u53d8\u957f\u7f16\u7801\uff0c\u867d\u7136\u51fa\u73b0\u53d8\u957f\u7684\u6982\u7387\u8f83\u4f4e\uff0c\u4f46\u4e0d\u4e3a 0\uff0c\u4ecd\u9700\u8981\u5f00\u53d1\u8005\u505a\u7279\u6b8a\u5904\u7406\u3002\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u4f1a\u5bfc\u81f4\u751f\u50fb\u5b57\u7b26\u51fa\u9519\uff0c\u5b57\u7b26\u4e32\u4ee5\u7801\u70b9\u4e3a\u5355\u4f4d\u7684\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4ecd\u7136 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002\u5e76\u4e14 UTF-16 \u6709\u5927\u5c0f\u7aef\u8f6c\u6362\u7684\u95ee\u9898\u3002 \u4ee3\u8868\u4f5c\uff1aWindows \u7cfb\u7edf API\u3001Java \u8bed\u8a00\u3001Windows \u6587\u4ef6\u7cfb\u7edf (NTFS)\u3001Qt\u3001Word\u3001JSON\uff0c\u4ed6\u4eec\u90fd\u662f UTF-16 \u7684\u53d7\u5bb3\u8005\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-16 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-16 \u4f9d\u7136\u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5904\u7406\u6ca1\u95ee\u9898\uff0c\u751f\u50fb\u5b57\u5c31\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4e14\u56e0\u4e3a\u51fa\u73b0\u6982\u7387\u4f4e\uff0c\u5f88\u5bb9\u6613\u4e0d\u53d1\u73b0\uff0c\u57cb\u4e0b\u9690\u60a3\u3002 Java \u5c31\u662f\u53d7\u5230\u4e86 UTF-16 \u5386\u53f2\u503a\u5f71\u54cd\uff0c char \u662f 16 \u4f4d\u7684\u7801\u4f4d\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\uff0c\u771f\u6b63\u7684\u4e00\u4e2a\u5b57\u7b26\u662f 32 \u4f4d\u7684 Character \u7c7b\u578b\u3002 \u7f16\u7a0b\u8bed\u8a00 \u7801\u70b9\u7c7b\u578b (32 \u4f4d) \u7801\u4f4d\u7c7b\u578b (16 \u4f4d) Java Character char Java \u7684 Character \u7c7b\u578b\u662f\u4e00\u4e2a 32 \u4f4d\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u5305\u542b\u4e86\u4e00\u4e2a Unicode \u7801\u4f4d\u3002 char \u7c7b\u578b\u662f\u4e00\u4e2a 16 \u4f4d\u7684\u503c\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e2a UTF-16 \u7f16\u7801\u7684\u7801\u70b9\u3002 String \u7684 charAt() \u65b9\u6cd5\u8fd4\u56de\u7684\u662f char \u7c7b\u578b\u7684\u7801\u4f4d\uff08\u7c7b\u4f3c\u4e8e\u5b57\u8282\uff09\uff0c\u5982\u679c\u8981\u83b7\u53d6 Character \u7c7b\u578b\u7684\u5b8c\u6574\u5b57\u7b26\uff0c\u5fc5\u987b\u4f7f\u7528 codePointAt() \u65b9\u6cd5\u3002\u8fd9\u662f Java \u8bed\u8a00\u8bbe\u8ba1\u4e0a\u7684\u4e00\u4e2a\u5931\u8bef\uff0c\u5df2\u7ecf\u65e0\u6cd5\u6539\u53d8\u3002 \u800c\u540e\u7eed\u65b0\u51fa\u7684 Kotlin \u662f Java \u7684\u5408\u6cd5\u7ee7\u627f\u8005\uff0c\u4ed6\u679c\u65ad\u653e\u5f03 UTF-16\uff0c\u52a0\u5165\u4e86 UTF-32 \u9635\u8425\u3002\u53ef\u89c1\uff0c\u8001\u8f6f\u4ef6\u575a\u6301\u7528 UTF-32 \u662f\u56e0\u4e3a\u4ed6\u4eec\u79ef\u91cd\u96be\u8fd4\uff0c\u65b0\u8f6f\u4ef6\u518d UTF-16 \u5c31\u662f\u81ea\u4f5c\u5b7d\u4e86\uff01 \u603b\u7ed3\uff1a\u4e0d\u8981\u652f\u6301 UTF-16 \u9635\u8425\uff0c\u9664\u975e\u4f60\u88ab\u8feb\u7ef4\u62a4\u53f2\u5c71\u3002 \u4f8b\u5982\u5c0f\u5f6d\u8001\u5e08\u53d1\u5fae\u4fe1\u670b\u53cb\u5708\u65f6\uff0c\u8f93\u5165 Emoji \u8868\u60c5\u540e\u526a\u5207\uff0c\u518d\u7c98\u8d34\uff0c\u5c31\u548c\u53d1\u73b0\u4e00\u4e2a Emoji \u88ab\u5207\u65ad\u6210\u4e86\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4e71\u7801\u7684\u5f62\u5f0f\u663e\u73b0\u3002\u4f30\u8ba1\u662f\u56e0\u4e3a\u5fae\u4fe1\u57fa\u4e8e Java \u7f16\u5199\uff0c\u75bc\u900a\u7a0b\u5e8f\u5458\u5bf9 UTF-16 \u4ee3\u7406\u5bf9\u5904\u7406\u7684\u4e0d\u5229\u7d22\u3002 Java \u4e2d\u4ee5\u7801\u70b9\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5199\u6cd5\uff1a String s = \"\u4f60\u597d\"; // \u6309\u7801\u70b9\u904d\u5386 for (int i = 0; i < s.length();) { Character c = s.codePointAt(i); System.out.println(String.format(\"U+%04X\", c)); i += Character.charCount(c); } // \u6309\u7801\u4f4d\u904d\u5386 for (char c : s.toCharArray()) { System.out.println(String.format(\"U+%04X\", (int) c)); } \u7531\u4e8e JSON \u662f\u548c Java \u4e00\u5757\u53d1\u660e\u7684\u3002\u5bf9\u4e8e\u8d85\u51fa 0xFFFF \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u91c7\u7528\u7684\u8f6c\u4e49\uff0c\u4e5f\u662f\u57fa\u4e8e UTF-16 \u7f16\u7801\u3002\u5373\u540c\u4e00\u4e2a\u5b57\u4f1a\u53d8\u6210\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4fdd\u8bc1 JSON \u6587\u4ef6\u603b\u662f ASCII \u683c\u5f0f\uff0c\u907f\u514d Windows \u7684 GBK \u7f16\u7801\u4e71\u505a\u989d\u5916\u7684\u5e72\u6270\u3002 // \u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7 {\"name\": \"\ud883\udede\"} {\"name\": \"\\ud883\\udfde\"} \u5728\u521a\u521a\u4ecb\u7ecd\u7684 C++ \u5e93 utfcpp \u4e2d\uff0c\u4e5f\u6709\u9488\u5bf9 UTF-16 \u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5982 utf16to32 \uff1a std::u16string s = u\"\u4f60\u597d\"; std::u32string u32 = utf16::utf16to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\ud883\udede'; s = utf16::utf32to16(u32); fmt::println(\"{}\", s); // \u4f60\ud883\udede fmt::println(\"{}\", u32.size()); // 2 fmt::println(\"{}\", s.size()); // 3 UTF-32 \u9635\u8425 \u652f\u6301 Unicode\uff0c\u6bcf\u4e2a\u7801\u70b9\u90fd\u7528\u4e00\u4e2a uint32_t \u6216 char32_t \u8868\u793a\u3002 \u5e94\u7528\u573a\u666f\uff1a\u9002\u5408\u9700\u8981\u7ecf\u5e38\u5904\u7406\u6587\u5b57\u7684\u9886\u57df\uff0c\u5982\u6587\u672c\u7f16\u8f91\u5668\u3001\u6d4f\u89c8\u5668\u7b49\u3002\u4f46\u4e0d\u9002\u5408\u5b58\u50a8\u548c\u4f20\u8f93\uff0c\u56e0\u4e3a\u6d6a\u8d39\u786c\u76d8\u548c\u7f51\u7edc\u5e26\u5bbd\u3002\u5b57\u7b26\u4e32\u4e00\u822c\u90fd\u957f\u671f\u4ee5 UTF-8 \u5b58\u50a8\uff0c\u53ea\u6709\u5728\u9700\u8981\u9891\u7e41\u7d22\u5f15\u7801\u4f4d\u65f6\uff0c\u624d\u9700\u8981\u8f6c\u6362\u4e3a UTF-32\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-32 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u3001\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u90fd\u662f O(1) O(1) \u7684\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u5f53\u4f5c\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u968f\u610f\u5904\u7406\u3002\u4f8b\u5982\u4f60\u53ef\u4ee5\u8bbe\u60f3\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff0c\u9700\u8981\u652f\u6301\u201c\u9000\u683c\u201d\u64cd\u4f5c\uff0c\u5982\u679c\u662f UTF-8 \u548c UTF-16 \u5c31\u9700\u8981\u7e41\u7410\u7684\u5224\u65ad\u4ee3\u7406\u5bf9\u3001\u5404\u79cd\u8f66\u53a2\uff0c\u800c UTF-32 \u7684\u5b57\u7b26\u4e32\u53ea\u9700\u8981\u4e00\u6b21 pop_back \u5c31\u641e\u5b9a\u4e86\u3002 \u7f3a\u70b9\uff1a\u6d6a\u8d39\u7a7a\u95f4\u5927\uff0c\u901a\u5e38\u5728\u4fdd\u5b58\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8f6c\u6362\u56de UTF-8 \u540e\u518d\u5199\u5165\u6587\u4ef6\uff0c\u6709\u4e00\u5b9a\u6027\u80fd\u5f00\u9500\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-32 \u9635\u8425\uff0c\u8bf7\u5168\u90e8\u4f7f\u7528 char32_t \u548c std::u32string \u3002\u5b57\u9762\u91cf\u5168\u7528 U\"\u4f60\u597d\" \u7684\u5f62\u5f0f\u4e66\u5199\uff0c\u8bfb\u6587\u4ef6\u65f6\u8f6c\u4e3a UTF-32\uff0c\u5199\u6587\u4ef6\u65f6\u8f6c\u56de UTF-8\u3002 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u7531\u4e8e C++26 \u524d\u6807\u51c6\u5e93\u5bf9\u7f16\u7801\u8f6c\u6362\u51e0\u4e4e\u6ca1\u6709\u652f\u6301\uff0c\u5728 C++ \u4e2d\u8f6c\u6362\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u90fd\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\u3002 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1a utfcpp \u5982\u679c\u4f60\u53ea\u662f\u9700\u8981\u4e0d\u540c UTF \u683c\u5f0f\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u6ca1\u6709\u5904\u7406 GBK \u7b49\u7684\u9700\u6c42\uff1a\u90a3\u4e48\u4e4b\u524d\u5df2\u7ecf\u4ecb\u7ecd\u4e86 utfcpp \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5e93\uff0c\u5df2\u7ecf\u591f\u7528\u3002 #include \"utf8/cpp20.h\" std::u8string s8 = u8\"\u4f60\u597d\"; std::u16string s16 = utf8::utf8to16(s8); std::u32string s32 = utf8::utf8to32(s8); std::string s = utf8::utf16to8(s16); s8 = utf8::utf16tou8(s16); \u6700\u540e\u8fd9\u4e24\u4e2a\u533a\u522b\u5728\u4e8e\uff0c utf16to8 \u8fd4\u56de std::string \uff0c utf16tou8 \u8fd4\u56de std::u8string \uff0c\u91cc\u9762\u90fd\u662f UTF-8 \u7f16\u7801\u7684\uff0c\u4e0d\u8fc7\u6709\u7684\u4eba\u559c\u6b22\u7528 std::string \u6765\u5b58\u50a8 UTF-8\uff0c\u4e0d\u559c\u6b22 std::u8string \uff0c\u6216\u8005\u8bf4\u4ed6\u4eec\u6ca1\u6709 C++20\uff0c\u4e0d\u652f\u6301 std::u8string \uff0c\u56e0\u6b64\u8fd9\u4e2a\u5e93\u6ee1\u8db3\u4ed6\u4eec\u7684\u4e0d\u540c\u9700\u8981\u3002\u4f46\u662f std::u8string \u4f5c\u4e3a\u53c2\u6570\u65f6\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u53c2\u6570\u53ef\u4ee5\u81ea\u52a8\u91cd\u8f7d\uff0c\u800c\u8fd4\u56de\u503c\u4e0d\u884c\u3002 \u7f3a\u70b9\u662f\u4ed6\u4e0d\u80fd\u5904\u7406 GBK\u3001Shift-JIS \u7b49\u975e Unicode \u7f16\u7801\uff0c\u4e5f\u4e0d\u80fd\u81ea\u52a8\u68c0\u6d4b\u5f53\u524d\u7684 ANSI \u533a\u57df\u8bbe\u7f6e\u3002 \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1a boost::locale \u5982\u679c\u4f60\u8fd8\u8981\u652f\u6301\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u6bd4\u5982 GBK\u3001Shift-JIS\u3001Latin-1\u3002 \u4e00\u79cd\u662f C \u8bed\u8a00\u7684 iconv \uff0c\u53e6\u4e00\u79cd\u662f\u73b0\u4ee3 C++ \u7684 boost::locale \u3002 \u867d\u7136\u529f\u80fd\u5dee\u4e0d\u591a\uff0c\u5e95\u5c42\u90fd\u662f\u8c03\u7528 icu \u7684\u3002 boost::locale \u7684 API \u66f4\u52a0\u53cb\u597d\uff0c\u800c\u4e14\u662f\u73b0\u4ee3 C++ \u98ce\u683c\u7684\u3002 # Ubuntu \u7528\u6237\u5b89\u88c5 Boost.locale \u65b9\u6cd5\uff1a $ sudo apt-get install libboost-locale-dev # Arch Linux \u7528\u6237\u5b89\u88c5 Boost \u5168\u5bb6\u6876\u65b9\u6cd5\uff1a $ sudo pacman -S boost \u4e0d\u559c\u6b22 Boost \u7684\u4eba\u6709\u96be\u4e86\u3002 UTF \u4e4b\u95f4\u4e92\u8f6c \u4f7f\u7528 boost::locale::conv::utf_to_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::utf_to_utf; int main() { std::string s8 = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c UTF-32\uff1a std::u32string s32 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-16\uff1a std::u16string s16 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-8\uff1a s8 = utf_to_utf(s32); std::cout << s8 << '\\n'; return 0; } \u6a21\u677f\u53c2\u6570\u4e2d\uff0c\u53ea\u9700\u6307\u5b9a\u8f6c\u6362\u5230\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5c31\u884c\uff0c\u6765\u81ea\u4ec0\u4e48\u7c7b\u578b\uff0c\u4ed6\u81ea\u5df1\u4f1a\u91cd\u8f7d\u7684\u3002 \u6bd4\u5982\u4ece char32_t \u8f6c\u5230 char16_t \uff0c\u53ea\u9700\u8981 utf_to_utf \u5c31\u53ef\u4ee5\uff0c\u975e\u5e38\u65b9\u4fbf\u3002 \u7f16\u8bd1\uff1a $ g++ -std=c++17 -lboost_locale main.cpp \u8f93\u51fa\uff1a \u4f60\u597d \u5efa\u8bae\u7528\u540c\u6837\u8de8\u5e73\u53f0\u7684 CMake \u94fe\u63a5 Boost\uff0c\u5426\u5219 Windows \u7528\u6237\u8981\u6709\u96be\u4e86\u2026\u2026 find_package(Boost REQUIRED COMPONENTS locale) target_link_libraries(\u4f60\u7684\u7a0b\u5e8f Boost::locale) \u4e0d\u8fc7 boost::locale \u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u90a3\u5c31\u662f\u4e0d\u652f\u6301 char8_t \u548c std::u8string \u3002 char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff01 \u7531\u4e8e Boost \u8f83\u8001\uff0c\u6ca1\u6709\u53ca\u65f6\u8ddf\u8fdb\uff0c\u6240\u4ee5\u4ed6\u5e76\u6ca1\u6709\u5b9e\u73b0\u9488\u5bf9 char8_t \u7684\u7279\u5316\uff0c\u5982\u679c\u4f7f\u7528\u4e86 utf_to_utf \u4f1a\u62a5 undefined reference \u9519\u8bef\uff0c\u5373\u627e\u4e0d\u5230\u7b26\u53f7\u3002\u6539\u6210 utf_to_utf \u5c31\u6ca1\u95ee\u9898\u4e86\u3002 GBK \u548c UTF \u4e92\u8f6c \u4f7f\u7528 boost::locale::conv::to/from_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::to_utf; using boost::locale::conv::from_utf; int main() { std::string s = \"\u4f60\u597d\"; // \u4ece GBK \u8f6c\u5230 UTF-16 std::wstring ws = to_utf(s, \"GBK\"); std::wcout << ws << '\\n'; // \u4ece UTF-16 \u8f6c\u56de GBK s = from_utf(ws, \"GBK\"); std::wcout << s << '\\n'; return 0; } \u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f GBK \u3001 Shift-JIS \u3001 Latin1 \u7b49\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u5728\u770b\u5230\u3002 \u8fd9\u91cc to_utf \u4f1a\u81ea\u52a8\u5224\u65ad wchar_t \u7684\u5927\u5c0f\u3002\u5982\u679c\u662f 2 \u5b57\u8282\uff08Windows \u5e73\u53f0\u60c5\u51b5\uff09\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-16\uff0c\u5982\u679c\u662f 4 \u5b57\u8282\uff08Linux \u5e73\u53f0\u60c5\u51b5\uff09\uff0c\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-32\u3002 \u800c to_char \u5219\u662f\u65e0\u8bba\u4ec0\u4e48\u5e73\u53f0\uff0c\u90fd\u4f1a\u8f6c\u4e3a UTF-16\u3002 from_utf \u4e0d\u9700\u8981\u6307\u5b9a\u4efb\u4f55\u6a21\u677f\u53c2\u6570\uff0c\u56e0\u4e3a\u4ed6\u603b\u662f\u8fd4\u56de std::string \uff08ANSI \u6216 GBK \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u53c2\u6570\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4f1a\u81ea\u52a8\u901a\u8fc7\u91cd\u8f7d\u5224\u65ad\uff0c\u4f8b\u5982 from_utf(ws, \"GBK\") \u8fd9\u91cc\u7684\u53c2\u6570\u662f wchar_t \uff0c\u90a3\u4e48\u5728 Windows \u4e0a\uff0c\u4ed6\u4f1a\u68c0\u6d4b\u5230 wchar_t \u662f 2 \u5b57\u8282\uff0c\u5c31\u8ba4\u4e3a\u662f UTF-16 \u5230 GBK \u7684\u8f6c\u6362\u3002 UTF \u548c ANSI \u4e92\u8f6c \u6211\u4eec\u7a0b\u5e8f\u7684\u7528\u6237\u4e0d\u4e00\u5b9a\u662f\u4e2d\u56fd\u7528\u6237\uff08GBK\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4fc4\u7f57\u65af\u7528\u6237\uff08CP1251\uff09\u3001\u65e5\u672c\u7528\u6237\uff08Shift-JIS\uff09\u3001\u897f\u73ed\u7259\u7528\u6237\uff08CP1252\uff09\u7b49\u3002 \u5982\u679c\u8981\u91c7\u7528\u7528\u6237\u7684\u533a\u57df\u8bbe\u7f6e\uff0c\u5373\u201cANSI\u201d\uff0c\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u7559\u7a7a\uff08 \"\" \uff09\u3002 \u7a7a\u5b57\u7b26\u4e32\u5c31\u8868\u793a\u5f53\u524d\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e86\uff0c\u5728\u4e2d\u56fd\u5927\u533a\u7b49\u4ef7\u4e8e \"GBK\" \uff0c\u4fc4\u7f57\u65af\u5927\u533a\u7b49\u4ef7\u4e8e \"CP1251\" \u7b49\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::to_utf; int main() { std::string u8s = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c ANSI std::string s = from_utf(u8s, \"\"); // ANSI \u8f6c UTF-8 u8s = to_utf(s, \"\"); return 0; } \u5927\u603b\u7ed3 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-16 utf_to_utf UTF-x UTF-32 utf_to_utf UTF-x Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 UTF-x \u8868\u793a\u53d6\u51b3\u4e8e\u53c2\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u5982\u679c\u53c2\u6570\u662f char16_t \u7684\u5b57\u7b26\u4e32 std::u16string \uff0c\u90a3 x \u5c31\u662f 16\u3002 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-16 to_utf(\"GBK\", string) GBK UTF-32 to_utf(\"GBK\", string) GBK Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-32 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 from_utf(\"GBK\", string) UTF-8 GBK from_utf(\"GBK\", u16string) UTF-16 GBK from_utf(\"GBK\", u32string) UTF-32 GBK from_utf(\"GBK\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 GBK from_utf(\"\", string) UTF-8 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u16string) UTF-16 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u32string) UTF-32 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u533a\u57df\u8bbe\u7f6e GBK \u548c Shift-JIS \u4e92\u8f6c #include #include using boost::locale::conv::between; using boost::locale::conv::from_utf; int main() { // \u521b\u5efa\u4e00\u4e2a Shift-JIS \u5b57\u7b26\u4e32 std::string jis = from_utf(u8\"\u65e5\u672c\u8a9e\", \"Shift-JIS\"); // \u4ece Shift-JIS \u8f6c\u5230 GBK std::string gbk = between(jis, \"GBK\", \"Shift-JIS\"); std::cout << gbk << '\\n'; // \u4ece GBK \u8f6c\u56de Shift-JIS jis = between(gbk, \"Shift-JIS\", \"GBK\"); std::cout << jis << '\\n'; return 0; } \u6ce8\u610f\uff01\u662f\u76ee\u6807\u7f16\u7801\u5728\u524d\uff01\u5982\u679c\u4f60\u8981\u4ece Shift-JIS \u8f6c\u6210 GBK\uff0c\u90a3\u4e48\u9700\u8981 between(jis, \"GBK\", \"Shift-JIS\") \uff0c\u8fd9\u771f\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\u3002\u4e0d\u4ec5 GBK \u548c Shift-JIS \u53ef\u80fd\u4e0d\u5c0f\u5fc3\u5f04\u53cd\u4e86\uff0c\u7f16\u8bd1\u5668\uff0c\u4e00\u70b9\u63d0\u793a\u90fd\u6ca1\u6709\uff0c\u800c\u4e14 jis \u548c \u201cGBK\u201d \u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5f88\u5bb9\u6613\u5927\u8111\u641e\u6df7\u3002\u8ba9\u6211\u6765\u8bbe\u8ba1\u7684\u8bdd\uff0c\u6211\u4f1a\u8fd9\u6837\u63d0\u4f9b API\uff1a decode(jis, Encoding::ShiftJIS).encode(Encoding::GBK) \uff0c\u5176\u4e2d Encoding \u662f\u4e00\u4e2a\u679a\u4e3e\uff0c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e0d\u4ec5\u907f\u514d\u72af\u9519\u7684\u673a\u4f1a\uff0c\u770b\u8d77\u6765\u4e5f\u66f4\u8f7b\u677e\u3002\u4e4b\u540e\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u9898\u8bfe\u4e2d\uff0c\u4f1a\u518d\u8be6\u7ec6\u8bb2\u89e3\u4ec0\u4e48\u662f\u597d\u7684 API \u8bbe\u8ba1\u3002 \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u5982\u679c\u9047\u5230\u65e0\u6cd5\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u8be5\u5982\u4f55\u5904\u7f6e\uff1f \u9ed8\u8ba4\u60c5\u51b5\u4e0b Boost \u4f1a\u5ffd\u89c6\u9519\u8bef\uff0c\u7f16\u7801\u5931\u8d25\u7684\u5b57\u7b26\u4f1a\u88ab\u4e22\u5f03\u3002 #include #include using boost::locale::conv::from_utf; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\"); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 std::cout << gbk << '\\n'; // \u5728 Windows \u7684 GBK \u7ec8\u7aef\u4e0a\uff0c\u53ea\u663e\u793a\u201c\u6211\u7231\u9762\u201d return 0; } \u53ef\u4ee5\u7528 method_type \u8fd9\u4e2a\u679a\u4e3e\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7684\u65b9\u5f0f\u3002 \u9ed8\u8ba4\u662f skip \uff0c\u8df3\u8fc7\u6240\u6709\u89e3\u7801\u51fa\u9519\u7684\u5730\u65b9\uff08\u5bfc\u81f4\u201c\ud883\udede\u201d\u4e22\u5931\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u5207\u6362\u5230 stop \uff0c\u5f53\u9047\u5230\u89e3\u7801\u9519\u8bef\u65f6\uff0c\u4f1a\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\", method_type::stop); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 // from_utf \u4f1a\u629b\u51fa `conversion_error` \u5f02\u5e38 std::cout << gbk << '\\n'; return 0; } \u4e3e\u4f8b\uff1a\u5c1d\u8bd5\u4ee5 GBK \u4fdd\u5b58\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u6539\u4e3a\u5e26\u6709 BOM \u7684 UTF-8\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; using boost::locale::conv::conversion_error; void try_save(std::u32string content, std::wstring path) { std::string binary; try { // \u5c1d\u8bd5\u5c06 UTF-32 \u8f6c\u6210 GBK \u7f16\u7801 binary = from_utf(content, \"GBK\", method_type::stop); } catch (conversion_error const &e) { // \u82e5 GBK \u65e0\u6cd5\u8868\u793a // \u6539\u7528\u524d\u9762\u5e26\u6709 BOM \u7684 UTF-8 \u7f16\u7801 binary = \"\\xEF\\xBB\\xBF\" + utf_to_utf(content); } std::ofstream(path) << binary; } \u4e3e\u4f8b\uff1a\u652f\u6301 UTF-8 \u5b57\u7b26\u4e32\uff08\u800c\u4e0d\u662f ANSI \u5b57\u7b26\u4e32\uff09\u7684\u6253\u5370\u51fd\u6570\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::utf_to_utf; void u8print(std::string msg) { std::cout << from_utf(msg, \"\"); // \u6216\u8005\uff1a // std::wcout << utf_to_utf(msg, \"\"); } \u66f4\u591a\u7ec6\u8282\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1ahttps://www.boost.org/doc/libs/1_81_0/libs/locale/doc/html/group__codepage.html \u66f4\u591a\u529f\u80fd\uff1f\uff01 \u7f16\u7801\u8f6c\u6362\u53ea\u662f boost::locale::conv \u8fd9\u4e2a\u5b50\u6a21\u5757\u4e0b\u7684\u4e00\u4e2a\u5c0f\u529f\u80fd\u800c\u5df2\uff01 boost::locale \u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u5982\u6309\u7167\u5730\u57df\u8bed\u8a00\u89c4\u8303\u683c\u5f0f\u5316\u6570\u5b57\u3001\u8d27\u5e01\u3001\u65e5\u671f\u3001\u65f6\u95f4\u7b49\uff0c\u4e0b\u4e00\u5c0f\u8282\u4e2d\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u3002\u5b8c\u5168\u662f std::locale \u7684\u4e0a\u4f4d\u66ff\u4ee3\u3002 Boost \u54ea\u91cc\u90fd\u597d\uff0c\u4f60\u60f3\u8981\u7684\u529f\u80fd\u5e94\u6709\u5c3d\u6709\u3002\u800c\u4e14\u4e0d\u9700\u8981 C++20\uff0c\u5f88\u4f4e\u7248\u672c\u7684 C++ \u4e5f\u80fd\u7528\u3002\u552f\u4e00\u7f3a\u70b9\u53ef\u80fd\u5c31\u662f\u592a\u80a5\u4e86\uff0c\u7f16\u8bd1\u6162\u3002 Windows \u7528\u6237\uff1aMultiByteToWideChar \u5982\u679c\u4f60\u662f Windows \u7a0b\u5e8f\u5458\uff0c\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u4e14\u9700\u8981\u5728 Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u89c4\u5b9a\u7684 ANSI\uff08\u5728\u4e2d\u56fd\u533a\u662f GBK\uff09\u7f16\u7801\u548c UTF-16 \u4e4b\u95f4\u8f6c\u6362\uff1a \u53ef\u4ee5\u7528 Windows \u5b98\u65b9\u63d0\u4f9b\u7684 MultiByteToWideChar \u548c WideCharToMultiByte \u51fd\u6570\u3002 \u8fd9\u4e24\u4e2a\u51fd\u6570\u56e0\u4e3a C \u8bed\u8a00\u7279\u8272\u7684\u7f18\u6545\uff0c\u53c2\u6570\u6bd4\u8f83\u591a\u800c\u6742\uff0c\u5efa\u8bae\u81ea\u5df1\u52a8\u624b\u5c01\u88c5\u6210\u66f4\u6613\u7528\u7684 C++ \u51fd\u6570\uff1a std::wstring ansi_to_wstring(const std::string &s) { // ACP = ANSI Code Page\uff0c\u544a\u8bc9\u4ed6\u5b57\u7b26\u4e32\u91cc\u7684\u662f\u5f53\u524d\u533a\u57df\u8bbe\u7f6e\u6307\u5b9a\u7684\u7f16\u7801\uff08\u5728\u4e2d\u56fd\u533a\uff0cANSI \u5c31\u662f GBK \u4e86\uff09 int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_ansi(const std::wstring &ws) { int len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } std::wstring utf8_to_wstring(const std::string &s) { int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_utf8(const std::wstring &ws) { int len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } C \u8bed\u8a00\u7279\u8272\uff1a\u6240\u6709\u8981\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff0c\u90fd\u9700\u8981\u8c03\u7528\u4e24\u904d\uff0c\u7b2c\u4e00\u6ce2\u5148\u6c42\u51fa\u957f\u5ea6\uff0c\u7b2c\u4e8c\u6ce2\u624d\u5199\u5165\u3002\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u4e0e\u5185\u5b58\u5206\u914d\u5668\u8026\u5408\uff0c\u6240\u6709\u7684 C \u98ce\u683c API \u90fd\u662f\u8fd9\u6837\u3002 MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b \u590d\u73b0\u6761\u4ef6\uff1a Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e3a\u4e2d\u6587 (GBK)\u3002 \u4f7f\u7528 MSVC \u7684 /utf-8 \u9009\u9879\u7f16\u8bd1\u3002 #include int main() { MessageBoxA(nullptr, \"\u6211\u7231\ud883\udede\ud883\udede\u9762\", \"\u6807\u9898\", MB_OK); // \u4f1a\u53d8\u6210\u4e71\u7801 return 0; } Linux \u7528\u6237\uff1a iconv \u5982\u679c\u4f60\u662f Linux \u7528\u6237\uff0c\u4e14\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u53ef\u4ee5\u4f7f\u7528 C \u8bed\u8a00\u7684 iconv \u5e93\u3002 iconv \u4e5f\u6709 Windows \u7684\u7248\u672c\uff0c\u4f46\u5b89\u88c5\u6bd4\u8f83\u56f0\u96be\u3002\u5982\u679c\u4f60\u8fde iconv \u90fd\u641e\u5f97\u5b9a\uff0c\u6ca1\u7406\u7531 Boost \u641e\u4e0d\u5b9a\u3002 #include #include std::string convert(std::string const &s, char const *from, char const *to) { iconv_t cd = iconv_open(to, from); if (cd == (iconv_t)-1) { throw std::runtime_error(\"iconv_open failed\"); } auto in = s.data(); auto inbytesleft = s.size(); size_t outbytesleft = inbytesleft * 4; std::string buffer(outbytesleft, 0); auto out = buffer.data(); iconv(cd, &in, &inbytesleft, &out, &outbytesleft); iconv_close(cd); buffer.resize(buffer.size() - outbytesleft); return buffer; } // \u4e3e\u4f8b\uff1aUTF-8 \u8f6c GBK std::string utf8_to_gbk(std::string const &s) { return convert(s, \"UTF-8\", \"GBK\"); } // \u4e3e\u4f8b\uff1aGBK \u8f6c UTF-8 std::string gbk_to_utf8(std::string const &s) { return convert(s, \"GBK\", \"UTF-8\"); } iconv \u547d\u4ee4\u884c\u5de5\u5177 iconv \u4e0d\u4ec5\u662f\u4e00\u4e2a\u5e93\uff0c\u4e5f\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5927\u591a Linux \u53d1\u884c\u7248\u90fd\u81ea\u5e26\u4e86\uff09\u3002\u7528\u6cd5\u5982\u4e0b\uff1a iconv -f \u6765\u81ea\u4ec0\u4e48\u7f16\u7801 -t \u5230\u4ec0\u4e48\u7f16\u7801 (\u8f93\u5165\u6587\u4ef6\u540d...) > \u8f93\u51fa\u6587\u4ef6\u540d \u5982\u4e0d\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u9ed8\u8ba4\u4ece\u7ec8\u7aef\u8f93\u5165\u6d41\u8bfb\u53d6\u3002 \u5982\u4e0d\u4f7f\u7528 > \u8f93\u51fa\u6587\u4ef6\u540d \u91cd\u5b9a\u5411\u8f93\u51fa\uff0c\u5219\u9ed8\u8ba4\u8f93\u51fa\u5230\u7ec8\u7aef\u3002 \u53ef\u4ee5\u7528 echo \u914d\u5408\u7ba1\u9053\u6765\u521b\u5efa\u8f93\u5165\u6d41\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 \u6b64\u5904\u663e\u793a\u4e71\u7801\u662f\u56e0\u4e3a\u6211\u7684\u7ec8\u7aef\u662f UTF-8 \u683c\u5f0f\uff0c\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 iconv \u8f93\u51fa\u7684 GBK \u683c\u5f0f\u6570\u636e\u3002 \u628a\u201c\u6211\u7231\u5c0f\u5f6d\u8001\u5e08\u201d\u8f6c\u6362\u4e3a GBK \u683c\u5f0f\u5199\u5165 gbk.txt \uff0c\u7136\u540e\u518d\u91cd\u65b0\u8fd8\u539f\u56de UTF-8 \u683c\u5f0f\u67e5\u770b\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK > gbk.txt $ cat gbk.txt \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 $ iconv -f GBK -t UTF-8 gbk.txt \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 Windows \u53ef\u80fd\u4e5f\u6709\u7c7b\u4f3c\u7684\u5de5\u5177\uff0c\u6bd4\u5982 iconv.exe \uff0c\u4f46\u6211\u6ca1\u627e\u5230\u3002 \u672c\u5730\u5316 (locale) \u672c\u5730\u5316\u662f\u6307\u6839\u636e\u7528\u6237\u7684\u8bed\u8a00\u3001\u5730\u533a\u7b49\u73af\u5883\uff0c\u663e\u793a\u4e0d\u540c\u7684\u754c\u9762\u3002\u6bd4\u5982\u8bf4\uff0c\u540c\u6837\u662f\u6587\u4ef6\u83dc\u5355\uff0c\u4e2d\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201c\u6587\u4ef6\u201d\u3001\u82f1\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201cFile\u201d\u3002 \u533a\u5206\u5b57\u7b26\u7c7b\u578b C \u8bed\u8a00\u63d0\u4f9b\u4e86 \u5934\u6587\u4ef6\uff0c\u91cc\u9762\u5c01\u88c5\u4e86\u5927\u91cf\u5f62\u5982 isspace \u3001 isdigit \u8fd9\u6837\u7684\u5224\u65ad\u5b57\u7b26\u5206\u7c7b\u7684\u51fd\u6570\u3002 #include C++ \u5bf9\u5176\u5b9e\u65bd\u4e86\u518d\u5c01\u88c5\uff0c\u6539\u540d\u4e3a \u3002\u82e5\u4f60\u5bfc\u5165\u7684\u662f\u8be5\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e26\u6709 std \u540d\u5b57\u7a7a\u95f4\u524d\u7f00\u7684\u65b9\u5f0f std::isspace \uff0c std::isdigit \u8bbf\u95ee\u4e86\uff0c\u770b\u8d77\u6765\u66f4\u52a0\u4e13\u4e1a\uff08\u786e\u4fe1\uff09\u3002 #include \u51fd\u6570\u6e05\u5355\uff1a \u51fd\u6570\u540d\u79f0 \u5224\u65ad\u7684\u5b57\u7b26\u7c7b\u578b isascii 0 \u5230 0x7F \u7684\u6240\u6709 ASCII \u5b57\u7b26 isalpha \u5927\u5c0f\u5199\u5b57\u6bcd A-Z a-z isupper \u5927\u5199\u5b57\u6bcd A-Z islower \u5c0f\u5199\u5b57\u6bcd a-z isdigit \u6570\u5b57 0-9 isxdigit \u5341\u516d\u8fdb\u5236\u6570\u5b57 A-F a-f 0-9 isprint \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5305\u62ec\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u6807\u70b9\u7b49 isgraph \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7a7a\u683c iscntrl \u63a7\u5236\u5b57\u7b26\uff0c\u9664\u53ef\u6253\u5370\u5b57\u7b26\u5916\u7684\u5168\u90e8 isspace \u7a7a\u767d\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u6362\u884c\u3001\u56de\u8f66\u3001\u5236\u8868\u7b26\u7b49 ispunct \u6807\u70b9\u7b26\u53f7 isalnum \u5b57\u6bcd\u6216\u6570\u5b57 \u66f4\u8be6\u7ec6\u7684\u8868\u683c\u53ef\u4ee5\u770b\uff1a https://en.cppreference.com/w/cpp/string/byte/isspace \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b TODO \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale TODO \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 TODO \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u72ed\u4e49\u7684\u6c49\u5b57\uff1a0x4E00 \u5230 0x9FA5\uff08\u201c\u4e00\u201d\u5230\u201c\u9fa5\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\uff1a0x2E80 \u5230 0x9FFF\uff08\u201c\u2e80\u201d\u5230\u201c\u9fff\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\u5305\u542b\u4e86\u51e0\u4e4e\u6240\u6709\u4e2d\u65e5\u97e9\u4f7f\u7528\u7684\u6c49\u5b57\u5b57\u7b26\uff0c\u800c\u72ed\u4e49\u7684\u6c49\u5b57\u53ea\u662f\u4e2d\u6587\u91cc\u6700\u5e38\u7528\u7684\u4e00\u90e8\u5206\u3002 TODO \u5bbd\u5b57\u7b26\u6d41 \u4e4b\u6240\u4ee5\u628a\u5bbd\u5b57\u7b26\u6d41\u653e\u5230\u6700\u540e\uff0c\u662f\u56e0\u4e3a\uff0c\u9996\u5148 iostream \u672c\u6765\u5c31\u662f\u4e00\u4e2a\u5931\u8d25\u7684\u8bbe\u8ba1\u3002 \u5c0f\u5f6d\u8001\u5e08\u5728\u672c\u4e66\u5f00\u5934\u5c31\u591a\u6b21\u5f3a\u8c03\u8fc7\u4ed6\u662f format \u5b5d\u5b50\u3002 \u800c\u5bbd\u5b57\u7b26 wchar_t \u672c\u8eab\u5c31\u5145\u65a5\u7740\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff08\u4f8b\u5982 Windows \u88ab UTF-16 \u80cc\u523a\uff09\u3002 \u73b0\u5728 iostream \u4e0e wchar_t \u4e00\u8d77\u51fa\u73b0\u5728\u6211\u9762\u524d\uff0c\u4e0d\u80fd\u8bf4\u662f\u68a6\u5e7b\u8054\u52a8\u5427\uff0c\u81f3\u5c11\u4e5f\u53ef\u4ee5\u8bf4\u662f\u7b54\u8fa9\u8d85\u4eba\u4e86\u3002 \u603b\u4e4b\uff0c\u6211\u4e2a\u4eba\u8fd8\u662f\u63a8\u8350\u7a0b\u5e8f\u5185\u90e8\u4ee5 UTF-8\uff08 char8_t \uff09\u6216 UTF-32\uff08 char32_t \uff09\u7684\u5b57\u7b26\u4e32\u6765\u5904\u7406\u4e07\u7269\u3002 UTF-8 \u6216 UTF-32 \u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u4e2d\u6587\u5904\u7406\u9700\u6c42\u662f\u5426\u65fa\u76db\uff0c\u662f\u5426\u5728\u4e4e\u7a7a\u95f4\uff0c\u662f\u5426\u9700\u8981\u5207\u7247\u548c\u7d22\u5f15\u7b49\u3002 \u5f53\u9700\u8981\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u518d\u7528 boost::locale \u3001 utfcpp \u7b49\u5de5\u5177\u8f6c\u6362\u6210 ANSI\uff08 char \uff09\u6216 UTF-16\uff08 wchar_t \uff09\u3002 \u5bf9\u4e8e Linux \u7528\u6237\uff0c\u4e5f\u53ef\u4ee5\u68c0\u6d4b\u5982\u679c\u662f Linux \u7cfb\u7edf\uff0c\u5219\u4ec0\u4e48\u8f6c\u6362\u90fd\u4e0d\u505a\uff0c\u56e0\u4e3a Linux \u7528\u6237\u51e0\u4e4e\u90fd\u662f UTF-8\uff0c\u90a3\u4e48 const char8_t * \u53ef\u4ee5\u5f3a\u8f6c\u4e3a const char * \u800c\u4e0d\u7528\u4efb\u4f55\u989d\u5916\u5f00\u9500\u3002 std::string to_os_string(std::string const &u8s) { #if _WIN32 // UTF-8 \u5230 ANSI return boost::locale::conv::from_utf(u8s, \"\"); #elif __linux__ // \u4e0d\u8f6c\u6362 return u8s; #else #error \"Unsupported system.\" #endif } \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u5b9e\u5728\u8981\u5b66\u7cdf\u7cd5\u7684\u5bbd\u5b57\u7b26\u6d41\uff0c\u90a3\u6211\u4e5f\u5949\u966a\u5230\u5e95\u3002 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring \u5728\u4ed6\u4eec\u770b\u6765\uff0c std::string \u662f\u5df2\u7ecf\u5e9f\u5f03\u7684\u3002\u4ed6\u4eec\u8ba4\u4e3a std::wstring \u624d\u662f\u771f\u6b63\u8de8\u5e73\u53f0\uff0c\u8de8\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u3002 std::wstring : \u5b57\u7b26\u4e32 std::string : \u5b57\u8282\u6570\u7ec4 std::wifstream : \u6587\u672c\u6d41 std::ifstream : \u4e8c\u8fdb\u5236\u6d41 \u770b\u8d77\u6765\u53ea\u8981\u5168\u90e8\u7edf\u4e00 wchar_t \u5c31\u80fd\u5b9e\u73b0\u8de8\u5e73\u53f0\u4e86\uff1f\u662f\u7684\uff0c\u9664\u4e86 Windows\u2026\u2026 \u6807\u51c6\u8ba4\u4e3a wchar_t \u5e94\u8be5\u5305\u542b 0 \u5230 0x10FFFF \u7684\u6240\u6709\u7684 Unicode \u5b57\u7b26\u7801\u70b9\uff0c\u9700\u8981\u662f 32 \u4f4d\u7684\u3002\u7136\u800c Windows \u7684 wchar_t \u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u662f 16 \u4f4d\u7684\uff0c\u9700\u8981\u7528\u4ee3\u7406\u5bf9\u624d\u80fd\u8868\u793a\u7a00\u6709\u5b57\u7b26\uff0c\u5e76\u4e0d\u80fd\u4e00\u4e2a wchar_t \u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002\u8fd9\u5bfc\u81f4\u5373\u4f7f\u7528\u4e86 wchar_t \u8fd8\u662f\u5b58\u5728\u8de8\u5e73\u53f0\u56f0\u96be\u7684\u95ee\u9898\uff1a\u4e00\u4e2a Linux \u7a0b\u5e8f\u7528 wchar_t \u53ef\u80fd\u4f1a\u5229\u7528 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u7279\u6027\uff0c\u65b9\u4fbf\u4e86\u6587\u672c\u5904\u7406\uff0c\u4f46\u79fb\u690d\u5230 Windows \u65f6\uff0c\u53d1\u73b0\u53d8\u6210\u4e86 UTF-16\uff0c\u9700\u8981\u5bf9\u4ee3\u7406\u5bf9\u505a\u7279\u6b8a\u5224\u65ad\u2026\u2026\u6ca1\u6709\u6ee1\u8db3\u8de8\u5e73\u53f0\u7684\u521d\u8877\uff0c\u4e5f\u505a\u4e0d\u5230\u5b9a\u957f\u7f16\u7801\u3002 char32_t \u505a\u5230\u4e86\u8de8\u5e73\u53f0\u7684 UTF-32\uff0c\u4e5f\u80fd\u5bb9\u7eb3\u5168\u90e8 Unicode \u7801\u70b9\uff0c\u53ef\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::to_wstring \uff0c\u5374\u6839\u672c\u6ca1\u6709 std::to_u32string \uff1b\u63d0\u4f9b\u4e86 std::wcout \uff0c\u5374\u6ca1\u6709\u63d0\u4f9b std::u32cout \u2026\u2026 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5bbd\u5b57\u7b26\u6d41\u5f88\u7cdf\u7cd5\uff0c\u8bf4\u662f\u8de8\u5e73\u53f0\uff0c\u8de8\u4e86\u4e2a\u5bc2\u5bde\u3002 wchar_t \u7cfb\u5217\u51fd\u6570 TODO std::wcout \u7684\u4f7f\u7528 TODO std::wfstream \u7684\u4f7f\u7528 TODO C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 TODO C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 TODO Windows \u4e13\u9898 \u9047\u5230\u5b57\u7b26\u7f16\u7801\u96be\u9898\u7684\uff0c\u4e3b\u8981\u662f Windows \u7a0b\u5e8f\u5458\u3002 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 \u4ece Windows NT \u7248\u672c\u5f00\u59cb\uff0c\u5bf9\u4e8e\u6240\u6709\u6d89\u53ca\u5b57\u7b26\u4e32\u7684\uff0c\u5176\u64cd\u4f5c\u7cfb\u7edf API \u63d0\u4f9b\u4e86\u4e24\u5957\u51fd\u6570\u3002 \u4e00\u5957\u662f A \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 A \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileA \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 ANSI\uff08\u5373 GBK\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u53e6\u4e00\u5957\u662f W \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 W \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileW \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 Unicode\uff08\u5373 UTF-16\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u5176\u4e2d CreateFileW \u624d\u662f Windows \u7cfb\u7edf\u771f\u6b63\u7684 API\u3002 \u800c CreateFileA \u662f\u4e3a\u4e86\u517c\u5bb9\u57fa\u4e8e ANSI \u7684\u8001\u7a0b\u5e8f. \u7531\u4e8e ANSI \u5728\u4e0d\u540c\u5730\u533a\u4f1a\u53d8\u5f97\u4e0d\u540c\uff0c\u4f7f\u7528\u8fd9\u7c7b\u51fd\u6570\u5199\u51fa\u7684\u7a0b\u5e8f\u4e0d\u5177\u6709\u56fd\u9645\u901a\u7528\u6027\u3002 \u5176\u5185\u90e8\u7684\u5b9e\u73b0\u53ea\u662f\u7b80\u7b80\u5355\u5355\u5730\u7ed9 const char * \u505a\u4e2a\u8f6c\u6362\uff0c\u4ece GBK \u8f6c\u5230 UTF-16\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528 CreateFileW\u3002 HANDLE CreateFileA(const char *lpFileName) { return CreateFileW(gbk_to_utf16(lpFileName)); } TCHAR \u6d41\u6d3e \u9664\u4e86\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b8f\uff0c\u8fd9\u4e2a\u5b8f\u6ca1\u6709\u4efb\u4f55\u540e\u7f00\uff0c\u4f8b\u5982 CreateFile \u3002 \u5176\u5b9a\u4e49\u5982\u4e0b\uff1a #ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif \u8fd9\u6837\u505a\u7684\u521d\u8877\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u53ef\u4ee5\u5199\u51fa\u4e00\u5957\u9488\u5bf9 MessageBox \u7684\u4ee3\u7801\u3002 \u5f53\u8001\u677f\u60f3\u8981\u57fa\u4e8e Unicode \u65f6\uff0c\u4ed6\u5c31 #define UNICODE \uff0c\u8fd9\u6837 MessageBox \u5c31\u53d8\u6210\u4e86 MessageBoxW \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u53d8\u6210 Unicode \u7684\uff0c\u56fd\u9645\u901a\u7528\u3002 \u5f53\u52b3\u4fdd\u8001\u677f\u60f3\u8981\u57fa\u4e8e ANSI \u65f6\uff0c\u4ed6\u5c31\u4e0d\u5b9a\u4e49 UNICODE \u5b8f\uff0c\u8fd9\u6837\u6240\u6709\u7684 MessageBox \u53c8\u53d8\u56de\u4e86 MessageBoxA \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u53c8\u53d8\u6210 ANSI \u7684\u4e86\u3002 \u6240\u6709\u6709 A/W \u533a\u5206\u7684\u7684 Windows API \u90fd\u6709\u8fd9\u6837\u4e00\u4e2a\u5b8f\uff0c\u6839\u636e UNICODE \u5b8f\u662f\u5426\u5b9a\u4e49\uff0c\u51b3\u5b9a\u91c7\u53d6\u54ea\u5957 API\u3002 \u6211\u4eec\u4f1f\u5927\u7684 Linux \u7cfb\u7edf\u5c31\u6ca1\u6709\u8fd9\u4e2a\u82e6\u607c\uff0c\u65e9\u5c31\u7edf\u4e00 UTF-8 \u4e86\u3002 \u9664\u6b64\u4e4b\u5916\uff0c \u4e2d\u8fd8\u5b9a\u4e49\u4e86 TEXT \u8fd9\u4e2a\u5b8f\u51fd\u6570\u3002 #ifdef UNICODE #define TEXT(s) L##s #else #define TEXT(s) s #endif \u7528\u6cd5\uff1a\u8981\u6c42\u7a0b\u5e8f\u5458\u628a\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u90fd\u7528 TEXT \u5b8f\u5305\u88f9\u3002 Qt \u4e5f\u6709\u7c7b\u4f3c\u7684\u5b8f\u5305\u88f9\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u505a\u6cd5\uff0c tr \uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u89e3\u51b3\u7f16\u7801\u95ee\u9898\uff0c\u800c\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u8bed\u8a00\u7ffb\u8bd1\u95ee\u9898\uff0c\u7a0d\u540e\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u4e00\u4e0b Qt \u4e2d\u7684\u5b57\u7b26\u4e32\u3002 \u5f53 UNICODE \u5b8f\u5b9a\u4e49\u65f6\uff0c TEXT \u4f1a\u81ea\u52a8\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u6dfb\u52a0 L \u524d\u7f00\uff0c\u4f7f\u5f97\u5b57\u7b26\u4e32\u53d8\u6210 Unicode \u7684\u3002\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\uff0c\u5219\u53c8\u53d8\u56de ANSI \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff08\u8ddf\u968f\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\u7684\u8bbe\u5b9a\uff09\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u6bb5\u4ee3\u7801\uff1a #include int main() { MessageBox(NULL, TEXT(\"\u4f60\u597d\uff0c\u4e16\u754c\"), TEXT(\"\u6807\u9898\"), MB_OK); } \u5f53\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u6807\u9898\", MB_OK); } \u5f53\u6ca1\u6709\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u6807\u9898\", MB_OK); } \u6b64\u5916\uff0c\u8fd8\u5b9a\u4e49\u4e86 TCHAR \u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u540c\u6837\u662f\u9488\u5bf9\u662f\u5426\u5b9a\u4e49 UNICODE \u5b8f\u800c\u5b9a\u4e49\u4e86\u4e24\u5957\u7248\u672c\u3002 #ifdef UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif \u8fd8\u4e3a printf \u548c wprintf \u5b9a\u4e49\u4e86 TCHAR \u7248\u672c\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709 strlen \u548c wcslen \uff0c strcpy \u548c wcscpy \uff0c\u7b49\u7b49\u3002 #ifdef UNICODE #define _tprintf wprintf #define _tcscpy wcscpy #define _tcslen wcslen #else #define _tprintf printf #define _tcscpy strcpy #define _tcslen strlen #endif \u4e0d\u89c9\u5f97\u8fd9\u5f88\u9177\u5417\uff1f\u5f88\u7b26\u5408\u6211\u5bf9\u5f3a\u8feb\u75c7\u7684\u60f3\u8c61\uff0c\u79d1\u6280\u5e76\u4e14\u5e26\u7740\u81ed\u5473\u3002 int main() { TCHAR str[] = TEXT(\"\u6bd4\u5c14\u76d6\u5b50\u6211\u6d4b\u8bd5\u4f60\u7684\u7801\"); } (* \u54e6\uff0c\u6211\u662f\u8bf4\uff0c\u6211\u8981\u6d4b\u8bd5\u4f60\u7684\u7f16\u7801\u683c\u5f0f *) \u9700\u8981\u5207\u6362\u65f6\uff0c\u5728 MSVC \u4e2d\uff0c\u6253\u5f00\u6216\u5173\u95ed /DUNICODE \u7f16\u8bd1\u9009\u9879\u5373\u53ef\u3002 \u4e0d\u8981\u89c9\u5f97\u8fd9\u662f\u4ec0\u4e48\u597d\u4e3b\u610f\uff0c\u8fd9\u6837\u505a\u7684\u540e\u679c\u662f\uff0c\u4f60\u5199\u51fa\u7684\u4ee3\u7801\u53ea\u80fd\u5728 Windows \u4e0b\u7f16\u8bd1\u3002 \u5199\u8d77\u6765\u7d2f\u6b7b\u4eba\uff0c\u5b9e\u9645\u54ea\u6709\u90a3\u4e48\u591a\u4e00\u76f4\u5728 ANSI \u548c Unicode \u4e4b\u95f4\u6765\u56de\u5207\u6362\u7684\u9700\u6c42\uff1f \u6211\u7684\u5efa\u8bae\u662f\uff0c\u7edf\u4e00 wchar_t \uff0c\u7edf\u4e00\u5168\u7528 W \u51fd\u6570\uff0c\u618b\u62a0\u62a0\u7d22\u7d22\u7684\u534a\u8fdb\u534a\u9000\u3002 UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f \u4e4b\u524d\u8bf4\u8fc7\u4e86\uff0cWindows \u5e73\u53f0\u5230\u5904\u90fd\u9ed8\u8ba4 GBK \u975e\u5e38\u9ebb\u70e6\uff0c\u8981\u5207\u6362\u5230 UTF-8 \u5de5\u4f5c\u6d41\uff1a \u7f16\u8bd1\u5668\u5f00\u542f /utf-8 \u9009\u9879 \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c system(\"chcp 65001\") \u8bbe\u7f6e\u6587\u4ef6\u7cfb\u7edf\u5b57\u7b26\u4e32\u7f16\u7801\uff0c setlocale(LC_ALL, \".utf-8\") // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 SetConsoleCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u5165\u7f16\u7801\uff0c\u7528\u4e8e std::cin #elif __unix__ // \u53cd\u6b63 Unix \u7cfb\u7edf\u9ed8\u8ba4\u90fd\u662f UTF-8\uff0c\u4e0d\u8bbe\u7f6e\u4e5f\u884c\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u5168\u5c40 locale \u662f\u4e3a\u4e86\u8ba9 iswspace \u63a5\u53d7\u5168\u89d2\u7a7a\u683c\u3001iswpunct \u63a5\u53d7\u5168\u89d2\u9017\u53f7 L'\uff0c' \u7b49 //setlocale(LC_ALL, \"zh_CN.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u4e2d\u6587\u672c\u5730\u5316\uff0c\u53ef\u4f7f strerror \u8f93\u51fa\u4e2d\u6587\uff08\u4f46\u7528\u6237\u5fc5\u987b locale-gen \u8fc7\u4e2d\u6587\uff01\uff09 //setlocale(LC_ALL, \"C.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u8bed\u8a00\u4e2d\u6027 locale\uff0c\u53ea\u5f71\u54cd iswspace\u3001iswpunct \u7b49\u51fd\u6570\uff0c\u4e0d\u4f1a\u4f7f strerror \u7b49\u8f93\u51fa\u4e2d\u6587 setlocale(LC_ALL, \".utf-8\"); // \u82e5\u4e0d\u5e26\u4efb\u4f55\u524d\u7f00\uff08\u63a8\u8350\uff09\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8bed\u8a00 $LANG\uff0c\u4f7f strerror \u81ea\u52a8\u9002\u5e94 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 std::wcout << L\"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u4f60\u90fd\u7edf\u4e00 UTF-8 \u4e86\uff0c\u8fd9\u7834 UTF-16 \u548c UTF-32 \u4e4b\u95f4\u6765\u56de\u8df3\u7684\u7834 wchar_t \u5c31\u522b\u7528\u4e86\u5457\uff01 return 0; } WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == false \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::cout << char(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f GBK \u7f16\u7801\u7684 char \u5e8f\u5217 // \u5982\u679c\u662f\u4e2d\u6587\u5b57\u7b26\uff0cWndProc(WM_CHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u5b57\u8282\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u5df1\u5224\u65ad\u548c\u62fc\u63a5 GBK \u5b57\u7b26\u4e32 return 0; case WM_UNICHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == true \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::wcout << wchar_t(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f UTF-16 \u7f16\u7801\u7684 wchar_t \u5e8f\u5217 // \u5982\u679c\u662f\u4ee3\u7406\u5bf9\uff0cWndProc(WM_UNICHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u7801\u4f4d\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u884c\u628a\u4ee3\u7406\u5bf9\u7ec4\u88c5\u6210\u5b8c\u6574\u7684 Unicode \u7801\u70b9 return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } } \u628a WndProc \u7684\u8f93\u5165\u5b58\u5165 std::u32string \u7684\u6848\u4f8b\uff1a std::u32string result; std::string ansi_buf; std::wstring utf16_buf; std::optional try_ansi_to_utf32(std::string const &s) { try { return boost::locale::conv::to_utf(s, \"\"); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } std::optional try_utf16_to_utf32(std::wstring const &s) { try { return boost::locale::conv::utf_to_utf(s); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: ansi_buf.push_back(char(wParam)); if (auto u = try_ansi_to_utf32(ansi_buf)) { result += u.value(); ansi_buf.clear(); } return 0; case WM_UNICHAR: utf16_buf.push_back(wchar_t(wParam)); if (auto u = try_utf16_to_utf32(utf16_buf)) { result += u.value(); utf16_buf.clear(); } return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); }; \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 \u6807\u51c6\u5e93\u7684 std::string \u6211\u4eec\u4e0d\u518d\u8d58\u8ff0\uff0c\u521a\u624d\u5728\u201c\u5bbd\u5b57\u7b26\u6d41\u201d\u4e2d\u4e5f\u4ecb\u7ecd\u4e86\u5b98\u65b9\u7684\u60f3\u6cd5\uff0c\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u4e13\u9898\u8bfe\u4f1a\u7ee7\u7eed\u8be6\u89e3\u3002 \u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u5173\u4e8e\u5b57\u7b26\u7f16\u7801\u7684\u95ee\u9898\uff0c\u63a2\u7d22\u5404\u5927\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u548c\u5e93\uff0c\u662f\u5982\u4f55\u5c01\u88c5\u5b57\u7b26\u4e32\u7c7b\uff0c\u5982\u4f55\u89e3\u51b3 UTF-8 \u53d8\u957f\u7f16\u7801\uff0cUTF-32 \u538b\u7f29\u7387\u4f4e\u7684\u95ee\u9898\u7684\uff0c\u5e0c\u671b\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u63d0\u4f9b\u4e00\u70b9\u7075\u611f\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u4e00\u4e2a\u597d\u7684\u5e93\u6216\u8bed\u8a00\uff0c\u90fd\u8981\u660e\u786e\u533a\u5206\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u6982\u5ff5\uff0c\u524d\u8005\u662f\u6587\u672c\u5185\u5bb9\uff0c\u540e\u8005\u662f\u7eaf\u4e8c\u8fdb\u5236\u5185\u5bb9\u3002 \u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u201c\u7f16\u7801\u201d\u5f97\u5230\u7eaf\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u800c\u5b57\u8282\u6570\u7ec4\u53ef\u4ee5\u201c\u89e3\u7801\u201d\u5f97\u5230\u539f\u59cb\u5b57\u7b26\u4e32\u3002 \u65e9\u671f\u7684 C \u8bed\u8a00\u5c31\u662f\u56e0\u4e3a\u628a\u5b57\u7b26\u548c\u5b57\u8282\u6df7\u4e3a\u4e00\u8c08\uff0c\u90fd\u4f7f\u7528\u4e86 char \u7c7b\u578b\uff0c\u624d\u4ea7\u751f\u4e86\u540e\u6765\u8fd9\u4e48\u591a\u4e71\u8c61\u3002\u540e\u6765\u901a\u8fc7\u6253\u8865\u4e01\u6253\u4e0a\u771f\u6b63\u7684\u5b57\u7b26 wchar_t \uff0c\u5374\u6ca1\u4ec0\u4e48\u4eba\u7528\uff0c\u800c\u4e14\u8fd8\u88ab Windows \u641e\u6210 16 \u4f4d\uff0c\u53cd\u800c\u4e0d\u8de8\u5e73\u53f0\u4e86\u3002 \u6b64\u5904\u5148\u5217\u4e00\u4e2a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u773c\u4e2d\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u8868\uff0c\u65b9\u4fbf\u4f60\u7406\u89e3\u3002 \u8bed\u8a00 \u5b57\u7b26 \u5b57\u7b26\u4e32 \u6587\u672c\u6d41 \u5b57\u8282\u6570\u7ec4 \u4e8c\u8fdb\u5236\u6d41 \u7f16\u7801/\u89e3\u7801 C char wchar_t * FILE * + fgetwc char * FILE * + fgetc wcstomb / mbstowc C++ wchar_t std::wstring std::wistream std::string / std::vector std::istream std::codecvt Qt QChar QString QTextStream QByteArray QDataStream QTextCodec Python3 str str open('r') bytes open('rb') str.encode() Python2 unicode unicode \u65e0 str open('r') unicode.encode() Java Character String Reader byte[] InputStream Charset.encode C# char string StreamReader byte[] Stream Encoding Rust char String BufRead u8 Read str::from_utf8 JS char String ReadableStream Uint8Array ReadableStream TextEncoder Go rune string Reader byte Reader utf8.DecodeRune PHP string string fopen string fopen mb_convert_encoding Swift Character String String.UnicodeScalarView UInt8 Data String.Encoding Kotlin Char String Reader ByteArray InputStream Charset.encode Obj-C unichar NSString NSInputStream uint8_t NSInputStream NSStringEncoding Lua integer table \u65e0 string io.open require'utf8' \u672c\u8bfe\u4e0d\u4f1a\u8bb2\u89e3\u8fd9\u4e9b\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u5177\u4f53\u7528\u6cd5\uff0c\u53ea\u63d0\u4f9b\u4e00\u4e9b\u6982\u5ff5\uff0c\u8ba9\u4f60\u77e5\u9053\u5927\u5bb6\u90fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u89e6\u7c7b\u65c1\u901a\u3002 Lua \u4e2d UTF-32 \u7684\u5b9a\u957f\u7f16\u7801\uff0c\u8981\u8fd9\u6837\u5b9e\u73b0\uff1a {80, 101, 110, 103} \u3002\u800c\u4ed6\u6240\u8c13\u7684 utf8 \u5e93\uff0c\u5c31\u662f\u8d1f\u8d23\u628a Lua \u81ea\u5df1\u7684 string \u5047\u8bbe\u4e3a utf8 \u7f16\u7801\uff0c\u89e3\u7801\u51fa\u4e00\u4e2a\u4e2a Unicode \u7801\u70b9\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u7ec4\u3002\u4ed6\u751a\u81f3\u4e0d\u662f Lua \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u662f\u4e2a\u7b2c\u4e09\u65b9\u7684\u5e93\uff0c\u8fd8\u662f\u9700\u8981\u7f16\u8bd1\u7684 C \u8bed\u8a00 .so \u6587\u4ef6\u3002\u8fd8\u6709 Lua \u5b5d\u5b50\u501f\u6b64\u786c\u8bf4\u6211\u4eec Lua \u662f\u5929\u751f UTF-8\uff01\u7136\u800c\u62ff\u7740\u4e2a UTF-8 \u7f16\u7801\u7684\u201c\u5b57\u8282\u6570\u7ec4\u201d string \u6765\u6253\u5f00 io.open \u6587\u4ef6\uff0c\u5c31\u4f1a\u62a5\u9519\u627e\u4e0d\u5230\u6587\u4ef6\uff08\u56e0\u4e3a\u4e2d\u56fd\u533a Windows \u7684 GBK\uff09\uff0c\u662f\u4e0d\u662f\u5f88\u597d\u7b11\u5462\uff1f Qt QString Qt \u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 Qt \u7684\u5b57\u8282\u6570\u7ec4\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 \u5b83\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u9645\u4e0a\u662f\u4e2a QChar \u6570\u7ec4\uff0c\u800c QChar \u662f unsigned short \uff0c\u5373 16 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u4e5f\u5c31\u662f UTF-16 \u7f16\u7801\u7684\u7801\u4f4d\u3002 QString str = \"\u4f60\u597d\uff0c\u4e16\u754c\"; // str.size() = 5 // str[0] = QChar(0x4f60) = u'\u4f60' // str[1] = QChar(0x597d) = u'\u597d' // str[2] = QChar(0xff0c) = u'\uff0c' // str[3] = QChar(0x4e16) = u'\u4e16' // str[4] = QChar(0x754c) = u'\u754c' \u53ef\u89c1\uff0c QString \u662f UTF-16 \u7f16\u7801\u7684\uff0c\u5c31\u548c Java \u4e00\u6837\uff0cQt \u4e5f\u662f UTF-16 \u6f6e\u7684\u53d7\u5bb3\u8005\u3002 \u6240\u4ee5\uff0cQString \u4e5f\u5b58\u5728\u7740\u4ee3\u7406\u5bf9\u53d8\u957f\u7f16\u7801\u7684\u95ee\u9898\u3002\u4f46\u81f3\u5c11\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5b57\u7b26\u6765\u8bf4\uff0c\u4e00\u4e2a 16 \u4f4d\u7684 QChar \u90fd\u5bb9\u7eb3\u7684\u4e0b\u4e86\u3002 QTextCodec Qt \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7f16\u7801\u8f6c\u6362\u51fd\u6570\uff0c\u7528\u4e8e\u5c06 QString \u8f6c\u6362\u6210 QByteArray \uff0c\u6216\u8005\u4ece QByteArray \u8f6c\u6362\u6210 QString \uff0c\u8fd9\u4e9b\u51fd\u6570\u540d\u90fd\u662f\u4ee5 to \u6216\u8005 from \u5f00\u5934\u7684\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u540d\uff0c\u4f8b\u5982 fromUtf8 \u3001 toUtf8 \u3001 toLocal8Bits \u3002 \u8fd9\u4e9b\u51fd\u6570\u7684\u5185\u90e8\uff0c\u90fd\u662f\u8c03\u7528 QTextCodec \u7c7b\u5b9e\u73b0\u7684\u8f6c\u6362\u3002 QTextCodec \u662f Qt \u7528\u4e8e\u5904\u7406\u5404\u79cd\u6587\u672c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u7684\u7c7b\u3002\u5b83\u7684\u9759\u6001\u65b9\u6cd5 codecForLocale \u8fd4\u56de\u4e86\u5f53\u524d\u7cfb\u7edf\u7684\u7f16\u7801\uff0c toUnicode \u548c fromUnicode \u5206\u522b\u662f\u5c06 QByteArray \u8f6c\u6362\u6210 QString \uff0c\u6216\u8005\u5c06 QString \u8f6c\u6362\u6210 QByteArray \u3002 QTextCodec *codec = QTextCodec::codecForLocale(); // \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7f16\u7801 QByteArray bytes = codec->fromUnicode(str); // \u5c06 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u5373 char[] QString str = codec->toUnicode(bytes); // \u5c06 QByteArray \u8f6c\u6362\u6210 QString QTextCodec \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u8f6c\u6362\u63a5\u53e3\uff0c\u4f8b\u5982 fromUnicode \u9664\u4e86\u63a5\u53d7 QString \uff0c\u8fd8\u63a5\u53d7 QChar \u6570\u7ec4\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f6c\u6362\u8303\u56f4\u3002 from/toLocal8Bits/Utf8/Latin1/Ascii \u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cQt \u5c01\u88c5\u4e86\u4e00\u4e9b\u5e38\u7528\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u8fd9\u6837\u4f60\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a QTextCodec\u3002\u90fd\u662f to \u548c from \u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u7684\u540d\u79f0\u3002 Local8Bits \u8868\u793a\u8fd0\u884c\u65f6\u68c0\u6d4b\u5230\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u5ba2\u6237\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 QByteArray bytes = str.toLocal8Bits(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801 QString::fromLocal8Bits(bytes); // \u518d\u4ece\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForLocale(); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Utf8 \u8868\u793a\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002 QByteArray bytes = str.toUtf8(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 UTF-8 \u7f16\u7801 QString::fromUtf8(bytes); // \u518d\u4ece UTF-8 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Latin1 \u8868\u793a ISO-8859-1 \u7f16\u7801\uff0c\u53c8\u79f0\u4e3a\u897f\u6b27\u7f16\u7801\uff0c\u5b83\u662f\u4e00\u4e2a\u5355\u5b57\u8282\u7f16\u7801\uff0c\u548c ASCII \u7f16\u7801\u76f8\u4f3c\uff0c\u4f46\u662f\u591a\u4e86 128-255 \u7684\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u6cd5\u8bed\u3001\u5fb7\u8bed\u3001\u897f\u73ed\u7259\u8bed\u3001\u8461\u8404\u7259\u8bed\u7b49\u5b57\u7b26\u3002 QByteArray bytes = str.toLatin1(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 Latin1 \u7f16\u7801 QString::fromLatin1(bytes); // \u518d\u4ece Latin1 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ISO-8859-1\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Ascii \u8868\u793a ASCII \u7f16\u7801\uff0c\u548c Latin1 \u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u65e0\u6cd5\u5904\u7406 128-255 \u8fd9\u4e00\u6bb5\u7684\u5b57\u7b26\u3002 QByteArray bytes = str.toAscii(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 ASCII \u7f16\u7801 QString::fromAscii(bytes); // \u518d\u4ece ASCII \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ASCII\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); \u5b57\u7b26\u4e32\u5e38\u91cf QString str = QStringLiterial(\"\u4f60\u597d\uff0c\u4e16\u754c\"); QStringLiterial \u53ef\u4ee5\u4fdd\u8bc1\uff0c\u8f6c\u6362\u65f6\u91c7\u7528\u7684\u662f\u6240\u8c13\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\uff08\u5b9e\u9645\u5e94\u8be5\u53eb\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\uff09\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5f00\u53d1\u8005\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u662f\u7f16\u8bd1\u671f\u786e\u5b9a\u7684\u3002\u800c\u5982\u679c\u5199 QString::fromLocal8Bits(\"\") \u5c31\u53d8\u6210 \u201cANSI\u201d\uff0c\u5ba2\u6237\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u4e86\u3002\u8fd9\u4e24\u4e2a\u5b57\u7b26\u7f16\u7801\uff0c\u6bd4\u5982\u5728\u4e4b\u524d\u8de8\u56fd galgame \u7684\u6848\u4f8b\u4e2d\uff0c\u5c31\u662f\u4e0d\u540c\u7684\u3002 QTextStream QTextStream \u662f Qt \u63d0\u4f9b\u7684\u6587\u672c\u6d41\u7c7b\uff08\u5e26\u6709\u7f13\u51b2\uff09\uff0c\u5b83\u53ef\u4ee5\u5c06\u6587\u672c\u5199\u5165\u5230\u6587\u4ef6\u3001\u5957\u63a5\u5b57\u3001\u6807\u51c6\u8f93\u51fa\u7b49\u8bbe\u5907\u3002 \u6587\u672c\u6d41\u548c\u4e8c\u8fdb\u5236\u6d41\u4e0d\u540c\uff0c\u4ed6\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u8fc7 QTextStream \u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u3002 \u5f53\u4f60\u5f80 QTextStream \u5199\u5165 QString \u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7f16\u7801\u683c\u5f0f\u81ea\u52a8\u8f6c\u6362\u4e3a QByteArray \uff0c\u7136\u540e\u624d\u5199\u5165\u3002 \u8bfb\u53d6\u65f6\u4e5f\u540c\u7406\uff0c\u4f1a\u628a\u8bfb\u5230\u7684 QByteArray \u901a\u8fc7\u7f16\u7801\u683c\u5f0f\u89e3\u7801\uff0c\u5f97\u5230 QString \u5b57\u7b26\u4e32\u3002 Qt \u503c\u5f97\u79f0\u9053\u7684\u4e00\u70b9\u662f\uff1a\u4ed6\u628a\u6587\u4ef6\u548c\u6587\u4ef6\u6d41\u533a\u5206\u5f00\u6765\uff0c\u6587\u4ef6 QFile \u53ea\u9700\u8981\u8d1f\u8d23\u6253\u5f00\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff1b\u800c\u6587\u672c\u6d41 QTextStream \u624d\u771f\u6b63\u8d1f\u8d23\u6570\u636e\u7684\u7f13\u51b2\uff0c\u89e3\u7801\u7b49\u64cd\u4f5c\u3002\u4f53\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u804c\u8d23\u5355\u4e00\u539f\u5219\u3002 QFile file(\"hello.txt\"); file.open(QIODevice::ReadWrite); QTextStream stream(&file); stream.setCodec(\"UTF-8\"); // \u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f stream << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u5199\u5165 QString\uff0cQTextStream \u4f1a\u81ea\u52a8\u5c06\u5176\u7528 UTF-8 \u7f16\u7801\u4e3a QByteArray \u540e\u5199\u5165 QFile \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u76f4\u63a5\u5199\u5165\u4e8c\u8fdb\u5236\u7684 QByteArray \uff0cQt \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a QDataStream \u7c7b\uff0c\u65b9\u4fbf\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002 \u5982\u6b64\u628a\u4e8c\u8fdb\u5236\u548c\u6587\u672c\u4e25\u683c\u533a\u5206\uff0c\u800c\u4e0d\u662f\u50cf\u53e4\u4ee3 C \u8bed\u8a00\u90a3\u6837\u5b57\u8282\u4e0e\u5b57\u7b26\u6df7\u6dc6\u4e0d\u6e05\uff0c\u5168\u7528\u7cdf\u7cd5\u7684 char \u6765\u8868\u793a\uff0c\u5145\u5206\u4f53\u73b0\u4e86\u5f3a\u7c7b\u578b\u7684\u5b89\u5168\u3002 Python 3 str \u5982\u4f55\u89e3\u51b3 UTF-32 \u5360\u7528\u7a7a\u95f4\u5927\u7684\u75db\u70b9\uff1fPython \u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u7edd\u5bf9\u503c\u5f97\u4e00\u770b\uff01 \u4e3a\u4e86\u65b9\u4fbf\u6587\u672c\u5904\u7406\uff0c\u6700\u597d\u4ee5\u5b9a\u957f\u7f16\u7801\uff0c\u4e5f\u5c31\u662f UTF-32\uff0c\u5b58\u50a8\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u7528 UTF-8 \u6216 UTF-16 \u6765\u5b58\u50a8\u7684\u8bdd\uff0c\u4f1a\u9047\u5230\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff1a \u4f8b\u5982\u50cf\u5b57\u7b26\u4e32\u7d22\u5f15\uff0c\u5b57\u7b26\u4e32\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\uff0c\u8981\u4e48\u7d22\u5f15\u51fa\u6765\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u4e86\uff1b\u8981\u4e48\u5c31\u9700\u8981 O(N) O(N) \u7684\u590d\u6742\u5ea6\uff0c\u9010\u4e00\u904d\u5386\u6bcf\u4e2a\u5b57\u8282\uff0c\u624d\u80fd\u786e\u5b9a\u771f\u6b63\u7684\u4f4d\u7f6e\uff1b\u54ea\u6015\u5168\u662f ASCII \u4e5f\u5f97\u8fd9\u4e48\u505a\uff0c\u56e0\u4e3a\u4e07\u4e00\u521a\u597d\u6709\u4e00\u4e2a\u662f\u4e2d\u6587\u5b57\u7b26\u5462\uff1f \u6240\u4ee5\uff0c\u5bf9\u4e8e\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684 Python \u6765\u8bf4\uff0cUTF-8 \u662f\u65e0\u6cd5\u63a5\u53d7\u7684\uff0c\u4f3c\u4e4e\u53ea\u80fd\u4ee5 UTF-32 \u6765\u5b58\u50a8\uff1f Python \u60f3\u5230\u4e86\u4e00\u4e2a\u5c0f\u5999\u62db\uff1a enum PyUnicodeType { PyUnicodeType_Latin1, PyUnicodeType_UCS2, PyUnicodeType_UCS4, }; struct PyUnicodeString { PyUnicodeType type; union { uint8_t *latin1; uint16_t *ucs2; uint32_t *ucs4; }; }; \u8fd9\u91cc\u7684 union \u662f\u4e00\u4e2a C \u8bed\u8a00\u7279\u6027\uff0c\u4ed6\u5141\u8bb8\u4f60\u628a\u591a\u4e2a\u6210\u5458\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u7a7a\u95f4\u3002\u901a\u5e38\u6765\u8bf4\u9700\u8981\u914d\u5408\u4e00\u4e2a enum \u8868\u793a\u7c7b\u578b\uff0c\u624d\u80fd\u5b89\u5168\u4f7f\u7528\u3002\u73b0\u4ee3 C++ \u7684 std::variant \u66f4\u5b89\u5168\u7684\u53d6\u4ee3\u4e86\u4ed6\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u5916\u6302\u4e00\u4e2a enum \u3002CPython \u89e3\u91ca\u5668\u56e0\u4e3a\u662f C \u8bed\u8a00\u5b9e\u73b0\uff0c\u53ea\u80fd\u7528 union \u6a21\u62df std::variant \u7684\u6548\u679c\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFF \u7684\u5b57\u7b26\uff08Latin-1 \u8303\u56f4\uff09\u65f6\uff0c\u5b9e\u9645\u4e0a\u6ca1\u5fc5\u8981\u5168\u7528\u8d85\u5927\u7684 uint32_t \u6765\u5b58\u50a8\u3002\u5b8c\u5168\u53ef\u4ee5\u53ea\u53d6\u51fa\u4f4e 8 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a\u66f4\u7d27\u51d1\u7684 uint8_t \u6570\u7ec4\uff0c\u5c31\u50cf Latin-1 \u7f16\u7801\u4e00\u6837\u3002 \u4f46\u662f\u5f53\u6309\u7d22\u5f15\u8bfb\u53d6\u5143\u7d20\u65f6\uff0c\u4f1a\u5224\u65ad\u5f53\u524d union \u91cc\u88c5\u7684\u662f\u54ea\u79cd\u7c7b\u578b\uff0c\u5982\u679c\u662f Latin-1 \u7684\uff0c\u90a3\u5c31\u4f1a\u7528\u7ed3\u6784\u4f53\u91cc\u7684 uint8_t * \u6307\u9488\u6765\u7d22\u5f15\u3002 \u8fd9\u6837\uff0c\u5bf9\u4e8e\u5168 ASCII \u7684\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u8001\u8001\u5b9e\u5b9e\u5b58 UTF-32 \u5185\u5b58\u5360\u7528\u76f4\u63a5\u51cf\u5c11\u4e86 75%\uff01\u552f\u4e00\u7684\u4ee3\u4ef7\u662f\u6309\u7d22\u5f15\u8bfb\u5b57\u7b26\u5143\u7d20\u65f6\u9700\u8981\u505a\u4e2a if-else \u5224\u65ad\u3002\u540c\u65f6\u53c8\u4e0d\u5931\u53bb\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff08\u65e9\u671f Unicode \u8303\u56f4\uff09\u65f6\uff0c\u5219\u662f\u53d6\u51fa\u4f4e 16 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a uint16_t \u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u6848\u4e5f\u79f0\u4e3a UCS-2\u3002 \u6ce8\u610f UCS-2 \u5e76\u4e0d\u7b49\u540c\u4e8e UTF-16\uff0cUTF-16 \u662f\u80fd\u591f\u8868\u793a\u5b8c\u6574\u7684 Unicode \u7684\u53d8\u957f\u7f16\u7801\uff08\u6709\u4ee3\u7406\u5bf9\uff09\uff1b\u800c UCS-2 \u662f\u6ca1\u6709\u4ee3\u7406\u5bf9\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u7f3a\u70b9\u662f\u53ea\u80fd\u8868\u793a 0xFFFF \u7684\u8303\u56f4\u3002 \u5bf9\u4e8e\u5927\u90e8\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5185\u5b58\u5360\u7528\u5c31\u51cf\u5c11\u4e86 50%\uff0c\u4e5f\u4e0d\u4e8f\u3002 \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u201c\ud883\udede\u201d\u8fd9\u6837\u7684\uff0c\u8d85\u8fc7 0xFFFF \u7684\u5b57\u7b26\uff0c\u624d\u4f1a\u91c7\u7528 uint32_t \u8001\u8001\u5b9e\u5b9e\u5b58\u50a8\u771f\u6b63\u7684 UTF-32\uff0c\u53c8\u79f0 UCS-4\u3002 \u8fd9\u4e24\u79cd\u53eb\u6cd5\u662f\u7b49\u4ef7\u7684\uff0c\u53cd\u6b63 Unicode \u4ece\u6765\u6ca1\u6709\u8d85\u8fc7 0xFFFFFFFF \u7684\uff0c\u73b0\u5728\u4ed6\u4eec\u90fd\u662f\u5b9a\u957f\u7f16\u7801\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684\u865a\u62df\u673a\u8bed\u8a00\u6765\u8bf4\uff0c\u53cd\u6b63\u672c\u6765\u5c31\u4e0d\u5728\u4e4e if-else \u5206\u652f\u8fd9\u70b9\u5c0f\u5f00\u9500\uff0c\u8fd9\u786e\u5b9e\u662f\u6700\u597d\u7684\u65b9\u6848\u3002 \u7f3a\u70b9\u5c31\u662f\uff0c\u5f53\u4f60\u5f80\u4e00\u4e2a\u5b8c\u5168\u662f ASCII \u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u7a81\u7136\u63d2\u5165\u4e00\u4e2a\u201c\ud883\udede\u201d\u65f6\uff0c\u4f1a\u4ea7\u751f\u5de8\u5927\u7684\u5185\u5b58\u91cd\u5206\u914d\u3002\u867d\u7136\u53ea\u6709\u4e00\u4e2a\u201c\ud883\udede\u201d\uff0c\u4f46\u4e3a\u6b64\uff0c\u5176\u4ed6\u6240\u6709 ASCII \u5b57\u7b26\u90fd\u5f97\u4e3a\u4ed6\u6269\u5f20\u5230 32 \u4f4d\u7684\u5927\u5c0f\u3002\u800c UTF-8 \u548c\u4f20\u7edf\u7684 UTF-32 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4e5f\u4e0d\u5efa\u8bae C++ \u7a0b\u5e8f\u5458\u81ea\u5df1\u624b\u6413\u4e2a\u8fd9\u6837\u7684 union \u5b57\u7b26\u4e32\u3002 Rust &str \u548c String \u800c Rust \u5219\u91c7\u7528\u4e86\u5b57\u7b26\u4e32\u5168\u5458 UTF-8 \u7684\u7b56\u7565\uff0c\u8fd9\u662f\u56e0\u4e3a Rust \u6700\u5e38\u7528\u4e8e\u4e92\u8054\u7f51\u65b9\u9762\u7684\u5e95\u5c42\u7cfb\u7edf\u8f6f\u4ef6\uff0c\u4e92\u8054\u7f51\u6700\u5e38\u7528\u7684\u6587\u672c\u7f16\u7801\u5c31\u662f UTF-8\uff0c\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\uff0c\u4e14\u56fd\u9645\u901a\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4e92\u8054\u7f51\u57fa\u5efa\u6700\u5e38\u89c1\u7684\u5e73\u53f0\u5c31\u662f Linux\uff0c\u4f7f\u7528 UTF-8 \u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u8c03\u7528 Linux \u7cfb\u7edf API \u65e0\u9700\u4efb\u4f55\u8f6c\u6362\u3002\u4e14\u6587\u672c\u6587\u4ef6\u57fa\u672c\u90fd\u53ef\u4ee5\u5047\u5b9a\u662f UTF-8 \u7f16\u7801\uff0c\u5199\u5165\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u590d\u6742\u5ea6\u4f4e\u81f3 O(1) O(1) \u3002\u4f5c\u4e3a\u4ee3\u4ef7\uff0c\u8fd9\u5bfc\u81f4\u6587\u672c\u5904\u7406\u4e0a\u7684\u4e00\u4e9b\u56f0\u96be\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u9700\u8981\u533a\u5206\u662f\u6309\u5b57\u8282\u7d22\u5f15\u8fd8\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u5982\u679c\u786e\u5b9e\u9700\u8981\u6309\u5b57\u7b26\u7d22\u5f15\u7684\u8bdd\uff0c\u590d\u6742\u5ea6\u5c31\u4f1a\u662f O(N) O(N) \u4e86\u3002 \u65e0\u8bba\u5982\u4f55\uff0c\u5982\u679c\u4f60\u9009\u62e9\u4e86 UTF-8 \u6d41\u6d3e\u7684\u8bdd\uff0cRust \u5b57\u7b26\u4e32\u7684\u201c\u8fed\u4ee3\u5668\u53cc\u8f68\u5236\u201d\u786e\u5b9e\u503c\u5f97\u79f0\u9053\uff1a let s = \"\u4f60\u597d\uff0c\u4e16\u754c\"; for c in s.chars() { // \u6309\u5b57\u7b26\u8fed\u4ee3 println!(\"{}\", c); } for b in s.bytes() { // \u6309\u5b57\u8282\u8fed\u4ee3 println!(\"{:02X}\", b); // \u6253\u5370\uff1aE4 BD A0 E5 A5 BD EF BC 8C E4 B8 96 E7 95 8C } Java String Java \u4e5f\u662f UTF-16 \u7684\u53cc\u5b57\u8282\u7f16\u7801\u3002 \u4eae\u70b9\uff1a\u4ed6\u7684 String \u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u65e0\u6cd5\u5c31\u5730\u4fee\u6539\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u6bcf\u6b21\u4ea7\u751f\u4f60\u8c03\u7528 += \u7684\u90fd\u662f\u4e00\u4e2a\u65b0 String\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u3002 \u4e5f\u5c31\u662f\u8bf4\uff1aJava \u7684 String \u867d\u7136\u662f\u201c\u5806\u201d\u4e2d\u7684\u5bf9\u8c61\uff0c\u5374\u65e0\u6cd5\u4ee5\u5f15\u7528\u4f20\u9012\u3002 \u8fd9\u907f\u514d\u4e86\u4ee5\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff1a void registerStudent(String name) { name += \".txt\"; File file(name); file.write(...); } void myTransaction() { String name = \"\u5c0f\u5f6d\"; lib.registerStudent(name); office.registerStudent(name); // \u8fd9\u91cc name \u662f\u5426\u88ab\u4fee\u6539\uff1f } \u5982\u679c Java \u7684 String \u548c\u666e\u901a\u5bf9\u8c61\u4e00\u6837\uff0c\u88ab\u8c03\u7528\u8005\u7684\u4fee\u6539\u53ef\u4ee5\u5bf9\u5916\u90e8\u53ef\u89c1\uff0c\u90a3\u5c82\u4e0d\u662f\u6211\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u4ee5 String \u4e3a\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u90fd\u9700\u8981\u64cd\u5fc3\uff1a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0d\u4f1a\u628a\u6211\u7684\u5b57\u7b26\u4e32\u4fee\u6539\u6389\uff1f \u6240\u4ee5\uff0cJava \u7ed9\u4ed6\u7684\u5bf9\u8c61\u6a21\u578b\u5f00\u4e86\u4e2a\u540e\u95e8\uff1a\u89c4\u5b9a\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u6309\u5f15\u7528\u4f20\u9012\uff0c\u9664\u4e86 String \uff01\u5c31\u53ea\u6709 String \u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u88ab\u8c03\u7528\u8005\u5185\u90e8\u7684\u4fee\u6539\u5bf9\u5916\u4e0d\u53ef\u89c1\u3002 C++ \u548c Rust \u53ea\u9700\u8981\u52a0\u4e2a\u5e38\u5f15\u7528\u5c31\u884c\uff0c\u800c Java \u53d7\u5bb3\u8005\u8981\u8003\u8651\u7684\u5c31\u591a\u4e86\u3002 \u603b\u4e4b\uff0c\u8fd9\u5c31\u662f\u6ca1\u6709 const \u7684\u5783\u573e\u8bed\u8a00\u7684\u4e11\u6001\uff0c\u9700\u8981\u9760\u5404\u79cd\u8bed\u6cd5\u89c4\u5219\u4e0a\u5f00\u6d1e\u624d\u80fd\u5f25\u8865\u8bbe\u8ba1\u65f6\u8003\u8651\u4e0d\u5468\u7684\u7f3a\u9677\uff0c\u5c31\u4e3a\u4e86\u4f3a\u5019\u8fd9\u5e2e\u5f15\u7528\u90fd\u5f04\u4e0d\u660e\u767d\u7684\u5783\u573e\u5c0f\u767d\u3002 \u6211\u662f\u8bf4\u5783\u573e\u56de\u6536 (garbage-collect) \u8bed\u8a00\uff0c\u7b80\u79f0\u5783\u573e\u8bed\u8a00\u3002\u6ca1\u6709\u8bf4 Java \u5783\u573e\u7684\u610f\u601d\uff0c\u4f60\u8bf4\u5bf9\u5427\uff1f\u5783\u573e\u8bed\u8a00\u3002 COW \u5b57\u7b26\u4e32 \u62c5\u5fe7\uff1a\u90a3\u5c82\u4e0d\u662f\u6bcf\u6b21\u6211 += \u5b9e\u9645\u4e0a\u90fd\u767d\u767d\u6df1\u62f7\u8d1d\u4e86\u4e00\u4efd\u65b0\u7684 String \uff1f\u522b\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5177\u4f53\u5b9e\u73b0\u4e0a\uff0cJava \u7684 String \u5728\u5e95\u5c42\u91c7\u7528\u4e86\u548c Qt \u7684 QString \u4e00\u6837\u7684 COW \u673a\u5236\uff1a \u5f53\u4e00\u4e2a QString \u88ab\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5bf9\u5176\u4e2d\u7684 QByteArray \u8fdb\u884c\u6df1\u62f7\u8d1d\uff0c\u800c\u662f\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u3002\u53ea\u6709\u5f53\u5176\u4e2d\u4e00\u4e2a QString \u88ab += \u7b49\u5e26\u6709\u526f\u4f5c\u7528\u64cd\u4f5c\u4fee\u6539\u65f6\uff0c\u624d\u4f1a\u6df1\u62f7\u8d1d\u4e00\u4efd\u65b0\u7684\uff0c\u8ba9\u4f60\u4fee\u6539\u3002\u8fd9\u6837\u5927\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f00\u9500\u3002 COW \u5b57\u7b26\u4e32\u7684\u7f3a\u70b9\u662f\uff1a\u5f53\u4f60\u5199\u591a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u672c\u6765\u591a\u7ebf\u7a0b\u53ea\u8bfb\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5b89\u5168\u7684\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u6709 COW\uff0c\u8fde\u53ea\u8bfb\u8bbf\u95ee\u90fd\u4f1a\u4e0d\u5b89\u5168\u4e86\u3002\u4e4b\u540e\u6211\u4eec\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4f1a\u8be6\u7ec6\u5206\u6790\u8fd9\u662f\u4e3a\u4ec0\u4e48\u3002 \u5176\u5b9e\u65e9\u671f\u7684 C++98 \u4e2d\uff0c std::string \u4e5f\u91c7\u7528\u4e86 COW \u673a\u5236\uff0c\u4f46\u540e\u6765\u56e0\u4e3a\u4e0d\u7b26\u5408\u7ebf\u7a0b\u5b89\u5168\u8981\u6c42\uff0c\u88ab\u8ffd\u6c42\u591a\u7ebf\u7a0b\u7684 C++11 \u8d23\u4ee4\u6539\u6b63\uff0c\u624d\u6709\u4e86\u540e\u6765 std::__cxx11::basic_string \u7684 ABI \u4e0d\u517c\u5bb9\u95ee\u9898\u3002\u6bd5\u7adf\u7ebf\u7a0b\u4e0d\u5b89\u5168\u5b9e\u5728\u592a\u4f24\u4e86\uff0c\u57fa\u672c\u610f\u5473\u7740\u591a\u7ebf\u7a0b\u5c31\u6ca1\u6cd5\u5171\u4eab std::string \u3002\u4e8b\u5b9e\u4e0a\uff0cQt \u6240\u6709\u7684\u5bf9\u8c61 QObject \uff0c\u5305\u62ec QString \u5728\u591a\u7ebf\u7a0b\u4e2d\u4f20\u9012\u65f6\uff0c\u5c31\u9700\u8981\u8c03\u7528 moveTo(QThread) \u8f6c\u79fb\u6240\u6709\u6743\uff0c\u624d\u80fd\u5b89\u5168\u5730\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5c31\u662f\u56e0\u4e3a Qt \u5927\u91cf\u4f7f\u7528\u4e86 COW \u673a\u5236\u3002 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme TODO \u6b63\u89c4\u5316 TODO \u96f6\u5bbd\u7a7a\u683c TODO \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 TODO \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 TODO \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u662f\u4e00\u4e2a 8 \u4f4d\u7f16\u7801\uff0c\u80fd\u8868\u793a 256 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u62c9\u4e01\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u3001\u5e38\u7528\u7684\u897f\u6b27\u5b57\u7b26\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u628a\u4e00\u4e2a Latin-1 \u7f16\u7801\u7684 char \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a wchar_t \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u63a5\u53e3\u6784\u9020 std::wstring \uff0c\u8fd9\u6837 char \u4f1a\u88ab\u9010\u4e2a\u8f6c\u6362\u4e3a wchar_t \u3002 std::string latin1 = \"I love P\\xE9ng\"; // 0xE9: \u00e9 std::wstring wstr(latin1.begin(), latin1.end()); std::wcout << wstr << '\\n'; \u8f93\u51fa\uff1a I love P\u00e9ng \u5e76\u4e0d\u6807\u51c6\u7684\u505a\u6cd5\uff0c\u8fd8\u662f\u5efa\u8bae\u7528 boost::locale::conv::to_utf(latin1, \"Latin-1\") \u3002 Latin-1 \u7684\u5999\u7528 \u7531\u4e8e Latin-1 \u8986\u76d6\u4e86\u6240\u6709\u7684 256 \u4e2a char \u7684\u53ef\u80fd\u503c\uff0c\u4efb\u4f55\u5b57\u8282\u6d41\u90fd\u53ef\u4ee5\u6210\u529f\u89e3\u7801\u3002 GBK \u548c UTF-8 \u6709\u81ea\u7ea0\u9519\u6027\uff0c\u6709\u4e9b\u8f93\u5165\u4f1a\u88ab\u584c\u7f29\u6210\u9519\u8bef\u201c\ufffd\u201d\u3002Latin-1 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u7167\u5355\u5168\u6536\uff01 \u56e0\u6b64\u6709\u65f6\uff0c\u4eba\u4eec\u53ef\u4ee5\u6b3a\u9a97\u4e00\u4e2a\u7f16\u7801\u5668\u8bf4\u201c\u6211\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u662f Latin-1\u201d\uff01\u8fd9\u6837\u7f16\u7801\u5668\u5c31\u4e0d\u4f1a\u5bf9\u8f93\u5165\u7684\u5b57\u8282\u6d41\u505a\u4efb\u4f55\u8f6c\u6362\uff0c\u4ece\u800c\u53ef\u4ee5\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u5f53\u6587\u672c\u6765\u4f20\uff0c\u89e3\u7801\u65f6\u4e5f\u6307\u5b9a Latin-1\uff0c\u539f\u539f\u672c\u672c\u7684\u53d6\u51fa\u6570\u636e\u3002 Base64 \u9632\u4e71\u7801 \u5982\u679c\u8981\u628a\u4e00\u4e32\u4e2d\u6587\u8f93\u5165\u4e00\u4e2a\u4e0d\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\uff0c\u53d1\u9001\u8ba9\u5bf9\u65b9\u6536\u5230\uff0c\u600e\u4e48\u529e\uff1f \u53ef\u4ee5\u7528 Latin-1 \u7f16\u7801\uff0c\u9a97\u8fd9\u4e2a\u8f6f\u4ef6\uff0c\u8ba9\u4ed6\u4ee5\u4e3a\u81ea\u5df1\u6536\u5230\u7684\u662f Latin-1 \u5b57\u7b26\u4e32\uff0c\u53cd\u6b63\u4ed6\u4e5f\u4e0d\u770b\u5185\u5bb9\uff0c\u4ece\u800c\u8ba9\u4ed6\u4e0d\u8981\u505a\u4efb\u4f55\u8f6c\u6362\u64cd\u4f5c\u3002 \u4e0d\u8fc7\u6709\u65f6\u5019\uff0c\u6587\u672c\u6846\u65e0\u6cd5\u8f93\u5165\u90e8\u5206\u7279\u6b8a\u7684\u63a7\u5236\u5b57\u7b26\uff0c\u800c UTF-8 \u5b57\u7b26\u4e32\u7f16\u7801\u51fa\u6765\u7684\u6587\u672c\uff0c\u8d85\u8fc7 0x80 \u7684\u90e8\u5206\uff0c\u53ef\u80fd\u843d\u5165 Latin-1 \u7684\u63a7\u5236\u5b57\u7b26\u4e2d\uff0c\u88ab\u8fd9\u4e2a\u8f6f\u4ef6\u9519\u8bef\u5730\u505a\u4e86\u7279\u6b8a\u5904\u7406\u3002 \u4e3a\u4e86\u907f\u514d\u53ea\u517c\u5bb9\u4e86 ASCII \u7684\u843d\u540e\u8f6f\u4ef6\u7834\u574f\u6211\u4eec\u7684\u5b57\u7b26\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u53ea\u652f\u6301 ASCII \u6587\u672c\u7684\u7f16\u8f91\u6846\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Base64 \u7f16\u7801\u5148\u628a\u4efb\u610f\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u3002 Base64 \u662f\u4e00\u79cd\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\uff0c\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u6bcf 6 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u8f6c\u6362\u4e3a\u4e00\u4e2a\u53ef\u6253\u5370\u7684 ASCII \u5b57\u7b26\uff08\u7528 A-Z a-z 0-9 - / \u8fd9 64 \u4e2a\u5b57\u7b26\u8868\u793a\uff09\u3002\u56e0\u6b64\uff0cBase64 \u7f16\u7801\u540e\u7684\u6587\u672c\uff0c\u6bcf 4 \u4e2a\u5b57\u7b26\u5c31\u6709 3 \u4e2a\u662f\u6709\u6548\u5b57\u7b26\uff0c\u5269\u4e0b\u7684 1 \u4e2a\u5b57\u7b26\u662f\u586b\u5145\u5b57\u7b26 = \u3002 \u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"\u5c0f\u5f6d\u8001\u5e08\" \uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u628a\u5b83\u901a\u8fc7\u90ae\u4ef6\u53d1\u51fa\u53bb\u3002\u800c\u8fd9\u4e2a\u90ae\u4ef6\u670d\u52a1\u5668\u4e0d\u652f\u6301 UTF-8 \u4e5f\u4e0d\u652f\u6301 GBK\uff0c\u53ea\u652f\u6301 ASCII\uff01 \u9996\u5148\u6211\u4eec\u7528 UTF-8 \u7f16\u7801\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff1a 0xE5 0xB0 0x8F 0xE5 0xBD 0xAD 0xE8 0x80 0x81 0xE5 0xB8 0x88 \u7136\u540e\u518d\u7528 Base64 \u4e8c\u6b21\u7f16\u7801\u6210\u666e\u901a\u7684\u53ef\u6253\u5370\u5b57\u6bcd\u548c\u6570\u5b57\u5e8f\u5217\uff1a 5bCP5b2t6ICB5biI \u5bf9\u65b9\u6536\u5230\u8fd9\u4e32\u795e\u79d8\u5b57\u7b26\u540e\uff0c\u518d\u7528 base64 \u89e3\u7801\uff0c\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff0c\u518d\u7528\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\u89e3\u7801\uff0c\u5c31\u80fd\u770b\u5230\u672c\u6765\u7684\u4e2d\u6587\u4e86\u3002 # \u53d1\u9001\u8005\uff1a import base64 secret = base64.b64encode(\"\u5c0f\u5f6d\u8001\u5e08\".encode()) # \u63a5\u6536\u8005\uff1a base64.b64decode(secret).decode() \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u7801 UTF-8 \u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f20\u8f93\u4efb\u610f\u975e\u6587\u672c\u7684\u6587\u4ef6\uff01\u4f8b\u5982\uff0c\u6709\u4eba\u5229\u7528 Base64 \u7f16\u7801\uff0c\u628a jpg \u56fe\u50cf\u6587\u4ef6\u76f4\u63a5\u5185\u5d4c\u5728 md \u6587\u4ef6\u91cc\uff01\uff08md \u6587\u4ef6\u53ea\u652f\u6301\u5305\u542b\u5408\u6cd5\u7684 UTF-8 \u6587\u672c\uff0c\u4e0d\u53ef\u80fd\u5305\u542b jpg \u7684\u4efb\u610f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u53ea\u80fd\u7528 Base64 \u5148\u7f16\u7801\u6210 ASCII \u8303\u56f4\u5185\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u9632\u6b62 md \u7f16\u8bd1\u5668\u62a5 UTF-8 \u89e3\u7801\u9519\u8bef\uff09 \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u8f93\u5165\u4e2d\u6587\u5b9e\u5728\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5148 Base64 \u8f6c\u6362\u6210\u7eaf\u82f1\u6587\u8bd5\u8bd5\u770b\uff0c\u53cd\u6b63\u65e0\u8bba\u8c01\u90fd\u517c\u5bb9 ASCII\u3002\u5982\u679c\u8fd9\u4e2a\u6587\u672c\u6846\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u8fd8\u53ef\u4ee5\u8bd5\u8bd5\u770b\u53ea\u6709 A-Z 0-9 \u7684 Base32 \u7f16\u7801\u3002","title":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b"},{"location":"unicode/#_1","text":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b \u5b57\u7b26\u96c6 ASCII Latin-1 Unicode \u603b\u7ed3 \u5b57\u7b26\u7f16\u7801 UTF-32 UTF-8 \u517c\u5bb9 ASCII \u89e3\u7801\u89c4\u5219 UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b \u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d UTF-16 \u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89 BOM \u6807\u8bb0 C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c \u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48 \u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a \u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae \u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6 \u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a .utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684 \u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528 \u9009\u62e9\u4f60\u7684\u9635\u8425\uff01 ANSI \u9635\u8425 UTF-8 \u9635\u8425 UTF-16 \u9635\u8425 UTF-32 \u9635\u8425 \u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362 \u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp \u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale UTF \u4e4b\u95f4\u4e92\u8f6c GBK \u548c UTF \u4e92\u8f6c UTF \u548c ANSI \u4e92\u8f6c \u5927\u603b\u7ed3 GBK \u548c Shift-JIS \u4e92\u8f6c \u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5 \u66f4\u591a\u529f\u80fd\uff1f\uff01 Windows \u7528\u6237\uff1aMultiByteToWideChar MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b Linux \u7528\u6237\uff1aiconv iconv \u547d\u4ee4\u884c\u5de5\u5177 \u672c\u5730\u5316 (locale) \u533a\u5206\u5b57\u7b26\u7c7b\u578b \u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b \u533a\u57df\u8bbe\u7f6e\u4e0e std::locale \u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316 \u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f \u5bbd\u5b57\u7b26\u6d41 \u5b98\u65b9\u773c\u4e2d\u7684 std::wstring wchar_t \u7cfb\u5217\u51fd\u6570 std::wcout \u7684\u4f7f\u7528 std::wfstream \u7684\u4f7f\u7528 C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 Windows \u4e13\u9898 Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570 TCHAR \u6d41\u6d3e UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165 \u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76 Qt QString QTextCodec from/toLocal8Bits/Utf8/Latin1/Ascii \u5b57\u7b26\u4e32\u5e38\u91cf QTextStream Python 3 str Rust &str \u548c String Java String COW \u5b57\u7b26\u4e32 Unicode \u77e5\u8bc6\u8fdb\u9636 \u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97 Grapheme \u6b63\u89c4\u5316 \u96f6\u5bbd\u7a7a\u683c \u7279\u6b8a\u63a7\u5236\u5b57\u7b26 \u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26 \u9ed1\u6697\u5c0f\u6280\u5de7 Latin-1 \u7684\u8f6c\u6362 Latin-1 \u7684\u5999\u7528 Base64 \u9632\u4e71\u7801","title":"\u5b57\u7b26\u7f16\u7801\u90a3\u4e9b\u4e8b"},{"location":"unicode/#_2","text":"\u8ba1\u7b97\u673a\u4e0d\u80fd\u76f4\u63a5\u5b58\u50a8\u5b57\u7b26\uff0c\u800c\u662f\u7528\u6570\u5b57\u6765\u4ee3\u66ff\uff0c\u8fd9\u5c31\u662f\u5b57\u7b26\u96c6\uff0c\u4e3a\u6bcf\u4e2a\u5b57\u7b26\u6307\u5b9a\u4e00\u4e2a\u6570\u5b57\u3002","title":"\u5b57\u7b26\u96c6"},{"location":"unicode/#ascii","text":"ASCII \u4e3a\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u7ec4\u3001\u6807\u70b9\u7b26\u53f7\u7b49 128 \u4e2a\u5b57\u7b26\uff0c\u6bcf\u4e2a\u90fd\u7528\u4e00\u4e2a 0 \u5230 127 \u8303\u56f4\u5185\u7684\u6570\u5b57\u5bf9\u5e94\u3002 \u5982\u679c\u4f60\u60f3\u8981\u8868\u793a\u4e00\u4e2a\u5b57\u7b26\uff0c\u5c31\u5728\u8fd9\u4e2a\u8868\u91cc\u5bfb\u627e\u5230\u76f8\u5e94\u7684\u6570\u5b57\u7f16\u53f7\uff0c\u7136\u540e\u5b58\u8fd9\u4e2a\u7f16\u53f7\u5373\u53ef\u3002 \u4f8b\u5982\u4e0b\u9762\u7684\u4e00\u4e32\u6570\u5b57\uff1a 80 101 110 103 \u5728 ASCII \u8868\u4e2d\u67e5\u627e\uff0c\u53d1\u73b0\u8fd9\u4e9b\u6570\u5b57\u5206\u522b\u5bf9\u5e94 P \u3001 e \u3001 n \u3001 g \u56db\u4e2a\u5b57\u6bcd\uff0c\u8fde\u8d77\u6765\u5c31\u8fd8\u539f\u5f97\u5230\u4e86\u539f\u672c\u7684\u5b57\u7b26\u4e32\u201cPeng\u201d\u3002","title":"ASCII"},{"location":"unicode/#latin-1","text":"Latin-1 \u6269\u5145\u4e86 ASCII \u5b57\u7b26\u96c6\uff0c\u4fdd\u6301 ASCII \u539f\u6709 0 \u5230 127 \u7684\u90e8\u5206\u6620\u5c04\u4e0d\u53d8\uff0c\u989d\u5916\u8ffd\u52a0\u4e86 128 \u5230 255 \u7684\u6620\u5c04\u5173\u7cfb\u3002\u56e0\u6b64\u4e5f\u88ab\u79f0\u4e3a EASCII\uff08\u6269\u5c55 ASCII\uff09\u3002","title":"Latin-1"},{"location":"unicode/#unicode","text":"Unicode \u5b57\u7b26\u96c6\u4e3a\u5168\u4e16\u754c\u7684\u6240\u6709\u5b57\u7b26\u90fd\u5bf9\u5e94\u4e86\u4e00\u4e2a\u6574\u6570\u3002 \u5b57\u7b26 \u7f16\u53f7 \u6211 25105 \u6212 25106 \u6213 25107 \u6214 25108 \u6215 25109 \u6216 25110 \u6217 25111 \u6218 25112 \u6219 25113 \u621a 25114 \u51fa\u4e8e\u5386\u53f2\u517c\u5bb9\u6027\u8003\u8651\uff0cUnicode \u5728 0 \u5230 256 \u533a\u95f4\u5185\u7684\u6620\u5c04\u548c ASCII\u3001Latin-1 \u662f\u5b8c\u5168\u76f8\u540c\u7684\u3002 \u5b57\u7b26 \u7f16\u53f7 P 80 e 101 n 110 g 103 Unicode \u7ecf\u8fc7\u4e86\u8bb8\u591a\u7248\u672c\u7684\u53d1\u5c55\uff0c\u65e9\u671f\u7684 Unicode \u53ea\u6536\u5f55\u4e86 65536 (0x10000) \u4e2a\u5b57\u7b26\uff0c\u540e\u6765\u6269\u5145\u5230\u4e86 1114112 (0x110000) \u4e2a\u5b57\u7b26\u3002 \u603b\u4e4b\uff0c\u73b0\u5728 Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u867d\u7136\u5360\u7528\u4e86 1114112 \u8fd9\u591a\u683c\u7801\u70b9\u7a7a\u95f4\uff0c\u4e0d\u8fc7\u5176\u4e2d\u5f88\u591a\u90fd\u662f\u7a7a\u53f7\uff0c\u7559\u5f85\u672a\u6765\u6269\u5145\u4f7f\u7528\u3002 Unicode \u5b57\u7b26\u6620\u5c04\u8868\u53ef\u4ee5\u5728\u7f51\u4e0a\u627e\u5230\uff1a https://symbl.cc/en/unicode-table/ https://www.compart.com/en/unicode/","title":"Unicode"},{"location":"unicode/#_3","text":"\u5b57\u7b26\u96c6: \u4ece\u5b57\u7b26\u5230\u6574\u6570\u7684\u4e00\u4e00\u6620\u5c04\u3002 ASCII: \u53ea\u6536\u5f55\u4e86\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 Latin-1: \u5728 ASCII \u57fa\u7840\u4e0a\u8ffd\u52a0\u4e86\u6ce8\u97f3\u5b57\u6bcd\uff0c\u6ee1\u8db3\u6b27\u6d32\u7528\u6237\u9700\u8981\u3002 Unicode: \u6536\u5f55\u4e86\u5168\u4e16\u754c\u6240\u6709\u6587\u5b57\u548c\u7b26\u53f7\u7684\u5b57\u7b26\u96c6\u3002 \u8ba1\u7b97\u673a\u5b58\u50a8\u5b57\u7b26\u65f6\uff0c\u5b9e\u9645\u4e0a\u662f\u5b58\u50a8\u4e86\u90a3\u4e2a\u5bf9\u5e94\u7684\u6574\u6570\u3002 \u8fd9\u4e9b\u6574\u6570\u5c31\u88ab\u79f0\u4e3a \u7801\u70b9 (code point) \uff0c\u6bcf\u4e2a\u5b57\u7b26\u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002 \u4e0d\u8fc7\uff0c\u7a0b\u5e8f\u5458\u901a\u5e38\u559c\u6b22\u7528\u5341\u516d\u8fdb\u5236\u4e66\u5199\u6570\u5b57\uff1a \u5b57\u7b26 \u7f16\u53f7 \u6211 0x6211 \u6212 0x6212 \u6213 0x6213 \u6214 0x6214 \u6215 0x6215 \u6216 0x6216 \u6217 0x6217 \u6218 0x6218 \u6219 0x6219 \u621a 0x621A \u4f8b\u5982\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\uff0c\u5728 Unicode \u8868\u4e2d\u7f16\u53f7\u4e3a 0x6211\u3002\u4e8e\u662f\u5f53\u8ba1\u7b97\u673a\u9700\u8981\u8868\u793a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\u65f6\uff0c\u5c31\u7528 0x6211 \u8fd9\u4e2a\u6574\u6570\u4ee3\u66ff\u3002 \u5982\u679c\u8981\u8868\u793a\u591a\u4e2a\u5b57\u7b26\uff0c\u90a3\u5c31\u7528\u4e00\u4e2a\u6574\u6570\u7684\u6570\u7ec4\u5427\uff01 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5904\u7406\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a 0x6211 0x7231 0x30EDE 0x30EDE 0x9762 0x21 \u8fd9\u4e00\u4e32\u6570\u5b57\u4ee3\u66ff\u3002","title":"\u603b\u7ed3"},{"location":"unicode/#_4","text":"Unicode \u53ea\u662f\u6307\u5b9a\u4e86\u6574\u6570\uff0c\u6ca1\u6709\u89c4\u5b9a\u6574\u6570\u5982\u4f55\u5728\u5185\u5b58\u4e2d\u5b58\u5728\u3002 \u5b57\u7b26\u7f16\u7801: \u5c06\u5b57\u7b26\u7684\u6574\u6570\u7f16\u53f7\u5e8f\u5217\u5316\u4e3a\u8ba1\u7b97\u673a\u53ef\u76f4\u63a5\u5b58\u50a8\u7684\u4e00\u4e2a\u6216\u591a\u4e2a\u5b9e\u9645\u5b58\u5728\u7684\u6574\u6570\u7c7b\u578b\u3002 Unicode \u5b57\u7b26\u53ef\u4ee5\u9009\u7528\u4ee5\u4e0b\u8fd9\u4e9b\u5b57\u7b26\u7f16\u7801\u6765\u5e8f\u5217\u5316\uff1a UTF-32: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u4e2a uint32_t \u6574\u6570\u5b58\u50a8\u3002 UTF-16: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 2 \u4e2a uint16_t \u6574\u6570\u5b58\u50a8\u3002 UTF-8: \u6bcf\u4e2a Unicode \u5b57\u7b26\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u5b58\u50a8\u3002 \u7ffb\u8bd1\u51fa\u6765\u7684\u8fd9\u4e9b\u5c0f\u6574\u6570\u53eb \u7801\u4f4d (code unit) \u3002\u4f8b\u5982\u5bf9\u4e8e UTF-8 \u800c\u8a00\uff0c\u6bcf\u4e2a uint8_t \u5c31\u662f\u4ed6\u7684\u7801\u4f4d\u3002","title":"\u5b57\u7b26\u7f16\u7801"},{"location":"unicode/#utf-32","text":"Unicode \u5b57\u7b26\u6620\u5c04\u7684\u6574\u6570\u8303\u56f4\u662f 0x0 \u5230 0x10FFFF\u3002 \u6700\u5927\u503c 0x10FFFF \u6709 21 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0cC \u8bed\u8a00\u4e2d uint32_t \u80fd\u5bb9\u7eb3 32 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\uff0c\u6240\u4ee5\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u76f4\u63a5\u7528 uint32_t \u6570\u7ec4\u6765\u4e00\u4e2a\u4e2a\u5bb9\u7eb3 Unicode \u5b57\u7b26\u7801\u70b9\u3002\u867d\u7136\u6d6a\u8d39\u4e86 11 \u4f4d\uff0c\u4f46\u81f3\u5c11\u6240\u6709 Unicode \u5b57\u7b26\u90fd\u80fd\u5b89\u5168\u5bb9\u7eb3\u3002 \u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff0c\u5c31\u53ef\u4ee5\u7528\uff1a std::vector s = { 0x00006211, // \u6211 0x00007231, // \u7231 0x00030EDE, // \ud883\udede 0x00030EDE, // \ud883\udede 0x00009762, // \u9762 0x00000021, // ! }; \u8fd9\u4e2a\u6570\u7ec4\u8868\u793a\u3002 UTF-32 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u56fa\u5b9a\u5bf9\u5e94\u4e00\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-32 \u662f \u5b9a\u957f\u7f16\u7801 \u3002\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u5c31\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002 \u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u53ef\u4ee5\u76f4\u63a5\u7528\u6570\u7ec4\u7684\u7d22\u5f15\u64cd\u4f5c\u3002 \u65e0\u8bba\u5bf9\u6570\u7ec4\u5982\u4f55\u5207\u7247\uff0c\u90fd\u4e0d\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u7834\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u5c31\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u53cd\u8f6c\uff0c\u4e0d\u4f1a\u4ea7\u751f\u7834\u574f\u5b57\u7b26\u7684\u95ee\u9898\u3002 \u7f3a\u70b9\u662f\uff1a \u6d6a\u8d39\u5b58\u50a8\u7a7a\u95f4\u3002 \u56e0\u6b64\uff0c\u6211\u4eec\u63a8\u8350\u5728\u8ba1\u7b97\u673a\u5185\u5b58\u4e2d\uff0c\u59cb\u7ec8\u91c7\u7528 UTF-32 \u5f62\u5f0f\u5904\u7406\u6587\u5b57\u3002 UTF-32 \u4e5f\u88ab\u79f0\u4e3a UCS-4\uff0c\u4ed6\u4fe9\u662f\u540c\u4e49\u8bcd\u3002","title":"UTF-32"},{"location":"unicode/#utf-8","text":"UTF-32 \u867d\u7136\u65b9\u4fbf\u4e86\u6587\u5b57\u5904\u7406\uff0c\u7136\u800c\uff0c\u5374\u6d6a\u8d39\u4e86\u5927\u91cf\u7684\u5b58\u50a8\u7a7a\u95f4\uff0c\u4e0d\u5229\u4e8e\u6587\u5b57\u5b58\u50a8\uff01\u4e00\u4e2a\u5b57\u7b26\uff0c\u65e0\u8bba\u4ed6\u662f\u5e38\u7528\u8fd8\u662f\u4e0d\u5e38\u7528\uff0c\u90fd\u8981\u9738\u5360 4 \u4e2a\u5b57\u8282\u7684\u7a7a\u95f4\u3002 Unicode \u7f16\u7801\u5b57\u7b26\u65f6\uff0c\u7279\u610f\u628a\u5e38\u7528\u7684\u5b57\u7b26\u9760\u524d\u6392\u5217\u4e86\u3002 \u4e16\u754c\u4e0a\u5e38\u7528\u8bed\u8a00\u6587\u5b57\u90fd\u88ab\u523b\u610f\u7f16\u7801\u5728\u4e86 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u8d85\u8fc7 0x10000 \u7684\u57fa\u672c\u90fd\u662f\u4e0d\u5e38\u7528\u7684\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\uff0c\u5f88\u591a\u90fd\u662f\u5df2\u7ecf\u65e0\u4eba\u4f7f\u7528\u7684\u53e4\u4ee3\u6587\u5b57\u548c\u751f\u50fb\u5b57\uff0c\u4f8b\u5982\u201c\ud883\udede\u201d\u3002\u4ec5\u4ec5\u662f\u4e3a\u4e86\u8fd9\u4e9b\u5076\u5c14\u4f7f\u7528\u7684\u7f55\u89c1\u6587\u5b57\uff0c\u5c31\u8981\u6c42\u6240\u6709\u6587\u5b57\u90fd\u7528\u540c\u6837\u7684 4 \u5b57\u8282\u5bbd\u5ea6\u5b58\u50a8\uff0c\u5b9e\u5728\u662f\u6709\u70b9\u6d6a\u8d39\u3002 \u5728 0 \u5230 0xFFFF \u533a\u95f4\u5185\uff0c\u540c\u6837\u6709\u6309\u7167\u5e38\u7528\u5ea6\u6392\u5e8f\uff1a 0 \u5230 0x7F \u662f\uff08\u6b27\u7f8e\u7528\u6237\uff09\u6700\u5e38\u7528\u7684\u82f1\u6587\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u534a\u89d2\u6807\u70b9\u3002 0x80 \u5230 0x7FF \u662f\u8868\u97f3\u6587\u5b57\u533a\uff0c\u5e38\u7528\u7684\u6ce8\u97f3\u5b57\u6bcd\u3001\u62c9\u4e01\u5b57\u6bcd\u3001\u5e0c\u814a\u5b57\u6bcd\u3001\u897f\u91cc\u5c14\u5b57\u6bcd\u3001\u5e0c\u4f2f\u6765\u5b57\u6bcd\u7b49\u3002 0x800 \u5230 0xFFFF \u662f\u8868\u610f\u6587\u5b57\uff0c\u7b80\u7e41\u4e2d\u6587\u3001\u65e5\u6587\u3001\u97e9\u6587\u3001\u6cf0\u6587\u3001\u9a6c\u6765\u6587\u3001\u963f\u62c9\u4f2f\u6587\u7b49\u3002 0x10000 \u5230 0x10FFFF \u662f\u4e0d\u5e38\u7528\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f8b\u5982\u7532\u9aa8\u6587\u3001\u57c3\u53ca\u8c61\u5f62\u6587\u5b57\u3001Emoji \u7b49\u3002 UTF-8 \u5c31\u662f\u4e3a\u4e86\u89e3\u51b3\u538b\u7f29\u95ee\u9898\u800c\u8bde\u751f\u7684\u3002 UTF-8 \u628a\u4e00\u4e2a\u7801\u70b9\u5e8f\u5217\u5316\u4e3a\u4e00\u4e2a\u6216\u591a\u4e2a\u7801\u4f4d\uff0c\u4e00\u4e2a\u7801\u4f4d\u7528 1 \u81f3 4 \u4e2a uint8_t \u6574\u6570\u8868\u793a\u3002 0 \u5230 0x7F \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 1 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 2 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 3 \u4e2a\u5b57\u8282\u8868\u793a\u3002 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u7528 4 \u4e2a\u5b57\u8282\u8868\u793a\u3002 \u5e8f\u5217\u5316\u89c4\u5219\u5982\u4e0b\uff1a","title":"UTF-8"},{"location":"unicode/#ascii_1","text":"\u5bf9\u4e8e 0 \u5230 0x7F \u7684\u5b57\u7b26\uff0c\u8fd9\u4e2a\u8303\u56f4\u7684\u5b57\u7b26\u9700\u8981 7 \u4f4d\u5b58\u50a8\u3002 \u6211\u4eec\u9009\u62e9\u76f4\u63a5\u5b58\u50a8\u5176\u503c\u3002 \u4f8b\u5982 \u2018P\u2019 \u4f1a\u88ab\u76f4\u63a5\u5b58\u50a8\u5176 Unicode \u503c\u7684 80\uff080x50\uff09\uff1a 01010000 \u7531\u4e8e Unicode \u5728 0 \u5230 0x7F \u8303\u56f4\u5185\u4e0e ASCII \u8868\u76f8\u540c\uff0c\u800c UTF-8 \u53c8\u628a 0 \u5230 0x7F \u7684\u503c\u76f4\u63a5\u5b58\u50a8\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u517c\u5bb9 ASCII\u3002\u8fd9\u4f7f\u5f97\u539f\u672c\u8bbe\u8ba1\u4e8e\u5904\u7406 ASCII \u7684 C \u8bed\u8a00\u51fd\u6570\uff0c\u4f8b\u5982 strlen\u3001strcat\u3001sprintf \u7b49\uff0c\u90fd\u53ef\u4ee5\u76f4\u63a5\u65e0\u7f1d\u5207\u6362\u5230 UTF-8\u3002\u53cd\u4e4b\u4ea6\u7136\uff0c\u4efb\u4f55\u8bbe\u8ba1\u7528\u4e8e UTF-8 \u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u5b8c\u5168\u63a5\u53d7 ASCII \u683c\u5f0f\u7684\u8f93\u5165\u6587\u672c\u3002 \u4f46\u90e8\u5206\u6d89\u53ca\u5b57\u7b26\u957f\u5ea6\u7684\u51fd\u6570\u4f1a\u6709\u4e9b\u8bb8\u4e0d\u517c\u5bb9\uff0c\u4f8b\u5982 strlen \u6c42\u51fa\u7684\u957f\u5ea6\u4f1a\u53d8\u6210\u5b57\u8282\u7684\u6570\u91cf\u800c\u4e0d\u662f\u5b57\u7b26\u7684\u6570\u91cf\u4e86\uff0c\u4f8b\u5982 strlen(\"\u6211\u4eec\") \u4f1a\u5f97\u5230 6 \u800c\u4e0d\u662f 2\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002","title":"\u517c\u5bb9 ASCII"},{"location":"unicode/#_5","text":"UTF-8 \u7684\u6784\u9020\u5c31\u50cf\u4e00\u5217\u5c0f\u706b\u8f66\u4e00\u6837\uff0c\u4e0d\u540c\u8303\u56f4\u5185\u7684\u7801\u4f4d\u4f1a\u88ab\u7f16\u7801\u6210\u4e0d\u540c\u957f\u5ea6\u7684\u5217\u8f66\uff0c\u4f46\u4ed6\u4eec\u90fd\u6709\u4e00\u4e2a\u8f66\u5934\u3002 \u6839\u636e\u706b\u8f66\u5934\u7684\u201c\u7b49\u7ea7\u201d\uff0c\u6211\u4eec\u53ef\u4ee5\u63a8\u65ad\u51fa\u540e\u9762\u62c9\u7740\u51e0\u8282\u8f66\u53a2\u3002 \u706b\u8f66\u5934\u662f\u4ec0\u4e48\u7b49\u7ea7\u7531\u4ed6\u7684\u4e8c\u8fdb\u5236\u524d\u7f00\u51b3\u5b9a\uff1a \u5982\u679c\u662f 0 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u662f\u5355\u72ec\u4e00\u53f0\u706b\u8f66\u5934\uff0c\u540e\u9762\u6ca1\u6709\u8f66\u53a2\u4e86\uff0c\u8fd9\u8868\u793a\u8f66\u5934\u91cc\u9762\u76f4\u63a5\u88c5\u7740 0 \u5230 0x7F \u8303\u56f4\u7684\u666e\u901a ASCII \u5b57\u7b26\u3002 \u5982\u679c\u662f 110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e00\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x80 \u5230 0x7FF \u8303\u56f4\u5185\u7684\u6b27\u6d32\u5b57\u7b26\u3002 \u5982\u679c\u662f 1110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e24\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x800 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u4e16\u754c\u5e38\u7528\u5b57\u7b26\u3002 \u5982\u679c\u662f 11110 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u540e\u9762\u62d6\u7740\u4e09\u8282\u8f66\u53a2\uff0c\u91cc\u9762\u88c5\u7740 0x10000 \u5230 0x10FFFF \u8303\u56f4\u5185\u7684\u751f\u50fb\u5b57\u7b26\u3002 \u5982\u679c\u662f 10 \u5f00\u5934\uff0c\u5c31\u8bf4\u660e\u8fd9\u662f\u4e00\u8282\u8f66\u53a2\uff0c\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002\u5982\u679c\u4f60\u770b\u5230\u4e00\u8282\u5355\u72ec\u7684\u8f66\u53a2\u5728\u524d\u9762\u65e0\u5934\u9a7e\u9a76\uff0c\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\u3002 \u5c0f\u670b\u53cb\u7528\u5c0f\u53f7\u5217\u8f66\u88c5\uff0c\u5927\u670b\u53cb\u7528\u5927\u53f7\u5217\u8f66\u88c5\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u4e32\u4e8c\u8fdb\u5236\uff1a 11100110 10000010 10000001 \u9996\u5148\uff0c\u770b\u5230\u7b2c\u4e00\u4e2a\u5b57\u8282\uff0c\u662f 1110 \u5f00\u5934\u7684\u4e09\u7ea7\u8f66\u5934\uff01\u8bf4\u660e\u540e\u9762\u8fd8\u6709\u4e24\u8282\u8f66\u53a2\u662f\u5c5e\u4e8e\u4ed6\u7684\u3002\u706b\u8f66\u5934\u4e2d 4 \u4f4d\u7528\u4e8e\u8868\u793a\u8f66\u5934\u7b49\u7ea7\u4e86\uff0c\u5269\u4e0b\u8fd8\u6709 4 \u4f4d\u7528\u4e8e\u88c5\u4e58\u5ba2\u3002 \u8f66\u53a2\u4e5f\u6709\u56fa\u5b9a\u7684\u524d\u7f00\uff0c\u6240\u6709\u7684\u8f66\u53a2\u90fd\u5fc5\u987b\u662f 10 \u5f00\u5934\u7684\u3002\u53bb\u9664\u8fd9\u5f00\u5934\u7684 2 \u4f4d\uff0c\u5269\u4e0b\u7684 6 \u4f4d\u5c31\u662f\u4e58\u5ba2\u3002 \u5bf9\u4e8e\u8fd9\u79cd\u4e09\u7ea7\u5217\u8f66\uff0c4 + 6 + 6 \u603b\u5171 16 \u4f4d\u4e8c\u8fdb\u5236\uff0c\u521a\u597d\u53ef\u4ee5\u88c5\u5f97\u4e0b 0xFFFF \u5185\u7684\u4e58\u5ba2\u3002 0110 000010 000001 \u7f16\u7801\u65f6\u5219\u662f\u53cd\u8fc7\u6765\u3002 \u4e58\u5ba2\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e09\u7247\uff0c\u4f8b\u5982\u5bf9\u4e8e\u201c\u6211\u201d\u8fd9\u4e2a\u4e58\u5ba2\uff0c\u201c\u6211\u201d\u7684\u7801\u70b9\u662f 0x6211\uff0c\u8f6c\u6362\u6210\u4e8c\u8fdb\u5236\u662f\uff1a 110010000010001 \u628a\u4e58\u5ba2\u5207\u5206\u6210\u9ad8 4 \u4f4d\u3001\u4e2d 6 \u4f4d\u548c\u4f4e 6 \u4f4d\uff1a 1100 100000 10001 \u52a0\u4e0a 1110 \u3001 10 \u548c 10 \u524d\u7f00\u540e\uff0c\u5f62\u6210\u4e00\u5217\u706b\u8f66\uff1a 11100110 10000010 10000001 \u8fd9\u6837\uff0c\u6211\u4eec\u5c31\u628a\u201c\u6211\u201d\u8fd9\u4e2a\u5b57\u7b26\uff0c\u7f16\u7801\u6210\u4e86\u4e09\u8282\u5217\u8f66\uff0c\u585e\u8fdb\u5b57\u8282\u6d41\u7684\u7f51\u7edc\u96a7\u9053\u91cc\u4e86\u3002 \u603b\u7ed3\uff1a \u524d\u7f00\u662f 0 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 7 \u540d\u3002 \u524d\u7f00\u662f 10 \u7684\u662f\u8f66\u53a2\uff1a\u8f66\u53a2\u4e0d\u4f1a\u5355\u72ec\u51fa\u73b0\uff0c\u53ea\u4f1a\u8ddf\u5728\u706b\u8f66\u5934\u5c41\u80a1\u540e\u9762\u3002 \u524d\u7f00\u662f 110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 5 \u540d + 1 \u8282\u8f66\u53a2\u8f7d\u5ba2 6 \u540d = \u5171 11 \u540d\u3002 \u524d\u7f00\u662f 1110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 4 \u540d + 2 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 16 \u540d\u3002 \u524d\u7f00\u662f 11110 \u7684\u706b\u8f66\u5934\uff1a\u706b\u8f66\u5934\u76f4\u63a5\u8f7d\u5ba2 3 \u540d + 3 \u8282\u8f66\u53a2\u5404\u8f7d\u5ba2 6 \u540d = \u5171 21 \u540d\u3002 \u9ad8\u7ea7\u8f66\u5934\u88c5\u4e86\u9632\u5f39\u94a2\u677f\uff0c\u8f7d\u5ba2\u7a7a\u95f4\u53d8\u5c11\uff0c\u53ea\u597d\u5300\u5230\u540e\u9762\u7684\u8f66\u53a2\u3002","title":"\u89e3\u7801\u89c4\u5219"},{"location":"unicode/#utf-8_1","text":"\u5982\u679c\u53d1\u73b0 10 \u5f00\u5934\u7684\u72ec\u7acb\u8f66\u53a2\uff0c\u5c31\u8bf4\u660e\u51fa\u95ee\u9898\u4e86\uff0c\u53ef\u80fd\u662f\u706b\u8f66\u88ab\u9519\u8bef\u62e6\u8170\u622a\u65ad\uff0c\u4e5f\u53ef\u80fd\u662f\u5b57\u7b26\u4e32\u88ab\u9519\u8bef\u5730\u53cd\u8f6c\u3002\u56e0\u4e3a 10 \u53ea\u53ef\u80fd\u662f\u706b\u8f66\u8f66\u53a2\uff0c\u4e0d\u53ef\u80fd\u51fa\u73b0\u5728\u706b\u8f66\u5934\u90e8\u3002\u6b64\u65f6\u89e3\u7801\u5668\u5e94\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u66ff\u6362\u3002 10000010 10000001 \u5728\u7f51\u7edc\u6536\u53d1\u5305\u65f6\uff0c\u5982\u679c\u4f60\u4e0d\u59a5\u5584\u5904\u7406 TCP \u7c98\u5305\u95ee\u9898\uff0c\u5c31\u53ef\u80fd\u706b\u8f66\u5934\u8fdb\u53bb\u4e86\uff0c\u706b\u8f66\u5c3e\u5df4\u8fd8\u9732\u5728\u96a7\u9053\u5916\u9762\uff0c\u4e00\u6bb5\u5b8c\u6574\u7684\u5217\u8f66\u88ab\u5207\u65ad\uff0c\u5bfc\u81f4 UTF-8 \u89e3\u8bfb\u7684\u65f6\u5019\u51fa\u9519\u3002\u6b63\u786e\u7684\u505a\u6cd5\u662f\u8bbe\u7acb\u4e00\u4e2a\u72b6\u6001\u673a\u6765\u89e3\u7801 UTF-8\u3002C \u8bed\u8a00\u7684 mbstate_t \u5c31\u662f\u8fd9\u79cd\u72b6\u6001\u673a\uff0c\u7a0d\u540e\u8bb2\u89e3\u3002 \u9664\u6b64\u4e4b\u5916\uff0c\u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\uff0c\u5374\u53d1\u73b0\u91cc\u9762\u88c5\u7740 0x394 (\u201c\u0394\u201d)\uff0c\u8fd9\u662f\u4e00\u4e2a\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u5c31\u80fd\u88c5\u4e0b\u7684\u6b27\u6d32\u5b57\u7b26\uff0c\u5374\u7528\u4e86\u4e09\u7ea7\u706b\u8f66\u5934\u88c5\uff0c\u8bf4\u660e\u88c5\u7bb1\u90a3\u8fb9\u7684\u4eba\u5077\u61d2\u6ee5\u7528\u8d44\u6e90\u4e86\uff01\u8fd9\u79cd\u60c5\u51b5\u4e0b UTF-8 \u89e3\u7801\u5668\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u8981\u4fdd\u8bc1\u7f16\u7801\u7684\u552f\u4e00\u6027\uff0c0x394 \u662f 0x7F \u5230 0x7FF \u8303\u56f4\u7684\uff0c\u5c31\u5e94\u8be5\u7528\u4e8c\u7ea7\u706b\u8f66\u5934\u88c5\u3002 \u4ee5\u53ca\uff0c\u5982\u679c\u53d1\u73b0 11111 \u5f00\u5934\u7684\u4e94\u7ea7\u706b\u8f66\u5934\uff0c\u4e5f\u8981\u62a5\u9519\uff0c\u56e0\u4e3a UTF-8 \u6700\u591a\u53ea\u652f\u6301\u56db\u7ea7\u706b\u8f66\u5934\u3002 \u5982\u679c\u68c0\u6d4b\u5230\u4e00\u4e2a\u56db\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u7684\u5b57\u7b26\u8303\u56f4\u8d85\u8fc7\u4e86 0x10FFFF\uff0c\u8fd9\u8d85\u51fa\u4e86 Unicode \u7684\u8303\u56f4\uff0c\u4e5f\u8981\u4ea7\u751f\u4e00\u4e2a\u62a5\u9519\u3002\u5982\u679c\u4e00\u4e2a\u4e09\u7ea7\u706b\u8f66\u5934\u62c6\u5f00\u540e\u53d1\u73b0\u5b57\u7b26\u8303\u56f4\u5904\u5728\u4fdd\u7559\u533a 0xD800 \u5230 0xDFFF \u5185\uff0c\u8fd9\u662f Unicode \u627f\u8bfa\u6c38\u4e0d\u52a0\u5165\u5b57\u7b26\u7684\u533a\u95f4\uff08\u7a0d\u540e\u8bb2\u89e3 UTF-16 \u65f6\u4f1a\u89e3\u91ca\u4e3a\u4ec0\u4e48\uff09\uff0c\u4e5f\u8981\u62a5\u9519\u3002\u603b\u4e4b Unicode \u7801\u70b9\u7684\u5408\u6cd5\u8303\u56f4\u662f 0x0 \u5230 0xD7FF\uff0c0xE000 \u5230 0x10FFFF\u3002 \u603b\u4e4b\uff0cUTF-8 \u5177\u6709\u4e00\u5b9a\u7684\u5197\u4f59\u548c\u81ea\u7ea0\u9519\u80fd\u529b\uff0c\u5982\u679c\u4f20\u8f93\u8fc7\u7a0b\u4e2d\u51fa\u73b0\u5dee\u9519\uff0c\u53ef\u80fd\u4f1a\u7206\u51fa\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u3002\u8fd9\u4e2a\u7279\u6b8a\u5b57\u7b26\u662f Unicode \u5b98\u65b9\u89c4\u5b9a\u7684\uff0c\u7801\u70b9\u4e3a 0xFFFD\uff0c\u51fa\u73b0\u4ed6\u5c31\u610f\u5473\u7740 UTF-8 \u89e3\u7801\u5931\u8d25\u4e86\u3002","title":"UTF-8 \u7684\u6297\u5e72\u6270\u80fd\u529b"},{"location":"unicode/#_6","text":"\u4f8b\u5982\u5f53\u8ba1\u7b97\u673a\u8981\u4ee5 UTF-8 \u683c\u5f0f\u5b58\u50a8\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d\u8fd9\u6bb5\u6587\u5b57\uff1a std::vector s = { 0xE6, 0x88, 0x91, // \u6211\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xE7, 0x88, 0xB1, // \u7231\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xF0, 0xB0, 0xAF, 0x9B, // \ud883\udede\uff0c\u9700\u8981\u56db\u7ea7\u5217\u8f66 0xE9, 0x9D, 0xA2, // \u9762\uff0c\u9700\u8981\u4e09\u7ea7\u5217\u8f66 0x21, // !\uff0c\u8fd9\u662f\u4e2a ASCII \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u76f4\u63a5\u7528\u5355\u4e2a\u706b\u8f66\u5934\u88c5 }; UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u7801\u70b9\u53ef\u80fd\u5bf9\u5e94\u591a\u4e2a\u7801\u4f4d\uff0c\u6240\u4ee5\u8bf4 UTF-8 \u662f\u4e00\u79cd \u53d8\u957f\u7f16\u7801 \u3002\u53d8\u957f\u7f16\u7801\u7684\u7f3a\u70b9\u662f\uff1a \u6570\u7ec4\u7684\u957f\u5ea6\uff0c\u4e0d\u4e00\u5b9a\u662f\u5b57\u7b26\u4e32\u4e2d\u5b9e\u9645\u5b57\u7b26\u7684\u4e2a\u6570\u3002\u56e0\u6b64\uff0c\u8981\u53d6\u51fa\u5355\u4e2a\u5b57\u7b26\uff0c\u9700\u8981\u904d\u5386\u6570\u7ec4\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\u3002 \u6570\u7ec4\u7684\u5355\u4e2a\u5143\u7d20\u7d22\u5f15\uff0c\u65e0\u6cd5\u4fdd\u8bc1\u53d6\u51fa\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\u3002 \u5bf9\u6570\u7ec4\u7684\u5207\u7247\uff0c\u53ef\u80fd\u4f1a\u628a\u4e00\u4e2a\u72ec\u7acb\u7684\u5b57\u7b26\u5207\u574f\u3002 \u53cd\u8f6c\u6570\u7ec4\uff0c\u4e0d\u4e00\u5b9a\u80fd\u628a\u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\uff0c\u56e0\u4e3a\u53ef\u80fd\u4e0d\u614e\u628a\u4e00\u4e2a\u5b57\u7b26\u7684\u591a\u4e2a\u7801\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u5b57\u7b26\u7834\u574f\u3002 \u4f18\u70b9\u662f\uff1a \u8282\u7ea6\u5b58\u50a8\u7a7a\u95f4\u3002 \u6211\u4eec\u63a8\u8350\u53ea\u5728\u7f51\u7edc\u901a\u4fe1\u3001\u786c\u76d8\u5b58\u50a8\u65f6\uff0c\u91c7\u7528 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u6587\u5b57\u3002 \u603b\u4e4b\uff0cUTF-8 \u9002\u5408\u5b58\u50a8\uff0cUTF-32 \u9002\u5408\u5904\u7406\u3002 \u6211\u4eec\u5efa\u8bae\u8ba1\u7b97\u673a\u4ece\u786c\u76d8\u6216\u7f51\u7edc\u4e2d\u8bfb\u51fa UTF-8 \u5b57\u7b26\u4e32\u540e\uff0c\u7acb\u5373\u5c06\u5176\u8f6c\u6362\u4e3a UTF-32\uff0c\u4ee5\u65b9\u4fbf\u540e\u7eed\u6587\u5b57\u5904\u7406\u3002\u5f53\u9700\u8981\u5199\u5165\u786c\u76d8\u6216\u7f51\u7edc\u65f6\uff0c\u518d\u8f6c\u6362\u56de UTF-8\uff0c\u907f\u514d\u786c\u76d8\u5bb9\u91cf\u548c\u7f51\u7edc\u5e26\u5bbd\u7684\u6d6a\u8d39\u3002 \u8ba1\u7b97\u673a\u9700\u8981\u5916\u7801\u548c\u5185\u7801\u4e24\u79cd\uff1a \u5916\u7801=\u786c\u76d8\u4e2d\u7684\u6587\u672c=UTF-32 \u5185\u7801=\u5185\u5b58\u4e2d\u7684\u6587\u672c=UTF-8","title":"\u201c\u6211\u7231\ud883\udede\ud883\udede\u9762!\u201d"},{"location":"unicode/#utf-16","text":"UTF-16 \u7684\u7b56\u7565\u662f\uff1a\u65e2\u7136\u5927\u591a\u6570\u5e38\u7528\u5b57\u7b26\u7684\u7801\u70b9\u90fd\u5728 0x0 \u5230 0xFFFF \u5185\uff0c\u7528 uint32_t \u6765\u5b58\u50a8\u4e5f\u592a\u6d6a\u8d39\u4e86\u3002\u4ed6\u7684\u65b9\u6848\u5982\u4e0b\uff1a \u5bf9\u4e8e 0x0 \u5230 0xFFFF \u8303\u56f4\u5185\u7684\u5b57\u7b26\uff0c\u5c31\u7528\u4e00\u4e2a uint16_t \u76f4\u63a5\u5b58\u3002 \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u53cd\u6b63\u4e0d\u5e38\u89c1\uff0c\u5c31\u62c6\u6210\u4e24\u4e2a uint16_t \u5b58\u3002\u8fd9\u4e2a\u62c6\u7684\u65b9\u6848\u5f88\u6709\u8bb2\u7a76\uff0c\u5982\u679c\u53ea\u662f\u666e\u901a\u7684\u62c6\uff0c\u7531\u4e8e\u89e3\u7801\u65f6\u6536\u5230\u7684\u662f\u4e2a\u6ca1\u5934\u6ca1\u5c3e\u7684\u5b57\u8282\u5e8f\u5217\uff0c\u65e0\u6cd5\u5206\u8fa8\u8fd9\u5230\u5e95\u662f\u4e24\u4e2a uint16_t \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u8fd8\u662f\u4e00\u4e2a uint16_t \u7684\u666e\u901a\u5b57\u7b26\u3002 \u4f8b\u5982\uff0c\u6211\u4eec\u628a\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u201c\ud883\udede\u201d\uff0c0x30EDE\u3002\u62c6\u6210\u4e24\u4e2a uint16_t \uff0c\u5f97\u5230 0x3 \u548c 0x0EDE\u3002\u5982\u679c\u76f4\u63a5\u5b58\u50a8\u8fd9\u4e24\u4e2a uint16_t \uff1a 0x0003 0x0EDE \u4e4b\u540e\u89e3\u7801\u65f6\uff0c\u5148\u8bfb\u5230 0x0003\uff0c\u8fd8\u4f1a\u4ee5\u4e3a\u4ed6\u662f\u5355\u72ec\u7684\u4e00\u4e2a uint16_t \uff0c\u8868\u793a 3 \u53f7\u5b57\u7b26\u201c\u201d\u3002\u540e\u9762\u7684 0x0EDE \u5c31\u53d8\u6210\u4e86\u4e00\u4e2a\u5355\u72ec\u7684 0x0EDE\uff0c\u53d8\u6210\u4e86 0x0EDE \u53f7\u5b57\u7b26 \u201c\u0ede\u201d\u3002\u8fd9\u6837\u4e00\u6765\uff0c\u201c\ud883\udede\u201d\u5c31\u53d8\u6210\u4e86\u4e24\u4e2a\u6beb\u4e0d\u76f8\u5e72\u7684\u5b57\u7b26\uff0c\u201c\u0ede\u201d\u4e86\u3002 \u4e3a\u4e86\u907f\u514d\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u4e24\u4e2a uint16_t \u9700\u8981\u91c7\u7528\u4e00\u79cd\u7279\u6b8a\u7684\u65b9\u5f0f\u4ee5\u793a\u533a\u5206\u3002\u8ba9\u89e3\u7801\u5668\u4e00\u770b\u5230\uff0c\u5c31\u80fd\u786e\u5b9a\u8fd9\u4e24\u4e2a uint16_t \u9700\u8981\u7ec4\u88c5\u6210\u540c\u4e00\u4e2a\u5b57\u7b26\u3002 \u8fd9\u5c31\u7528\u5230\u4e86\u4e00\u4e2a\u201c\u6f0f\u6d1e\u201d\uff1aUnicode \u5e76\u6ca1\u6709\u628a\u7801\u70b9\u5206\u914d\u7684\u6ee1\u6ee1\u5f53\u5f53\uff0c\u6216\u8bb8\u662f\u51fa\u4e8e\u5148\u89c1\u4e4b\u660e\uff0c\u5728 0xD800 \u5230 0xDFFF \u4e4b\u95f4\u9884\u7559\u4e86\u4e00\u5927\u6bb5\u7a7a\u53f7\uff1a UTF-16 \u5c31\u662f\u5229\u7528\u4e86\u8fd9\u4e00\u6bb5\u7a7a\u95f4\uff0c\u4ed6\u89c4\u5b9a\uff1a0xD800 \u5230 0xDFFF \u4e4b\u95f4\u7684\u7801\u70b9\u5c06\u6c38\u8fdc\u4e0d\u7528\u6765\u8868\u793a\u5b57\u7b26\uff0c\u800c\u662f\u4f5c\u4e3a \u4ee3\u7406\u5bf9 (surrogate-pair) \u3002\u5176\u4e2d 0xD800 \u5230 0xDBFF \u662f \u9ad8\u4f4d\u4ee3\u7406 (high surrogate) \uff0c0xDC00 \u5230 0xDFFF \u662f \u4f4e\u4f4d\u4ee3\u7406 (low surrogate) \u3002\u9ad8\u4ee3\u7406\u5728\u524d\uff0c\u4f4e\u4ee3\u7406\u5728\u540e\u3002 \u4e00\u4e2a\u8d85\u8fc7 0xFFFF \u7684\u7a00\u6709\u5b57\u7b26\uff0c\u4f1a\u88ab\u62c6\u6210\u4e24\u6bb5\uff0c\u4e00\u6bb5\u653e\u5728\u9ad8\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u6bb5\u653e\u5728\u4f4e\u4f4d\u4ee3\u7406\u91cc\uff0c\u4e00\u524d\u4e00\u540e\u653e\u5165 uint16_t \u5e8f\u5217\u4e2d\u3002 \u642d\u8f7d\u8d85\u5bbd\u8d85\u9650\u8d27\u7269\u7684\u8f66\u8f86\u9700\u8981\u88ab\u62c6\u5206\u6210\u4e24\u6bb5\u518d\u8fdb\u5165\u96a7\u9053\u3002 \u5177\u4f53\u62c6\u5206\u65b9\u6cd5\u5982\u4e0b\uff1a \u5bf9\u4e8e 0xFFFF \u5230 0x10FFFF \u8303\u56f4\u7684\u7801\u70b9\uff0c\u9996\u5148\u5c06\u5176\u503c\u51cf\u53bb 0x10000\uff0c\u53d8\u6210\u4e00\u4e2a\u8303\u56f4 0x0 \u5230 0xFFFFF \u8303\u56f4\u5185\u7684\u6570\u5b57\uff0c\u8fd9\u80fd\u4fdd\u8bc1\u4ed6\u4eec\u53ea\u9700 20 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u5373\u53ef\u8868\u793a\u3002 \u4f8b\u5982\u201c\ud883\udede\u201d\u5bf9\u5e94\u7684\u7801\u70b9 0x30EDE\uff0c\u51cf\u53bb\u540e\u5c31\u53d8\u6210 0x20EDE\u3002 \u7136\u540e\uff0c\u5199\u51fa 0x20EDE \u7684\u4e8c\u8fdb\u5236\u8868\u793a\uff1a 00100000111011011110 \u603b\u5171 20 \u4f4d\uff0c\u6211\u4eec\u5c06\u5176\u62c6\u6210\u9ad8\u4f4e\u5404 10 \u4f4d\uff1a 0010000011 1011011110 \u5404\u81ea\u5199\u51fa\u76f8\u5e94\u7684\u5341\u516d\u8fdb\u5236\u6570\uff1a 0x083 0x2DE \u56e0\u4e3a\u6700\u591a\u53ea\u6709 10 \u4f4d\uff0c\u8fd9\u4e24\u4e2a\u6570\u90fd\u4f1a\u5728 0 \u5230 0x3FF \u7684\u8303\u56f4\u5185\u3002 \u800c 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u9884\u7559\u7684\u7a7a\u95f4\uff0c\u521a\u597d\u53ef\u4ee5\u5206\u522b\u5bb9\u7eb3 0x400 \u4e2a\u6570\uff01 \u6240\u4ee5\uff0c\u6211\u4eec\u5c06\u62c6\u5206\u51fa\u6765\u7684\u4e24\u4e2a 10 \u4f4d\u6570\uff0c\u5206\u522b\u52a0\u4e0a 0xD800 \u548c 0xDC00\uff1a 0xD800+0x083=0xD883 0xDC00+0x2DE=0xDFDE \u8fd9\u4e24\u4e2a\u6570\uff0c\u5fc5\u5b9a\u662f 0xD800 \u5230 0xDBFF\uff0c\u548c 0xDC00 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u6570\u3002\u800c\u8fd9\u4e24\u4e2a\u8303\u56f4\u90fd\u662f Unicode \u59d4\u5458\u4f1a\u9884\u7559\u7684\u4ee3\u7406\u5bf9\u533a\u95f4\uff0c\u7edd\u5bf9\u6ca1\u6709\u666e\u901a\u5b57\u7b26\u3002\u6240\u4ee5\uff0c\u751f\u6210\u7684\u4e24\u4e2a\u4ee3\u7406\u5bf9\u4e0d\u4f1a\u4e0e\u666e\u901a\u5b57\u7b26\u4ea7\u751f\u6b67\u4e49\uff0c\u53ef\u4ee5\u653e\u5fc3\u653e\u8fdb uint16_t \u6570\u7ec4\uff0c\u89e3\u7801\u5668\u5982\u679c\u68c0\u6d4b\u5230\u4ee3\u7406\u5bf9\uff0c\u5c31\u8bf4\u660e\u662f\u4e24\u8282\u8f66\u53a2\uff0c\u53ef\u4ee5\u653e\u5fc3\u8fde\u7eed\u8bfb\u53d6\u4e24\u4e2a uint16_t \u3002 \u6240\u4ee5\uff0c 0xD883 0xDFDE \u5c31\u662f\u201c\ud883\udede\u201d\u7528 UTF-16 \u7f16\u7801\u540e\u7684\u7ed3\u679c\u3002 \u4ee3\u7406\u5b57\u7b26\u4e0d\u662f\u4e00\u4e2a\u5b8c\u6574\u7684\u5b57\u7b26\uff0c\u5f53\u89e3\u7801\u5668\u68c0\u6d4b\u5230\u4e00\u4e2a 0xD800 \u5230 0xDBFF \u8303\u56f4\u5185\u7684\u9ad8\u4ee3\u7406\u65f6\uff0c\u5c31\u9884\u793a\u7740\u8fd8\u9700\u8981\u518d\u8bfb\u53d6\u4e00\u4e2a\u4f4e\u4ee3\u7406\uff0c\u624d\u80fd\u62fc\u63a5\u6210\u4e00\u4e2a\u7a00\u6709\u5b57\u7b26\u3002 \u5982\u679c\u63a5\u4e0b\u6765\u8bfb\u5230\u7684\u4e0d\u662f 0xDC00 \u5230 0xDFFF \u8303\u56f4\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u800c\u662f\u666e\u901a\u5b57\u7b26\u7684\u8bdd\uff0c\u90a3\u5c31\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u53ef\u80fd\u662f\u4e2d\u95f4\u88ab\u4eba\u4e22\u5305\u4e86\uff0c\u9700\u8981\u62a5\u9519\u6216\u8005\u7528\u9519\u8bef\u5b57\u7b26\u201c\ufffd\u201d\u9876\u66ff\u3002 \u53e6\u5916\uff0c\u5982\u679c\u8bfb\u5230\u4e86\u4e00\u4e2a\u5355\u72ec\u5b58\u5728\u7684 0xD800 \u5230 0xDFFF \u8303\u56f4\u5185\u7684\u4f4e\u4ee3\u7406\u5b57\u7b26\uff0c\u90a3\u4e5f\u8bf4\u660e\u51fa\u9519\u4e86\uff0c\u56e0\u4e3a\u4ee3\u7406\u5b57\u7b26\u53ea\u6709\u6210\u5bf9\u51fa\u73b0\u624d\u6709\u610f\u4e49\uff0c\u4f4e\u4ee3\u7406\u5b57\u7b26\u4e0d\u53ef\u80fd\u5355\u72ec\u5728\u5f00\u5934\u51fa\u73b0\u3002 \u53ef\u89c1\uff0cUTF-16 \u548c UTF-8 \u4e00\u6837\uff0c\u90fd\u662f\u201c\u5c0f\u706b\u8f66\u201d\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0cUTF-16 \u540c\u6837\u4e5f\u6709\u7740\u7c7b\u4f3c\u4e8e UTF-8 \u7684\u6297\u5e72\u6270\u673a\u5236\u3002","title":"UTF-16"},{"location":"unicode/#_7","text":"\u5728\u8ba1\u7b97\u673a\u4e2d\uff0c\u591a\u5b57\u8282\u7684\u6574\u6570\u7c7b\u578b\uff08\u5982 uint16_t \u548c uint32_t \uff09\u9700\u8981\u88ab\u62c6\u6210\u591a\u4e2a\u5b57\u8282\u6765\u5b58\u50a8\u3002\u62c6\u5f00\u540e\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u6309\u4ec0\u4e48\u987a\u5e8f\u5b58\u5165\u5185\u5b58\uff1f\u4e0d\u540c\u7684\u786c\u4ef6\u67b6\u6784\u4ea7\u751f\u4e86\u4e89\u6267\uff1a \u5927\u7aef\u6d3e (bit endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u4e5f\u5c31\u662f\u5927\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5c0f\u7aef\u6d3e (little endian)\uff1a\u4f4e\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u4f4e\u4f4d\uff0c\u9ad8\u5730\u5740\u5b58\u653e\u6574\u6570\u7684\u9ad8\u4f4d\uff0c\u4e5f\u5c31\u662f\u5c0f\u6570\u9760\u524d\uff01\u8fd9\u6837\u6570\u503c\u7684\u9ad8\u4f4d\u548c\u4f4e\u4f4d\u548c\u8ba1\u7b97\u673a\u7535\u8def\u7684\u8ba1\u7b97\u4e60\u60ef\u4e00\u81f4\u3002\u4f8b\u5982\uff0c0x12345678\uff0c\u5728\u5185\u5b58\u4e2d\u5c31\u662f\uff1a 0x78 0x56 0x34 0x12 \u4f8b\u5982\uff0cIntel \u7684 x86 \u67b6\u6784\u548c ARM \u516c\u53f8\u7684 ARM \u67b6\u6784\u90fd\u662f\u5c0f\u7aef\u6d3e\uff0c\u800c Motorola \u516c\u53f8\u7684 68k \u67b6\u6784\u548c Sun \u516c\u53f8\u7684 SPARC \u67b6\u6784\u90fd\u662f\u5927\u7aef\u6d3e\u3002 \u8fd9\u5176\u5b9e\u662f\u5f88\u65e0\u804a\u7684\u4e89\u6267\uff0c\u4e3a\u4eba\u7c7b\u7684\u4e66\u5199\u4e60\u60ef\u6539\u53d8\u8ba1\u7b97\u673a\u7684\u8bbe\u8ba1\u6beb\u65e0\u9053\u7406\uff0c\u6bd5\u7adf\u4e16\u754c\u4e0a\u4e5f\u6709\u4ece\u53f3\u5f80\u5de6\u4e66\u5199\u7684\u6587\u5b57\u548c\u4ece\u4e0a\u5f80\u4e0b\u4e66\u5199\u7684\u6587\u5b57\uff0c\u751a\u81f3\u6709\u5de6\u53f3\u6765\u56de\u4e66\u5199\u7684\u6587\u5b57\u2026\u2026\u5982\u679c\u8981\u4f3a\u5019\u4eba\u7c7b\uff0c\u4f60\u600e\u4e48\u4e0d\u6539\u6210\u5341\u8fdb\u5236\u5462\uff1f\u603b\u4e4b\uff0c\u6211\u8ba4\u4e3a\u5c0f\u7aef\u624d\u662f\u6700\u9002\u5408\u8ba1\u7b97\u673a\u7684\uff0c\u5e02\u9762\u4e0a\u5927\u591a\u6570\u4e3b\u6d41\u786c\u4ef6\u90fd\u662f\u5c0f\u7aef\u67b6\u6784\u3002 \u5728\u7f51\u7edc\u901a\u4fe1\u65f6\uff0c\u53d1\u6d88\u606f\u548c\u6536\u6d88\u606f\u7684\u53ef\u80fd\u662f\u4e0d\u540c\u7684\u67b6\u6784\uff0c\u5982\u679c\u53d1\u6d88\u606f\u7684\u662f\u5c0f\u7aef\u67b6\u6784\uff0c\u6536\u6d88\u606f\u7684\u662f\u5927\u7aef\u67b6\u6784\uff0c\u90a3\u4e48\u53d1\u51fa\u53bb\u7684\u662f 0x12345678\uff0c\u6536\u5230\u7684\u5c31\u4f1a\u53d8\u6210 0x78563421 \u4e86\u3002 \u56e0\u6b64\u4e92\u8054\u7f51\u4e00\u822c\u89c4\u5b9a\uff0c\u6240\u6709\u591a\u5b57\u8282\u7684\u6570\u636e\u5728\u7f51\u7edc\u5305\u4e2d\u7edf\u4e00\u91c7\u7528\u5927\u7aef\u3002\u5bf9\u4e8e\u5927\u7aef\u67b6\u6784\uff0c\u4ed6\u4eec\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u5bf9\u4e8e\u5c0f\u7aef\u67b6\u6784\uff0c\u5728\u53d1\u5305\u524d\u9700\u8981\u628a\u81ea\u5df1\u7684\u5c0f\u7aef\u6570\u636e\u505a\u5b57\u8282\u5e8f\u53cd\u8f6c\uff0c\u53d8\u6210\u5927\u7aef\u7684\u4ee5\u540e\uff0c\u518d\u53d1\u9001\u3002\u4e4b\u540e\u7684\u7f51\u7edc\u4e13\u9898\u8bfe\u4e2d\u6211\u4eec\u4f1a\u8be6\u89e3\u8fd9\u4e00\u5757\u3002 \u57fa\u4e8e\u5b57\u8282\u7801\u7684\u865a\u62df\u673a\u8bed\u8a00\u901a\u5e38\u4f1a\u89c4\u5b9a\u4e00\u4e2a\u5b57\u8282\u5e8f\uff1a\u50cf Java \u8fd9\u79cd\u9762\u5411\u4e92\u8054\u7f51\u8bed\u8a00\uff0c\u7d22\u6027\u4e5f\u89c4\u5b9a\u4e86\u7edf\u4e00\u91c7\u7528\u5927\u7aef\uff0c\u65e0\u8bba JVM \u8fd0\u884c\u5728\u5927\u7aef\u673a\u5668\u8fd8\u662f\u5c0f\u7aef\u673a\u5668\u4e0a\u3002\u8fd9\u4f7f\u5f97\u4ed6\u4e0e\u4e92\u8054\u7f51\u901a\u4fe1\u6bd4\u8f83\u65b9\u4fbf\uff0c\u800c\u5728 x86 \u548c ARM \u67b6\u6784\u4e0a\uff0c\u4e0e\u672c\u5730\u53ea\u63a5\u53d7\u5c0f\u7aef\u6570\u636e\u7684 API\uff0c\u4f8b\u5982 OpenGL\uff0c\u6c9f\u901a\u8f83\u4e3a\u56f0\u96be\uff0c\u9700\u8981\u505a\u989d\u5916\u7684\u5b57\u8282\u5e8f\u8f6c\u6362\u3002\u800c C# \u4e3b\u6253\u6e38\u620f\u4e1a\u52a1\uff08\u4f8b\u5982 Unity\uff09\uff0c\u9700\u8981\u8003\u8651\u6027\u80fd\uff0c\u6240\u4ee5\u89c4\u5b9a\u5168\u90e8\u91c7\u7528\u5c0f\u7aef\u3002\u4f5c\u4e3a\u5e95\u5c42\u7f16\u7a0b\u8bed\u8a00\u7684 C++ \u5219\u662f\u5165\u4e61\u968f\u4fd7\uff0c\u4f60\u7684\u786c\u4ef6\u662f\u4ec0\u4e48\u7aef\uff0c\u4ed6\u5c31\u662f\u4ec0\u4e48\u7aef\uff0c\u4e0d\u4e3b\u52a8\u505a\u4efb\u4f55\u989d\u5916\u7684\u8f6c\u6362\u3002 UTF-16 \u548c UTF-32 \u7684\u7801\u4f4d\u90fd\u662f\u591a\u5b57\u8282\u7684\uff0c\u4e5f\u4f1a\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u3002\u4f8b\u5982\uff0cUTF-16 \u4e2d\u7684 uint16_t \u5e8f\u5217\uff1a 0x1234 0x5678 \u5728\u5927\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x12 0x34 0x56 0x78 \u5728\u5c0f\u7aef\u6d3e\u7684\u673a\u5668\u4e2d\uff0c\u5c31\u662f\uff1a 0x34 0x12 0x78 0x56 \u8fd9\u6837\u4e00\u6765\uff0cUTF-16 \u548c UTF-32 \u7684\u5b57\u8282\u6d41\uff0c\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u53ef\u80fd\u4f1a\u6709\u4e0d\u540c\u7684\u987a\u5e8f\u3002\u8fd9\u7ed9\u8de8\u5e73\u53f0\u7684\u6587\u672c\u5904\u7406\u5e26\u6765\u4e86\u9ebb\u70e6\u3002 \u6240\u4ee5\u5f53\u4f60\u9700\u8981\u628a UTF-16 \u5b58\u5165\u786c\u76d8\u548c\u5728\u7f51\u7edc\u53d1\u9001\u65f6\uff0c\u8fd8\u9700\u8981\u989d\u5916\u6307\u660e\u4f60\u7528\u7684\u662f\u5927\u7aef\u7684 UTF-16 \u8fd8\u662f\u5c0f\u7aef\u7684 UTF-16\u3002 \u56e0\u6b64 UTF-16 \u548c UTF-32 \u8fdb\u4e00\u6b65\u5206\u88c2\u4e3a\uff1a UTF-16LE\uff1a\u5c0f\u7aef\u7684 UTF-16 UTF-16BE\uff1a\u5927\u7aef\u7684 UTF-16 UTF-32LE\uff1a\u5c0f\u7aef\u7684 UTF-32 UTF-32BE\uff1a\u5927\u7aef\u7684 UTF-32 \u5982\u679c\u53ea\u5728\u5185\u5b58\u7684 wchar_t \u4e2d\u4f7f\u7528\u5c31\u4e0d\u7528\u533a\u5206\uff0c\u9ed8\u8ba4\u8ddf\u968f\u5f53\u524d\u673a\u5668\u7684\u5927\u5c0f\u7aef\u3002\u6240\u4ee5 UTF-16 \u548c UTF-32 \u901a\u5e38\u53ea\u4f1a\u51fa\u73b0\u5728\u5185\u5b58\u4e2d\u7528\u4e8e\u5feb\u901f\u5904\u7406\u548c\u8ba1\u7b97\uff0c\u5f88\u5c11\u7528\u5728\u5b58\u50a8\u548c\u901a\u4fe1\u4e2d\u3002 UTF-8 \u662f\u57fa\u4e8e\u5355\u5b57\u8282\u7684\u7801\u4f4d\uff0c\u706b\u8f66\u5934\u7684\u987a\u5e8f\u4e5f\u6709\u4e25\u683c\u89c4\u5b9a\uff0c\u706b\u8f66\u5934\u603b\u662f\u5728\u6700\u524d\uff0c\u6839\u672c\u4e0d\u53d7\u5b57\u8282\u5e8f\u5927\u5c0f\u7aef\u5f71\u54cd\uff0c\u4e5f\u5c31\u6ca1\u6709\u5f71\u54cd\u3002 \u7531\u4e8e\u538b\u7f29\u7387\u4f4e\uff0c\u53c8\u5b58\u5728\u5927\u5c0f\u7aef\u5b57\u8282\u5e8f\u4e0d\u540c\u7684\u95ee\u9898\u3002\u800c\u4e92\u8054\u7f51\u6570\u636e\u9700\u8981\u4fdd\u8bc1\u76f8\u540c\u7684\u5927\u5c0f\u7aef\uff0c\u5728\u6536\u53d1\u5305\u65f6\u9700\u8981\u989d\u5916\u8f6c\u6362\uff0c\u56e0\u800c\u53ef\u80fd\u4e0d\u592a\u9002\u5408\u7f51\u7edc\u3002\u800c UTF-8 \u7684\u5b58\u50a8\u5355\u4f4d\u662f\u5b57\u8282\uff0c\u5929\u751f\u6ca1\u6709\u5927\u5c0f\u7aef\u56f0\u6270\u3002\u66f4\u5999\u7684\u662f\uff0c\u4ed6\u4e14\u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u800c\u4e92\u8054\u7f51\u53c8\u662f\u53e4\u8463\u4e2d\u95f4\u4ef6\u6700\u591a\u7684\u5730\u65b9\u2026\u2026 \u603b\u4e4b\uff0c\u5b8c\u5168\u57fa\u4e8e\u5b57\u8282\u7684 UTF-8 \u662f\u6700\u9002\u5408\u7f51\u7edc\u901a\u4fe1\u548c\u786c\u76d8\u5b58\u50a8\u7684\u6587\u672c\u7f16\u7801\u683c\u5f0f\uff0c\u800c UTF-32 \u662f\u6700\u9002\u5408\u5728\u5185\u5b58\u4e2d\u5904\u7406\u7684\u683c\u5f0f\u3002","title":"\u5b57\u8282\u5e8f\u95ee\u9898\uff0c\u5927\u5c0f\u7aef\u4e4b\u4e89"},{"location":"unicode/#bom","text":"0xFEFF \u662f\u4e00\u4e2a\u7279\u6b8a\u7684\u4e0d\u53ef\u89c1\u5b57\u7b26\u201c\ufeff\u201d\uff0c\u8fd9\u662f\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u6ca1\u6709\u4efb\u4f55\u6548\u679c\u3002 \u4f60\u53ef\u4ee5\u628a\u8fd9\u4e2a\u5b57\u7b26\u52a0\u5728\u6587\u672c\u6587\u4ef6\u7684\u5934\u90e8\uff0c\u544a\u8bc9\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\uff0c\u8fd9\u4e2a\u6587\u4ef6\u662f\u7528\u4ec0\u4e48\u7f16\u7801\u7684\u3002 \u5982\u679c\u662f UTF-16 \u548c UTF-32\uff0c\u56e0\u4e3a 0xFEFF \u4e0d\u5bf9\u79f0\uff0c\u4ed6\u8fd8\u80fd\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\u3002\u56e0\u6b64 0xFEFF \u88ab\u79f0\u4e3a\u5b57\u8282\u5e8f\u6807\u5fd7\uff08Byte-order-mark\uff0cBOM\uff09\u3002 \u5982\u679c\u8bfb\u53d6\u8be5\u6587\u4ef6\u7684\u8f6f\u4ef6\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u90a3\u4e48\u4ed6\u7167\u5e38\u8bfb\u51fa 0xFEFF\uff0c\u4e00\u4e2a\u96f6\u5bbd\u7a7a\u683c\uff0c\u5728\u6587\u672c\u4e2d\u4e0d\u663e\u793a\uff0c\u4e0d\u5f71\u54cd\u89c6\u89c9\u7ed3\u679c\u3002 \u4e00\u4e9b\u8001\u7684\u7f16\u8bd1\u5668\uff08\u8fdc\u53e4 MinGW\uff0c\u73b0\u5728\u5df2\u7ecf\u6ca1\u6709\u4e86\uff09\u4e0d\u652f\u6301\u89e3\u6790 BOM\uff0c\u4f1a\u628a\u5e26\u6709 BOM \u7684 UTF-8 \u7684 .cpp \u6e90\u7801\u6587\u4ef6\uff0c\u5f53\u4f5c\u5934\u90e8\u5e26\u6709\u9519\u8bef\u5b57\u7b26\u7684\u4e71\u7801\u6587\u4ef6\uff0c\u4ece\u800c\u62a5\u9519\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u7684\u8bb0\u4e8b\u672c\u4fdd\u5b58\u4e3a UTF-8 \u65f6\uff0c\u603b\u662f\u4f1a\u52a0\u4e0a BOM\u3002\u5982\u679c\u8bb0\u4e8b\u672c\u53d1\u73b0\u4e00\u4e2a\u6587\u4ef6\u6ca1\u6709 BOM\uff0c\u4f1a\u5f53\u4f5c ANSI\uff08GBK\uff09\u6765\u8bfb\u53d6\u3002 0xFEFF \u5728\u4e0d\u540c\u7684\u7f16\u7801\u4e0b\u4f1a\u4ea7\u751f\u4e0d\u540c\u7684\u7ed3\u679c\uff1a UTF-8\uff1a 0xEF 0xBB 0xBF \uff0c\u4ed6\u4f1a\u5360\u7528 3 \u5b57\u8282\uff0c\u800c\u4e14\u4e0d\u4f1a\u544a\u8bc9\u4f60\u662f\u5927\u7aef\u8fd8\u662f\u5c0f\u7aef\uff0c\u56e0\u4e3a UTF-8 \u662f\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\u7684\u3002 UTF-16\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE \u3002 UTF-32\uff1a\u5982\u679c\u662f\u5927\u7aef\uff0c\u5c31\u662f 0x00 0x00 0xFE 0xFF \uff0c\u5982\u679c\u662f\u5c0f\u7aef\uff0c\u5c31\u662f 0xFF 0xFE 0x00 0x00 \u3002 \u56e0\u6b64\uff0c\u5728\u6587\u672c\u5934\u90e8\u52a0\u4e0a BOM \u6709\u52a9\u4e8e\u8f6f\u4ef6\u63a8\u6d4b\u8be5\u6587\u4ef6\u662f\u4ec0\u4e48\u7f16\u7801\u7684\uff08\u5982\u679c\u90a3\u8f6f\u4ef6\u652f\u6301\u89e3\u6790 BOM \u7684\u8bdd\uff09\u3002 \u4f8b\u5982 Windows \u73af\u5883\u4e2d\uff0c\u6240\u6709\u7684\u6587\u672c\u6587\u4ef6\u90fd\u88ab\u9ed8\u8ba4\u5047\u5b9a\u4e3a ANSI\uff08GBK\uff09\u7f16\u7801\uff0c\u5982\u679c\u4f60\u8981\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u4e3a UTF-8 \u7f16\u7801\uff0c\u5c31\u9700\u8981\u52a0\u4e0a BOM \u6807\u5fd7\u3002\u5f53 MSVC \u8bfb\u53d6\u65f6\uff0c\u770b\u5230\u5f00\u5934\u662f 0xEF 0xBB 0xBF \uff0c\u5c31\u660e\u767d\u8fd9\u662f\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u6587\u4ef6\u3002\u8fd9\u6837\uff0cMSVC \u5c31\u80fd\u6b63\u786e\u5730\u5904\u7406\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4e86\u3002\u5982\u679c MSVC \u6ca1\u770b\u5230 BOM\uff0c\u4f1a\u9ed8\u8ba4\u4ee5\u4e3a\u662f ANSI\uff08GBK\uff09\u7f16\u7801\u7684\uff0c\u4ece\u800c\u4e2d\u6587\u5b57\u7b26\u4e32\u5e38\u91cf\u4f1a\u4e71\u7801\u3002\u5f00\u542f /utf-8 \u9009\u9879\u4e5f\u80fd\u8ba9 MSVC \u628a\u6ca1\u6709 BOM \u7684\u6e90\u7801\u6587\u4ef6\u5f53\u4f5c UTF-8 \u6765\u89e3\u6790\uff0c\u9002\u5408\u8de8\u5e73\u53f0\u5b9d\u5b9d\u4f53\u8d28\u3002","title":"BOM \u6807\u8bb0"},{"location":"unicode/#cc","text":"","title":"C/C++ \u4e2d\u7684\u5b57\u7b26\u7f16\u7801"},{"location":"unicode/#_8","text":"\u7c7b\u578b \u5927\u5c0f \u7f16\u7801 \u5b57\u9762\u91cf Linux char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e $LC_ALL \u201chello\u201d Windows char 1 \u5b57\u8282 \u53d6\u51b3\u4e8e\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e \u201chello\u201d Linux wchar_t 4 \u5b57\u8282 UTF-32 L\u201dhello\u201d Windows wchar_t 2 \u5b57\u8282 UTF-16 L\u201dhello\u201d char8_t 1 \u5b57\u8282 UTF-8 u8\u201dhello\u201d char16_t 2 \u5b57\u8282 UTF-16 u\u201dhello\u201d char32_t 4 \u5b57\u8282 UTF-32 U\u201dhello\u201d \u7531\u6b64\u53ef\u89c1\uff0c char \u548c wchar_t \u662f\u4e0d\u8de8\u5e73\u53f0\u7684\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f GBK\u3002\u5bf9\u4e8e\u7f8e\u56fd\u533a Windows \u6765\u8bf4\uff0c\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\u3002 \u5bf9\u4e8e Linux \u7528\u6237\u6765\u8bf4\uff0c\u5982\u679c\u4f60\u6ca1\u6709\u4e13\u95e8\u4fee\u6539\u8fc7\uff0c $LC_ALL \u9ed8\u8ba4\u662f en_US.UTF-8 \u6216 C.UTF-8 \u3002 \u8fd9\u5e26\u6765\u4e86\u5de8\u5927\u7684\u6df7\u6dc6\uff01\u5f88\u591a\u7f8e\u56fd\u7a0b\u5e8f\u5458\u6f5c\u610f\u8bc6\u91cc\u4f1a\u60f3\u5f53\u7136\u5730\u628a char \u5f53\u4f5c UTF-8 \u6765\u7528\u3002\u5f88\u591a\u5f00\u6e90\u9879\u76ee\uff0c\u7b2c\u4e09\u65b9\u5e93\uff0c\u751a\u81f3\u5f88\u591a\u56fd\u4eba\u505a\u7684\u9879\u76ee\uff0c\u90fd\u88ab\u8fd9\u79cd\u201c\u60f3\u5f53\u7136\u201d\u4f20\u67d3\u4e86\u3002 \u597d\u6d88\u606f\u662f\u65e0\u8bba\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u662f\u4ec0\u4e48\uff0c\u80af\u5b9a\u517c\u5bb9 ASCII\u3002\u4f8b\u5982 GBK \u548c UTF-8 \u90fd\u517c\u5bb9 ASCII\uff0c\u5426\u5219\u5c31\u548c\u6240\u6709\u7684 C \u8bed\u8a00\u7ecf\u5178\u51fd\u6570\u5982 strlen \uff0c\u6362\u884c\u7b26 '\\n' \uff0c\u8def\u5f84\u5206\u9694\u7b26 '/' \u548c '\\\\' \u51b2\u7a81\u4e86\u3002 wchar_t \u5c31\u597d\u4e00\u4e9b\uff0c\u867d\u7136\u5728 Windows \u7cfb\u7edf\u4e0a\u662f\u7cdf\u7cd5\u7684 UTF-16\uff0c\u4f46\u81f3\u5c11\u7a33\u5b9a\u4e86\uff0c\u4e0d\u4f1a\u968f\u7740\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u800c\u968f\u610f\u6539\u53d8\uff0c\u53ea\u8981\u4f60\u4e0d\u6253\u7b97\u8de8\u5e73\u53f0\uff0c wchar_t \u5c31\u662f Windows \u7a0b\u5e8f\u7684\u6807\u914d\u3002","title":"\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#utf-8-ascii","text":"UTF-8 \u7684\u706b\u8f66\u5934\u548c\u8f66\u53a2\uff0c\u90fd\u662f 1 \u5f00\u5934\u7684\uff0c\u800c ASCII \u7684\u5355\u4f53\u706b\u8f66\u5934\u6c38\u8fdc\u662f 0 \u5f00\u5934\u3002\u8fd9\u5f88\u91cd\u8981\uff0c\u4e0d\u4ec5\u706b\u8f66\u5934\u9700\u8981\u548c ASCII \u533a\u5206\u5f00\u6765\uff0c\u8f66\u53a2\u4e5f\u9700\u8981\u3002\u8003\u8651\u8fd9\u6837\u4e00\u4e2a\u573a\u666f\uff1a std::u32string path = \"\u4e00\u4e2a\u8001\u4f2f.txt\"; \u201c\u4e00\u4e2a\u8001\u4f2f\u201d \u8f6c\u6362\u4e3a Unicode \u7801\u70b9\u5206\u522b\u662f\uff1a 0x4E00 0x4E2A 0x8001 0x4F2F \u5982\u679c\u8ba9\u4ed6\u4eec\u539f\u5c01\u4e0d\u52a8\u76f4\u63a5\u5b58\u50a8\u8fdb char \u6570\u7ec4\u91cc\uff1a 0x4E 0x00 0x4E 0x2A 0x80 0x01 0x4F 0x2F \u5c31\u51fa\u95ee\u9898\u4e86\uff01\u9996\u5148\uff0c\u8fd9\u91cc 0x4E00 \u7684 0x00 \u90e8\u5206\uff0c\u4f1a\u88ab C \u8bed\u8a00\u5f53\u4f5c\u662f\u5b57\u7b26\u4e32\u7684\u7ed3\u5c3e\u3002\u5982\u679c\u62ff\u8fd9\u6837\u7684\u5b57\u7b26\u4e32\u53bb\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u7684 open \u51fd\u6570\uff0c\u4ed6\u4f1a\u4ee5\u4e3a\u4f60\u5728\u6253\u5f00 0x4E \u5355\u4e2a\u5b57\u7b26\u7684\u6587\u4ef6\u540d\uff0c\u4e5f\u5c31\u662f \"N\" \u3002 \u66f4\u7cdf\u7cd5\u7684\u662f\uff0c0x2F \u5bf9\u5e94\u7684 ASCII \u5b57\u7b26\u662f '/' \uff0c\u662f\u8def\u5f84\u5206\u9694\u7b26\u3002\u64cd\u4f5c\u7cfb\u7edf\u4f1a\u4ee5\u4e3a\u4f60\u8981\u521b\u5efa\u4e00\u4e2a\u5b50\u6587\u4ef6\u5939\u4e0b\u7684\u6587\u4ef6 \"N\\x00N*\\x80\\x01O/.txt\" \uff0c\u6587\u4ef6\u5939\u540d\u5b57\u53eb \"N\\x00N*\\x80\\x01O\" \u800c\u6587\u4ef6\u53eb \".txt\" \u3002 \u4e3a\u4e86\u80fd\u8ba9\u9488\u5bf9 ASCII \u8bbe\u8ba1\u7684\u64cd\u4f5c\u7cfb\u7edf API \u652f\u6301\u4e2d\u6587\u6587\u4ef6\u540d\uff0c\u5c31\u53ea\u80fd\u7ed5\u5f00\u6240\u6709 0x7F \u4ee5\u4e0b\u7684\u503c\u3002\u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48 UTF-8 \u5bf9\u8f66\u53a2\u4e5f\u5168\u90e8\u62ac\u9ad8\u5230 0x80 \u4ee5\u4e0a\uff0c\u907f\u514d\u64cd\u4f5c\u7cfb\u7edf\u4e0d\u614e\u628a\u8f66\u53a2\u5f53\u4f5c\u662f '/' \u6216 '\\0' \u3002","title":"\u601d\u8003\uff1aUTF-8 \u4e3a\u4ec0\u4e48\u5b8c\u7f8e\u517c\u5bb9 ASCII"},{"location":"unicode/#utf-8_2","text":"\u7531\u4e8e\u5de8\u5927\u7684\u60ef\u6027\uff0c\u5f88\u591a\u4eba\u90fd\u60f3\u5f53\u7136\u7684\u628a std::string \u5f53\u4f5c UTF-8 \u6765\u4f7f\u7528\u3002\u5bf9\u4e8e\u7b80\u5355\u7684\u6253\u5370\uff0c\u5e38\u89c4\u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\uff0c\u662f\u6ca1\u95ee\u9898\u7684\u3002 \u5b57\u7b26\u4e32\u64cd\u4f5c\u6709\u4e0b\u9762\u8fd9\u51e0\u79cd\uff0c\u5f97\u76ca\u4e8e UTF-8 \u4f18\u79c0\u7684\u5e8f\u5217\u5316\u6d89\u53ca\u548c\u5197\u4f59\u6297\u5e72\u6270\u673a\u5236\uff0c\u7edd\u5927\u591a\u6570 ASCII \u652f\u6301\u7684\u64cd\u4f5c\uff0cUTF-8 \u5b57\u7b26\u4e32\u90fd\u80fd\u8f7b\u677e\u80dc\u4efb\uff0c\u552f\u72ec\u5176\u4e2d \u6d89\u53ca\u201c\u7d22\u5f15\u201d\u548c\u201c\u957f\u5ea6\u201d\u7684 \u4e00\u90e8\u5206\u64cd\u4f5c\u4e0d\u884c\u3002\u8fd9\u662f\u7531\u4e8e\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff0c\u5982\u679c\u9700\u8981\u505a\u201c\u7d22\u5f15\u201d\u7c7b\u64cd\u4f5c\uff0c\u8fd8\u662f\u5efa\u8bae\u5148\u8f6c\u6362\u6210\u5b9a\u957f\u7684 UTF-32 \u7f16\u7801\u3002 \u64cd\u4f5c UTF-8 UTF-32 GBK \u6c42\u5b57\u7b26\u4e32\u957f\u5ea6 \u00d7 \u221a \u00d7 \u5224\u65ad\u76f8\u7b49 \u221a \u221a \u221a \u5b57\u5178\u5e8f\u7684\u5927\u5c0f\u6bd4\u8f83 \u221a \u221a \u00d7 \u5b57\u7b26\u4e32\u62fc\u63a5 \u221a \u221a \u221a \u641c\u7d22\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u641c\u7d22\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u4e0b\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u6309\u7d22\u5f15\u83b7\u53d6\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u904d\u5386\u6240\u6709\u5b57\u7b26 \u00d7 \u221a \u00d7 \u6309\u5b50\u5b57\u7b26\u4e32\u5207\u7247 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5207\u7247 \u00d7 \u221a \u00d7 \u67e5\u627e\u5e76\u66ff\u6362\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u67e5\u627e\u5e76\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u221a \u221a \u00d7 \u6309\u7d22\u5f15\u5220\u9664\u5b50\u5b57\u7b26\u4e32 \u00d7 \u221a \u00d7 \u5220\u9664\u5355\u4e2a\u5b57\u7b26 \u00d7 \u221a \u00d7 \u4e3a\u4ec0\u4e48\uff1f\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u5b9e\u9a8c\uff1a std::string s = \"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \uff08\u4f7f\u7528 /utf-8 \u7f16\u8bd1\uff09\u8fd0\u884c\u540e\uff0c\u4f1a\u5f97\u5230 6\u3002 \u56e0\u4e3a std::string \u7684 size() \u8fd4\u56de\u7684\u662f char \u7684\u6570\u91cf\uff0c\u800c\u4e0d\u662f\u771f\u6b63\u5b57\u7b26\u7684\u6570\u91cf\u3002\u5728 UTF-8 \u4e2d\uff0c\u4e00\u4e2a\u975e ASCII \u7684\u5b57\u7b26\u4f1a\u88ab\u7f16\u7801\u4e3a\u591a\u4e2a char \uff0c\u5bf9\u4e8e\u4e2d\u6587\u800c\u8a00\uff0c\u4e2d\u6587\u90fd\u5728 0x2E80 \u5230 0x9FFF \u8303\u56f4\u5185\uff0c\u5c5e\u4e8e\u4e09\u7ea7\u5217\u8f66\uff0c\u4e5f\u5c31\u662f\u6bcf\u4e2a\u6c49\u5b57\u4f1a\u88ab\u7f16\u7801\u6210 3 \u4e2a char \u3002 char \u662f\u5b57\u8282\uff08\u7801\u4f4d\uff09\u800c\u4e0d\u662f\u771f\u6b63\u7684\u5b57\u7b26\uff08\u7801\u70b9\uff09\u3002\u771f\u6b63\u7684 Unicode \u5b57\u7b26\u5e94\u8be5\u662f char32_t \u7c7b\u578b\u7684\u3002\u8c03\u7528 std::string \u7684 size() \u6216\u8005 strlen \u5f97\u5230\u7684\u53ea\u662f\u201c\u5b57\u8282\u6570\u91cf\u201d\u3002 \u800c UTF-32 \u4e2d\uff0c\u6bcf\u4e2a\u5b57\u7b26\uff08\u7801\u70b9\uff09\u90fd\u5bf9\u5e94\u4e00\u4e2a\u72ec\u7acb\u7684 char32_t \uff08\u7801\u4f4d\uff09\uff0c size() \u5c31\u662f\u771f\u6b63\u7684\u201c\u5b57\u7b26\u6570\u91cf\u201d\uff0c\u8fd9\u5c31\u662f\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 std::u32string s = U\"\u4f60\u597d\"; fmt::println(\"s \u7684\u957f\u5ea6\uff1a{}\", s.size()); \u5982\u679c\u4f60\u7684\u64cd\u4f5c\u53ea\u6d89\u53ca\u5b57\u7b26\u4e32\u67e5\u62fc\u63a5\u4e0e\u67e5\u627e\uff0c\u90a3\u5c31\u53ef\u4ee5\u7528 UTF-8\u3002\u5982\u679c\u5927\u91cf\u6d89\u53ca\u7d22\u5f15\uff0c\u5207\u7247\uff0c\u5355\u4e2a\u5b57\u7b26\u7684\u64cd\u4f5c\uff0c\u90a3\u5c31\u5fc5\u987b\u7528 UTF-32\uff08\u5426\u5219\u4e00\u9047\u5230\u6c49\u5b57\u5c31\u4f1a\u51fa\u9519\uff09\u3002 std::vector slogan = { \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\", \"\u5168\u4e16\u754c\u7a0b\u5e8f\u5458\u5927\u56e2\u7ed3\u4e07\u5c81\", }; std::string joined; for (auto const &s: slogan) { joined += s; // \u53ea\u662f\u62fc\u63a5\u800c\u5df2\uff0cUTF-8 \u6ca1\u95ee\u9898 } UTF-8 \u6309\u7d22\u5f15\u5207\u7247\u7684\u51fa\u9519\u6848\u4f8b\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-8 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u8282\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\ufffd\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::println(\"UTF-32 \u4e0b\uff0c\u524d\u56db\u4e2a\u5b57\u7b26\uff1a{}\", s.substr(0, 4)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u53ea\u6709\u5f53\u7d22\u5f15\u6765\u81ea find \u7684\u7ed3\u679c\u65f6\uff0cUTF-8 \u5b57\u7b26\u4e32\u7684\u5207\u7247\u624d\u80fd\u6b63\u5e38\u5de5\u4f5c\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(\"\u516c\"); // pos = 12 fmt::println(\"UTF-8 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u8282\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; size_t pos = s.find(U'\u516c'); // pos = 4 fmt::println(\"UTF-32 \u4e0b\uff0c\u201c\u516c\u201d\u524d\u7684\u6240\u6709\u5b57\u7b26\uff1a{}\", s.substr(0, pos)); // \u4f1a\u6253\u5370 \u201c\u5c0f\u5f6d\u8001\u5e08\u201d \u6ce8\u610f\u5230\u8fd9\u91cc UTF-8 \u7684 \"\u516c\" \u9700\u8981\u662f\u5b57\u7b26\u4e32\uff0c\u800c\u4e0d\u662f\u5355\u4e2a\u5b57\u7b26\u3002 UTF-8 \u65e0\u6cd5\u53d6\u51fa\u5355\u4e2a\u975e ASCII \u5b57\u7b26\uff0c\u5bf9\u4e8e\u5355\u4e2a\u4e2d\u6587\u5b57\u7b26\uff0c\u4ecd\u7136\u53ea\u80fd\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u8fbe\uff08\u7531\u591a\u4e2a\u5b57\u8282\u7ec4\u6210\uff09\u3002 std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-8 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u8282\uff1a{}\", s[0]); // \u53ef\u80fd\u4f1a\u6253\u5370 \u2018\u00e5\u2019 (0xE5)\uff0c\u56e0\u4e3a\u201c\u5c0f\u201d\u7684 UTF-8 \u7f16\u7801\u662f 0xE5 0xB0 0x8F // \u4e5f\u53ef\u80fd\u662f\u4e71\u7801\u201c\ufffd\u201d\uff0c\u53d6\u51b3\u4e8e\u7ec8\u7aef\u7406\u89e3\u7684\u7f16\u7801\u683c\u5f0f std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; fmt::print(\"UTF-32 \u4e0b\u7b2c\u4e00\u4e2a\u5b57\u7b26\uff1a{}\", s[0]); // \u4f1a\u6253\u5370 \u2018\u5c0f\u2019 UTF-8 \u5b57\u7b26\u4e32\u7684\u53cd\u8f6c\u4e5f\u4f1a\u51fa\u95ee\u9898\uff1a std::string s = \"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u4ee5\u5b57\u8282\u4e3a\u5355\u4f4d\u53cd\u8f6c\uff0c\u5bfc\u81f4\u4e71\u7801 std::u32string s = U\"\u5c0f\u5f6d\u8001\u5e08\u516c\u5f00\u8bfe\u4e07\u5c81\"; strrev(s.data()); // \u4f1a\u628a\u6309\u5b57\u7b26\u6b63\u5e38\u53cd\u8f6c\uff0c\u5f97\u5230 \u201c\u5c81\u4e07\u8bfe\u5f00\u516c\u5e08\u8001\u5f6d\u5c0f\u201d \u603b\u7ed3\uff1aUTF-8 \u53ea\u80fd\u62fc\u63a5\u3001\u67e5\u627e\u3001\u6253\u5370\u3002\u4e0d\u80fd\u7d22\u5f15\u3001\u5207\u7247\u3001\u53cd\u8f6c\u3002 \u6309\u7d22\u5f15\u5207\u7247\u4e0d\u884c\uff0c\u4f46\u5982\u679c\u7d22\u5f15\u662f find \u51fa\u6765\u7684\u5c31\u6ca1\u95ee\u9898\u3002","title":"UTF-8 \u786e\u5b9e\u51e0\u4e4e\u5b8c\u7f8e\u652f\u6301\u5b57\u7b26\u4e32\u6240\u6709\u64cd\u4f5c"},{"location":"unicode/#ansi-unicode","text":"\u5728 Windows \u5b98\u65b9\u7684\u8bf4\u8f9e\u4e2d\uff0c\u6709\u201cUnicode \u7f16\u7801\u201d\u548c\u201cANSI \u7f16\u7801\u201d\u7684\u8bf4\u6cd5\u3002\u5f53\u4f60\u4f7f\u7528 Windows \u81ea\u5e26\u7684\u8bb0\u4e8b\u672c\u7a0b\u5e8f\uff0c\u4fdd\u5b58\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5c31\u4f1a\u770b\u5230\u8fd9\u6837\u7684\u9009\u5355\uff1a \u7ffb\u8bd1\u4e00\u4e0b\uff1a \u201cANSI\u201d\u6307\u7684\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u8bbe\u7f6e\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\u3002 \u6240\u8c13\u201cUnicode\u201d\u5176\u5b9e\u6307\u7684\u662f UTF-16\u3002 \u6240\u8c13\u201cUnicode big endian\u201d\u6307\u7684\u662f\u5927\u7aef UTF-16\u3002 \u201cUTF-8\u201d\u6307\u7684\u662f UTF-8 with BOM \u800c\u4e0d\u662f\u6b63\u5e38\u7684 UTF-8\u3002 \u5b9e\u9645\u4e0a Unicode \u53ea\u662f\u4e00\u4e2a\u5b57\u7b26\u96c6\uff0c\u53ea\u662f\u628a\u5b57\u7b26\u6620\u5c04\u5230\u6574\u6570\uff0c\u66f4\u6ca1\u6709\u4ec0\u4e48\u5927\u7aef\u5c0f\u7aef\uff0cUTF-16 \u624d\u662f\u7f16\u7801\u683c\u5f0f\u3002 \u800c ANSI \u672c\u6765\u5e94\u8be5\u662f ASCII \u7684\u610f\u601d\uff0c char \u672c\u6765\u5c31\u53ea\u652f\u6301 ASCII\u3002 \u4f46\u7531\u4e8e\u5f53\u65f6\u5404\u56fd\u8feb\u5207\u9700\u8981\u652f\u6301\u81ea\u5df1\u672c\u56fd\u7684\u6587\u5b57\uff0c\u5c31\u5728\u517c\u5bb9 ASCII \u7684\u57fa\u7840\u4e0a\uff0c\u53d1\u5c55\u51fa\u4e86\u81ea\u5df1\u7684\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u7f16\u7801\u3002\u8fd9\u4e9b\u5f53\u5730\u7279\u4f9b\u7684\u5b57\u7b26\u96c6\u91cc\u53ea\u5305\u542b\u4e86\u672c\u56fd\u6587\u5b57\uff0c\u6240\u6709\u8fd9\u4e9b\u5404\u56fd\u7684\u5b57\u7b26\u7f16\u7801\u4e5f\u90fd\u548c UTF-8 \u7c7b\u4f3c\uff0c\u91c7\u7528\u706b\u8f66\u5934\u5f0f\u7684\u53d8\u957f\u7f16\u7801\uff0c\u5bf9 0 \u5f00\u5934\u7684 ASCII \u90e8\u5206\u4e5f\u90fd\u662f\u517c\u5bb9\u3002\u6240\u4ee5 Windows \u7d22\u6027\u628a ANSI \u5f53\u4f5c\u201c\u5404\u56fd\u672c\u5730\u6587\u5b57\u7f16\u7801\u201d\u7684\u7b80\u79f0\u4e86\u3002\u4f46\u540e\u6765\u4e92\u8054\u7f51\u7684\u51fa\u73b0\uff0c\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u5e26\u6765\u4e86\u5de8\u5927\u7684\u4fe1\u606f\u4ea4\u6362\u56f0\u96be\u3002 \u4f8b\u5982\u4f60\u5728\u73a9\u4e00\u4e9b\u65e5\u672c\u7684 galgame \u65f6\uff0c\u4f1a\u53d1\u73b0\u91cc\u9762\u6587\u5b57\u5168\u90e8\u4e71\u7801\u3002\u8fd9\u662f\u56e0\u4e3a Windows \u5728\u5404\u4e2a\u5730\u533a\u53d1\u884c\u7684\u662f\u201c\u7279\u4f9b\u7248\u201d\uff1a\u5728\u4e2d\u56fd\u5927\u9646\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 GBK \u5b57\u7b26\u96c6\uff0c\u5728\u65e5\u672c\u5730\u533a\uff0c\u4ed6\u53d1\u884c\u7684 Windows \u91c7\u7528 Shift-JIS \u5b57\u7b26\u96c6\u3002\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u7a0b\u5e8f\u65f6\uff0c\u7a0b\u5e8f\u5185\u90e8\u5b58\u50a8\u7684\u662f Shift-JIS \u7684\u90a3\u4e9b\u201c\u6574\u6570\u201d\u3002\u8fd9\u5bfc\u81f4\u65e5\u672c\u7684 galgame \u5728\u4e2d\u56fd\u5927\u9646\u7279\u4f9b\u7684 Windows \u4e2d\uff0c\u628a Shift-JIS \u7684\u201c\u6574\u6570\u201d\u7528 GBK \u7684\u8868\u6765\u89e3\u8bfb\u4e86\uff0c\u4ece\u800c\u4e71\u7801\uff08GBK \u91cc\u7684\u65e5\u6587\u533a\u57df\u5e76\u6ca1\u6709\u548c Shift-JIS \u91cd\u53e0\uff09\u3002\u9700\u8981\u7528 Locale Emulator \u628a Shift-JIS \u7ffb\u8bd1\u6210 Unicode \u8bfb\u7ed9 Windows \u542c\u3002\u5982\u679c\u65e5\u672c\u7a0b\u5e8f\u5458\u4ece\u4e00\u5f00\u59cb\u5c31\u7edf\u4e00\u7528 Unicode \u6765\u5b58\u50a8\uff0c\u4e2d\u56fd\u533a\u73a9\u5bb6\u7684 Windows \u4e5f\u7edf\u4e00\u7528 Unicode \u89e3\u6790\uff0c\u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\u3002 \u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0cUnicode \u7ec4\u7ec7\u51fa\u73b0\u4e86\uff0c\u4ed6\u7684\u4f7f\u547d\u5c31\u662f\u7edf\u4e00\u5168\u4e16\u754c\u7684\u5b57\u7b26\u96c6\uff0c\u4fdd\u8bc1\u5168\u4e16\u754c\u6240\u6709\u7684\u6587\u5b57\u90fd\u80fd\u5728\u5168\u4e16\u754c\u6240\u6709\u7684\u8ba1\u7b97\u673a\u4e0a\u663e\u793a\u51fa\u6765\u3002\u9996\u5148\u521b\u529e\u4e86 Unicode \u5b57\u7b26\u96c6\uff0c\u7136\u540e\u89c4\u5b9a\u4e86 UTF-8\u3001UTF-16\u3001UTF-32 \u4e09\u79cd\u5b57\u7b26\u7f16\u7801\uff0c\u6700\u7ec8 UTF-8 \u6210\u4e3a\u5916\u7801\u7684\u4e3b\u6d41\uff0cUTF-32 \u6210\u4e3a\u5185\u7801\u7684\u4e3b\u6d41\u3002 \u63a5\u4e0b\u6765\u4e3a\u4e86\u65b9\u4fbf\u8bb0\u5fc6\uff0c\u6211\u4eec\u7d22\u6027\u5c31\u987a\u7740\u5fae\u8f6f\u7684\u8fd9\u4e2a\u8bf4\u6cd5\uff1a \u7ba1 char \u53eb ANSI\uff1a\u968f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002 \u7ba1 wchar_t \u53eb Unicode\uff1a\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u5728 Linux \u4e0a\u662f UTF-32\u3002","title":"\u8f76\u4e8b\uff1a\u201cANSI\u201d \u4e0e \u201cUnicode\u201d \u662f\u4ec0\u4e48"},{"location":"unicode/#utf-16_1","text":"\u5fae\u8f6f\u7ba1 UTF-16 \u53eb Unicode \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u95ee\u9898\uff1a \u56e0\u4e3a\u5f53\u5e74 Unicode 5.0 \u7684\u65f6\u5019\u53ea\u6709 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff0c16 \u4f4d\u5c31\u88c5\u5f97\u4e0b\uff0c\u6240\u4ee5\u5f53\u65f6 UTF-16 \u8fd8\u662f\u4e00\u4e2a \u5b9a\u957f\u7f16\u7801 \u3002\u5fae\u8f6f\u4e8e\u662f\u51b3\u5b9a\u628a wchar_t \u5b9a\u4e49\u6210 2 \u5b57\u8282\uff0c\u5e76\u5728 NT \u5185\u6838\u4e2d\uff0c\u4e3a\u6bcf\u4e2a\u7cfb\u7edf\u8c03\u7528\u90fd\u5347\u7ea7\u6210\u4e86\u57fa\u4e8e wchar_t \u5b57\u7b26\u4e32\u7684 \u201cW \u7cfb\u201d API\u3002 \u6bd4\u5c14\u76d6\u5b50\u5f53\u65f6\u4ee5\u4e3a\u8fd9\u6837 UTF-16 \u5b9a\u957f\u5185\u7801\u5c31\u4e00\u52b3\u6c38\u9038\u4e86\uff0c\u5e76\u53f7\u53ec\u6240\u6709\u7a0b\u5e8f\u90fd\u6539\u7528 UTF-16 \u505a\u5185\u7801\uff0c\u522b\u7528 \u201cA \u7cfb\u201d API \u4e86\u3002 \u8d77\u521d\uff0c\u6240\u6709\u4eba\u90fd\u4ee5\u4e3a UTF-16 \u5c31\u662f\u6700\u7ec8\u7b54\u6848\u3002 \u6ca1\u60f3\u5230\u540e\u6765 Unicode \u59d4\u5458\u4f1a\u201c\u80cc\u523a\u201d\u4e86\u6bd4\u5c14\u76d6\u5b50\uff01\u5077\u5077\u628a\u8303\u56f4\u66f4\u65b0\u5230\u4e86 0x10FFFF\uff0c\u7a81\u7834\u4e86 16 \u4f4d\u6574\u6570\u7684\u5bb9\u91cf\u3002\u539f\u6765\u7684 UTF-16 \u5df2\u7ecf\u5bb9\u7eb3\u4e0d\u4e0b\uff0c\u53ea\u597d\u5229\u7528\u4e4b\u524d\u9884\u7559\u7684 0xD800 \u5230 0xDFFF \u7a7a\u53f7\u533a\u95f4\u4e11\u964b\u5730\u5b9e\u73b0\u4e86\u53d8\u957f\u7f16\u7801\u3002 \u76f4\u5230 UTF-16 \u4e00\u591c\u4e4b\u95f4\u6210\u4e86\u4e11\u964b\u7684 \u53d8\u957f\u7f16\u7801 \u3002 \u95f9\u4e86\u534a\u5929\uff0cWindows \u8d39\u5fc3\u8d39\u529b\u66ff Unicode \u59d4\u5458\u4f1a\u597d\u4e0d\u5bb9\u6613\u63a8\u5e7f\u7684 wchar_t \uff0c\u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801 \u7684\u597d\u5904\u3002\u53ef \u201cW \u7cfb\u201d API \u5374\u53c8\u710a\u6b7b\u5728\u4e86 NT \u5185\u6838\u6700\u5e95\u5c42\uff0c\u53cd\u590d\u6765\u5751\u7b2c\u4e00\u6b21\u7528 Windows \u7f16\u7a0b\u7684\u521d\u5b66\u8005\u3002 \u6bd4\u5c14\u76d6\u5b50\uff1a\u4f60\u8fd9\u6837\u663e\u5f97\u6211\u5f88\u5c0f\u4e11\u8bf6\uff1f \u9664 Windows \u5916\uff0cJava \u4e5f\u662f\u201cUTF-16 \u80cc\u523a\u201d\u7684\u53d7\u5bb3\u8005\uff0c\u4ed6\u4eec\u60f3\u5f53\u7136\u7684\u628a char \u5b9a\u4e49\u4e3a UTF-16\uff0c\u4ee5\u4e3a\u8fd9\u5c31\u662f\u672a\u6765\u6c38\u4e45\u7684\u5b9a\u957f\u5185\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u2026\u2026 \u76f4\u5230 Unicode \u52a0\u5165\u4e86 0x10FFFF\uff0cJava \u4e0d\u5f97\u4e0d\u91cd\u65b0\u5b9a\u4e49\u4e86\u4e2a Character \u4f5c\u4e3a UTF-32 \u5b57\u7b26\uff0c\u8fd8\u5f04\u4e2a char \u5230 Character \u7684\u8f6c\u6362\uff0c\u597d\u4e0d\u5c34\u5c2c\uff01 Linux \u6210\u7acb\u4e8e 1991 \u5e74\uff0c\u5f53\u65f6 Unicode \u4e5f\u624d\u521a\u521a\u51fa\u73b0\u3002Unicode \u5ba3\u5e03\u52a0\u5165 0x10FFFF \u540e\uff0cLinux \u624d\u5f00\u59cb\u5f15\u5165\u652f\u6301 Unicode\u3002\u5728\u77e5\u9053\u4e86 Unicode \u5305\u542b 0x10FFFF \u540e\uff0c\u4ed6\u4eec\u4e00\u5f00\u59cb\u5c31\u628a wchar_t \u5b9a\u4e49\u6210 4 \u5b57\u8282\uff0c\u9003\u8fc7\u4e86 UTF-16 \u7684\u80cc\u523a\u3002 \u540e\u6765\u65b0\u51fa\u7684\u8bed\u8a00\uff0c\u5982 Python 3\u3001Go\u3001Rust\u3001Swift\u3001Kotlin\uff0c\u628a\u5b57\u7b26\u94a6\u5b9a\u4e3a UTF-32 \u4e86\u3002\u4ed6\u4eec\u53ea\u6709\u5728\u8c03\u7528 Windows API \u65f6\uff0c\u624d\u4f1a\u4e34\u65f6\u8f6c\u6362\u4e3a UTF-16 \u6765\u8c03\u7528\uff0c\u9664\u6b64\u4e4b\u5916\u518d\u65e0 UTF-16 \u51fa\u73b0\u3002 \u8bb8\u591a\u7cdf\u7cd5\u7684\u535a\u5ba2\u58f0\u79f0\uff1a\u662f\u56e0\u4e3a\u201cUTF-16 \u6700\u6709\u5229\u4e8e\u4e2d\u6587\u538b\u7f29\u201d\uff0c\u6240\u4ee5 Java \u548c Windows \u624d\u91c7\u7528\u7684\uff1f\u7136\u800c\u5c31\u6211\u4e86\u89e3\u5230\u7684\u5b9e\u9645\u60c5\u51b5\u662f\u56e0\u4e3a\u4ed6\u4eec\u9519\u8bef\u7684\u4ee5\u4e3a 0xFFFF \u662f Unicode \u7684\u4e0a\u9650\u624d\u9519\u8bef\u91c7\u7528\u4e86\uff0c\u4e0d\u7136\u4e3a\u4ec0\u4e48\u540e\u6765\u7684\u65b0\u8bed\u8a00\u90fd\u91c7\u7528\u4e86 UTF-32 \u5185\u7801 + UTF-8 \u5916\u7801\u7684\u7ec4\u5408\uff1f\u800c\u4e14\u5728\u5916\u7801\u4e2d\u91c7\u7528 UTF-8 \u6216 UTF-16 \u538b\u7f29\u786e\u5b9e\u6ca1\u95ee\u9898\uff0c\u4f46\u662f Java \u548c Windows \u7684\u5931\u8bef\u5728\u4e8e\u628a UTF-16 \u5f53\u4f5c\u5185\u7801\u4e86\uff01\u5185\u7801\u5c31\u7406\u5e94\u662f\u5b9a\u957f\u7f16\u7801\u7684\u624d\u65b9\u4fbf\uff0c\u5982\u679c\u4f60\u6709\u4e0d\u540c\u60f3\u6cd5\uff0c\u6b22\u8fce\u7559\u8a00\u8ba8\u8bba\u3002 \u603b\u4e4b\uff0cUTF-16 \u662f\u7cdf\u7c95\uff0c\u4f46\u4ed6\u662f Windows \u552f\u4e00\u5b8c\u6574\u652f\u6301\u7684 Unicode \u63a5\u53e3\u3002\u4e0d\u5efa\u8bae\u8f6f\u4ef6\u5185\u90e8\u7528 UTF-16 \u5b58\u50a8\u6587\u5b57\uff0c\u4f60\u53ef\u4ee5\u7528\u66f4\u7d27\u51d1\u7684 UTF-8 \u6216\u66f4\u65b9\u4fbf\u5207\u7247\u7684 UTF-32\uff0c\u53ea\u9700\u5728\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u524d\u4e34\u65f6\u8f6c\u6362\u6210 UTF-16 \u5c31\u884c\u3002","title":"\u5c0f\u7b11\u8bdd\uff1aUTF-16 \u7684\u80cc\u523a"},{"location":"unicode/#stdu8string","text":"\u5fc5\u987b\u6307\u51fa\uff1a\u5728 std::string \u4e2d\u88c5 UTF-8 \u5e76\u4e0d\u662f\u672a\u5b9a\u4e49\u884c\u4e3a\uff0c\u5728 std::u8string \u91cc\u540c\u6837\u53ef\u4ee5\u88c5 GBK\u3002\u8fd9\u5c31\u597d\u6bd4\u4e00\u4e2a\u540d\u53eb Age \u7684\u679a\u4e3e\u7c7b\u578b\uff0c\u5b9e\u9645\u5374\u88c5\u7740\u6027\u522b\u4e00\u6837\u3002 enum Age { // \u9519\u8bef\u793a\u8303 Male, Female, Custom, }; // \u9664\u4e86\u8ff7\u60d1\u540c\u4e8b\u5916\uff0c\u628a\u5e74\u9f84\u548c\u6027\u522b\u7684\u7c7b\u578b\u6df7\u7528\u6ca1\u6709\u597d\u5904 void registerStudent(Age age, Age sex); \u533a\u5206\u7c7b\u578b\u53ea\u662f\u5927\u591a\u6570\u4eba\u8bbe\u8ba1\u63a5\u53e3\u7684\u89c4\u8303\uff0c\u53ea\u662f\u65b9\u4fbf\u4f60\u901a\u8fc7\u770b\u51fd\u6570\u63a5\u53e3\u4e00\u773c\u533a\u5206\u8fd9\u4e2a\u51fd\u6570\u63a5\u53d7\u7684\u662f\u4ec0\u4e48\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u6ca1\u6709\u5f3a\u5236\u6027\u3002\u4f8b\u5982\u4e0b\u9762\u8fd9\u6bb5\u4ee3\u7801\u4e00\u770b\u5c31\u77e5\u9053\u8fd9\u4e9b\u51fd\u6570\u9700\u8981\u7684\u662f\u4ec0\u4e48\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 void thisFuncAcceptsANSI(std::string msg); void thisFuncAcceptsUTF8(std::u8string msg); void thisFuncAcceptsUTF16(std::u16string msg); void thisFuncAcceptsUnicode(std::wstring msg); void thisFuncAcceptsUTF32(std::u32string msg); \u6ca1\u6709 char8_t \u4e4b\u524d\uff0c\u7528\u7c7b\u578b\u522b\u540d\u540c\u6837\u53ef\u4ee5\u8d77\u5230\u5dee\u4e0d\u591a\u7684\u8bf4\u660e\u6548\u679c\uff08\u7f3a\u70b9\u662f\u65e0\u6cd5\u91cd\u8f7d\uff09\uff1a using ANSIString = std::string; using UTF8String = std::string; using UTF16String = std::vector; void thisFuncAcceptsANSI(ANSIString msg); void thisFuncAcceptsUTF8(UTF8String msg); void thisFuncAcceptsUTF16(UTF16String msg); \u4e4b\u6240\u4ee5\u6211\u4f1a\u8bf4\uff0c std::string \u5e94\u8be5\u88c5 ANSI \u5b57\u7b26\u4e32\uff0c\u662f\u56e0\u4e3a\u6240\u6709\u6807\u51c6\u5e93\u5b98\u65b9\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u4f1a\u5047\u5b9a std::string \u7c7b\u578b\u662f ANSI \u7f16\u7801\u683c\u5f0f\uff08GBK\uff09\u3002\u5e76\u4e0d\u662f\u8bf4\uff0c\u4f60\u4e0d\u80fd\u7528 std::string \u5b58\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\u7684\u5185\u5bb9\u3002 \u5982\u679c\u4f60\u5c31\u662f\u60f3\u7528 std::string \u88c5 UTF-8 \u4e5f\u53ef\u4ee5\uff0c\u53ea\u4e0d\u8fc7\u4f60\u8981\u6ce8\u610f\u5728\u4f20\u5165\u6240\u6709\u4f7f\u7528\u4e86\u6587\u4ef6\u8def\u5f84\u7684\u51fd\u6570\uff0c\u5982 fopen \uff0c std::ifstream \u7684\u6784\u9020\u51fd\u6570\u524d\uff0c\u9700\u8981\u505a\u4e00\u4e2a\u8f6c\u6362\uff0c\u8f6c\u6210 GBK \u7684 std::string \u6216 UTF-16 \u7684 std::wstring \u540e\uff0c\u624d\u80fd\u4f7f\u7528\uff0c\u5f88\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u5982\u679c\u4f60\u59cb\u7ec8\u7528 std::u8string \u88c5 UTF-8\uff0c\u90a3\u4e48\u5f53\u4f60\u628a\u5b83\u8f93\u5165\u4e00\u4e2a\u63a5\u53d7 ANSI \u7684\u666e\u901a std::string \u53c2\u6570\u65f6\uff0c\u5c31\u4f1a\u53d1\u751f\u7c7b\u578b\u4e0d\u5339\u914d\u9519\u8bef\uff0c\u5f3a\u8feb\u4f60\u91cd\u65b0\u6e05\u9192\uff0c\u6216\u662f\u5f3a\u8feb\u4f60\u4f7f\u7528\u4e00\u4e2a\u8f6c\u6362\u51fd\u6570\uff0c\u7a0d\u540e\u4f1a\u4ecb\u7ecd\u8fd9\u4e2a\u8f6c\u6362\u51fd\u6570\u7684\u5199\u6cd5\u3002 \u4f8b\u5982\u5f53\u4f60\u4f7f\u7528 std::cout << u8string \u65f6\u4f1a\u62a5\u9519\uff0c\u8feb\u4f7f\u4f60\u6539\u4e3a std::cout << u8toansi(u8string) \u624d\u80fd\u7f16\u8bd1\u901a\u8fc7\uff0c\u4ece\u800c\u907f\u514d\u4e86\u628a UTF-8 \u7684\u5b57\u7b26\u4e32\u6253\u5370\u5230\u4e86\u53ea\u652f\u6301 GBK \u7684\u63a7\u5236\u53f0\u4e0a\u3002 \u5176\u4e2d\u8f6c\u6362\u51fd\u6570\u7b7e\u540d\u4e3a std::string u8toansi(std::u8string s) \uff0c\u5f88\u53ef\u60dc\uff0c\u6807\u51c6\u5e93\u5e76\u6ca1\u6709\u63d0\u4f9b\u8fd9\u4e2a\u51fd\u6570\uff0c\u76f4\u5230 C++26 \u524d\uff0c\u6807\u51c6\u5e93\u5bf9\u5b57\u7b26\u7f16\u7801\u652f\u6301\u4e00\u76f4\u5f88\u5dee\uff0c\u4f60\u4e0d\u5f97\u4e0d\u81ea\u5df1\u5b9e\u73b0\u6216\u4f9d\u8d56\u7b2c\u4e09\u65b9\u5e93\u3002 \u603b\u4e4b\uff0c char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u5411\u8c03\u7528\u8005\u6697\u793a\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u4f8b\u5982\u8fd9\u6837\u4e00\u4e2a\u51fd\u6570\uff1a thisFuncAcceptUTF8(std::u8string msg); \u5982\u679c\u8c03\u7528\u8005\u559c\u6b22\u7528 std::string \u88c5 UTF-8 \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u7528\uff1a std::string msg; // \u8c03\u7528\u8005\u786e\u4fe1\uff0c\u8fd9\u4e2a msg \u867d\u7136\u662f `std::string`\uff0c\u4f46\u91cc\u9762\u7684\u5185\u5bb9\u5c31\u662f UTF-8 // \u90a3\u4e48\u4ed6\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e3a u8string\uff0c\u6765\u8bc1\u660e\u81ea\u5df1\u5934\u8111\u6e05\u9192 thisFuncAcceptUTF8(std::u8string((char8_t *)msg.data(), msg.size()));","title":"\u5f3a\u7c7b\u578b\u7684 std::u8string \u53ea\u662f\u541b\u5b50\u534f\u8bae"},{"location":"unicode/#_9","text":"C++ \u5b98\u65b9\u5b9a\u4e49\u4e2d\uff0c\u5b58\u5728\u4e24\u79cd\u5b57\u7b26\u96c6\u3002\u4e00\u79cd\u662f \u6e90\u7801\u5b57\u7b26\u96c6 (source charset) \uff0c\u4e00\u79cd\u662f \u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u3002 \u8fd9\u771f\u662f\u7cdf\u7cd5\u7684\u672f\u8bed\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u8fd9\u4e2a\u540d\u5b57\u5177\u6709\u8bef\u5bfc\u6027\uff0c\u4ed6\u548c\u8fd0\u884c\u65f6\u6839\u672c\u6ca1\u6709\u5173\u7cfb\uff0c\u660e\u660e\u662f\u7f16\u8bd1\u671f\u5c31\u786e\u5b9a\u7684\u3002\u6240\u4ee5\u5c0f\u5f6d\u8001\u5e08\u66ff\u4ed6\u6539\u4e2a\u540d\u5b57\uff0c\u5b9e\u9645\u5e94\u8be5\u53eb\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u96c6\u201d\u3002 \u800c\u4e14\u4ed6\u4eec\u53eb\u5b57\u7b26\u96c6\u4e5f\u4e0d\u5408\u7406\uff0c\u5e94\u8be5\u53eb\u5b57\u7b26\u7f16\u7801\u624d\u5bf9\uff0cUTF-8 \u548c UTF-16 \u90fd\u662f Unicode \u5b57\u7b26\u96c6\u7684\u4e24\u79cd\u5b57\u7b26\u7f16\u7801\u683c\u5f0f\uff0c\u4f46\u4ed6\u4eec\u660e\u663e\u662f\u4e0d\u540c\u7684\u3002 \u7136\u540e\uff0c\u518d\u5f15\u5165\u4e00\u4e2a\u771f\u6b63\u7684\uff0c\u8fd0\u884c\u65f6\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u8f6f\u4ef6\u5ba2\u6237\u7535\u8111\u7684\u533a\u57df\u8bbe\u7f6e\u3002 \u6700\u7ec8\uff0c\u7ecf\u8fc7\u5c0f\u5f6d\u8001\u5e08\u6539\u826f\u7684\u672f\u8bed\u5982\u4e0b\uff1a \u6e90\u7801\u5b57\u7b26\u7f16\u7801: .cpp \u6e90\u7801\u6587\u4ef6\u65f6\u7528\u7684\u5b57\u7b26\u7f16\u7801\u3002\u4f8b\u5982\u7a0b\u5e8f\u5458\u7528\u8bb0\u4e8b\u672c\u4fdd\u5b58 .cpp \u6e90\u7801\u6587\u4ef6\u65f6\uff0c\u9009\u62e9 \u201cUTF-8\u201d \u4fdd\u5b58\u5c31\u662f UTF-8\uff0c\u9009\u62e9 \u201cANSI\u201d \u4fdd\u5b58\u5c31\u662f GBK\u3002 \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f char \u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u5b58\u50a8\u7684\u5b57\u7b26\u7f16\u7801\u3002\u9ed8\u8ba4\u662f\u6211\u4eec\u7a0b\u5e8f\u5458\uff08\u5f00\u53d1\u8005\uff09\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801: \u6307\u7684\u662f\u6211\u4eec\u7684\u7a0b\u5e8f\u5728\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u8fd0\u884c\u65f6\uff0c\u5ba2\u6237\u7684\u64cd\u4f5c\u7cfb\u7edf API \u7684 const char * \u671f\u671b\u63a5\u53d7\u600e\u6837\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002\u9ed8\u8ba4\u662f\u5ba2\u6237\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 \u8fd9\u4e09\u4e2a\u53ef\u4ee5\u5404\u6709\u4e0d\u540c\u3002 \u5176\u4e2d \u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801 \u548c \u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801 \u7684\u4e0d\u5339\u914d\uff0c\u662f Windows \u8f6f\u4ef6\u51fa\u73b0\u4e71\u7801\u7684\u4e3b\u8981\u539f\u56e0\u3002 \u800c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u53ea\u4e8b\u5173\u4f60\u5982\u4f55\u4fdd\u5b58\u6e90\u7801\uff0c\u53ea\u662f\u8ba9\u7f16\u8bd1\u5668\u80fd\u591f\u6210\u529f\u8bfb\u53d6\u4f60\u7684\u6e90\u7801\uff0c\u5bf9\u8fd0\u884c\u65f6\u7684\u4e71\u7801\u95ee\u9898\u6ca1\u6709\u5f71\u54cd\u3002\u7f16\u8bd1\u5668\u8bfb\u5b8c\u6e90\u7801\u540e\uff0c\u8981\u5728\u5e38\u91cf\u533a\u751f\u6210\u5b57\u7b26\u4e32\u5e38\u91cf\u65f6\uff0c\u8fd8\u662f\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u7684\u3002 \u4f8b\u5982\u4e4b\u524d\u8bf4\u7684\u65e5\u672c galgame \u5728\u4e2d\u56fd\u7535\u8111\u4e0a\u6253\u5f00\u7206\u51fa\u4e71\u7801\uff0c\u5c31\u662f\u56e0\u4e3a\u662f\u65e5\u672c\u7a0b\u5e8f\u5458\u7f16\u8bd1\u4e86 galgame\uff08\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u4e3a Shift-JIS\uff09\uff0c\u5728\u4e2d\u56fd\u5ba2\u6237\u7535\u8111\u4e0a\u6253\u5f00\uff08\u8fd0\u884c\u65f6\u5b57\u7b26\u7f16\u7801\u4e3a GBK\uff09\u5bfc\u81f4\u7684\u3002 \u65e5\u672c\u7a0b\u5e8f\u5458\u4f7f\u7528\u4ec0\u4e48\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u6839\u672c\u65e0\u5173\u7d27\u8981\u2026\u2026\u54ea\u6015\u4ed6\u4eec\u4f7f\u7528\u4e86 UTF-8 \u4fdd\u5b58\u6e90\u7801\uff0cMSVC \u7f16\u8bd1\u65f6\u4ecd\u7136\u4f1a\u5c06\u5176\u8f6c\u6362\u4e3a Shift-JIS \u7f16\u7801\u7684\u5b57\u9762\u91cf\u6765\u5b58\u50a8\u5728\u53ef\u6267\u884c\u6587\u4ef6\u7684\u5e38\u91cf\u533a\u4e2d\u3002 \u4f60\u53ef\u80fd\u4f1a\u95ee\uff0c\u4e3a\u4ec0\u4e48\u4e0d\u628a\u8fd9\u4e9b\u82b1\u91cc\u80e1\u54e8\u73a9\u5e94\u7edf\u4e00\u4e3a UTF-8\uff0c\u8fd9\u6837\u5c31\u4e0d\u7528\u8f6c\u6362\u6765\u8f6c\u6362\u53bb\u4e86\uff1f\u8fd8\u4e0d\u662f\u56e0\u4e3a\u5386\u53f2\u9057\u7559\uff0c\u4e00\u4e9b\u52b3\u4fdd\u7a0b\u5e8f\u5458\u4e0d\u80af\u628a\u4ed6\u4eec GBK \u7f16\u7801\u7684\u6e90\u7801\u6539\u6210 UTF-8 \u4fdd\u5b58\uff0c\u800c\u4e14\uff0cWindows \u4e5f\u5b8c\u5168\u4e0d\u63d0\u4f9b\u57fa\u4e8e UTF-8 \u7684\u8de8\u5e73\u53f0 API\uff08\u53ea\u63d0\u4f9b GBK \u548c UTF-16 \u4e24\u79cd\uff0c\u5c31\u662f\u4e0d\u7ed9 UTF-8 \u7684\uff0c\u975e\u5e38\u6076\u5fc3\u4eba\uff09\u3002\u6240\u4ee5 MSVC \u81f3\u4eca\u4ecd\u7136\u9ed8\u8ba4\u662f GBK \u7f16\u7801\u7684\uff08\u66f4\u51c6\u786e\u7684\u8bf4\u662f ANSI\uff0c\u8ddf\u968f\u4f60\u7cfb\u7edf\u7684\u533a\u57df\u8bbe\u7f6e\u800c\u53d8\uff0c\u5728\u4e2d\u56fd\u5c31 GBK\uff0c\u5728\u7f8e\u56fd\u5c31 UTF-8\uff0c\u5728\u6b27\u6d32\u5c31 Latin-1\uff0c\u975e\u5e38\u7684\u53cc\u6807\uff09\u3002\u5fae\u8f6f\u5404\u79cd\u626f\u76ae\u6548\u7387\u4f4e\u4e0b\uff0cAPI \u5f04\u4e86\u4e00\u5957\u53c8\u4e00\u5957\u4e92\u76f8\u6781\u9650\u62c9\u626f\uff0c\u800c\u6211\u4eec Linux \u548c GCC \u65e9\u5df2\u9ed8\u8ba4\u5c31\u662f UTF-8\u2026\u2026 \u6211\u7406\u89e3\u4f60\u73b0\u5728\u5927\u8111\u5e72\u70e7\u7684\u5fc3\u60c5\u3002\u4f3a\u5019\u8fd9\u4e9b\u5386\u53f2\u7b54\u8fa9\u5f88\u590d\u6742\uff0c\u4e5f\u5f88\u65e0\u804a\uff0c\u6beb\u65e0\u610f\u4e49\uff01\u53ea\u662f\u4e3a\u4e86\u64e6\u53cd Unicode \u52b3\u4fdd\u7684\u5c41\u80a1\u3002","title":"\u6e90\u7801\u5b57\u7b26\u96c6\u4e0e\u8fd0\u884c\u5b57\u7b26\u96c6"},{"location":"unicode/#_10","text":"\u5bf9\u4e8e\u8de8\u5e73\u53f0\u8f6f\u4ef6\u6765\u8bf4\uff0c\u6211\u63a8\u8350\u5927\u5bb6\u628a\u4e09\u4e2a\u5168\u90e8\u8bbe\u4e3a UTF-8\uff01\uff08\u8981\u505a\u5230\u8fd9\u4e00\u70b9\uff0c\u4e3b\u8981\u662f\u4f3a\u5019 MSVC\uff09 Linux + GCC \u7528\u6237\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0c\u4f60\u4eec\u6240\u6709\u5b57\u7b26\u96c6\u9ed8\u8ba4\u7684\u8bbe\u5b9a\u5c31\u662f UTF-8\u3002 Windows + MSVC \u7528\u6237\u8bf7\u5f00\u542f /utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\uff0c\u73b0\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u5728\u5185\u5b58\u4e2d\u90fd\u662f UTF-8 \u4e86\u3002 Windows + MinGW \u7528\u6237\u8bf7\u5f00\u542f -finput-charset=utf-8 \u548c -fexec-charset=utf-8 \uff0c\u8fd9\u4f1a\u628a\u201c\u6e90\u7801\u5b57\u7b26\u7f16\u7801\u201d\u548c\u201c\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\u201d\u90fd\u8bbe\u4e3a UTF-8\u3002 \u6240\u6709\u6e90\u7801\u6587\u4ef6\u7edf\u4e00\u4ee5 UTF-8 \u7f16\u7801\u4fdd\u5b58\uff0c\u4e14\u5c3d\u91cf\u5728\u6700\u524d\u9762\u52a0\u4e0a 0xFEFF \u8fd9\u4e2a BOM \u6807\u8bb0\uff0c\u9632\u6b62 MSVC \u8111\u62bd\u5f53\u4f5c GBK \u6765\u8bfb\u53d6\u3002 \u5728 main \u51fd\u6570\u524d\uff0c\u52a0\u4e24\u884c\uff1a // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 return 0; } \u8fd9\u6837\u4e00\u5957\u6253\u4e0b\u6765\uff0c\u5c31\u53ef\u4ee5\u4fdd\u8bc1\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u8bd1\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u64cd\u4f5c\u7cfb\u7edf\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u6587\u672c\u7f16\u8f91\u5668\uff0c\u65e0\u8bba\u4f60\u4f7f\u7528\u4ec0\u4e48\u7f16\u7801\uff0c\u4f60\u7684\u7a0b\u5e8f\u90fd\u53ef\u4ee5\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u8bfb\u53d6\u6e90\u7801\uff0c\u6b63\u786e\u7684\u4ee5 UTF-8 \u7f16\u7801\u6765\u5b58\u50a8\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u6b63\u786e\u7684\u628a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u8def\u5f84\u8f6c\u4e3a UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u3002 \u5728 CMake \u4e2d\uff0c\u53ea\u5bf9 MSVC \u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a if (MSVC) target_compile_options(\u4f60\u7684\u7a0b\u5e8f PRIVATE /utf-8) else() \u4e5f\u53ef\u4ee5\u5728\u6700\u524d\u9762 add_compile_options \uff0c\u5b9e\u73b0\u5bf9\u6240\u6709\u4e4b\u540e\u5b9a\u4e49\u7684\u7a0b\u5e8f\u5168\u5c40\u542f\u7528\u8be5\u9009\u9879\u3002 \u5728\u6211\u81ea\u5df1\u7684\u9879\u76ee\u4e2d\uff0c\u6211\u90fd\u4f1a\u8fd9\u6837\u5f00\u542f\uff0c\u89e3\u51b3 MSVC \u4e0d\u8de8\u5e73\u53f0\u7684\u95ee\u9898\uff1a if (MSVC) add_compile_options(/Zc:preprocessor /utf-8 /DNOMINMAX /D_USE_MATH_DEFINES /EHsc /bigobj) else() if (WIN32) add_compile_options(-finput-charset=utf-8 -fexec-charset=utf-8) endif() add_compile_options(-Wall -Wextra -Werror=return-type) endif() add_executable(\u4f60\u7684\u7a0b\u5e8f \u4f60\u7684\u6587\u4ef6.cpp) # \u81ea\u52a8\u7ee7\u627f\u4e86\u4e0a\u9762\u6240\u6709\u7684\u7f16\u8bd1\u5668\u9009\u9879","title":"\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5e94\u8be5\u600e\u4e48\u505a"},{"location":"unicode/#utf-8-locale","text":"Windows \u5b98\u65b9\u63d0\u4f9b\u7684\u771f\u6b63 API \u662f _wfopen \u3002 fopen \u53ea\u662f\u4ed6\u4eec\u63d0\u4f9b\u7684\u201cPOSIX \u517c\u5bb9\u5c42\u201d \u5305\u88c5\uff0c\u5176\u4f1a\u628a\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53c2\u6570\u901a\u8fc7 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u540e\uff0c\u8f6c\u53d1\u7ed9 _wfopen \u3002 \u51fa\u4e8e\u8de8\u5e73\u53f0\u7684\u8981\u6c42\uff0c\u6211\u4eec\u4e0d\u80fd\u4f7f\u7528 _wfopen \u8fd9\u79cd\u5176\u4ed6\u5e73\u53f0\u6ca1\u6709\u7684\u51fd\u6570\uff0c\u4e5f\u4e0d\u60f3\u7528\u90a3\u8fde 2 \u5b57\u8282 4 \u5b57\u8282\u90fd\u98d8\u5ffd\u4e0d\u5b9a\u7684 wchar_t \uff0c\u66f4\u4e0d\u60f3\u8ba9 std::string \u5b58\u6839\u672c\u4e0d\u80fd\u8de8\u5e73\u53f0\u7684 GBK\u3002 \u53ea\u8981\u8ba9 fopen \u7684 \u201cGBK \u5230 UTF-16\u201d \u8f6c\u6362\u51fd\u6570\u66ff\u6362\u6210 \u201cUTF-8 \u5230 UTF-16\u201d \u5c31\u884c\u4e86\u3002\u8fc7\u53bb\uff0c\u6211\u4eec\u65e0\u6cd5\u66ff\u6362\uff0c\u6700\u65b0\u7684 Windows \u5728\u4e00\u6b21\u66f4\u65b0\u4e2d\uff0c\u652f\u6301\u4e86 \".utf-8\" locale \u8fd9\u4e00\u9ed1\u79d1\u6280\uff0c\u4e13\u95e8\u6ee1\u8db3\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u7684\u9700\u8981\u3002 // \u9ed8\u8ba4 locale fopen(\"\u4f60\u597d.txt\") == _wfopen(gbk_to_utf16(\"\u4f60\u597d.txt\")); // \u8bbe\u7f6e\u4e86 utf-8 locale \u540e fopen(\"\u4f60\u597d.txt\") == _wfopen(utf8_to_utf16(\"\u4f60\u597d.txt\")); \u82e5\u4e0d\u8bbe\u7f6e setlocale(LC_ALL, \".utf-8\") \uff0c\u5219 fopen \u548c ifstream \u9ed8\u8ba4\u4f1a\u628a\u4f60\u63d0\u4f9b\u7684 const char * \u6587\u4ef6\u8def\u5f84\uff0c\u5f53\u4f5c GBK \u7f16\u7801\u7684\uff0c\u800c\u6211\u4eec\u8bbe\u7f6e\u4e86 /utf-8 \u6216 -fexec-charset=utf-8 \u540e\uff0c\u5b57\u7b26\u4e32\u5b57\u9762\u91cf\u7f16\u7801\u5df2\u7ecf\u662f UTF-8 \u4e86\uff0c\u8fd9\u6837 UTF-8 \u7684\u5b57\u7b26\u4e32\u5e38\u91cf\u8f93\u5165\u8fdb\u671f\u671b const char * \u7684 fopen \u53c2\u6570\uff0c\u5c31\u4f1a\u51fa\u4e71\u7801\u95ee\u9898\u4e86\u3002 \u4e0d\u8fc7\u8981\u6ce8\u610f\uff0c .utf-8 locale \u53ea\u662f\u5f71\u54cd\u4e86\u6807\u51c6\u5e93\uff01\u5e76\u4e0d\u6539\u53d8\u7cfb\u7edf API\u3002 \u76f4\u63a5\u8c03\u7528\u7cfb\u7edf API \u65f6\uff0cA \u7cfb API \u4ecd\u7136\u6709\u95ee\u9898\u3002 MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u63d0\u793a\", MB_OK); // \u4e0d\u884c\uff0c.utf-8 \u53ea\u662f\u8ba9\u6807\u51c6\u5e93\u53d8\u6210 UTF-8 \u63a5\u53e3\u4e86\uff0cA \u7cfb Windows API \u4ecd\u7136\u662f GBK MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u63d0\u793a\", MB_OK); // \u6ca1\u95ee\u9898\uff01\u7528 UTF-16 \u7684 wchar_t \u5b57\u9762\u91cf\u6765\u8c03\u7528 W \u63a5\u53e3\u603b\u662f\u6ca1\u95ee\u9898\u7684 \u8fd8\u662f\u9700\u8981\u6211\u4eec\u624b\u52a8\u8f6c\u6362 UTF-8 \u5230 UTF-16 \u540e\u8c03\u7528 W \u7cfb API\u2026\u2026\u4f46\u662f\u53cd\u6b63\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u5f88\u5c11\u9700\u8981\u76f4\u63a5\u8c03\u7528 Windows API\uff0c\u90fd\u662f\u901a\u8fc7\u901a\u7528\u7684 C/C++ \u6807\u51c6\u5e93\uff0c\u56e0\u6b64 .utf-8 locale \u53ef\u80fd\u662f\u8de8\u5e73\u53f0\u7a0b\u5e8f\u5458\u60f3\u8fdb\u519b UTF-8 \u7684\u6700\u4f73\u9009\u62e9\u3002","title":".utf-8 locale \u662f\u5982\u4f55\u5de5\u4f5c\u7684"},{"location":"unicode/#b-wchar_t","text":"\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\uff0c\u90fd\u662f\u9488\u5bf9 char \u7684\uff0c\u53ea\u6709 char \u88ab\u6545\u610f\u9488\u5bf9\u4e86\uff0c\u5b58\u5728\u5b57\u7b26\u7f16\u7801\u4e0d\u7edf\u4e00\u7684\u95ee\u9898\u3002 \u5982\u679c\u5168\u90e8\u7528 wchar_t \u7684\u8bdd\uff0c\u867d\u7136\u5728 Linux \u4e0a\u662f UTF-32\uff0c\u5728 Windows \u4e0a\u662f UTF-16\uff0c\u4e0d\u7edf\u4e00\u4e86\u3002\u4f46\u81f3\u5c11\u5728\u540c\u4e00\u4e2a Windows \u64cd\u4f5c\u7cfb\u7edf\u4e0a\uff0c\u90fd\u662f\u7edf\u4e00\u7684 UTF-16\u3002 \u6240\u4ee5\u8fd8\u6709\u4e00\u79cd\u65b9\u5f0f\u662f\u5168\u9762\u91c7\u7528 wchar_t \u548c std::wstring \uff0c\u8fd9\u6837\u65e0\u8bba\u4f60\u7684\u8fd0\u884c\u5b57\u7b26\u96c6\u548c\u533a\u57df\u8bbe\u7f6e\u5982\u4f55\uff0c\u90fd\u5bf9 wchar_t \u548c\u57fa\u4e8e const wchar_t * \u7684\u51fd\u6570\u6ca1\u6709\u4efb\u4f55\u5f71\u54cd\u3002 C \u8bed\u8a00\u6807\u51c6\u6ca1\u6709 _wfopen \uff0c\u4f46\u662f std::ifstream \u6709\u57fa\u4e8e std::wstring \u7684\u6784\u9020\u51fd\u6570\uff0c\u5c31 C++ \u6807\u51c6\u5e93\u6765\u770b std::wstring \u7684\u652f\u6301\u8fd8\u662f\u5f88\u4e30\u5bcc\u7684\uff0c\u57fa\u672c std::string \u6709\u7684 std::wstring \u90fd\u6709\uff0c\u4f8b\u5982 std::to_string \u548c std::to_wstring \uff0c std::cout \u548c std::wcout \u3002\u672c\u7ae0\u8282\u6700\u540e\u6211\u4eec\u4f1a\u8be6\u7ec6\u4ecb\u7ecd\u5bbd\u5b57\u7b26\u6d41\u7684\u7528\u6cd5\u3002 \u7f3a\u70b9\u662f\uff0c\u9996\u5148\u6bcf\u6b21\u90fd\u9700\u8981\u5199 L\"\u4f60\u597d\" \u8fd9\u4e2a L \u524d\u7f00\u5f88\u9ebb\u70e6\uff0c\u5bb9\u6613\u5fd8\u8bb0\u3002 \u800c\u4e14\u5f88\u591a\u7b2c\u4e09\u65b9\u5e93\u90fd\u5728\u7528 std::string \uff0c\u5e76\u6ca1\u6709\u63d0\u4f9b std::wstring \u7684 API\u3002 \u4f8b\u5982 openvdb \u7684\u6587\u4ef6\u5199\u5165\u51fd\u6570\uff1a void openvdb::io::File::write(std::string const &filename); \u8fd9\u6837\u5c31\u5f88\u9ebb\u70e6\u4e86\uff0c\u5982\u679c\u4f60\u5185\u90e8\u5168\u662f UTF-16 \u7684 std::wstring \u6765\u8868\u793a\u5b57\u7b26\u4e32\uff0c\u8c03\u7528\u7b2c\u4e09\u65b9\u5e93\u524d\u5c31\u9700\u8981\u8f6c\u6210 GBK \u7684 std::string \u3002\u53ef\u4ee5\u7528 boost::locale::conv::to_utf \u8fd9\u4e2a\u51fd\u6570\u8f6c\u6362\uff0c\u4f46\u4e5f\u5f88\u9ebb\u70e6\uff0c\u800c\u4e14\u5982\u679c std::wstring \u542b\u6709 GBK \u8303\u56f4\u4e4b\u5916\u7684 \u201c\ud883\udede\u201d\uff0cGBK \u65e0\u6cd5\u8868\u793a\uff0c\u53c8\u4f1a\u6709\u7f16\u7801\u5931\u8d25\u7684\u95ee\u9898\u3002 \u8fd8\u6709 stbi_load \u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u63d0\u4f9b\u7684\u51fd\u6570\uff0c\u90fd\u662f\u53ea\u63d0\u4f9b\u4e86 const char * \u7684\u63a5\u53e3\uff0c\u591a\u4e86\u53bb\u4e86\u3002 setlocale(LC_ALL, \".utf-8\") \u7684\u597d\u5904\u662f\u53ef\u4ee5\u8ba9\u8fd9\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5168\u81ea\u52a8\u90fd\u4ece GBK \u65e0\u7f1d\u5207\u6362\u5230 UTF-8\uff0c\u800c\u4e0d\u7528\u5bf9\u4ed6\u4eec\u7684\u6e90\u7801\u505a\u4efb\u4f55\u66f4\u6539\u3002\u56e0\u4e3a\u4ed6\u4eec\u5185\u90e8\u90fd\u662f\u8c03\u7528\u7684 fopen \u548c ifstream \u3002","title":"\u65b9\u6848 B\uff1a\u6295\u5954 wchar_t \u6d41\u6d3e"},{"location":"unicode/#u8","text":"\u4e2d\u56fd\u533a Windows\uff0cMSVC\uff0c\u7f16\u8bd1\u9009\u9879\uff1a /std:c++17 std::string s = \"\u4f60\u597d\"; hexdump(s); // C4 E3 BA C3 (GBK) std::string s = u8\"\u4f60\u597d\"; hexdump(s); // E4 BD A0 E5 A5 BD (UTF-8) u8 \u524d\u7f00\u544a\u8bc9\u7f16\u8bd1\u5668\uff0c\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u7f16\u7801\u5b58\u50a8\u3002\u65e0\u8bba\u8fd0\u884c\u5b57\u7b26\u96c6 (execution charset) \u662f\u4e0d\u662f UTF-8\u3002 \u7f16\u8bd1\u5668\u4fdd\u8bc1\u4f1a\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5e38\u91cf\u8f6c\u6362\u4e3a UTF-8 \u7f16\u7801\u7684 char \u5b57\u8282\u5e8f\u5217\uff0c\u5b58\u50a8\u5728\u5b57\u7b26\u4e32\u5e38\u91cf\u533a\u3002 \u8fd9\u5bf9\u4e8e\u5df2\u7ecf\u8bbe\u7f6e\u4e86 /utf-8 \u9009\u9879\uff0c\u8fd0\u884c\u5b57\u7b26\u96c6\u5df2\u7ecf\u4fdd\u8bc1\u662f UTF-8 \u7684\u6211\u4eec\u6765\u8bf4\u6beb\u65e0\u4f5c\u7528\u3002\u53ea\u662f\u5bf9\u4e8e\u4e0d\u7528 /utf-8 \u7684\u540c\u5b66\uff0c\u4ed6\u4eec\u60f3\u8981\u4e34\u65f6\u521b\u5efa\u4e00\u4e2a UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5c31\u53ef\u4ee5\u7528 u8 \u524d\u7f00\u3002 \u5728 C++17 \u548c\u4e4b\u524d\uff0c u8\"\u4f60\u597d\" \u4ea7\u751f\u7684\u662f const char [] \u7c7b\u578b\u7684\u5e38\u91cf\u3002 \u5728 C++20 \u4e2d\uff0c\u5f15\u5165\u4e86 char8_t \u3002\u7136\u540e\uff0c\u4ed6\u4eec\u89c4\u5b9a\uff0c u8\"\u4f60\u597d\" \u73b0\u5728\u4ea7\u751f\u7684\u662f const char8_t [] \u7c7b\u578b\u7684\u5e38\u91cf\u4e86\u3002 \u8fd9\u5bfc\u81f4\u4e86\u4e00\u4e9b\u517c\u5bb9\u6027\u95ee\u9898\uff0c\u6bd4\u5982\u4ee5\u524d\u4f60\u5199\u7684\uff1a std::string s = u8\"\u4f60\u597d\"; \u73b0\u5728\u65e0\u6cd5\u7f16\u8bd1\u901a\u8fc7\u4e86\uff0c\u56e0\u4e3a const char8_t [] \u65e0\u6cd5\u7528\u4e8e\u6784\u9020\u53ea\u652f\u6301 const char [] \u7684 std::string \u3002 \u597d\u5728 C++23 \u53c8\u4fee\u590d\u4e86\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u4eec\u5141\u8bb8 const char8_t [] \u9690\u5f0f\u8f6c\u6362\u4e3a const char [] \uff0cC++17 \u4e4b\u524d\u7684\u8fd9\u79cd\u4ee3\u7801\u53c8\u80fd\u6b63\u5e38\u901a\u8fc7\u7f16\u8bd1\u3002\u6240\u4ee5\uff0c\u5982\u679c\u60f3\u5feb\u4e50\u5730\u7528 u8 \u5b57\u9762\u91cf\uff0c\u8981\u4e48 C++17\uff0c\u8981\u4e48 C++23\uff0c\u8df3\u8fc7 C++20 \u6bd4\u8f83\u597d\u3002 \u4f60\u53ef\u4ee5\u770b\u5230\uff0cC++ \u7248\u672c\u7684\u66f4\u65b0\u5e76\u4e0d\u662f 100% \u5b8c\u5168\u5411\u524d\u517c\u5bb9\u7684\uff0c\u6709\u65f6\u4e5f\u4f1a\u6709\u7834\u574f\u6027\u7684\u53d8\u66f4\uff0c\u4f46\u6bd4\u8f83\u5c11\uff0c\u5e73\u65f6\u611f\u89c9\u4e0d\u5230\u3002\u6bd4\u5982 C++11 \u4e4b\u524d auto \u5c31\u6709\u5176\u4ed6\u7684\u529f\u80fd\uff0c\u540e\u6765\u51b3\u5b9a\u8fd9\u4e2a\u529f\u80fd\u6ca1\u4ec0\u4e48\u7528\uff0c\u5c31\u628a auto \u6539\u6210\u53e6\u4e00\u4e2a\u610f\u601d\u4e86\u3002 \u9664\u4e86 u8 \u4ee5\u5916\uff0c\u8fd8\u6709\u8fd9\u4e9b\uff1a \u524d\u7f00 \u7f16\u7801 \u5b57\u7b26\u7c7b\u578b \u201c\u4f60\u597d\u201d \u8fd0\u884c\u5b57\u7b26\u96c6 (\u9ed8\u8ba4\u662f\u5f00\u53d1\u8005\u7535\u8111\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u800c\u4e0d\u662f\u5ba2\u6237\u7684) const char [] L\u201d\u4f60\u597d\u201d Windows \u4e0a UTF-16\uff1bLinux \u4e0a UTF-32 const wchar_t [] u8\u201d\u4f60\u597d\u201d UTF-8 const char8_t [] u\u201d\u4f60\u597d\u201d UTF-16 const char16_t [] U\u201d\u4f60\u597d\u201d UTF-32 const char32_t [] \u53ea\u4e0d\u8fc7\u662f\u5f00\u53d1\u8005\u548c\u5ba2\u6237\u5f80\u5f80\u5904\u4e8e\u540c\u4e00\u4e2a\u5730\u533a\uff0c\u6240\u4ee5 \"\u4f60\u597d\" \u770b\u8d77\u6765\u597d\u50cf\u53ef\u4ee5\u76f4\u63a5\u8f93\u5165\u5230 std::cout \u4e2d\u4e00\u6837\u3002\u5b9e\u9645\u4e0a\u4ed6\u53ea\u662f\u5f00\u53d1\u8005\u7535\u8111\u7684 ANSI\uff0c\u800c\u4e0d\u662f\u5ba2\u6237\u7535\u8111\u7684 ANSI\uff0c\u5982\u679c\u76f4\u63a5\u62ff\u6765\u6253\u5370\uff0c\u4f1a\u5bfc\u81f4\u4ee5 \"\" \u5e38\u91cf\u5f62\u5f0f\u5199\u6b7b\u7684\u5b57\u7b26\u4e32\u4f1a\u5728\u5ba2\u6237\u7535\u8111\u4e0a\u51fa\u73b0\u4e71\u7801\u3002\u9664\u975e\u8fd9\u4e2a\u5b57\u7b26\u4e32\u53ea\u5305\u542b ASCII\uff0c\u56e0\u4e3a\u6240\u6709 ANSI \u90fd\u517c\u5bb9 ASCII\uff0c\u624d\u6070\u597d\u907f\u514d\u4e86\u4e71\u7801\u3002","title":"u8 \u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u4f5c\u7528"},{"location":"unicode/#_11","text":"","title":"\u9009\u62e9\u4f60\u7684\u9635\u8425\uff01"},{"location":"unicode/#ansi","text":"\u628a\u5b57\u7b26\u4e32\u5f53\u4f5c\u7eaf\u7cb9\u7684\u201c\u5b57\u8282\u6d41\u201d\uff0c\u65e0\u89c6\u5b57\u7b26\u7f16\u7801\u3002\u6216\u8005\u8bf4\uff0c\u4f60\u4ece\u7cfb\u7edf\u8f93\u5165\u8fdb\u6765\u7684\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u6211\u5c31\u5b58\u50a8\u7684\u4ec0\u4e48\u7f16\u7801\u3002\u5bf9\u4e8e Unicode \u5219\u91c7\u53d6\u5b8c\u5168\u6446\u70c2\u7684\u6001\u5ea6\uff0c\u5b8c\u5168\u65e0\u89c6 Unicode \u7684\u5b58\u5728\u3002 \u9002\u7528\u573a\u666f\uff1a\u901a\u5e38\u4e0e\u6587\u5b57\u5904\u7406\u9886\u57df\u65e0\u5173\u7684\u8f6f\u4ef6\u4f1a\u91c7\u53d6\u8fd9\u79cd\u65b9\u6848\u3002 \u4f18\u70b9\uff1a\u65b9\u4fbf\uff0c\u4e14\u5185\u90e8\u5bf9\u5b57\u7b26\u4e32\u65e0\u4efb\u4f55\u8f6c\u6362\u548c\u5224\u65ad\uff0c\u6548\u7387\u6700\u9ad8\u3002 \u7f3a\u70b9\uff1a\u5728\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u8bfb\u5199\u5e26\u6709\u4e2d\u6587\u7684\u6587\u4ef6\u8def\u5f84\u65f6\uff0c\u4f1a\u9971\u53d7\u4e71\u7801\u548c\u627e\u4e0d\u5230\u6587\u4ef6\u7684\u56f0\u6270\u3002 \u65b9\u6cd5\uff1a\u5b8c\u5168\u4f7f\u7528 const char * \u548c std::string \u3002 \u4ee3\u8868\u4f5c\uff1aLinux \u6587\u4ef6\u7cfb\u7edf ext4\u3001Lua \u7f16\u7a0b\u8bed\u8a00\u3001\u73b0\u4ee3 Python \u4e2d\u7684 bytes \u7c7b\u578b\u3001HTTP \u7684 ? \u53c2\u6570\u3001\u65e9\u671f FAT32 \u6587\u4ef6\u7cfb\u7edf\u7b49\u3002 \u8fd9\u7c7b\u8f6f\u4ef6\u662f\u6700\u5e38\u89c1\u7684\u521d\u5b66\u8005\u5199\u6cd5\uff0c\u5982\u679c\u4f60\u4ece\u672a\u60f3\u8fc7\u5b57\u7b26\u7f16\u7801\u95ee\u9898\uff0c\u4ece\u4e0d\u4e86\u89e3 wchar_t \u3001 char32_t \u4e4b\u95f4\u7684\u6218\u4e89\uff0c\u53ea\u77e5\u9053 char \uff0c\u90a3\u4e48\u4f60\u5df2\u7ecf\u81ea\u52a8\u5728\u6b64\u9635\u8425\u91cc\u3002 \u6709\u4eba\u8bf4 Linux \u6587\u4ef6\u7cfb\u7edf\u662f UTF-8\uff1f\u5e76\u4e0d\u662f\uff01Linux \u6587\u4ef6\u7cfb\u7edf\u6839\u672c\u4e0d\u4f1a\u68c0\u9a8c\u4f60\u7684\u6587\u4ef6\u540d\u662f\u4e0d\u662f\u5408\u6cd5\u7684 UTF-8\uff0c\u53ea\u4e0d\u8fc7\u662f\u56e0\u4e3a\u4f60\u8bbe\u5b9a\u4e86 export LC_ALL=zh_CN.UTF-8 \uff0c\u8fd9\u4f1a\u4f7f\u6240\u6709\u7a0b\u5e8f\uff08\u5305\u62ec\u7ec8\u7aef\u6a21\u62df\u5668\uff09\u5047\u5b9a\u6587\u4ef6\u540d\u548c\u6587\u4ef6\u5185\u5bb9\u90fd\u6309 UTF-8 \u7f16\u7801\uff0c\u4ece\u800c\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf\u5404\u7c7b API \u65f6\uff08\u5982 open\u3001write\uff09\u90fd\u4f1a\u4f7f\u7528 UTF-8 \u7f16\u7801\u7684 const char * \u8f93\u5165\uff0c\u5728 Linux \u7cfb\u7edf API \u770b\u6765\uff0c\u6240\u8c13\u201c\u6587\u4ef6\u540d\u201d\u53ea\u662f\u7eaf\u7cb9\u7684\u5b57\u8282\u6d41\uff0c\u53ea\u8981\u4fdd\u8bc1\u4e0d\u5305\u542b '/' \u548c '\\0' \uff0c\u65e0\u8bba\u4f60\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4ed6\u90fd\u4e0d\u5728\u4e4e\u3002\u800c\u6240\u6709\u7684 locale \u90fd\u517c\u5bb9 ASCII\uff0c\u6240\u4ee5\u7edd\u4e0d\u4f1a\u51fa\u73b0\u4e00\u4e2a\u4e2d\u6587\u6c49\u5b57\u7f16\u7801\u540e\u4ea7\u751f '/' \u7684\u60c5\u51b5\uff08\u4f8b\u5982 GB2312 \u4f1a\u628a\u4e00\u4e2a\u4e2d\u6587\u7f16\u7801\u6210\u4e24\u4e2a 0x80 \u5230 0xFF \u533a\u95f4\u7684\u5b57\u8282\uff0c\u548c ASCII \u7684\u8303\u56f4\u6ca1\u6709\u91cd\u53e0\uff0c\u66f4\u4e0d\u53ef\u80fd\u51fa\u73b0 '/' \uff09\uff0c\u5373\u4f7f\u6362\u6210 export LC_ALL=zh_CN.GB2312 \uff0cLinux \u6587\u4ef6\u7cfb\u7edf\u4e00\u6837\u80fd\u6b63\u5e38\u5de5\u4f5c\uff0c\u53ea\u4e0d\u8fc7\u8bfb\u53d6\u4f60\u4e4b\u524d\u4ee5 UTF-8 \u5199\u5165\u7684\u6587\u4ef6\u4f1a\u53d8\u6210\u4e71\u7801\u800c\u5df2\u3002 \u5bf9\u4e8e\u4e2d\u56fd\u533a\u7684 Windows \u800c\u8a00\uff0c\u4ed6\u7684\u6240\u6709 A \u51fd\u6570\u53ea\u652f\u6301 GBK \u7f16\u7801\u3002\u8fd9\u610f\u5473\u7740\u5982\u679c\u4f60 Lua \u4e2d\u628a\u5b57\u7b26\u4e32\u201c\u5f53\u4f5c\u201d UTF-8 \u6765\u7528\u3002\u90a3\u4e48\u5f53\u4f60\u5728\u8c03\u7528 Lua \u7684 io.open \u524d\uff0c\u9700\u8981\u5148\u505a\u4e00\u4e2a UTF-8 \u5230 GBK \u7684\u8f6c\u6362\uff0c\u8fd9\u8fd8\u4f1a\u5bfc\u81f4\u4e22\u5931\u90e8\u5206\u4e0d\u5728 GBK \u5185\u7684\u5b57\u7b26\uff0c\u6bd4\u5982\u5982\u679c\u4f60\u7684\u6587\u4ef6\u540d\u5305\u542b Emoji\uff0c\u90a3\u5c31\u4f1a\u53d8\u6210 ??? \u4e71\u7801\u3002\u800c\u4f7f\u7528 W \u51fd\u6570\u7684 UTF-16 \u5c31\u4e0d\u4f1a\uff0c\u56e0\u4e3a UTF-16 \u80fd\u5bb9\u7eb3\u5b8c\u6574\u7684 Unicode \u6620\u5c04\u3002\u800c\u5b8c\u5168\u6446\u70c2\u7684 Lua\uff0c\u5176 io.open \u53ea\u662f\u4f7f\u7528 C \u8bed\u8a00\u5e93\u51fd\u6570 fopen \uff0c fopen \u53c8\u662f\u57fa\u4e8e Windows \u7684 A \u7cfb\u5217\u51fd\u6570\uff0cLua \u53c8\u6ca1\u6709\u63d0\u4f9b\u5bf9 Windows C \u8fd0\u884c\u65f6\u5e93\u7279\u6709\u7684 _wfopen \u51fd\u6570\u7684\u5c01\u88c5\uff0c\u4ece\u800c\u6c38\u8fdc\u4e0d\u53ef\u80fd\u6253\u5f00\u4e00\u4e2a\u5e26\u6709 Emoji \u7684\u6587\u4ef6\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 ANSI \u9635\u8425\uff0c\u4f60\u4ec0\u4e48\u90fd\u4e0d\u9700\u8981\u505a\uff0cchar \u6ee1\u5929\u98de\u6446\u70c2\u3002","title":"ANSI \u9635\u8425"},{"location":"unicode/#utf-8_3","text":"\u652f\u6301 Unicode\uff0c\u5b57\u7b26\u4e32\u7edf\u4e00\u4ee5 UTF-8 \u5f62\u5f0f\u5b58\u50a8\u3001\u5904\u7406\u548c\u4f20\u8f93\u3002 \u5e94\u7528\u573a\u666f\uff1a\u5e38\u89c1\u4e8e\u6587\u5b57\u5904\u7406\u9700\u6c42\u4e0d\u5927\uff0c\u4f46\u6709\u5f3a\u70c8\u7684\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u7279\u522b\u662f\u4e92\u8054\u7f51\u65b9\u9762\u7684\u8f6f\u4ef6\u3002\u4ed6\u4eec\u901a\u5e38\u53ea\u7528\u5230\u5b57\u7b26\u4e32\u7684\u62fc\u63a5\u3001\u67e5\u627e\u3001\u5207\u7247\u901a\u5e38\u4e5f\u53ea\u662f\u5728\u56fa\u5b9a\u7684\u4f4d\u7f6e\uff08\u4f8b\u5982\u6587\u4ef6\u5206\u9694\u7b26 '/' \uff09\u3002\u4e5f\u975e\u5e38\u9002\u5408\u4e3b\u8981\u9762\u5bf9\u7684\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0cUTF-8 \u662f\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u6700\u9ad8\u7684\uff0c\u6240\u4ee5\u4e5f\u5e7f\u6cdb\u7528\u4e8e\u7f16\u8bd1\u5668\u3001\u6570\u636e\u5e93\u4e4b\u7c7b\u7684\u573a\u666f\u3002\u540c\u65f6\u56e0\u4e3a UTF-8 \u5b8c\u5168\u517c\u5bb9 ASCII\uff0c\u4f7f\u5f97\u4ed6\u80fd\u8f7b\u6613\u9002\u914d\u8fdc\u53e4\u7684 C \u8bed\u8a00\u7a0b\u5e8f\u548c\u5e93\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-8 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8de8\u5e73\u53f0\uff0c\u5728\u7f51\u7edc\u4f20\u8f93\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u7801\uff0cUTF-8 \u662f\u4e92\u8054\u7f51\u7684\u4e3b\u6d41\u7f16\u7801\u683c\u5f0f\uff0c\u4e0d\u540c\u5e73\u53f0\u4e0a\u8fd0\u884c\u7684 UTF-8 \u8f6f\u4ef6\u53ef\u4ee5\u968f\u610f\u5171\u4eab\u6587\u672c\u6570\u636e\u3002\u517c\u5bb9 ASCII\uff0c\u65b9\u4fbf\u590d\u7528\u73b0\u6709\u5e93\u548c\u751f\u6001\u3002\u5bf9\u82f1\u6587\u7c7b\u6587\u672c\u538b\u7f29\u7387\u9ad8\uff0c\u5bf9\u4e2d\u6587\u6587\u672c\u4e5f\u4e0d\u7b97\u592a\u5dee\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e\u5e95\u5c42 API \u5747\u91c7\u7528 UTF-16 \u7684 Windows \u7cfb\u7edf\uff0c\u9700\u8981\u8fdb\u884c\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u635f\u5931\u3002\u4e14\u5b57\u7b26\u4e32\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4f1a\u53d8\u6210 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002 \u4ee3\u8868\u4f5c\uff1aRust \u8bed\u8a00\u3001Go \u8bed\u8a00\u3001CMake \u6784\u5efa\u7cfb\u7edf\u3001Julia \u8bed\u8a00\u7b49\u3002 \u5728 C++ \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7 u8\"\u4f60\u597d\" \u521b\u5efa\u4e00\u4e2a\u4fdd\u8bc1\u5185\u90e8\u662f UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u7c7b\u578b\u4e3a char8_t [] \u3002 \u5982\u679c\u7528\u65e0\u524d\u7f00\u7684 \"\u4f60\u597d\" \u521b\u5efa\uff0c\u5219 MSVC \u9ed8\u8ba4\u4f1a\u4ee5\u7f16\u8bd1\u8005\u6240\u5728\u7cfb\u7edf\u7684\u201c\u533a\u57df\u8bbe\u7f6e (locale)\u201d \u4f5c\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u7f16\u7801\u683c\u5f0f\uff08\u800c\u4e0d\u662f\u8fd0\u884c\u8005\u7684\u533a\u57df\u8bbe\u7f6e\uff01\uff09\uff0c\u5f00\u542f /utf-8 \u9009\u9879\u53ef\u4ee5\u8ba9 MSVC \u7f16\u8bd1\u5668\u9ed8\u8ba4\u91c7\u7528 UTF-8 \u7f16\u7801\uff0c\u5373\u8ba9 \"\u4f60\u597d\" \u548c u8\"\u4f60\u597d\" \u4e00\u6837\u91c7\u7528 UTF-8\u3002\u800c GCC \u9ed8\u8ba4\u5c31\u662f UTF-8\uff0c\u9664\u975e\u624b\u52a8\u6307\u5b9a -fexec-charset=GBK \u6765\u5207\u6362\u5230 GBK\u3002\u7a0d\u540e\u4f1a\u8be6\u7ec6\u8ba8\u8bba\u7f16\u8bd1\u5668\u7684\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002 \u5047\u8bbe\u4f60\u901a\u8fc7 /utf-8 \u6216 -fexec-charset=utf-8 \u641e\u5b9a\u4e86\u7f16\u8bd1\u671f\u5e38\u91cf\u5b57\u7b26\u4e32\u7684\u7f16\u7801\u3002\u63a5\u4e0b\u6765\u8fd8\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u6587\u4ef6\u7cfb\u7edf\u3002 Linux \u6587\u4ef6\u7cfb\u7edf\u5185\u90e8\uff0c\u5747\u4f7f\u7528 8 \u4f4d\u7c7b\u578b char \u5b58\u50a8\uff0c\u5c06\u6587\u4ef6\u540d\u5f53\u4f5c\u5e73\u51e1\u7684\u5b57\u8282\u6d41\uff0c\u4e0d\u4f1a\u505a\u4efb\u4f55\u8f6c\u6362\u3002\u56e0\u6b64\u4f60\u7528 UTF-8 \u521b\u5efa\u548c\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u5176\u4ed6\u4f7f\u7528 UTF-8 \u533a\u57df\u8bbe\u7f6e\u7684\u8f6f\u4ef6\u90fd\u53ef\u4ee5\u7167\u5e38\u6253\u5f00\uff0c\u4e0d\u4f1a\u6709\u4e71\u7801\u95ee\u9898\u3002 \u5176\u5b9e Windows \u4e0a\u4ee5 GBK \u7f16\u7801\u7684\u538b\u7f29\u6587\u4ef6\u6216\u6587\u672c\u6587\u4ef6\uff0c\u62f7\u8d1d\u5230 Linux \u4e0a\u6253\u5f00\u51fa\u73b0\u4e71\u7801\u95ee\u9898\uff0c\u5c31\u662f\u56e0\u4e3a Linux \u7684\u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u90fd\u662f UTF-8 \u7684\u3002\u5b9e\u9645\u4e0a\u5982\u679c\u628a\u4f60\u7684\u6587\u4ef6\u62f7\u7ed9\u4e00\u4e2a\u7f8e\u56fd\u7684 Windows \u7528\u6237\uff0c\u4ed6\u4e5f\u4f1a\u770b\u5230\u4e71\u7801\uff0c\u56e0\u4e3a\u7f8e\u56fd\u5927\u533a\u7684 Windows \u533a\u57df\u8bbe\u7f6e\u9ed8\u8ba4\u662f UTF-8\uff0c\u800c\u4e2d\u56fd\u5927\u533a\u7684\u662f GBK\uff0c\u7a0d\u540e\u6211\u4eec\u4f1a\u8bb2\u5230\u89e3\u51b3\u65b9\u6848\u3002 \u800c Windows \u7684 NTFS \u6587\u4ef6\u7cfb\u7edf\uff0c\u91c7\u7528 16 \u4f4d\u7684 wchar_t \u5b58\u50a8\uff0cWindows \u7684\u6240\u6709 API\uff0c\u4e5f\u90fd\u662f\u57fa\u4e8e wchar_t \u7684\uff0cWindows \u5185\u6838\u5185\u90e8\u4e5f\u90fd\u7528 wchar_t \u50a8\u5b58\u6587\u672c\u5b57\u7b26\u4e32\uff0c\u53ea\u6709\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6d41\u4f1a\u7528 char \u5b58\u50a8\u3002\u8fd9\u7c7b\u57fa\u4e8e wchar_t \u7684\u7cfb\u7edf API \u90fd\u6709\u4e00\u4e2a W \u540e\u7f00\uff0c\u4f8b\u5982\uff1a MessageBoxW(NULL, L\"\u4f60\u597d\", L\"\u6807\u9898\", MB_OK); \u8fd9\u4e2a MessageBoxW \u51fd\u6570\uff0c\u53ea\u63a5\u53d7 const wchar_t * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002 L\"\u4f60\u597d\" \u662f\u4e00\u4e2a wchar_t [] \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u5b83\u7684\u5185\u90e8\u7f16\u7801\u7c7b\u578b\u56fa\u5b9a\u662f UTF-16\uff0c\u4e0d\u4f1a\u968f\u7740\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u800c\u53d8\u3002\u4e4b\u540e\u7684\u4e00\u8282\u4e2d\u4f1a\u8be6\u7ec6\u8bb2\u89e3 W \u548c A \u51fd\u6570\u7684\u95ee\u9898\u3002 \u867d\u7136\u4e5f\u6709\u63d0\u4f9b A \u540e\u7f00\u7684\u7cfb\u5217\u51fd\u6570\uff0c\u4ed6\u4eec\u548c W \u4e00\u6837\uff0c\u53ea\u4e0d\u8fc7\u662f\u63a5\u53d7 const char * \u7c7b\u578b\u7684\u5b57\u7b26\u4e32\u3002\u95ee\u9898\u5728\u4e8e\uff0c\u8fd9\u4e9b\u5b57\u7b26\u4e32\u90fd\u5fc5\u987b\u662f\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u91cc\u7684\u90a3\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u4e5f\u5c31\u662f GBK \u7f16\u7801\uff01\u800c\u4e14\u65e0\u6cd5\u4fee\u6539\u3002 \u5f53\u8c03\u7528 A \u7cfb\u51fd\u6570\u65f6\uff0c\u4ed6\u4eec\u5185\u90e8\u4f1a\u628a GBK \u7f16\u7801\u8f6c\u6362\u4e3a UTF-16 \u7f16\u7801\uff0c\u7136\u540e\u8c03\u7528 Windows \u5185\u6838\u3002 \u8fd9\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\uff0c\u800c\u6240\u6709\u7684 C/C++ \u6807\u51c6\u5e93\u90fd\u662f\u57fa\u4e8e A \u51fd\u6570\u7684\uff01\u5982\u679c\u4f60\u7528 const char * \u5b57\u7b26\u4e32\u8c03\u7528 C \u6807\u51c6\u5e93\uff0c\u76f8\u5f53\u4e8e\u8c03\u7528\u4e86 A \u51fd\u6570\u3002\u800c A \u51fd\u6570\u53ea\u63a5\u53d7 GBK\uff0c\u4f46\u4f60\u5374\u8f93\u5165\u4e86 UTF-8\uff01\u4ece\u800c UTF-8 \u4e2d\u6240\u6709\u9664 ASCII \u4ee5\u5916\u7684\uff0c\u5404\u79cd\u4e2d\u6587\u5b57\u7b26\u3001Emoji \u90fd\u4f1a\u53d8\u6210\u4e71\u7801\u3002 \u4f8b\u5982 fopen \u51fd\u6570\uff0c\u53ea\u6709 fopen(const char *path, const char *mode) \u8fd9\u4e00\u4e2a\u57fa\u4e8e char \u7684\u7248\u672c\uff0c\u91cc\u9762\u4e5f\u662f\u76f4\u63a5\u8c03\u7528\u7684 A \u51fd\u6570\uff0c\u5b8c\u5168\u4e0d\u7ed9\u6211\u9009\u62e9\u7684\u7a7a\u95f4\u3002\u867d\u7136 Windows \u4e5f\u63d0\u4f9b\u4e86 _wfopen(const wchar_t *path, const wchar_t *mode) \uff0c\u4f46\u90a3\u65e2\u4e0d\u662f POSIX \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4e5f\u4e0d\u662f C \u8bed\u8a00\u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u7528\u8fd9\u6837\u7684\u51fd\u6570\u5c31\u610f\u5473\u7740\u65e0\u6cd5\u8de8\u5e73\u53f0\u3002 Windows \u5b98\u65b9\u8ba4\u4e3a\uff1a W \u51fd\u6570\u624d\u662f\u771f\u6b63\u7684 API\uff0c A \u51fd\u6570\u53ea\u662f\u5e94\u4ed8\u4e0d\u542c\u8bdd\u7684\u5b9d\u5b9d\u3002\u53ef\u4f60\u5c31\u6ca1\u53d1\u73b0\u4f60\u81ea\u5df1\u7684 C/C++ \u6807\u51c6\u5e93\u4e5f\u5168\u90e8\u5728\u8c03\u7528\u7684 A \u51fd\u6570\u4e48\uff1f \u603b\u4e4b\uff0c A \u51fd\u6570\u662f\u6b8b\u5e9f\u7684\uff0c\u6211\u4eec\u53ea\u80fd\u7528 W \u51fd\u6570\uff0c\u5c3d\u7ba1 UTF-16 \u662f\u5386\u53f2\u503a\uff0c\u4f46\u6211\u4eec\u522b\u65e0\u9009\u62e9\uff0c W \u51fd\u6570\u662f\u552f\u4e00\u80fd\u652f\u6301\u5b8c\u6574 Unicode \u5b57\u7b26\u8f93\u5165\u7684\u65b9\u5f0f\u3002 // \u5047\u8bbe\u8fd9\u6bb5 C++ \u4ee3\u7801\u4f7f\u7528 /utf-8 \u9009\u9879\u7f16\u8bd1\uff1a std::ifstream f(\"\u4f60\u597d.txt\"); // \u627e\u4e0d\u5230\u6587\u4ef6\uff0c\u5373\u4f7f\u201c\u4f60\u597d.txt\u201d\u5b58\u5728 std::ofstream f(\"\u4f60\u597d.txt\"); // \u4f1a\u521b\u5efa\u4e00\u4e2a\u4e71\u7801\u6587\u4ef6 \u6b63\u786e\u7684\u505a\u6cd5\u662f\u91c7\u7528 std::filesystem::u8path \u8fd9\u4e2a\u51fd\u6570\u505a UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\uff1a // C++17\uff0c\u9700\u8981\u7528 u8path \u8fd9\u4e2a\u51fd\u6570\u6784\u9020 path \u5bf9\u8c61\uff1a std::ifstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::u8path(\"\u4f60\u597d.txt\")); // C++20 \u5f15\u5165 char8_t\uff0c\u533a\u5206\u4e8e\u666e\u901a char\uff0cpath \u7c7b\u4e5f\u6709\u4e86\u9488\u5bf9 const char8_t * \u7684\u6784\u9020\u51fd\u6570\u91cd\u8f7d\uff1a std::ifstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::ofstream f(std::filesystem::path(u8\"\u4f60\u597d.txt\")); std::filesystem::path \u7c7b\u7684 c_str() \u5728 Windows \u4e0a\u8fd4\u56de const wchar_t * \uff0c\u5728 Linux \u4e0a\u8fd4\u56de const char * \u3002\u8fd9\u5f88\u5408\u7406\uff0c\u56e0\u4e3a Windows \u6587\u4ef6\u7cfb\u7edf\u786e\u5b9e\u4ee5 wchar_t \u5b58\u50a8\u8def\u5f84\u540d\uff0c\u800c Linux \u6587\u4ef6\u7cfb\u7edf\u5b8c\u5168\u7528 char \u3002 \u6bcf\u6b21\u9700\u8981\u52a0 std::filesystem::u8path \u4e5f\u633a\u9ebb\u70e6\u7684\uff0c\u5bb9\u6613\u5fd8\u8bb0\uff0c\u4e00\u5fd8\u8bb0\u5c31\u65e0\u6cd5\u8bbf\u95ee\u4e2d\u6587\u76ee\u5f55\u3002 \u5f88\u591a\u8f6f\u4ef6\u5728 Windows \u4e0a\u65e0\u6cd5\u652f\u6301\u4e2d\u6587\u8def\u5f84\u540d\uff0c\u5c31\u662f\u56e0\u4e3a\u4ed6\u4eec\u4e60\u60ef\u4e86 Linux \u6216 MacOS \u7684\u5168 UTF-8 \u73af\u5883\uff0c\u5bf9\u6587\u4ef6\u8def\u5f84\u6ca1\u6709\u4efb\u4f55\u8f6c\u6362\u3002\u800c Windows \u5e95\u5c42\u5168\u662f UTF-16\uff0c\u6839\u672c\u6ca1\u6709\u63d0\u4f9b UTF-8 \u7684 API\uff0c\u4f60 UTF-8 \u53ea\u80fd\u8f6c\u6362\u6210 UTF-16 \u624d\u80fd\u907f\u514d\u4e2d\u6587\u4e71\u7801\u3002\u4e2a\u4eba\u8ba4\u4e3a\uff0c\u6b7b\u6d3b\u4e0d\u80af\u63a5\u53d7\u660e\u6446\u7740\u5df2\u7ecf\u662f\u56fd\u9645\u901a\u7528\u6807\u51c6\u7684 UTF-8\uff0cA \u51fd\u6570\u7684\u7f16\u7801\u8fde\u5f53\u524d\u8fdb\u7a0b\u5207\u6362\u7684\u65b9\u6cd5\u90fd\u4e0d\u7ed9\u4e00\u4e2a\uff0c\u8fd9\u4e2a\u5e94\u8be5\u7531 Windows \u5168\u8d23\u627f\u62c5\u3002 \u597d\u6d88\u606f\u662f\uff0c\u6700\u8fd1 MSVC \u6807\u51c6\u5e93\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6848\uff0c\u5728\u4f60\u7684\u7a0b\u5e8f\u5f00\u5934\uff0c\u52a0\u4e0a setlocale(LC_ALL, \".utf-8\") \u5c31\u53ef\u4ee5\u8ba9 C \u548c C++ \u6807\u51c6\u5e93\u8fdb\u5165 UTF-8 \u6a21\u5f0f\uff1a\u4e0d\u518d\u8c03\u7528 A \u7cfb\u51fd\u6570\u64cd\u4f5c\u6587\u4ef6\uff0c\u800c\u662f\u4f1a\u628a\u6587\u4ef6\u540d\u4ece UTF-8 \u8f6c\u6362\u6210 UTF-16 \u540e\u518d\u8c03\u7528\u771f\u6b63\u7a33\u5b9a\u7684 W \u7cfb\u51fd\u6570\u3002 setlocale(LC_ALL, \".utf-8\"); // \u53ea\u9700\u8981\u8fd9\u4e00\u884c FILE *fp = fopen(u8\"\u4f60\u597d.txt\", \"r\"); // \u53ef\u4ee5\u4e86 std::ifstream fin(u8\"\u4f60\u597d.txt\"); // \u53ef\u4ee5\u4e86 setlocale(LC_ALL, \".utf-8\"); \u53ea\u662f\u628a C \u6807\u51c6\u5e93\u7684 const char * \u53c2\u6570\u53d8\u6210\u4e86\u63a5\u53d7 UTF-8\uff0c\u5e76\u4e0d\u4f1a\u8ba9\u7cfb\u7edf\u7684 A \u51fd\u6570\u4e5f\u53d8\u6210 UTF-8 \u54e6\uff0c\u8c03\u7528\u672c\u5730 API \u65f6\u4ecd\u9700 UTF-8 \u5230 UTF-16 \u7684\u8f6c\u6362\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-8 \u9635\u8425\uff0c\u5f00\u542f /utf-8 \uff0c\u7a0b\u5e8f\u5f00\u5934\u5199 setlocale(LC_ALL, \".utf-8\") \u3002Linux \u7528\u6237\u5219\u4ec0\u4e48\u90fd\u4e0d\u7528\u505a\u3002 \u770b\u770b\u5404\u5927\u8f6f\u4ef6\u7ad9\u5728 UTF-8 \u9635\u8425\u7684\u7406\u7531\uff1a CMake\uff1a\u4f5c\u4e3a\u8de8\u5e73\u53f0\u7684\u6784\u5efa\u7cfb\u7edf\uff0c\u4e3a\u4e86\u8ba9\u9879\u76ee\u7684 CMakeLists.txt \u80fd\u8de8\u5e73\u53f0\u5171\u7528\u800c\u4e0d\u5fc5\u91cd\u5199\uff0c\u4ed6\u7406\u6240\u5f53\u7136\u5730\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\uff1a\u6240\u6709 CMakeLists.txt \u90fd\u5fc5\u987b\u4ee5 UTF-8 \u683c\u5f0f\u4e66\u5199\uff0c\u4e14\u7edf\u4e00\u4f7f\u7528\u6b63\u659c\u6760 '/' \u8def\u5f84\u5206\u9694\u7b26\u3002 CMake \u4f1a\u81ea\u52a8\u5728 Windows \u7cfb\u7edf\u4e0a\uff0c\u5c06 UTF-8 \u5b57\u7b26\u4e32\u8f6c\u6362\u6210 UTF-16 \u540e\uff0c\u8c03\u7528 Windows \u7cfb\u7edf API\uff0c\u5728 Linux \u7cfb\u7edf\u4e0a\u5219\u4e0d\u505a\u8f6c\u6362\u3002\u5728 Windows \u7cfb\u7edf\u4e0a\u8fd8\u4f1a\u81ea\u52a8\u628a\u6587\u4ef6\u8def\u5f84\u4e2d\u7684\u6b63\u659c\u6760 '/' \u8f6c\u6362\u6210 Windows \u4e13\u5c5e\u7684\u53cd\u659c\u6760 '\\\\' \uff0c\u65e0\u9700\u7528\u6237\u64cd\u5fc3\u3002 \u5c0f\u5f6d\u8001\u5e08\u81ea\u4e3b\u7814\u53d1\u7684 Zeno \u8282\u70b9\u4eff\u771f\u8f6f\u4ef6\uff1a\u7531\u4e8e\u4fdd\u5b58\u7684\u9879\u76ee\u5de5\u7a0b\u6587\u4ef6\u9700\u8981\u5728 Linux \u548c Windows \u5e73\u53f0\u4e0a\u4e92\u901a\uff0c\u4e0d\u80fd\u91c7\u7528 Windows \u5404\u81ea\u4e3a\u653f\u7684 GBK \u683c\u5f0f\uff0c\u4e14\u5de5\u7a0b\u6587\u4ef6\u5185\u5bb9\u662f\u4ee5 ASCII \u4e3a\u4e3b\u7684\u201c\u4ee3\u7801\u201d\u7c7b\u6587\u672c\uff0c\u6240\u4ee5\u6211\u4eec\u4e5f\u7ad9\u5728\u4e86 UTF-8 \u9635\u8425\u4e2d\u3002 Rust \u548c Go\uff1a\u4e25\u683c\u533a\u5206\u201c\u5b57\u7b26 (32 \u4f4d)\u201d\u548c\u201c\u5b57\u8282 (8 \u4f4d)\u201d\u7684\u6982\u5ff5\u3002\u5728\u5b57\u7b26\u4e32\u7c7b\u578b\u4e2d\u5b58\u50a8\u5b57\u8282\uff0c\u4f46\u53ef\u4ee5\u9009\u62e9\u4ee5\u5b57\u8282\u65b9\u5f0f\u8bfb\u53d6\u6216\u4ee5\u5b57\u7b26\u65b9\u5f0f\u8bfb\u53d6\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-8 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-8 \u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5904\u7406\u5207\u7247\u548c\u7d22\u5f15\u65f6\u4e0d\u65b9\u4fbf\u3002 \u7f16\u7a0b\u8bed\u8a00 \u5b57\u7b26\u7c7b\u578b (32 \u4f4d) \u5b57\u8282\u7c7b\u578b (8 \u4f4d) Rust char u8 Go rune byte Julia Char UInt8 \u4e3a\u6b64\uff0c\u8fd9\u4e9b\u8bed\u8a00\u90fd\u4e3a\u5b57\u7b26\u4e32\u63d0\u4f9b\u4e86\u4e24\u5957 API\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u4e00\u79cd\u662f\u6309\u5b57\u8282\u7d22\u5f15\u3002\u6309\u5b57\u7b26\u7d22\u5f15\u65f6\uff0c\u4f1a\u4ece\u5934\u5f00\u59cb\uff0c\u9010\u4e2a\u89e3\u6790\u7801\u4f4d\uff0c\u76f4\u5230\u89e3\u6790\u5230\u60f3\u8981\u7684\u5b57\u7b26\u4e3a\u6b62\uff0c\u590d\u6742\u5ea6 O(N) O(N) \u3002\u6309\u5b57\u8282\u7d22\u5f15\u65f6\uff0c\u76f4\u63a5\u8df3\u5230\u6307\u5b9a\u5b57\u8282\uff0c\u65e0\u9700\u89e3\u6790\uff0c\u590d\u6742\u5ea6 O(1) O(1) \u3002 let s = \"\u4f60\u597d\"; // \u6309\u5b57\u7b26\u904d\u5386 for c in s.chars() { // c: char println!(\"{}\", c); } // \u6309\u5b57\u8282\u904d\u5386 for b in s.bytes() { // b: u8 println!(\"{:02x}\", b); } \u5728 C++ \u4e2d\uff0c\u82e5\u8981\u91c7\u7528\u8fd9\u79cd UTF-8 \u65b9\u6848\uff0c\u53ef\u4ee5\u4f7f\u7528 utfcpp \u8fd9\u4e2a\u5e93\uff1a https://github.com/nemtrif/utfcpp \u7a0d\u540e\u6211\u4eec\u4f1a\u4ee5\u6848\u4f8b\u8be6\u7ec6\u6f14\u793a\u8fd9\u4e2a\u5e93\u7684\u7528\u6cd5\uff0c\u4e5f\u4f1a\u5c1d\u8bd5\u81ea\u5df1\u624b\u6413\u3002 \u65b9\u6cd51\uff1a\u4f7f\u7528 utf8to32 \u4e00\u6b21\u6027\u5b8c\u6210\u8f6c\u6362\uff0c\u7528\u5b8c\u540e\u518d\u8f6c\u56de\u53bb\u3002 std::string s = \"\u4f60\u597d\"; std::u32string u32 = utf8::utf8to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\u574f'; s = utf8::utf32to8(u32); fmt::println(\"{}\", s); // \u4f60\u574f \u65b9\u6cd52\uff1a utfcpp \u4e5f\u5c01\u88c5\u4e86\u4e00\u4e2a utf8::iterator \u8fed\u4ee3\u5668\u9002\u914d\u5668\uff0c\u6548\u679c\u7c7b\u4f3c\u4e8e Rust \u7684 .chars() \uff0c\u53ef\u4ee5\u5b57\u7b26\u800c\u4e0d\u662f\u5b57\u8282\u904d\u5386\u5b57\u7b26\u4e32\u5bb9\u5668\u3002 char s[] = \"\u4f60\u597d\"; utf8::unchecked::iterator bit(s); utf8::unchecked::iterator eit(s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u5b89\u5168\uff08\u5e26\u8fb9\u754c\u68c0\u6d4b\uff09\u7684\u7248\u672c char s[] = \"\u4f60\u597d\"; utf8::iterator bit(s, s, s + strlen(s)); utf8::iterator eit(s + strlen(s), s, s + strlen(s)); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } // \u57fa\u4e8e std::string \u7684\u7248\u672c std::string s = \"\u4f60\u597d\"; utf8::iterator bit(s.begin(), s.begin(), s.end()); utf8::iterator eit(s.end(), s.begin(), s.end()); for (auto it = bit; it != eit; ++it) { // *it: char32_t fmt::println(\"U+{:04X}\", *it); } \u7531\u4e8e\u8fed\u4ee3\u5668\u63a5\u53e3\u590d\u6742\u96be\u61c2\uff0c\u5efa\u8bae\u5148\u5c01\u88c5\u6210\u5e26\u6709 begin() \u548c end() \u7684 range \u5bf9\u8c61\uff0c\u65b9\u4fbf\u4f7f\u7528 C++17 range-based loop \u8bed\u6cd5\u76f4\u89c2\u904d\u5386\uff1a template struct Utf8Range { utf8::iterator bit; utf8::iterator eit; template Utf8Range(T &&t) : bit(std::begin(t), std::begin(t), std::end(t)) , eit(std::end(t), std::begin(t), std::end(t)) {} auto begin() const { return bit; } auto end() const { return eit; } }; template Utf8Range(T &&t) -> Utf8Range; // \u4ee5\u4e0b\u662f\u65b0\u7c7b\u7684\u4f7f\u7528\u65b9\u6cd5 std::string s = \"\u4f60\u597d\"; for (char32_t c : Utf8Range(s)) { fmt::println(\"U+{:04X}\", c); }","title":"UTF-8 \u9635\u8425"},{"location":"unicode/#utf-16_2","text":"\u652f\u6301 Unicode \u8fc7\u65e9\uff0c\u8bef\u4ee5\u4e3a 0xFFFF \u5c31\u662f Unicode \u7684\u4e0a\u9650\u3002 \u4e00\u5f00\u59cb\uff0c\u4eba\u4eec\u9519\u8bef\u5730\u628a UTF-16 \u5f53\u6210\u6c38\u8fdc\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u4e00\u52b3\u6c38\u9038\u89e3\u51b3\u4e71\u7801\u95ee\u9898\uff0c\u6240\u4ee5\u90a3\u6bb5\u65f6\u671f\u7684\u8f6f\u4ef6\u90fd\u5927\u4e3e\u4f7f\u7528 UTF-16 \u4f5c\u4e3a\u5185\u7801\u3002\u6ca1\u60f3\u5230\u540e\u6765 Unicode \u53c8\u5f15\u5165 0x10FFFF \u8303\u56f4\u7684\u7a00\u6709\u5b57\u7b26\uff0c\u800c\u73b0\u6709\u7684\u5df2\u7ecf\u91c7\u7528\u4e86 16 \u4f4d\u5185\u7801\u7684\u8f6f\u4ef6\u53c8\u5df2\u7ecf\u65e0\u6cd5\u6839\u9664\uff0c\u53ea\u597d\u4f7f\u7528\u201c\u4ee3\u7406\u5bf9\u201d\u673a\u5236\uff0c\u589e\u91cf\u66f4\u65b0\u4fee\u590d\u4e86\u73b0\u6709\u7684 16 \u4f4d\u5185\u7801\u8f6f\u4ef6\u3002UTF-16 \u65e2\u6ca1\u6709 UTF-8 \u517c\u5bb9 ASCII \u7684\u597d\u5904\uff0c\u53c8\u6ca1\u6709 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u597d\u5904\uff0c\u7559\u4e0b\u5386\u53f2\u503a\u3002 \u4e8b\u5b9e\u4e0a\uff0cUnicode \u5df2\u7ecf\u65e0\u6cd5\u7ee7\u7eed\u6269\u5bb9\u7a81\u7834 0x10FFFF\uff0c\u5c31\u662f\u56e0\u4e3a\u53cc uint16_t \u7f16\u7801\u7684\u4ee3\u7406\u5bf9\u6700\u591a\u53ea\u80fd\u5bb9\u7eb3\u989d\u5916 0x100000 \u4e2a\u5b57\u7b26\u7684\u7a7a\u95f4\u3002\u672c\u6765 UTF-8 \u4e00\u5f00\u59cb\u7684\u8349\u6848\u662f\u6253\u7b97\u6700\u591a\u652f\u6301 8 \u8282\u5217\u8f66\uff0c\u5b8c\u5168\u5bb9\u7eb3\u9ad8\u8fbe 0x7FFFFFFF \u8303\u56f4\u7684\u5b57\u7b26\u3002\u4e3a\u4e86\u8ba9 Windows \u8fd8\u80fd\u7ee7\u7eed\u7528\uff0cUnicode \u624d\u88ab\u8feb\u6b62\u6b65 0x10FFFF\uff0cUTF-8 \u4e5f\u7ec8\u7ed3\u4e8e 4 \u8282\u5217\u8f66\u3002 \u5e94\u7528\u573a\u666f\uff1a\u901a\u5e38\u8ba4\u4e3a\uff0cUTF-16 \u662f\u7eaf\u7cb9\u7684\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff0c\u65b0\u8f6f\u4ef6\u4e0d\u5e94\u8be5\u518d\u4f7f\u7528 UTF-16\u3002\u53ea\u6709\u5728\u548c\u8fd9\u4e9b\u7cdf\u7c95\u8f6f\u4ef6\u7684 API \u6253\u4ea4\u9053\u65f6\uff0c\u624d\u5fc5\u987b\u8f6c\u6362\u4e3a UTF-16\u3002\u4f46\u4e5f\u6709\u4eba\u6307\u51fa\uff1aUTF-16 \u662f\u7eaf\u4e2d\u6587\u538b\u7f29\u7387\u6700\u9ad8\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u6240\u4ee5 UTF-16 \u8fd8\u6bd4\u8f83\u9002\u5408\u7eaf\u4e2d\u6587\u6216\u4ee5\u4e2d\u6587\u5185\u5bb9\u4e3a\u4e3b\u7684\u6587\u672c\u6570\u636e\u538b\u7f29\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-16 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u8c03\u7528 Windows \u7cfb\u7edf API \u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u76f4\u63a5\u5c31\u80fd\u8c03\u7528\uff0c\u6700\u9002\u5408 Windows \u672c\u5730\u5f00\u53d1\uff0c\u975e\u8de8\u5e73\u53f0\u3002\u4e14\u5bf9\u7eaf\u4e2d\u6587\u5185\u5bb9\u53ef\u6bd4 UTF-8 \u989d\u5916\u8282\u7701 33% \u7a7a\u95f4\u3002 \u7f3a\u70b9\uff1a\u5bf9\u4e8e Windows \u4ee5\u5916\u7684\u7cfb\u7edf\u5c31\u9700\u8981\u8f6c\u6362\u56de UTF-8\uff0c\u6709\u5c11\u91cf\u6027\u80fd\u5f00\u9500\u3002\u4e14\u5982\u679c\u5b58\u50a8\u7684\u5185\u5bb9\u4e3b\u8981\u662f\u7eaf\u82f1\u6587\uff0c\u5982 XML \u4ee3\u7801\u7b49\uff0c\u5185\u5b58\u5360\u7528\u4f1a\u6bd4 UTF-8 \u7ffb\u500d\u3002\u800c\u4e14 UTF-16 \u4ecd\u7136\u662f\u53d8\u957f\u7f16\u7801\uff0c\u867d\u7136\u51fa\u73b0\u53d8\u957f\u7684\u6982\u7387\u8f83\u4f4e\uff0c\u4f46\u4e0d\u4e3a 0\uff0c\u4ecd\u9700\u8981\u5f00\u53d1\u8005\u505a\u7279\u6b8a\u5904\u7406\u3002\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u4f1a\u5bfc\u81f4\u751f\u50fb\u5b57\u7b26\u51fa\u9519\uff0c\u5b57\u7b26\u4e32\u4ee5\u7801\u70b9\u4e3a\u5355\u4f4d\u7684\u7684\u6b63\u786e\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u7684\u590d\u6742\u5ea6\u4ecd\u7136 O(N) O(N) \u800c\u4e0d\u662f\u901a\u5e38\u7684 O(1) O(1) \u3002\u5e76\u4e14 UTF-16 \u6709\u5927\u5c0f\u7aef\u8f6c\u6362\u7684\u95ee\u9898\u3002 \u4ee3\u8868\u4f5c\uff1aWindows \u7cfb\u7edf API\u3001Java \u8bed\u8a00\u3001Windows \u6587\u4ef6\u7cfb\u7edf (NTFS)\u3001Qt\u3001Word\u3001JSON\uff0c\u4ed6\u4eec\u90fd\u662f UTF-16 \u7684\u53d7\u5bb3\u8005\u3002 \u8fd9\u76f8\u5f53\u4e8e\u662f\u628a UTF-16 \u5f53\u4f5c\u4e86\u5185\u7801\uff0c\u4f46 UTF-16 \u4f9d\u7136\u662f\u4e00\u79cd\u53d8\u957f\u7f16\u7801\uff0c\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5904\u7406\u6ca1\u95ee\u9898\uff0c\u751f\u50fb\u5b57\u5c31\u5bb9\u6613\u51fa\u95ee\u9898\uff0c\u4e14\u56e0\u4e3a\u51fa\u73b0\u6982\u7387\u4f4e\uff0c\u5f88\u5bb9\u6613\u4e0d\u53d1\u73b0\uff0c\u57cb\u4e0b\u9690\u60a3\u3002 Java \u5c31\u662f\u53d7\u5230\u4e86 UTF-16 \u5386\u53f2\u503a\u5f71\u54cd\uff0c char \u662f 16 \u4f4d\u7684\u7801\u4f4d\uff0c\u800c\u4e0d\u662f\u5b57\u7b26\uff0c\u771f\u6b63\u7684\u4e00\u4e2a\u5b57\u7b26\u662f 32 \u4f4d\u7684 Character \u7c7b\u578b\u3002 \u7f16\u7a0b\u8bed\u8a00 \u7801\u70b9\u7c7b\u578b (32 \u4f4d) \u7801\u4f4d\u7c7b\u578b (16 \u4f4d) Java Character char Java \u7684 Character \u7c7b\u578b\u662f\u4e00\u4e2a 32 \u4f4d\u7684\u503c\uff0c\u8fd9\u4e2a\u503c\u5305\u542b\u4e86\u4e00\u4e2a Unicode \u7801\u4f4d\u3002 char \u7c7b\u578b\u662f\u4e00\u4e2a 16 \u4f4d\u7684\u503c\uff0c\u5b83\u5305\u542b\u4e86\u4e00\u4e2a UTF-16 \u7f16\u7801\u7684\u7801\u70b9\u3002 String \u7684 charAt() \u65b9\u6cd5\u8fd4\u56de\u7684\u662f char \u7c7b\u578b\u7684\u7801\u4f4d\uff08\u7c7b\u4f3c\u4e8e\u5b57\u8282\uff09\uff0c\u5982\u679c\u8981\u83b7\u53d6 Character \u7c7b\u578b\u7684\u5b8c\u6574\u5b57\u7b26\uff0c\u5fc5\u987b\u4f7f\u7528 codePointAt() \u65b9\u6cd5\u3002\u8fd9\u662f Java \u8bed\u8a00\u8bbe\u8ba1\u4e0a\u7684\u4e00\u4e2a\u5931\u8bef\uff0c\u5df2\u7ecf\u65e0\u6cd5\u6539\u53d8\u3002 \u800c\u540e\u7eed\u65b0\u51fa\u7684 Kotlin \u662f Java \u7684\u5408\u6cd5\u7ee7\u627f\u8005\uff0c\u4ed6\u679c\u65ad\u653e\u5f03 UTF-16\uff0c\u52a0\u5165\u4e86 UTF-32 \u9635\u8425\u3002\u53ef\u89c1\uff0c\u8001\u8f6f\u4ef6\u575a\u6301\u7528 UTF-32 \u662f\u56e0\u4e3a\u4ed6\u4eec\u79ef\u91cd\u96be\u8fd4\uff0c\u65b0\u8f6f\u4ef6\u518d UTF-16 \u5c31\u662f\u81ea\u4f5c\u5b7d\u4e86\uff01 \u603b\u7ed3\uff1a\u4e0d\u8981\u652f\u6301 UTF-16 \u9635\u8425\uff0c\u9664\u975e\u4f60\u88ab\u8feb\u7ef4\u62a4\u53f2\u5c71\u3002 \u4f8b\u5982\u5c0f\u5f6d\u8001\u5e08\u53d1\u5fae\u4fe1\u670b\u53cb\u5708\u65f6\uff0c\u8f93\u5165 Emoji \u8868\u60c5\u540e\u526a\u5207\uff0c\u518d\u7c98\u8d34\uff0c\u5c31\u548c\u53d1\u73b0\u4e00\u4e2a Emoji \u88ab\u5207\u65ad\u6210\u4e86\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4e71\u7801\u7684\u5f62\u5f0f\u663e\u73b0\u3002\u4f30\u8ba1\u662f\u56e0\u4e3a\u5fae\u4fe1\u57fa\u4e8e Java \u7f16\u5199\uff0c\u75bc\u900a\u7a0b\u5e8f\u5458\u5bf9 UTF-16 \u4ee3\u7406\u5bf9\u5904\u7406\u7684\u4e0d\u5229\u7d22\u3002 Java \u4e2d\u4ee5\u7801\u70b9\u904d\u5386\u4e00\u4e2a\u5b57\u7b26\u4e32\u7684\u5199\u6cd5\uff1a String s = \"\u4f60\u597d\"; // \u6309\u7801\u70b9\u904d\u5386 for (int i = 0; i < s.length();) { Character c = s.codePointAt(i); System.out.println(String.format(\"U+%04X\", c)); i += Character.charCount(c); } // \u6309\u7801\u4f4d\u904d\u5386 for (char c : s.toCharArray()) { System.out.println(String.format(\"U+%04X\", (int) c)); } \u7531\u4e8e JSON \u662f\u548c Java \u4e00\u5757\u53d1\u660e\u7684\u3002\u5bf9\u4e8e\u8d85\u51fa 0xFFFF \u8303\u56f4\u7684\u5b57\u7b26\uff0c\u91c7\u7528\u7684\u8f6c\u4e49\uff0c\u4e5f\u662f\u57fa\u4e8e UTF-16 \u7f16\u7801\u3002\u5373\u540c\u4e00\u4e2a\u5b57\u4f1a\u53d8\u6210\u4e24\u4e2a\u4ee3\u7406\u5bf9\uff0c\u4ee5\u4fdd\u8bc1 JSON \u6587\u4ef6\u603b\u662f ASCII \u683c\u5f0f\uff0c\u907f\u514d Windows \u7684 GBK \u7f16\u7801\u4e71\u505a\u989d\u5916\u7684\u5e72\u6270\u3002 // \u4ee5\u4e0b\u4e24\u79cd\u5199\u6cd5\u7b49\u4ef7 {\"name\": \"\ud883\udede\"} {\"name\": \"\\ud883\\udfde\"} \u5728\u521a\u521a\u4ecb\u7ecd\u7684 C++ \u5e93 utfcpp \u4e2d\uff0c\u4e5f\u6709\u9488\u5bf9 UTF-16 \u7684\u8f6c\u6362\u51fd\u6570\uff0c\u5982 utf16to32 \uff1a std::u16string s = u\"\u4f60\u597d\"; std::u32string u32 = utf16::utf16to32(s); fmt::println(\"U+{:04X}\", u32[0]); fmt::println(\"U+{:04X}\", u32[1]); u32[1] = U'\ud883\udede'; s = utf16::utf32to16(u32); fmt::println(\"{}\", s); // \u4f60\ud883\udede fmt::println(\"{}\", u32.size()); // 2 fmt::println(\"{}\", s.size()); // 3","title":"UTF-16 \u9635\u8425"},{"location":"unicode/#utf-32_1","text":"\u652f\u6301 Unicode\uff0c\u6bcf\u4e2a\u7801\u70b9\u90fd\u7528\u4e00\u4e2a uint32_t \u6216 char32_t \u8868\u793a\u3002 \u5e94\u7528\u573a\u666f\uff1a\u9002\u5408\u9700\u8981\u7ecf\u5e38\u5904\u7406\u6587\u5b57\u7684\u9886\u57df\uff0c\u5982\u6587\u672c\u7f16\u8f91\u5668\u3001\u6d4f\u89c8\u5668\u7b49\u3002\u4f46\u4e0d\u9002\u5408\u5b58\u50a8\u548c\u4f20\u8f93\uff0c\u56e0\u4e3a\u6d6a\u8d39\u786c\u76d8\u548c\u7f51\u7edc\u5e26\u5bbd\u3002\u5b57\u7b26\u4e32\u4e00\u822c\u90fd\u957f\u671f\u4ee5 UTF-8 \u5b58\u50a8\uff0c\u53ea\u6709\u5728\u9700\u8981\u9891\u7e41\u7d22\u5f15\u7801\u4f4d\u65f6\uff0c\u624d\u9700\u8981\u8f6c\u6362\u4e3a UTF-32\u3002 \u65b9\u6cd5\uff1a\u59cb\u7ec8\u4ee5 UTF-32 \u7f16\u7801\u5b58\u50a8\u548c\u5904\u7406\u5b57\u7b26\u4e32\u3002 \u4f18\u70b9\uff1a\u5b57\u7b26\u4e32\u7684\u6309\u7801\u4f4d\u53cd\u8f6c\u3001\u5207\u7247\u3001\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\u90fd\u662f O(1) O(1) \u7684\u590d\u6742\u5ea6\uff0c\u53ef\u4ee5\u5f53\u4f5c\u666e\u901a\u6570\u7ec4\u4e00\u6837\uff0c\u968f\u610f\u5904\u7406\u3002\u4f8b\u5982\u4f60\u53ef\u4ee5\u8bbe\u60f3\u4e00\u4e2a\u6587\u672c\u7f16\u8f91\u6846\uff0c\u9700\u8981\u652f\u6301\u201c\u9000\u683c\u201d\u64cd\u4f5c\uff0c\u5982\u679c\u662f UTF-8 \u548c UTF-16 \u5c31\u9700\u8981\u7e41\u7410\u7684\u5224\u65ad\u4ee3\u7406\u5bf9\u3001\u5404\u79cd\u8f66\u53a2\uff0c\u800c UTF-32 \u7684\u5b57\u7b26\u4e32\u53ea\u9700\u8981\u4e00\u6b21 pop_back \u5c31\u641e\u5b9a\u4e86\u3002 \u7f3a\u70b9\uff1a\u6d6a\u8d39\u7a7a\u95f4\u5927\uff0c\u901a\u5e38\u5728\u4fdd\u5b58\u65f6\uff0c\u4ecd\u7136\u9700\u8981\u8f6c\u6362\u56de UTF-8 \u540e\u518d\u5199\u5165\u6587\u4ef6\uff0c\u6709\u4e00\u5b9a\u6027\u80fd\u5f00\u9500\u3002 \u603b\u7ed3\uff1a\u8981\u652f\u6301 UTF-32 \u9635\u8425\uff0c\u8bf7\u5168\u90e8\u4f7f\u7528 char32_t \u548c std::u32string \u3002\u5b57\u9762\u91cf\u5168\u7528 U\"\u4f60\u597d\" \u7684\u5f62\u5f0f\u4e66\u5199\uff0c\u8bfb\u6587\u4ef6\u65f6\u8f6c\u4e3a UTF-32\uff0c\u5199\u6587\u4ef6\u65f6\u8f6c\u56de UTF-8\u3002","title":"UTF-32 \u9635\u8425"},{"location":"unicode/#_12","text":"\u7531\u4e8e C++26 \u524d\u6807\u51c6\u5e93\u5bf9\u7f16\u7801\u8f6c\u6362\u51e0\u4e4e\u6ca1\u6709\u652f\u6301\uff0c\u5728 C++ \u4e2d\u8f6c\u6362\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u5e38\u90fd\u9700\u8981\u7b2c\u4e09\u65b9\u5e93\u3002","title":"\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362"},{"location":"unicode/#utf-utfcpp","text":"\u5982\u679c\u4f60\u53ea\u662f\u9700\u8981\u4e0d\u540c UTF \u683c\u5f0f\u4e4b\u95f4\u7684\u8f6c\u6362\uff0c\u6ca1\u6709\u5904\u7406 GBK \u7b49\u7684\u9700\u6c42\uff1a\u90a3\u4e48\u4e4b\u524d\u5df2\u7ecf\u4ecb\u7ecd\u4e86 utfcpp \u8fd9\u4e2a\u65b9\u4fbf\u7684\u5e93\uff0c\u5df2\u7ecf\u591f\u7528\u3002 #include \"utf8/cpp20.h\" std::u8string s8 = u8\"\u4f60\u597d\"; std::u16string s16 = utf8::utf8to16(s8); std::u32string s32 = utf8::utf8to32(s8); std::string s = utf8::utf16to8(s16); s8 = utf8::utf16tou8(s16); \u6700\u540e\u8fd9\u4e24\u4e2a\u533a\u522b\u5728\u4e8e\uff0c utf16to8 \u8fd4\u56de std::string \uff0c utf16tou8 \u8fd4\u56de std::u8string \uff0c\u91cc\u9762\u90fd\u662f UTF-8 \u7f16\u7801\u7684\uff0c\u4e0d\u8fc7\u6709\u7684\u4eba\u559c\u6b22\u7528 std::string \u6765\u5b58\u50a8 UTF-8\uff0c\u4e0d\u559c\u6b22 std::u8string \uff0c\u6216\u8005\u8bf4\u4ed6\u4eec\u6ca1\u6709 C++20\uff0c\u4e0d\u652f\u6301 std::u8string \uff0c\u56e0\u6b64\u8fd9\u4e2a\u5e93\u6ee1\u8db3\u4ed6\u4eec\u7684\u4e0d\u540c\u9700\u8981\u3002\u4f46\u662f std::u8string \u4f5c\u4e3a\u53c2\u6570\u65f6\u4e0d\u9700\u8981\uff0c\u56e0\u4e3a\u53c2\u6570\u53ef\u4ee5\u81ea\u52a8\u91cd\u8f7d\uff0c\u800c\u8fd4\u56de\u503c\u4e0d\u884c\u3002 \u7f3a\u70b9\u662f\u4ed6\u4e0d\u80fd\u5904\u7406 GBK\u3001Shift-JIS \u7b49\u975e Unicode \u7f16\u7801\uff0c\u4e5f\u4e0d\u80fd\u81ea\u52a8\u68c0\u6d4b\u5f53\u524d\u7684 ANSI \u533a\u57df\u8bbe\u7f6e\u3002","title":"\u4e0d\u540c UTF \u4e4b\u95f4\u4e92\u8f6c\uff1autfcpp"},{"location":"unicode/#boostlocale","text":"\u5982\u679c\u4f60\u8fd8\u8981\u652f\u6301\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u6bd4\u5982 GBK\u3001Shift-JIS\u3001Latin-1\u3002 \u4e00\u79cd\u662f C \u8bed\u8a00\u7684 iconv \uff0c\u53e6\u4e00\u79cd\u662f\u73b0\u4ee3 C++ \u7684 boost::locale \u3002 \u867d\u7136\u529f\u80fd\u5dee\u4e0d\u591a\uff0c\u5e95\u5c42\u90fd\u662f\u8c03\u7528 icu \u7684\u3002 boost::locale \u7684 API \u66f4\u52a0\u53cb\u597d\uff0c\u800c\u4e14\u662f\u73b0\u4ee3 C++ \u98ce\u683c\u7684\u3002 # Ubuntu \u7528\u6237\u5b89\u88c5 Boost.locale \u65b9\u6cd5\uff1a $ sudo apt-get install libboost-locale-dev # Arch Linux \u7528\u6237\u5b89\u88c5 Boost \u5168\u5bb6\u6876\u65b9\u6cd5\uff1a $ sudo pacman -S boost \u4e0d\u559c\u6b22 Boost \u7684\u4eba\u6709\u96be\u4e86\u3002","title":"\u8de8\u5e73\u53f0\u7684\u4efb\u610f\u7f16\u7801\u8f6c\u6362\uff1aboost::locale"},{"location":"unicode/#utf","text":"\u4f7f\u7528 boost::locale::conv::utf_to_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::utf_to_utf; int main() { std::string s8 = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c UTF-32\uff1a std::u32string s32 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-16\uff1a std::u16string s16 = utf_to_utf(s8); // UTF-32 \u8f6c UTF-8\uff1a s8 = utf_to_utf(s32); std::cout << s8 << '\\n'; return 0; } \u6a21\u677f\u53c2\u6570\u4e2d\uff0c\u53ea\u9700\u6307\u5b9a\u8f6c\u6362\u5230\u7684\u662f\u4ec0\u4e48\u7c7b\u578b\u5c31\u884c\uff0c\u6765\u81ea\u4ec0\u4e48\u7c7b\u578b\uff0c\u4ed6\u81ea\u5df1\u4f1a\u91cd\u8f7d\u7684\u3002 \u6bd4\u5982\u4ece char32_t \u8f6c\u5230 char16_t \uff0c\u53ea\u9700\u8981 utf_to_utf \u5c31\u53ef\u4ee5\uff0c\u975e\u5e38\u65b9\u4fbf\u3002 \u7f16\u8bd1\uff1a $ g++ -std=c++17 -lboost_locale main.cpp \u8f93\u51fa\uff1a \u4f60\u597d \u5efa\u8bae\u7528\u540c\u6837\u8de8\u5e73\u53f0\u7684 CMake \u94fe\u63a5 Boost\uff0c\u5426\u5219 Windows \u7528\u6237\u8981\u6709\u96be\u4e86\u2026\u2026 find_package(Boost REQUIRED COMPONENTS locale) target_link_libraries(\u4f60\u7684\u7a0b\u5e8f Boost::locale) \u4e0d\u8fc7 boost::locale \u6709\u4e00\u4e2a\u7f3a\u70b9\uff0c\u90a3\u5c31\u662f\u4e0d\u652f\u6301 char8_t \u548c std::u8string \u3002 char8_t \u662f C++20 \u5f15\u5165\u7684\u65b0\u5b57\u7b26\u7c7b\u578b\uff0c\u7528\u4e8e\u5f3a\u7c7b\u578b\u7684\u541b\u5b50\u534f\u8bae\uff0c\u548c char \u5e76\u6ca1\u6709\u5b9e\u9645\u533a\u522b\u3002\u53ea\u662f\u65b9\u4fbf\u4e86\u51fd\u6570\u7c7b\u578b\u7b7e\u540d\u66f4\u52a0\u4e00\u76ee\u4e86\u7136\uff0c\u8fd9\u4e2a\u53c2\u6570\u53ea\u80fd\u63a5\u53d7 UTF-8 \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff01 \u7531\u4e8e Boost \u8f83\u8001\uff0c\u6ca1\u6709\u53ca\u65f6\u8ddf\u8fdb\uff0c\u6240\u4ee5\u4ed6\u5e76\u6ca1\u6709\u5b9e\u73b0\u9488\u5bf9 char8_t \u7684\u7279\u5316\uff0c\u5982\u679c\u4f7f\u7528\u4e86 utf_to_utf \u4f1a\u62a5 undefined reference \u9519\u8bef\uff0c\u5373\u627e\u4e0d\u5230\u7b26\u53f7\u3002\u6539\u6210 utf_to_utf \u5c31\u6ca1\u95ee\u9898\u4e86\u3002","title":"UTF \u4e4b\u95f4\u4e92\u8f6c"},{"location":"unicode/#gbk-utf","text":"\u4f7f\u7528 boost::locale::conv::to/from_utf \u5c31\u80fd\u8f7b\u6613\u505a\u5230\u3002 #include #include using boost::locale::conv::to_utf; using boost::locale::conv::from_utf; int main() { std::string s = \"\u4f60\u597d\"; // \u4ece GBK \u8f6c\u5230 UTF-16 std::wstring ws = to_utf(s, \"GBK\"); std::wcout << ws << '\\n'; // \u4ece UTF-16 \u8f6c\u56de GBK s = from_utf(ws, \"GBK\"); std::wcout << s << '\\n'; return 0; } \u7b2c\u4e8c\u4e2a\u53c2\u6570\u53ef\u4ee5\u662f GBK \u3001 Shift-JIS \u3001 Latin1 \u7b49\u5176\u4ed6\u7f16\u7801\u683c\u5f0f\uff0c\u5b8c\u6574\u7684\u5217\u8868\u53ef\u4ee5\u5728\u770b\u5230\u3002 \u8fd9\u91cc to_utf \u4f1a\u81ea\u52a8\u5224\u65ad wchar_t \u7684\u5927\u5c0f\u3002\u5982\u679c\u662f 2 \u5b57\u8282\uff08Windows \u5e73\u53f0\u60c5\u51b5\uff09\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-16\uff0c\u5982\u679c\u662f 4 \u5b57\u8282\uff08Linux \u5e73\u53f0\u60c5\u51b5\uff09\uff0c\u4f1a\u8ba4\u4e3a\u4f60\u8981\u8f6c\u4e3a UTF-32\u3002 \u800c to_char \u5219\u662f\u65e0\u8bba\u4ec0\u4e48\u5e73\u53f0\uff0c\u90fd\u4f1a\u8f6c\u4e3a UTF-16\u3002 from_utf \u4e0d\u9700\u8981\u6307\u5b9a\u4efb\u4f55\u6a21\u677f\u53c2\u6570\uff0c\u56e0\u4e3a\u4ed6\u603b\u662f\u8fd4\u56de std::string \uff08ANSI \u6216 GBK \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff09\uff0c\u53c2\u6570\u662f\u4ec0\u4e48\u7f16\u7801\uff0c\u4f1a\u81ea\u52a8\u901a\u8fc7\u91cd\u8f7d\u5224\u65ad\uff0c\u4f8b\u5982 from_utf(ws, \"GBK\") \u8fd9\u91cc\u7684\u53c2\u6570\u662f wchar_t \uff0c\u90a3\u4e48\u5728 Windows \u4e0a\uff0c\u4ed6\u4f1a\u68c0\u6d4b\u5230 wchar_t \u662f 2 \u5b57\u8282\uff0c\u5c31\u8ba4\u4e3a\u662f UTF-16 \u5230 GBK \u7684\u8f6c\u6362\u3002","title":"GBK \u548c UTF \u4e92\u8f6c"},{"location":"unicode/#utf-ansi","text":"\u6211\u4eec\u7a0b\u5e8f\u7684\u7528\u6237\u4e0d\u4e00\u5b9a\u662f\u4e2d\u56fd\u7528\u6237\uff08GBK\uff09\uff0c\u4e5f\u53ef\u80fd\u662f\u4fc4\u7f57\u65af\u7528\u6237\uff08CP1251\uff09\u3001\u65e5\u672c\u7528\u6237\uff08Shift-JIS\uff09\u3001\u897f\u73ed\u7259\u7528\u6237\uff08CP1252\uff09\u7b49\u3002 \u5982\u679c\u8981\u91c7\u7528\u7528\u6237\u7684\u533a\u57df\u8bbe\u7f6e\uff0c\u5373\u201cANSI\u201d\uff0c\u53ef\u4ee5\u628a\u5b57\u7b26\u4e32\u7559\u7a7a\uff08 \"\" \uff09\u3002 \u7a7a\u5b57\u7b26\u4e32\u5c31\u8868\u793a\u5f53\u524d\u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e86\uff0c\u5728\u4e2d\u56fd\u5927\u533a\u7b49\u4ef7\u4e8e \"GBK\" \uff0c\u4fc4\u7f57\u65af\u5927\u533a\u7b49\u4ef7\u4e8e \"CP1251\" \u7b49\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::to_utf; int main() { std::string u8s = u8\"\u4f60\u597d\"; // UTF-8 \u8f6c ANSI std::string s = from_utf(u8s, \"\"); // ANSI \u8f6c UTF-8 u8s = to_utf(s, \"\"); return 0; }","title":"UTF \u548c ANSI \u4e92\u8f6c"},{"location":"unicode/#_13","text":"\u51fd\u6570\u540d\u79f0 \u4ece \u5230 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-8 utf_to_utf UTF-x UTF-16 utf_to_utf UTF-x UTF-32 utf_to_utf UTF-x Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 UTF-x \u8868\u793a\u53d6\u51b3\u4e8e\u53c2\u6570\u7c7b\u578b\u7684\u5927\u5c0f\uff0c\u5982\u679c\u53c2\u6570\u662f char16_t \u7684\u5b57\u7b26\u4e32 std::u16string \uff0c\u90a3 x \u5c31\u662f 16\u3002 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-8 to_utf(\"GBK\", string) GBK UTF-16 to_utf(\"GBK\", string) GBK UTF-32 to_utf(\"GBK\", string) GBK Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-8 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-16 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e UTF-32 to_utf(\"\", string) \u533a\u57df\u8bbe\u7f6e Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u51fd\u6570\u540d\u79f0 \u4ece \u5230 from_utf(\"GBK\", string) UTF-8 GBK from_utf(\"GBK\", u16string) UTF-16 GBK from_utf(\"GBK\", u32string) UTF-32 GBK from_utf(\"GBK\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 GBK from_utf(\"\", string) UTF-8 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u16string) UTF-16 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", u32string) UTF-32 \u533a\u57df\u8bbe\u7f6e from_utf(\"\", wstring) Linux \u4e0a UTF-32\uff1bWin \u4e0a UTF-16 \u533a\u57df\u8bbe\u7f6e","title":"\u5927\u603b\u7ed3"},{"location":"unicode/#gbk-shift-jis","text":"#include #include using boost::locale::conv::between; using boost::locale::conv::from_utf; int main() { // \u521b\u5efa\u4e00\u4e2a Shift-JIS \u5b57\u7b26\u4e32 std::string jis = from_utf(u8\"\u65e5\u672c\u8a9e\", \"Shift-JIS\"); // \u4ece Shift-JIS \u8f6c\u5230 GBK std::string gbk = between(jis, \"GBK\", \"Shift-JIS\"); std::cout << gbk << '\\n'; // \u4ece GBK \u8f6c\u56de Shift-JIS jis = between(gbk, \"Shift-JIS\", \"GBK\"); std::cout << jis << '\\n'; return 0; } \u6ce8\u610f\uff01\u662f\u76ee\u6807\u7f16\u7801\u5728\u524d\uff01\u5982\u679c\u4f60\u8981\u4ece Shift-JIS \u8f6c\u6210 GBK\uff0c\u90a3\u4e48\u9700\u8981 between(jis, \"GBK\", \"Shift-JIS\") \uff0c\u8fd9\u771f\u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u8bbe\u8ba1\u3002\u4e0d\u4ec5 GBK \u548c Shift-JIS \u53ef\u80fd\u4e0d\u5c0f\u5fc3\u5f04\u53cd\u4e86\uff0c\u7f16\u8bd1\u5668\uff0c\u4e00\u70b9\u63d0\u793a\u90fd\u6ca1\u6709\uff0c\u800c\u4e14 jis \u548c \u201cGBK\u201d \u90fd\u662f\u5b57\u7b26\u4e32\uff0c\u5f88\u5bb9\u6613\u5927\u8111\u641e\u6df7\u3002\u8ba9\u6211\u6765\u8bbe\u8ba1\u7684\u8bdd\uff0c\u6211\u4f1a\u8fd9\u6837\u63d0\u4f9b API\uff1a decode(jis, Encoding::ShiftJIS).encode(Encoding::GBK) \uff0c\u5176\u4e2d Encoding \u662f\u4e00\u4e2a\u679a\u4e3e\uff0c\u5f3a\u5927\u7684\u7c7b\u578b\u7cfb\u7edf\uff0c\u4e0d\u4ec5\u907f\u514d\u72af\u9519\u7684\u673a\u4f1a\uff0c\u770b\u8d77\u6765\u4e5f\u66f4\u8f7b\u677e\u3002\u4e4b\u540e\u7684\u8bbe\u8ba1\u6a21\u5f0f\u4e13\u9898\u8bfe\u4e2d\uff0c\u4f1a\u518d\u8be6\u7ec6\u8bb2\u89e3\u4ec0\u4e48\u662f\u597d\u7684 API \u8bbe\u8ba1\u3002","title":"GBK \u548c Shift-JIS \u4e92\u8f6c"},{"location":"unicode/#_14","text":"\u5982\u679c\u9047\u5230\u65e0\u6cd5\u7f16\u7801\u7684\u5b57\u7b26\uff0c\u8be5\u5982\u4f55\u5904\u7f6e\uff1f \u9ed8\u8ba4\u60c5\u51b5\u4e0b Boost \u4f1a\u5ffd\u89c6\u9519\u8bef\uff0c\u7f16\u7801\u5931\u8d25\u7684\u5b57\u7b26\u4f1a\u88ab\u4e22\u5f03\u3002 #include #include using boost::locale::conv::from_utf; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\"); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 std::cout << gbk << '\\n'; // \u5728 Windows \u7684 GBK \u7ec8\u7aef\u4e0a\uff0c\u53ea\u663e\u793a\u201c\u6211\u7231\u9762\u201d return 0; } \u53ef\u4ee5\u7528 method_type \u8fd9\u4e2a\u679a\u4e3e\u6765\u6307\u5b9a\u9519\u8bef\u5904\u7406\u7684\u65b9\u5f0f\u3002 \u9ed8\u8ba4\u662f skip \uff0c\u8df3\u8fc7\u6240\u6709\u89e3\u7801\u51fa\u9519\u7684\u5730\u65b9\uff08\u5bfc\u81f4\u201c\ud883\udede\u201d\u4e22\u5931\uff09\u3002 \u6211\u4eec\u53ef\u4ee5\u5207\u6362\u5230 stop \uff0c\u5f53\u9047\u5230\u89e3\u7801\u9519\u8bef\u65f6\uff0c\u4f1a\u76f4\u63a5\u629b\u51fa\u5f02\u5e38\uff0c\u7ec8\u6b62\u7a0b\u5e8f\u6267\u884c\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; int main() { std::string utf8 = u8\"\u6211\u7231\ud883\udede\ud883\udede\u9762\"; // UTF-8 \u8f6c GBK std::string gbk = from_utf(utf8, \"GBK\", method_type::stop); // \u9519\u8bef\uff0c\u201c\ud883\udede\u201d\u65e0\u6cd5\u7528 GBK \u8868\u793a\uff01 // from_utf \u4f1a\u629b\u51fa `conversion_error` \u5f02\u5e38 std::cout << gbk << '\\n'; return 0; } \u4e3e\u4f8b\uff1a\u5c1d\u8bd5\u4ee5 GBK \u4fdd\u5b58\uff0c\u5982\u679c\u5931\u8d25\uff0c\u5219\u6539\u4e3a\u5e26\u6709 BOM \u7684 UTF-8\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::method_type; using boost::locale::conv::conversion_error; void try_save(std::u32string content, std::wstring path) { std::string binary; try { // \u5c1d\u8bd5\u5c06 UTF-32 \u8f6c\u6210 GBK \u7f16\u7801 binary = from_utf(content, \"GBK\", method_type::stop); } catch (conversion_error const &e) { // \u82e5 GBK \u65e0\u6cd5\u8868\u793a // \u6539\u7528\u524d\u9762\u5e26\u6709 BOM \u7684 UTF-8 \u7f16\u7801 binary = \"\\xEF\\xBB\\xBF\" + utf_to_utf(content); } std::ofstream(path) << binary; } \u4e3e\u4f8b\uff1a\u652f\u6301 UTF-8 \u5b57\u7b26\u4e32\uff08\u800c\u4e0d\u662f ANSI \u5b57\u7b26\u4e32\uff09\u7684\u6253\u5370\u51fd\u6570\u3002 #include #include using boost::locale::conv::from_utf; using boost::locale::conv::utf_to_utf; void u8print(std::string msg) { std::cout << from_utf(msg, \"\"); // \u6216\u8005\uff1a // std::wcout << utf_to_utf(msg, \"\"); } \u66f4\u591a\u7ec6\u8282\u8be6\u89c1\u5b98\u65b9\u6587\u6863\uff1ahttps://www.boost.org/doc/libs/1_81_0/libs/locale/doc/html/group__codepage.html","title":"\u6307\u5b9a\u5904\u7406\u9519\u8bef\u7684\u65b9\u6cd5"},{"location":"unicode/#_15","text":"\u7f16\u7801\u8f6c\u6362\u53ea\u662f boost::locale::conv \u8fd9\u4e2a\u5b50\u6a21\u5757\u4e0b\u7684\u4e00\u4e2a\u5c0f\u529f\u80fd\u800c\u5df2\uff01 boost::locale \u8fd8\u63d0\u4f9b\u4e86\u66f4\u591a\u529f\u80fd\uff0c\u5982\u6309\u7167\u5730\u57df\u8bed\u8a00\u89c4\u8303\u683c\u5f0f\u5316\u6570\u5b57\u3001\u8d27\u5e01\u3001\u65e5\u671f\u3001\u65f6\u95f4\u7b49\uff0c\u4e0b\u4e00\u5c0f\u8282\u4e2d\u6211\u4eec\u7ee7\u7eed\u4ecb\u7ecd\u3002\u5b8c\u5168\u662f std::locale \u7684\u4e0a\u4f4d\u66ff\u4ee3\u3002 Boost \u54ea\u91cc\u90fd\u597d\uff0c\u4f60\u60f3\u8981\u7684\u529f\u80fd\u5e94\u6709\u5c3d\u6709\u3002\u800c\u4e14\u4e0d\u9700\u8981 C++20\uff0c\u5f88\u4f4e\u7248\u672c\u7684 C++ \u4e5f\u80fd\u7528\u3002\u552f\u4e00\u7f3a\u70b9\u53ef\u80fd\u5c31\u662f\u592a\u80a5\u4e86\uff0c\u7f16\u8bd1\u6162\u3002","title":"\u66f4\u591a\u529f\u80fd\uff1f\uff01"},{"location":"unicode/#windows-multibytetowidechar","text":"\u5982\u679c\u4f60\u662f Windows \u7a0b\u5e8f\u5458\uff0c\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u4e14\u9700\u8981\u5728 Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u89c4\u5b9a\u7684 ANSI\uff08\u5728\u4e2d\u56fd\u533a\u662f GBK\uff09\u7f16\u7801\u548c UTF-16 \u4e4b\u95f4\u8f6c\u6362\uff1a \u53ef\u4ee5\u7528 Windows \u5b98\u65b9\u63d0\u4f9b\u7684 MultiByteToWideChar \u548c WideCharToMultiByte \u51fd\u6570\u3002 \u8fd9\u4e24\u4e2a\u51fd\u6570\u56e0\u4e3a C \u8bed\u8a00\u7279\u8272\u7684\u7f18\u6545\uff0c\u53c2\u6570\u6bd4\u8f83\u591a\u800c\u6742\uff0c\u5efa\u8bae\u81ea\u5df1\u52a8\u624b\u5c01\u88c5\u6210\u66f4\u6613\u7528\u7684 C++ \u51fd\u6570\uff1a std::wstring ansi_to_wstring(const std::string &s) { // ACP = ANSI Code Page\uff0c\u544a\u8bc9\u4ed6\u5b57\u7b26\u4e32\u91cc\u7684\u662f\u5f53\u524d\u533a\u57df\u8bbe\u7f6e\u6307\u5b9a\u7684\u7f16\u7801\uff08\u5728\u4e2d\u56fd\u533a\uff0cANSI \u5c31\u662f GBK \u4e86\uff09 int len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_ACP, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_ansi(const std::wstring &ws) { int len = WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_ACP, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } std::wstring utf8_to_wstring(const std::string &s) { int len = MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), nullptr, 0); std::wstring ws(len, 0); MultiByteToWideChar(CP_UTF8, 0, s.c_str(), s.size(), ws.data(), ws.size()); return ws; } std::string wstring_to_utf8(const std::wstring &ws) { int len = WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), nullptr, 0, nullptr, nullptr); std::string s(len, 0); WideCharToMultiByte(CP_UTF8, 0, ws.c_str(), ws.size(), s.data(), s.size(), nullptr, nullptr); return s; } C \u8bed\u8a00\u7279\u8272\uff1a\u6240\u6709\u8981\u8fd4\u56de\u5b57\u7b26\u4e32\u7684\u51fd\u6570\uff0c\u90fd\u9700\u8981\u8c03\u7528\u4e24\u904d\uff0c\u7b2c\u4e00\u6ce2\u5148\u6c42\u51fa\u957f\u5ea6\uff0c\u7b2c\u4e8c\u6ce2\u624d\u5199\u5165\u3002\u8fd9\u662f\u4e3a\u4e86\u907f\u514d\u4e0e\u5185\u5b58\u5206\u914d\u5668\u8026\u5408\uff0c\u6240\u6709\u7684 C \u98ce\u683c API \u90fd\u662f\u8fd9\u6837\u3002","title":"Windows \u7528\u6237\uff1aMultiByteToWideChar"},{"location":"unicode/#messageboxa","text":"\u590d\u73b0\u6761\u4ef6\uff1a Windows \u7cfb\u7edf\u533a\u57df\u8bbe\u7f6e\u4e3a\u4e2d\u6587 (GBK)\u3002 \u4f7f\u7528 MSVC \u7684 /utf-8 \u9009\u9879\u7f16\u8bd1\u3002 #include int main() { MessageBoxA(nullptr, \"\u6211\u7231\ud883\udede\ud883\udede\u9762\", \"\u6807\u9898\", MB_OK); // \u4f1a\u53d8\u6210\u4e71\u7801 return 0; }","title":"MessageBoxA \u51fa\u73b0\u4e71\u7801\u95ee\u9898\u89e3\u51b3\u6848\u4f8b"},{"location":"unicode/#linux-iconv","text":"\u5982\u679c\u4f60\u662f Linux \u7528\u6237\uff0c\u4e14\u6ca1\u6709\u8de8\u5e73\u53f0\u9700\u6c42\uff0c\u4e0d\u60f3\u7528 Boost\uff0c\u53ef\u4ee5\u4f7f\u7528 C \u8bed\u8a00\u7684 iconv \u5e93\u3002 iconv \u4e5f\u6709 Windows \u7684\u7248\u672c\uff0c\u4f46\u5b89\u88c5\u6bd4\u8f83\u56f0\u96be\u3002\u5982\u679c\u4f60\u8fde iconv \u90fd\u641e\u5f97\u5b9a\uff0c\u6ca1\u7406\u7531 Boost \u641e\u4e0d\u5b9a\u3002 #include #include std::string convert(std::string const &s, char const *from, char const *to) { iconv_t cd = iconv_open(to, from); if (cd == (iconv_t)-1) { throw std::runtime_error(\"iconv_open failed\"); } auto in = s.data(); auto inbytesleft = s.size(); size_t outbytesleft = inbytesleft * 4; std::string buffer(outbytesleft, 0); auto out = buffer.data(); iconv(cd, &in, &inbytesleft, &out, &outbytesleft); iconv_close(cd); buffer.resize(buffer.size() - outbytesleft); return buffer; } // \u4e3e\u4f8b\uff1aUTF-8 \u8f6c GBK std::string utf8_to_gbk(std::string const &s) { return convert(s, \"UTF-8\", \"GBK\"); } // \u4e3e\u4f8b\uff1aGBK \u8f6c UTF-8 std::string gbk_to_utf8(std::string const &s) { return convert(s, \"GBK\", \"UTF-8\"); }","title":"Linux \u7528\u6237\uff1aiconv"},{"location":"unicode/#iconv","text":"iconv \u4e0d\u4ec5\u662f\u4e00\u4e2a\u5e93\uff0c\u4e5f\u662f\u4e00\u4e2a\u547d\u4ee4\u884c\u5de5\u5177\uff08\u5927\u591a Linux \u53d1\u884c\u7248\u90fd\u81ea\u5e26\u4e86\uff09\u3002\u7528\u6cd5\u5982\u4e0b\uff1a iconv -f \u6765\u81ea\u4ec0\u4e48\u7f16\u7801 -t \u5230\u4ec0\u4e48\u7f16\u7801 (\u8f93\u5165\u6587\u4ef6\u540d...) > \u8f93\u51fa\u6587\u4ef6\u540d \u5982\u4e0d\u6307\u5b9a\u8f93\u5165\u6587\u4ef6\u540d\uff0c\u9ed8\u8ba4\u4ece\u7ec8\u7aef\u8f93\u5165\u6d41\u8bfb\u53d6\u3002 \u5982\u4e0d\u4f7f\u7528 > \u8f93\u51fa\u6587\u4ef6\u540d \u91cd\u5b9a\u5411\u8f93\u51fa\uff0c\u5219\u9ed8\u8ba4\u8f93\u51fa\u5230\u7ec8\u7aef\u3002 \u53ef\u4ee5\u7528 echo \u914d\u5408\u7ba1\u9053\u6765\u521b\u5efa\u8f93\u5165\u6d41\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 \u6b64\u5904\u663e\u793a\u4e71\u7801\u662f\u56e0\u4e3a\u6211\u7684\u7ec8\u7aef\u662f UTF-8 \u683c\u5f0f\uff0c\u65e0\u6cd5\u6b63\u786e\u89e3\u6790 iconv \u8f93\u51fa\u7684 GBK \u683c\u5f0f\u6570\u636e\u3002 \u628a\u201c\u6211\u7231\u5c0f\u5f6d\u8001\u5e08\u201d\u8f6c\u6362\u4e3a GBK \u683c\u5f0f\u5199\u5165 gbk.txt \uff0c\u7136\u540e\u518d\u91cd\u65b0\u8fd8\u539f\u56de UTF-8 \u683c\u5f0f\u67e5\u770b\uff1a $ echo \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 | iconv -f UTF-8 -t GBK > gbk.txt $ cat gbk.txt \ufffd\u04b0\ufffd\u0421\ufffd\ufffd\ufffd\ufffd\u02a6 $ iconv -f GBK -t UTF-8 gbk.txt \u6211\u7231\u5c0f\u5f6d\u8001\u5e08 Windows \u53ef\u80fd\u4e5f\u6709\u7c7b\u4f3c\u7684\u5de5\u5177\uff0c\u6bd4\u5982 iconv.exe \uff0c\u4f46\u6211\u6ca1\u627e\u5230\u3002","title":"iconv \u547d\u4ee4\u884c\u5de5\u5177"},{"location":"unicode/#locale","text":"\u672c\u5730\u5316\u662f\u6307\u6839\u636e\u7528\u6237\u7684\u8bed\u8a00\u3001\u5730\u533a\u7b49\u73af\u5883\uff0c\u663e\u793a\u4e0d\u540c\u7684\u754c\u9762\u3002\u6bd4\u5982\u8bf4\uff0c\u540c\u6837\u662f\u6587\u4ef6\u83dc\u5355\uff0c\u4e2d\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201c\u6587\u4ef6\u201d\u3001\u82f1\u6587\u7528\u6237\u770b\u5230\u7684\u662f\u201cFile\u201d\u3002","title":"\u672c\u5730\u5316 (locale)"},{"location":"unicode/#_16","text":"C \u8bed\u8a00\u63d0\u4f9b\u4e86 \u5934\u6587\u4ef6\uff0c\u91cc\u9762\u5c01\u88c5\u4e86\u5927\u91cf\u5f62\u5982 isspace \u3001 isdigit \u8fd9\u6837\u7684\u5224\u65ad\u5b57\u7b26\u5206\u7c7b\u7684\u51fd\u6570\u3002 #include C++ \u5bf9\u5176\u5b9e\u65bd\u4e86\u518d\u5c01\u88c5\uff0c\u6539\u540d\u4e3a \u3002\u82e5\u4f60\u5bfc\u5165\u7684\u662f\u8be5\u5934\u6587\u4ef6\uff0c\u90a3\u4e48\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e26\u6709 std \u540d\u5b57\u7a7a\u95f4\u524d\u7f00\u7684\u65b9\u5f0f std::isspace \uff0c std::isdigit \u8bbf\u95ee\u4e86\uff0c\u770b\u8d77\u6765\u66f4\u52a0\u4e13\u4e1a\uff08\u786e\u4fe1\uff09\u3002 #include \u51fd\u6570\u6e05\u5355\uff1a \u51fd\u6570\u540d\u79f0 \u5224\u65ad\u7684\u5b57\u7b26\u7c7b\u578b isascii 0 \u5230 0x7F \u7684\u6240\u6709 ASCII \u5b57\u7b26 isalpha \u5927\u5c0f\u5199\u5b57\u6bcd A-Z a-z isupper \u5927\u5199\u5b57\u6bcd A-Z islower \u5c0f\u5199\u5b57\u6bcd a-z isdigit \u6570\u5b57 0-9 isxdigit \u5341\u516d\u8fdb\u5236\u6570\u5b57 A-F a-f 0-9 isprint \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u5305\u62ec\u5b57\u6bcd\u3001\u6570\u5b57\u548c\u6807\u70b9\u7b49 isgraph \u53ef\u6253\u5370\u5b57\u7b26\uff0c\u4e0d\u5305\u62ec\u7a7a\u683c iscntrl \u63a7\u5236\u5b57\u7b26\uff0c\u9664\u53ef\u6253\u5370\u5b57\u7b26\u5916\u7684\u5168\u90e8 isspace \u7a7a\u767d\u5b57\u7b26\uff0c\u5982\u7a7a\u683c\u3001\u6362\u884c\u3001\u56de\u8f66\u3001\u5236\u8868\u7b26\u7b49 ispunct \u6807\u70b9\u7b26\u53f7 isalnum \u5b57\u6bcd\u6216\u6570\u5b57 \u66f4\u8be6\u7ec6\u7684\u8868\u683c\u53ef\u4ee5\u770b\uff1a https://en.cppreference.com/w/cpp/string/byte/isspace","title":"\u533a\u5206\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#_17","text":"TODO","title":"\u533a\u5206\u5bbd\u5b57\u7b26\u7c7b\u578b"},{"location":"unicode/#stdlocale","text":"TODO","title":"\u533a\u57df\u8bbe\u7f6e\u4e0e std::locale"},{"location":"unicode/#_18","text":"TODO","title":"\u65f6\u95f4\u65e5\u671f\u683c\u5f0f\u5316"},{"location":"unicode/#_19","text":"\u72ed\u4e49\u7684\u6c49\u5b57\uff1a0x4E00 \u5230 0x9FA5\uff08\u201c\u4e00\u201d\u5230\u201c\u9fa5\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\uff1a0x2E80 \u5230 0x9FFF\uff08\u201c\u2e80\u201d\u5230\u201c\u9fff\u201d\uff09 \u5e7f\u4e49\u7684\u6c49\u5b57\u5305\u542b\u4e86\u51e0\u4e4e\u6240\u6709\u4e2d\u65e5\u97e9\u4f7f\u7528\u7684\u6c49\u5b57\u5b57\u7b26\uff0c\u800c\u72ed\u4e49\u7684\u6c49\u5b57\u53ea\u662f\u4e2d\u6587\u91cc\u6700\u5e38\u7528\u7684\u4e00\u90e8\u5206\u3002 TODO","title":"\u6b63\u5219\u8868\u8fbe\u5f0f\u5339\u914d\u6c49\u5b57\uff1f"},{"location":"unicode/#_20","text":"\u4e4b\u6240\u4ee5\u628a\u5bbd\u5b57\u7b26\u6d41\u653e\u5230\u6700\u540e\uff0c\u662f\u56e0\u4e3a\uff0c\u9996\u5148 iostream \u672c\u6765\u5c31\u662f\u4e00\u4e2a\u5931\u8d25\u7684\u8bbe\u8ba1\u3002 \u5c0f\u5f6d\u8001\u5e08\u5728\u672c\u4e66\u5f00\u5934\u5c31\u591a\u6b21\u5f3a\u8c03\u8fc7\u4ed6\u662f format \u5b5d\u5b50\u3002 \u800c\u5bbd\u5b57\u7b26 wchar_t \u672c\u8eab\u5c31\u5145\u65a5\u7740\u5386\u53f2\u9057\u7559\u7cdf\u7c95\uff08\u4f8b\u5982 Windows \u88ab UTF-16 \u80cc\u523a\uff09\u3002 \u73b0\u5728 iostream \u4e0e wchar_t \u4e00\u8d77\u51fa\u73b0\u5728\u6211\u9762\u524d\uff0c\u4e0d\u80fd\u8bf4\u662f\u68a6\u5e7b\u8054\u52a8\u5427\uff0c\u81f3\u5c11\u4e5f\u53ef\u4ee5\u8bf4\u662f\u7b54\u8fa9\u8d85\u4eba\u4e86\u3002 \u603b\u4e4b\uff0c\u6211\u4e2a\u4eba\u8fd8\u662f\u63a8\u8350\u7a0b\u5e8f\u5185\u90e8\u4ee5 UTF-8\uff08 char8_t \uff09\u6216 UTF-32\uff08 char32_t \uff09\u7684\u5b57\u7b26\u4e32\u6765\u5904\u7406\u4e07\u7269\u3002 UTF-8 \u6216 UTF-32 \u7684\u9009\u62e9\u53d6\u51b3\u4e8e\u4f60\u7684\u4e2d\u6587\u5904\u7406\u9700\u6c42\u662f\u5426\u65fa\u76db\uff0c\u662f\u5426\u5728\u4e4e\u7a7a\u95f4\uff0c\u662f\u5426\u9700\u8981\u5207\u7247\u548c\u7d22\u5f15\u7b49\u3002 \u5f53\u9700\u8981\u8c03\u7528\u64cd\u4f5c\u7cfb\u7edf API \u8bfb\u5199\u6587\u4ef6\u65f6\uff0c\u518d\u7528 boost::locale \u3001 utfcpp \u7b49\u5de5\u5177\u8f6c\u6362\u6210 ANSI\uff08 char \uff09\u6216 UTF-16\uff08 wchar_t \uff09\u3002 \u5bf9\u4e8e Linux \u7528\u6237\uff0c\u4e5f\u53ef\u4ee5\u68c0\u6d4b\u5982\u679c\u662f Linux \u7cfb\u7edf\uff0c\u5219\u4ec0\u4e48\u8f6c\u6362\u90fd\u4e0d\u505a\uff0c\u56e0\u4e3a Linux \u7528\u6237\u51e0\u4e4e\u90fd\u662f UTF-8\uff0c\u90a3\u4e48 const char8_t * \u53ef\u4ee5\u5f3a\u8f6c\u4e3a const char * \u800c\u4e0d\u7528\u4efb\u4f55\u989d\u5916\u5f00\u9500\u3002 std::string to_os_string(std::string const &u8s) { #if _WIN32 // UTF-8 \u5230 ANSI return boost::locale::conv::from_utf(u8s, \"\"); #elif __linux__ // \u4e0d\u8f6c\u6362 return u8s; #else #error \"Unsupported system.\" #endif } \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u5b9e\u5728\u8981\u5b66\u7cdf\u7cd5\u7684\u5bbd\u5b57\u7b26\u6d41\uff0c\u90a3\u6211\u4e5f\u5949\u966a\u5230\u5e95\u3002","title":"\u5bbd\u5b57\u7b26\u6d41"},{"location":"unicode/#stdwstring","text":"\u5728\u4ed6\u4eec\u770b\u6765\uff0c std::string \u662f\u5df2\u7ecf\u5e9f\u5f03\u7684\u3002\u4ed6\u4eec\u8ba4\u4e3a std::wstring \u624d\u662f\u771f\u6b63\u8de8\u5e73\u53f0\uff0c\u8de8\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u3002 std::wstring : \u5b57\u7b26\u4e32 std::string : \u5b57\u8282\u6570\u7ec4 std::wifstream : \u6587\u672c\u6d41 std::ifstream : \u4e8c\u8fdb\u5236\u6d41 \u770b\u8d77\u6765\u53ea\u8981\u5168\u90e8\u7edf\u4e00 wchar_t \u5c31\u80fd\u5b9e\u73b0\u8de8\u5e73\u53f0\u4e86\uff1f\u662f\u7684\uff0c\u9664\u4e86 Windows\u2026\u2026 \u6807\u51c6\u8ba4\u4e3a wchar_t \u5e94\u8be5\u5305\u542b 0 \u5230 0x10FFFF \u7684\u6240\u6709\u7684 Unicode \u5b57\u7b26\u7801\u70b9\uff0c\u9700\u8981\u662f 32 \u4f4d\u7684\u3002\u7136\u800c Windows \u7684 wchar_t \u7531\u4e8e\u5386\u53f2\u539f\u56e0\uff0c\u662f 16 \u4f4d\u7684\uff0c\u9700\u8981\u7528\u4ee3\u7406\u5bf9\u624d\u80fd\u8868\u793a\u7a00\u6709\u5b57\u7b26\uff0c\u5e76\u4e0d\u80fd\u4e00\u4e2a wchar_t \u5bf9\u5e94\u4e00\u4e2a\u7801\u70b9\u3002\u8fd9\u5bfc\u81f4\u5373\u4f7f\u7528\u4e86 wchar_t \u8fd8\u662f\u5b58\u5728\u8de8\u5e73\u53f0\u56f0\u96be\u7684\u95ee\u9898\uff1a\u4e00\u4e2a Linux \u7a0b\u5e8f\u7528 wchar_t \u53ef\u80fd\u4f1a\u5229\u7528 UTF-32 \u5b9a\u957f\u7f16\u7801\u7684\u7279\u6027\uff0c\u65b9\u4fbf\u4e86\u6587\u672c\u5904\u7406\uff0c\u4f46\u79fb\u690d\u5230 Windows \u65f6\uff0c\u53d1\u73b0\u53d8\u6210\u4e86 UTF-16\uff0c\u9700\u8981\u5bf9\u4ee3\u7406\u5bf9\u505a\u7279\u6b8a\u5224\u65ad\u2026\u2026\u6ca1\u6709\u6ee1\u8db3\u8de8\u5e73\u53f0\u7684\u521d\u8877\uff0c\u4e5f\u505a\u4e0d\u5230\u5b9a\u957f\u7f16\u7801\u3002 char32_t \u505a\u5230\u4e86\u8de8\u5e73\u53f0\u7684 UTF-32\uff0c\u4e5f\u80fd\u5bb9\u7eb3\u5168\u90e8 Unicode \u7801\u70b9\uff0c\u53ef\u6807\u51c6\u5e93\u63d0\u4f9b\u4e86 std::to_wstring \uff0c\u5374\u6839\u672c\u6ca1\u6709 std::to_u32string \uff1b\u63d0\u4f9b\u4e86 std::wcout \uff0c\u5374\u6ca1\u6709\u63d0\u4f9b std::u32cout \u2026\u2026 \u8fd9\u5c31\u662f\u4e3a\u4ec0\u4e48\u5bbd\u5b57\u7b26\u6d41\u5f88\u7cdf\u7cd5\uff0c\u8bf4\u662f\u8de8\u5e73\u53f0\uff0c\u8de8\u4e86\u4e2a\u5bc2\u5bde\u3002","title":"\u5b98\u65b9\u773c\u4e2d\u7684 std::wstring"},{"location":"unicode/#wchar_t","text":"TODO","title":"wchar_t \u7cfb\u5217\u51fd\u6570"},{"location":"unicode/#stdwcout","text":"TODO","title":"std::wcout \u7684\u4f7f\u7528"},{"location":"unicode/#stdwfstream","text":"TODO","title":"std::wfstream \u7684\u4f7f\u7528"},{"location":"unicode/#c","text":"TODO","title":"C \u8bed\u8a00\u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362"},{"location":"unicode/#c-codecvt","text":"TODO","title":"C++ \u5b57\u7b26\u4e32\u7f16\u7801\u8f6c\u6362 <codecvt>"},{"location":"unicode/#windows","text":"\u9047\u5230\u5b57\u7b26\u7f16\u7801\u96be\u9898\u7684\uff0c\u4e3b\u8981\u662f Windows \u7a0b\u5e8f\u5458\u3002","title":"Windows \u4e13\u9898"},{"location":"unicode/#windows-api-w","text":"\u4ece Windows NT \u7248\u672c\u5f00\u59cb\uff0c\u5bf9\u4e8e\u6240\u6709\u6d89\u53ca\u5b57\u7b26\u4e32\u7684\uff0c\u5176\u64cd\u4f5c\u7cfb\u7edf API \u63d0\u4f9b\u4e86\u4e24\u5957\u51fd\u6570\u3002 \u4e00\u5957\u662f A \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 A \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileA \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 ANSI\uff08\u5373 GBK\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u53e6\u4e00\u5957\u662f W \u7cfb\u5217\u51fd\u6570\uff0c\u4ee5 W \u7ed3\u5c3e\uff0c\u4f8b\u5982 CreateFileW \uff0c\u8fd9\u4e9b\u51fd\u6570\u63a5\u6536 Unicode\uff08\u5373 UTF-16\uff09\u7f16\u7801\u7684\u5b57\u7b26\u4e32\u3002 \u5176\u4e2d CreateFileW \u624d\u662f Windows \u7cfb\u7edf\u771f\u6b63\u7684 API\u3002 \u800c CreateFileA \u662f\u4e3a\u4e86\u517c\u5bb9\u57fa\u4e8e ANSI \u7684\u8001\u7a0b\u5e8f. \u7531\u4e8e ANSI \u5728\u4e0d\u540c\u5730\u533a\u4f1a\u53d8\u5f97\u4e0d\u540c\uff0c\u4f7f\u7528\u8fd9\u7c7b\u51fd\u6570\u5199\u51fa\u7684\u7a0b\u5e8f\u4e0d\u5177\u6709\u56fd\u9645\u901a\u7528\u6027\u3002 \u5176\u5185\u90e8\u7684\u5b9e\u73b0\u53ea\u662f\u7b80\u7b80\u5355\u5355\u5730\u7ed9 const char * \u505a\u4e2a\u8f6c\u6362\uff0c\u4ece GBK \u8f6c\u5230 UTF-16\uff0c\u7136\u540e\u76f4\u63a5\u8c03\u7528 CreateFileW\u3002 HANDLE CreateFileA(const char *lpFileName) { return CreateFileW(gbk_to_utf16(lpFileName)); }","title":"Windows API \u7684\u672c\u6e90\u662f W \u7cfb\u51fd\u6570"},{"location":"unicode/#tchar","text":"\u9664\u4e86\u53c8\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5b8f\uff0c\u8fd9\u4e2a\u5b8f\u6ca1\u6709\u4efb\u4f55\u540e\u7f00\uff0c\u4f8b\u5982 CreateFile \u3002 \u5176\u5b9a\u4e49\u5982\u4e0b\uff1a #ifdef UNICODE #define MessageBox MessageBoxW #else #define MessageBox MessageBoxA #endif \u8fd9\u6837\u505a\u7684\u521d\u8877\u662f\uff0c\u7a0b\u5e8f\u5458\u53ea\u53ef\u4ee5\u5199\u51fa\u4e00\u5957\u9488\u5bf9 MessageBox \u7684\u4ee3\u7801\u3002 \u5f53\u8001\u677f\u60f3\u8981\u57fa\u4e8e Unicode \u65f6\uff0c\u4ed6\u5c31 #define UNICODE \uff0c\u8fd9\u6837 MessageBox \u5c31\u53d8\u6210\u4e86 MessageBoxW \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u5c31\u4f1a\u81ea\u52a8\u53d8\u6210 Unicode \u7684\uff0c\u56fd\u9645\u901a\u7528\u3002 \u5f53\u52b3\u4fdd\u8001\u677f\u60f3\u8981\u57fa\u4e8e ANSI \u65f6\uff0c\u4ed6\u5c31\u4e0d\u5b9a\u4e49 UNICODE \u5b8f\uff0c\u8fd9\u6837\u6240\u6709\u7684 MessageBox \u53c8\u53d8\u56de\u4e86 MessageBoxA \uff0c\u7a0b\u5e8f\u5458\u7684\u4ee3\u7801\u53c8\u53d8\u6210 ANSI \u7684\u4e86\u3002 \u6240\u6709\u6709 A/W \u533a\u5206\u7684\u7684 Windows API \u90fd\u6709\u8fd9\u6837\u4e00\u4e2a\u5b8f\uff0c\u6839\u636e UNICODE \u5b8f\u662f\u5426\u5b9a\u4e49\uff0c\u51b3\u5b9a\u91c7\u53d6\u54ea\u5957 API\u3002 \u6211\u4eec\u4f1f\u5927\u7684 Linux \u7cfb\u7edf\u5c31\u6ca1\u6709\u8fd9\u4e2a\u82e6\u607c\uff0c\u65e9\u5c31\u7edf\u4e00 UTF-8 \u4e86\u3002 \u9664\u6b64\u4e4b\u5916\uff0c \u4e2d\u8fd8\u5b9a\u4e49\u4e86 TEXT \u8fd9\u4e2a\u5b8f\u51fd\u6570\u3002 #ifdef UNICODE #define TEXT(s) L##s #else #define TEXT(s) s #endif \u7528\u6cd5\uff1a\u8981\u6c42\u7a0b\u5e8f\u5458\u628a\u6240\u6709\u7684\u5b57\u7b26\u4e32\u5e38\u91cf\uff0c\u90fd\u7528 TEXT \u5b8f\u5305\u88f9\u3002 Qt \u4e5f\u6709\u7c7b\u4f3c\u7684\u5b8f\u5305\u88f9\u5b57\u7b26\u4e32\u5e38\u91cf\u7684\u505a\u6cd5\uff0c tr \uff0c\u4f46\u5b83\u5e76\u4e0d\u662f\u4e3a\u4e86\u89e3\u51b3\u7f16\u7801\u95ee\u9898\uff0c\u800c\u662f\u4e3a\u4e86\u89e3\u51b3\u591a\u8bed\u8a00\u7ffb\u8bd1\u95ee\u9898\uff0c\u7a0d\u540e\u4f1a\u4e13\u95e8\u4ecb\u7ecd\u4e00\u4e0b Qt \u4e2d\u7684\u5b57\u7b26\u4e32\u3002 \u5f53 UNICODE \u5b8f\u5b9a\u4e49\u65f6\uff0c TEXT \u4f1a\u81ea\u52a8\u4e3a\u5b57\u7b26\u4e32\u5e38\u91cf\u6dfb\u52a0 L \u524d\u7f00\uff0c\u4f7f\u5f97\u5b57\u7b26\u4e32\u53d8\u6210 Unicode \u7684\u3002\u5982\u679c\u6ca1\u6709\u5b9a\u4e49\uff0c\u5219\u53c8\u53d8\u56de ANSI \u7f16\u7801\u7684\u5b57\u7b26\u4e32\uff08\u8ddf\u968f\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\u7684\u8bbe\u5b9a\uff09\u3002 \u4f8b\u5982\u4e0b\u9762\u8fd9\u4e00\u6bb5\u4ee3\u7801\uff1a #include int main() { MessageBox(NULL, TEXT(\"\u4f60\u597d\uff0c\u4e16\u754c\"), TEXT(\"\u6807\u9898\"), MB_OK); } \u5f53\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxW(NULL, L\"\u4f60\u597d\uff0c\u4e16\u754c\", L\"\u6807\u9898\", MB_OK); } \u5f53\u6ca1\u6709\u5b9a\u4e49 UNICODE \u65f6\uff0c\u7b49\u4ef7\u4e8e\uff1a #include int main() { MessageBoxA(NULL, \"\u4f60\u597d\uff0c\u4e16\u754c\", \"\u6807\u9898\", MB_OK); } \u6b64\u5916\uff0c\u8fd8\u5b9a\u4e49\u4e86 TCHAR \u8fd9\u4e2a\u7c7b\u578b\u522b\u540d\uff0c\u540c\u6837\u662f\u9488\u5bf9\u662f\u5426\u5b9a\u4e49 UNICODE \u5b8f\u800c\u5b9a\u4e49\u4e86\u4e24\u5957\u7248\u672c\u3002 #ifdef UNICODE typedef wchar_t TCHAR; #else typedef char TCHAR; #endif \u8fd8\u4e3a printf \u548c wprintf \u5b9a\u4e49\u4e86 TCHAR \u7248\u672c\uff0c\u4e0d\u4ec5\u5982\u6b64\uff0c\u8fd8\u6709 strlen \u548c wcslen \uff0c strcpy \u548c wcscpy \uff0c\u7b49\u7b49\u3002 #ifdef UNICODE #define _tprintf wprintf #define _tcscpy wcscpy #define _tcslen wcslen #else #define _tprintf printf #define _tcscpy strcpy #define _tcslen strlen #endif \u4e0d\u89c9\u5f97\u8fd9\u5f88\u9177\u5417\uff1f\u5f88\u7b26\u5408\u6211\u5bf9\u5f3a\u8feb\u75c7\u7684\u60f3\u8c61\uff0c\u79d1\u6280\u5e76\u4e14\u5e26\u7740\u81ed\u5473\u3002 int main() { TCHAR str[] = TEXT(\"\u6bd4\u5c14\u76d6\u5b50\u6211\u6d4b\u8bd5\u4f60\u7684\u7801\"); } (* \u54e6\uff0c\u6211\u662f\u8bf4\uff0c\u6211\u8981\u6d4b\u8bd5\u4f60\u7684\u7f16\u7801\u683c\u5f0f *) \u9700\u8981\u5207\u6362\u65f6\uff0c\u5728 MSVC \u4e2d\uff0c\u6253\u5f00\u6216\u5173\u95ed /DUNICODE \u7f16\u8bd1\u9009\u9879\u5373\u53ef\u3002 \u4e0d\u8981\u89c9\u5f97\u8fd9\u662f\u4ec0\u4e48\u597d\u4e3b\u610f\uff0c\u8fd9\u6837\u505a\u7684\u540e\u679c\u662f\uff0c\u4f60\u5199\u51fa\u7684\u4ee3\u7801\u53ea\u80fd\u5728 Windows \u4e0b\u7f16\u8bd1\u3002 \u5199\u8d77\u6765\u7d2f\u6b7b\u4eba\uff0c\u5b9e\u9645\u54ea\u6709\u90a3\u4e48\u591a\u4e00\u76f4\u5728 ANSI \u548c Unicode \u4e4b\u95f4\u6765\u56de\u5207\u6362\u7684\u9700\u6c42\uff1f \u6211\u7684\u5efa\u8bae\u662f\uff0c\u7edf\u4e00 wchar_t \uff0c\u7edf\u4e00\u5168\u7528 W \u51fd\u6570\uff0c\u618b\u62a0\u62a0\u7d22\u7d22\u7684\u534a\u8fdb\u534a\u9000\u3002","title":"TCHAR \u6d41\u6d3e"},{"location":"unicode/#utf-8_4","text":"\u4e4b\u524d\u8bf4\u8fc7\u4e86\uff0cWindows \u5e73\u53f0\u5230\u5904\u90fd\u9ed8\u8ba4 GBK \u975e\u5e38\u9ebb\u70e6\uff0c\u8981\u5207\u6362\u5230 UTF-8 \u5de5\u4f5c\u6d41\uff1a \u7f16\u8bd1\u5668\u5f00\u542f /utf-8 \u9009\u9879 \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c system(\"chcp 65001\") \u8bbe\u7f6e\u6587\u4ef6\u7cfb\u7edf\u5b57\u7b26\u4e32\u7f16\u7801\uff0c setlocale(LC_ALL, \".utf-8\") // \u7f16\u8bd1\u9009\u9879\uff1a/std:c++17 /utf-8 int main() { #if _WIN32 // \u70ed\u77e5\u8bc6\uff1a64 \u4f4d Windows \u4e5f\u4f1a\u5b9a\u4e49 _WIN32 \u5b8f\uff0c\u6240\u4ee5 _WIN32 \u53ef\u4ee5\u7528\u4e8e\u68c0\u6d4b\u662f\u5426\u662f Windows \u7cfb\u7edf setlocale(LC_ALL, \".utf-8\"); // \u8bbe\u7f6e\u6807\u51c6\u5e93\u8c03\u7528\u7cfb\u7edf API \u6240\u7528\u7684\u7f16\u7801\uff0c\u7528\u4e8e fopen\uff0cifstream \u7b49\u51fd\u6570 SetConsoleOutputCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u51fa\u7f16\u7801\uff0c\u6216\u8005\u5199 system(\"chcp 65001\") \u4e5f\u884c\uff0c\u8fd9\u91cc CP_UTF8 = 65001 SetConsoleCP(CP_UTF8); // \u8bbe\u7f6e\u63a7\u5236\u53f0\u8f93\u5165\u7f16\u7801\uff0c\u7528\u4e8e std::cin #elif __unix__ // \u53cd\u6b63 Unix \u7cfb\u7edf\u9ed8\u8ba4\u90fd\u662f UTF-8\uff0c\u4e0d\u8bbe\u7f6e\u4e5f\u884c\uff0c\u8fd9\u91cc\u8bbe\u7f6e\u5168\u5c40 locale \u662f\u4e3a\u4e86\u8ba9 iswspace \u63a5\u53d7\u5168\u89d2\u7a7a\u683c\u3001iswpunct \u63a5\u53d7\u5168\u89d2\u9017\u53f7 L'\uff0c' \u7b49 //setlocale(LC_ALL, \"zh_CN.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u4e2d\u6587\u672c\u5730\u5316\uff0c\u53ef\u4f7f strerror \u8f93\u51fa\u4e2d\u6587\uff08\u4f46\u7528\u6237\u5fc5\u987b locale-gen \u8fc7\u4e2d\u6587\uff01\uff09 //setlocale(LC_ALL, \"C.utf-8\"); // \u8bbe\u7f6e\u4f7f\u7528\u8bed\u8a00\u4e2d\u6027 locale\uff0c\u53ea\u5f71\u54cd iswspace\u3001iswpunct \u7b49\u51fd\u6570\uff0c\u4e0d\u4f1a\u4f7f strerror \u7b49\u8f93\u51fa\u4e2d\u6587 setlocale(LC_ALL, \".utf-8\"); // \u82e5\u4e0d\u5e26\u4efb\u4f55\u524d\u7f00\uff08\u63a8\u8350\uff09\uff0c\u5219\u9ed8\u8ba4\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u73af\u5883\u53d8\u91cf\u4e2d\u7684\u8bed\u8a00 $LANG\uff0c\u4f7f strerror \u81ea\u52a8\u9002\u5e94 #endif // \u8fd9\u91cc\u5f00\u59cb\u5199\u4f60\u7684\u4e3b\u7a0b\u5e8f\u5427\uff01 // ... std::cout << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u6ca1\u95ee\u9898\uff01 std::ifstream fin(\"\u4f60\u597d.txt\"); // \u6ca1\u95ee\u9898\uff01 std::wcout << L\"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u4f60\u90fd\u7edf\u4e00 UTF-8 \u4e86\uff0c\u8fd9\u7834 UTF-16 \u548c UTF-32 \u4e4b\u95f4\u6765\u56de\u8df3\u7684\u7834 wchar_t \u5c31\u522b\u7528\u4e86\u5457\uff01 return 0; }","title":"UTF-8 \u6d3e\u7684\u8de8\u5e73\u53f0\u8f6f\u4ef6\u4f55\u53bb\u4f55\u4ece\uff1f"},{"location":"unicode/#wndproc","text":"LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == false \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::cout << char(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f GBK \u7f16\u7801\u7684 char \u5e8f\u5217 // \u5982\u679c\u662f\u4e2d\u6587\u5b57\u7b26\uff0cWndProc(WM_CHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u5b57\u8282\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u5df1\u5224\u65ad\u548c\u62fc\u63a5 GBK \u5b57\u7b26\u4e32 return 0; case WM_UNICHAR: // \u5bf9\u4e8e IsWindowUnicode(hwnd) == true \u7684\u7a97\u53e3\uff0c\u4f1a\u8fdb\u5165\u8fd9\u91cc std::wcout << wchar_t(wParam); // \u6b64\u65f6 wParam \u8f93\u5165\u7684\u662f UTF-16 \u7f16\u7801\u7684 wchar_t \u5e8f\u5217 // \u5982\u679c\u662f\u4ee3\u7406\u5bf9\uff0cWndProc(WM_UNICHAR) \u4f1a\u88ab\u8c03\u7528\u591a\u6b21\uff0c\u6bcf\u6b21\u4e00\u4e2a\u7801\u4f4d\uff0c\u7a0b\u5e8f\u5458\u9700\u8981\u81ea\u884c\u628a\u4ee3\u7406\u5bf9\u7ec4\u88c5\u6210\u5b8c\u6574\u7684 Unicode \u7801\u70b9 return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); } } \u628a WndProc \u7684\u8f93\u5165\u5b58\u5165 std::u32string \u7684\u6848\u4f8b\uff1a std::u32string result; std::string ansi_buf; std::wstring utf16_buf; std::optional try_ansi_to_utf32(std::string const &s) { try { return boost::locale::conv::to_utf(s, \"\"); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } std::optional try_utf16_to_utf32(std::wstring const &s) { try { return boost::locale::conv::utf_to_utf(s); } catch (boost::locale::conv::conversion_error const &) { return std::nullopt; } } LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CHAR: ansi_buf.push_back(char(wParam)); if (auto u = try_ansi_to_utf32(ansi_buf)) { result += u.value(); ansi_buf.clear(); } return 0; case WM_UNICHAR: utf16_buf.push_back(wchar_t(wParam)); if (auto u = try_utf16_to_utf32(utf16_buf)) { result += u.value(); utf16_buf.clear(); } return 0; default: return DefWindowProc(hwnd, uMsg, wParam, lParam); };","title":"WndProc \u63a5\u53d7\u8f93\u5165\u6cd5\u7684\u4e2d\u6587\u8f93\u5165"},{"location":"unicode/#_21","text":"\u6807\u51c6\u5e93\u7684 std::string \u6211\u4eec\u4e0d\u518d\u8d58\u8ff0\uff0c\u521a\u624d\u5728\u201c\u5bbd\u5b57\u7b26\u6d41\u201d\u4e2d\u4e5f\u4ecb\u7ecd\u4e86\u5b98\u65b9\u7684\u60f3\u6cd5\uff0c\u4e4b\u540e\u7684\u5b57\u7b26\u4e32\u4e13\u9898\u8bfe\u4f1a\u7ee7\u7eed\u8be6\u89e3\u3002 \u8fd9\u91cc\u6211\u4eec\u4e3b\u8981\u63a2\u7a76\u5173\u4e8e\u5b57\u7b26\u7f16\u7801\u7684\u95ee\u9898\uff0c\u63a2\u7d22\u5404\u5927\u5e38\u89c1\u7684\u7f16\u7a0b\u8bed\u8a00\u548c\u5e93\uff0c\u662f\u5982\u4f55\u5c01\u88c5\u5b57\u7b26\u4e32\u7c7b\uff0c\u5982\u4f55\u89e3\u51b3 UTF-8 \u53d8\u957f\u7f16\u7801\uff0cUTF-32 \u538b\u7f29\u7387\u4f4e\u7684\u95ee\u9898\u7684\uff0c\u5e0c\u671b\u5728\u4f60\u7684\u9879\u76ee\u4e2d\u63d0\u4f9b\u4e00\u70b9\u7075\u611f\u3002 \u901a\u5e38\u6765\u8bf4\uff0c\u4e00\u4e2a\u597d\u7684\u5e93\u6216\u8bed\u8a00\uff0c\u90fd\u8981\u660e\u786e\u533a\u5206\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u6982\u5ff5\uff0c\u524d\u8005\u662f\u6587\u672c\u5185\u5bb9\uff0c\u540e\u8005\u662f\u7eaf\u4e8c\u8fdb\u5236\u5185\u5bb9\u3002 \u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u201c\u7f16\u7801\u201d\u5f97\u5230\u7eaf\u4e8c\u8fdb\u5236\u7684\u5b57\u8282\u6570\u7ec4\uff0c\u800c\u5b57\u8282\u6570\u7ec4\u53ef\u4ee5\u201c\u89e3\u7801\u201d\u5f97\u5230\u539f\u59cb\u5b57\u7b26\u4e32\u3002 \u65e9\u671f\u7684 C \u8bed\u8a00\u5c31\u662f\u56e0\u4e3a\u628a\u5b57\u7b26\u548c\u5b57\u8282\u6df7\u4e3a\u4e00\u8c08\uff0c\u90fd\u4f7f\u7528\u4e86 char \u7c7b\u578b\uff0c\u624d\u4ea7\u751f\u4e86\u540e\u6765\u8fd9\u4e48\u591a\u4e71\u8c61\u3002\u540e\u6765\u901a\u8fc7\u6253\u8865\u4e01\u6253\u4e0a\u771f\u6b63\u7684\u5b57\u7b26 wchar_t \uff0c\u5374\u6ca1\u4ec0\u4e48\u4eba\u7528\uff0c\u800c\u4e14\u8fd8\u88ab Windows \u641e\u6210 16 \u4f4d\uff0c\u53cd\u800c\u4e0d\u8de8\u5e73\u53f0\u4e86\u3002 \u6b64\u5904\u5148\u5217\u4e00\u4e2a\u4e0d\u540c\u7f16\u7a0b\u8bed\u8a00\u773c\u4e2d\u5b57\u7b26\u4e32\u548c\u5b57\u8282\u6570\u7ec4\u7684\u8868\uff0c\u65b9\u4fbf\u4f60\u7406\u89e3\u3002 \u8bed\u8a00 \u5b57\u7b26 \u5b57\u7b26\u4e32 \u6587\u672c\u6d41 \u5b57\u8282\u6570\u7ec4 \u4e8c\u8fdb\u5236\u6d41 \u7f16\u7801/\u89e3\u7801 C char wchar_t * FILE * + fgetwc char * FILE * + fgetc wcstomb / mbstowc C++ wchar_t std::wstring std::wistream std::string / std::vector std::istream std::codecvt Qt QChar QString QTextStream QByteArray QDataStream QTextCodec Python3 str str open('r') bytes open('rb') str.encode() Python2 unicode unicode \u65e0 str open('r') unicode.encode() Java Character String Reader byte[] InputStream Charset.encode C# char string StreamReader byte[] Stream Encoding Rust char String BufRead u8 Read str::from_utf8 JS char String ReadableStream Uint8Array ReadableStream TextEncoder Go rune string Reader byte Reader utf8.DecodeRune PHP string string fopen string fopen mb_convert_encoding Swift Character String String.UnicodeScalarView UInt8 Data String.Encoding Kotlin Char String Reader ByteArray InputStream Charset.encode Obj-C unichar NSString NSInputStream uint8_t NSInputStream NSStringEncoding Lua integer table \u65e0 string io.open require'utf8' \u672c\u8bfe\u4e0d\u4f1a\u8bb2\u89e3\u8fd9\u4e9b\u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u5177\u4f53\u7528\u6cd5\uff0c\u53ea\u63d0\u4f9b\u4e00\u4e9b\u6982\u5ff5\uff0c\u8ba9\u4f60\u77e5\u9053\u5927\u5bb6\u90fd\u662f\u600e\u4e48\u5b9e\u73b0\u7684\uff0c\u89e6\u7c7b\u65c1\u901a\u3002 Lua \u4e2d UTF-32 \u7684\u5b9a\u957f\u7f16\u7801\uff0c\u8981\u8fd9\u6837\u5b9e\u73b0\uff1a {80, 101, 110, 103} \u3002\u800c\u4ed6\u6240\u8c13\u7684 utf8 \u5e93\uff0c\u5c31\u662f\u8d1f\u8d23\u628a Lua \u81ea\u5df1\u7684 string \u5047\u8bbe\u4e3a utf8 \u7f16\u7801\uff0c\u89e3\u7801\u51fa\u4e00\u4e2a\u4e2a Unicode \u7801\u70b9\uff0c\u8fd4\u56de\u4e00\u4e2a\u8fd9\u6837\u7684\u6570\u7ec4\u3002\u4ed6\u751a\u81f3\u4e0d\u662f Lua \u6807\u51c6\u7684\u4e00\u90e8\u5206\uff0c\u662f\u4e2a\u7b2c\u4e09\u65b9\u7684\u5e93\uff0c\u8fd8\u662f\u9700\u8981\u7f16\u8bd1\u7684 C \u8bed\u8a00 .so \u6587\u4ef6\u3002\u8fd8\u6709 Lua \u5b5d\u5b50\u501f\u6b64\u786c\u8bf4\u6211\u4eec Lua \u662f\u5929\u751f UTF-8\uff01\u7136\u800c\u62ff\u7740\u4e2a UTF-8 \u7f16\u7801\u7684\u201c\u5b57\u8282\u6570\u7ec4\u201d string \u6765\u6253\u5f00 io.open \u6587\u4ef6\uff0c\u5c31\u4f1a\u62a5\u9519\u627e\u4e0d\u5230\u6587\u4ef6\uff08\u56e0\u4e3a\u4e2d\u56fd\u533a Windows \u7684 GBK\uff09\uff0c\u662f\u4e0d\u662f\u5f88\u597d\u7b11\u5462\uff1f","title":"\u5e38\u89c1\u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u63a2\u7a76"},{"location":"unicode/#qt-qstring","text":"Qt \u7684\u5b57\u7b26\u4e32\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 Qt \u7684\u5b57\u8282\u6570\u7ec4\u7c7b\u578b\u662f QString \u3002\u5b83\u53ef\u4ee5\u5bb9\u7eb3\u4efb\u610f Unicode \u5b57\u7b26\u96c6\u7684\u5b57\u7b26\u4e32\u3002 \u5b83\u7684\u6570\u636e\u7ed3\u6784\u5b9e\u9645\u4e0a\u662f\u4e2a QChar \u6570\u7ec4\uff0c\u800c QChar \u662f unsigned short \uff0c\u5373 16 \u4f4d\u65e0\u7b26\u53f7\u6574\u6570\uff0c\u4e5f\u5c31\u662f UTF-16 \u7f16\u7801\u7684\u7801\u4f4d\u3002 QString str = \"\u4f60\u597d\uff0c\u4e16\u754c\"; // str.size() = 5 // str[0] = QChar(0x4f60) = u'\u4f60' // str[1] = QChar(0x597d) = u'\u597d' // str[2] = QChar(0xff0c) = u'\uff0c' // str[3] = QChar(0x4e16) = u'\u4e16' // str[4] = QChar(0x754c) = u'\u754c' \u53ef\u89c1\uff0c QString \u662f UTF-16 \u7f16\u7801\u7684\uff0c\u5c31\u548c Java \u4e00\u6837\uff0cQt \u4e5f\u662f UTF-16 \u6f6e\u7684\u53d7\u5bb3\u8005\u3002 \u6240\u4ee5\uff0cQString \u4e5f\u5b58\u5728\u7740\u4ee3\u7406\u5bf9\u53d8\u957f\u7f16\u7801\u7684\u95ee\u9898\u3002\u4f46\u81f3\u5c11\u5bf9\u5e38\u89c1\u7684\u4e2d\u6587\u5b57\u7b26\u6765\u8bf4\uff0c\u4e00\u4e2a 16 \u4f4d\u7684 QChar \u90fd\u5bb9\u7eb3\u7684\u4e0b\u4e86\u3002","title":"Qt QString"},{"location":"unicode/#qtextcodec","text":"Qt \u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u7684\u7f16\u7801\u8f6c\u6362\u51fd\u6570\uff0c\u7528\u4e8e\u5c06 QString \u8f6c\u6362\u6210 QByteArray \uff0c\u6216\u8005\u4ece QByteArray \u8f6c\u6362\u6210 QString \uff0c\u8fd9\u4e9b\u51fd\u6570\u540d\u90fd\u662f\u4ee5 to \u6216\u8005 from \u5f00\u5934\u7684\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u540d\uff0c\u4f8b\u5982 fromUtf8 \u3001 toUtf8 \u3001 toLocal8Bits \u3002 \u8fd9\u4e9b\u51fd\u6570\u7684\u5185\u90e8\uff0c\u90fd\u662f\u8c03\u7528 QTextCodec \u7c7b\u5b9e\u73b0\u7684\u8f6c\u6362\u3002 QTextCodec \u662f Qt \u7528\u4e8e\u5904\u7406\u5404\u79cd\u6587\u672c\u7f16\u7801\u4e4b\u95f4\u8f6c\u6362\u7684\u7c7b\u3002\u5b83\u7684\u9759\u6001\u65b9\u6cd5 codecForLocale \u8fd4\u56de\u4e86\u5f53\u524d\u7cfb\u7edf\u7684\u7f16\u7801\uff0c toUnicode \u548c fromUnicode \u5206\u522b\u662f\u5c06 QByteArray \u8f6c\u6362\u6210 QString \uff0c\u6216\u8005\u5c06 QString \u8f6c\u6362\u6210 QByteArray \u3002 QTextCodec *codec = QTextCodec::codecForLocale(); // \u8fd4\u56de\u5f53\u524d\u7cfb\u7edf\u7f16\u7801 QByteArray bytes = codec->fromUnicode(str); // \u5c06 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u5373 char[] QString str = codec->toUnicode(bytes); // \u5c06 QByteArray \u8f6c\u6362\u6210 QString QTextCodec \u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u66f4\u52a0\u7ec6\u7c92\u5ea6\u7684\u8f6c\u6362\u63a5\u53e3\uff0c\u4f8b\u5982 fromUnicode \u9664\u4e86\u63a5\u53d7 QString \uff0c\u8fd8\u63a5\u53d7 QChar \u6570\u7ec4\uff0c\u53ef\u4ee5\u6307\u5b9a\u8f6c\u6362\u8303\u56f4\u3002","title":"QTextCodec"},{"location":"unicode/#fromtolocal8bitsutf8latin1ascii","text":"\u4e3a\u4e86\u65b9\u4fbf\u4f7f\u7528\uff0cQt \u5c01\u88c5\u4e86\u4e00\u4e9b\u5e38\u7528\u5b57\u7b26\u7f16\u7801\u7684\u8f6c\u6362\u51fd\u6570\uff0c\u8fd9\u6837\u4f60\u4e0d\u5fc5\u6bcf\u6b21\u90fd\u521b\u5efa\u4e00\u4e2a QTextCodec\u3002\u90fd\u662f to \u548c from \u5f00\u5934\uff0c\u540e\u9762\u8ddf\u7740\u7f16\u7801\u7684\u540d\u79f0\u3002 Local8Bits \u8868\u793a\u8fd0\u884c\u65f6\u68c0\u6d4b\u5230\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\uff0c\u4e5f\u5c31\u662f\u5ba2\u6237\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u3002 QByteArray bytes = str.toLocal8Bits(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801 QString::fromLocal8Bits(bytes); // \u518d\u4ece\u5f53\u524d\u7cfb\u7edf\u7684\u5b57\u7b26\u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForLocale(); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Utf8 \u8868\u793a\u4f7f\u7528 UTF-8 \u7f16\u7801\u3002 QByteArray bytes = str.toUtf8(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 UTF-8 \u7f16\u7801 QString::fromUtf8(bytes); // \u518d\u4ece UTF-8 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"UTF-8\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Latin1 \u8868\u793a ISO-8859-1 \u7f16\u7801\uff0c\u53c8\u79f0\u4e3a\u897f\u6b27\u7f16\u7801\uff0c\u5b83\u662f\u4e00\u4e2a\u5355\u5b57\u8282\u7f16\u7801\uff0c\u548c ASCII \u7f16\u7801\u76f8\u4f3c\uff0c\u4f46\u662f\u591a\u4e86 128-255 \u7684\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u6cd5\u8bed\u3001\u5fb7\u8bed\u3001\u897f\u73ed\u7259\u8bed\u3001\u8461\u8404\u7259\u8bed\u7b49\u5b57\u7b26\u3002 QByteArray bytes = str.toLatin1(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 Latin1 \u7f16\u7801 QString::fromLatin1(bytes); // \u518d\u4ece Latin1 \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ISO-8859-1\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes); Ascii \u8868\u793a ASCII \u7f16\u7801\uff0c\u548c Latin1 \u7684\u60c5\u51b5\u7c7b\u4f3c\uff0c\u53ea\u4e0d\u8fc7\u4ed6\u65e0\u6cd5\u5904\u7406 128-255 \u8fd9\u4e00\u6bb5\u7684\u5b57\u7b26\u3002 QByteArray bytes = str.toAscii(); // \u5c06 UTF-16 \u7684 QString \u8f6c\u6362\u6210 QByteArray\uff0c\u4f7f\u7528 ASCII \u7f16\u7801 QString::fromAscii(bytes); // \u518d\u4ece ASCII \u7f16\u7801\u8f6c\u6362\u56de UTF-16 \u7684 QString // \u7b49\u4ef7\u4e8e\uff1a QTextCodec *codec = QTextCodec::codecForName(\"ASCII\"); QByteArray bytes = codec->fromUnicode(str); QString str = codec->toUnicode(bytes);","title":"from/toLocal8Bits/Utf8/Latin1/Ascii"},{"location":"unicode/#_22","text":"QString str = QStringLiterial(\"\u4f60\u597d\uff0c\u4e16\u754c\"); QStringLiterial \u53ef\u4ee5\u4fdd\u8bc1\uff0c\u8f6c\u6362\u65f6\u91c7\u7528\u7684\u662f\u6240\u8c13\u201c\u8fd0\u884c\u5b57\u7b26\u96c6\u201d\uff08\u5b9e\u9645\u5e94\u8be5\u53eb\u5b57\u9762\u91cf\u5b57\u7b26\u7f16\u7801\uff09\uff0c\u4e5f\u5c31\u662f\u6211\u4eec\u5f00\u53d1\u8005\u7535\u8111\u4e0a\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\uff0c\u662f\u7f16\u8bd1\u671f\u786e\u5b9a\u7684\u3002\u800c\u5982\u679c\u5199 QString::fromLocal8Bits(\"\") \u5c31\u53d8\u6210 \u201cANSI\u201d\uff0c\u5ba2\u6237\u7684\u201c\u533a\u57df\u8bbe\u7f6e\u201d\u4e86\u3002\u8fd9\u4e24\u4e2a\u5b57\u7b26\u7f16\u7801\uff0c\u6bd4\u5982\u5728\u4e4b\u524d\u8de8\u56fd galgame \u7684\u6848\u4f8b\u4e2d\uff0c\u5c31\u662f\u4e0d\u540c\u7684\u3002","title":"\u5b57\u7b26\u4e32\u5e38\u91cf"},{"location":"unicode/#qtextstream","text":"QTextStream \u662f Qt \u63d0\u4f9b\u7684\u6587\u672c\u6d41\u7c7b\uff08\u5e26\u6709\u7f13\u51b2\uff09\uff0c\u5b83\u53ef\u4ee5\u5c06\u6587\u672c\u5199\u5165\u5230\u6587\u4ef6\u3001\u5957\u63a5\u5b57\u3001\u6807\u51c6\u8f93\u51fa\u7b49\u8bbe\u5907\u3002 \u6587\u672c\u6d41\u548c\u4e8c\u8fdb\u5236\u6d41\u4e0d\u540c\uff0c\u4ed6\u9700\u8981\u6307\u5b9a\u4e00\u4e2a\u7f16\u7801\u683c\u5f0f\uff0c\u901a\u8fc7 QTextStream \u6784\u9020\u51fd\u6570\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u6307\u5b9a\u3002 \u5f53\u4f60\u5f80 QTextStream \u5199\u5165 QString \u65f6\uff0c\u4f1a\u8c03\u7528\u8fd9\u4e2a\u7f16\u7801\u683c\u5f0f\u81ea\u52a8\u8f6c\u6362\u4e3a QByteArray \uff0c\u7136\u540e\u624d\u5199\u5165\u3002 \u8bfb\u53d6\u65f6\u4e5f\u540c\u7406\uff0c\u4f1a\u628a\u8bfb\u5230\u7684 QByteArray \u901a\u8fc7\u7f16\u7801\u683c\u5f0f\u89e3\u7801\uff0c\u5f97\u5230 QString \u5b57\u7b26\u4e32\u3002 Qt \u503c\u5f97\u79f0\u9053\u7684\u4e00\u70b9\u662f\uff1a\u4ed6\u628a\u6587\u4ef6\u548c\u6587\u4ef6\u6d41\u533a\u5206\u5f00\u6765\uff0c\u6587\u4ef6 QFile \u53ea\u9700\u8981\u8d1f\u8d23\u6253\u5f00\u6587\u4ef6\u5c31\u53ef\u4ee5\u4e86\uff1b\u800c\u6587\u672c\u6d41 QTextStream \u624d\u771f\u6b63\u8d1f\u8d23\u6570\u636e\u7684\u7f13\u51b2\uff0c\u89e3\u7801\u7b49\u64cd\u4f5c\u3002\u4f53\u73b0\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u804c\u8d23\u5355\u4e00\u539f\u5219\u3002 QFile file(\"hello.txt\"); file.open(QIODevice::ReadWrite); QTextStream stream(&file); stream.setCodec(\"UTF-8\"); // \u8bbe\u7f6e\u6587\u4ef6\u7684\u7f16\u7801\u683c\u5f0f stream << \"\u4f60\u597d\uff0c\u4e16\u754c\\n\"; // \u5199\u5165 QString\uff0cQTextStream \u4f1a\u81ea\u52a8\u5c06\u5176\u7528 UTF-8 \u7f16\u7801\u4e3a QByteArray \u540e\u5199\u5165 QFile \u5982\u679c\u4f60\u786e\u5b9e\u9700\u8981\u76f4\u63a5\u5199\u5165\u4e8c\u8fdb\u5236\u7684 QByteArray \uff0cQt \u4e5f\u63d0\u4f9b\u4e86\u4e00\u4e2a QDataStream \u7c7b\uff0c\u65b9\u4fbf\u4e8c\u8fdb\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u3002 \u5982\u6b64\u628a\u4e8c\u8fdb\u5236\u548c\u6587\u672c\u4e25\u683c\u533a\u5206\uff0c\u800c\u4e0d\u662f\u50cf\u53e4\u4ee3 C \u8bed\u8a00\u90a3\u6837\u5b57\u8282\u4e0e\u5b57\u7b26\u6df7\u6dc6\u4e0d\u6e05\uff0c\u5168\u7528\u7cdf\u7cd5\u7684 char \u6765\u8868\u793a\uff0c\u5145\u5206\u4f53\u73b0\u4e86\u5f3a\u7c7b\u578b\u7684\u5b89\u5168\u3002","title":"QTextStream"},{"location":"unicode/#python-3-str","text":"\u5982\u4f55\u89e3\u51b3 UTF-32 \u5360\u7528\u7a7a\u95f4\u5927\u7684\u75db\u70b9\uff1fPython \u7684\u5b57\u7b26\u4e32\u5b9e\u73b0\u7edd\u5bf9\u503c\u5f97\u4e00\u770b\uff01 \u4e3a\u4e86\u65b9\u4fbf\u6587\u672c\u5904\u7406\uff0c\u6700\u597d\u4ee5\u5b9a\u957f\u7f16\u7801\uff0c\u4e5f\u5c31\u662f UTF-32\uff0c\u5b58\u50a8\u5b57\u7b26\u4e32\u3002 \u5982\u679c\u7528 UTF-8 \u6216 UTF-16 \u6765\u5b58\u50a8\u7684\u8bdd\uff0c\u4f1a\u9047\u5230\u53d8\u957f\u7f16\u7801\u7684\u56fa\u6709\u7f3a\u9677\uff1a \u4f8b\u5982\u50cf\u5b57\u7b26\u4e32\u7d22\u5f15\uff0c\u5b57\u7b26\u4e32\u6c42\u957f\u5ea6\u7b49\u64cd\u4f5c\uff0c\u8981\u4e48\u7d22\u5f15\u51fa\u6765\u7684\u662f\u5b57\u8282\u800c\u4e0d\u662f\u5b57\u7b26\u4e86\uff1b\u8981\u4e48\u5c31\u9700\u8981 O(N) O(N) \u7684\u590d\u6742\u5ea6\uff0c\u9010\u4e00\u904d\u5386\u6bcf\u4e2a\u5b57\u8282\uff0c\u624d\u80fd\u786e\u5b9a\u771f\u6b63\u7684\u4f4d\u7f6e\uff1b\u54ea\u6015\u5168\u662f ASCII \u4e5f\u5f97\u8fd9\u4e48\u505a\uff0c\u56e0\u4e3a\u4e07\u4e00\u521a\u597d\u6709\u4e00\u4e2a\u662f\u4e2d\u6587\u5b57\u7b26\u5462\uff1f \u6240\u4ee5\uff0c\u5bf9\u4e8e\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684 Python \u6765\u8bf4\uff0cUTF-8 \u662f\u65e0\u6cd5\u63a5\u53d7\u7684\uff0c\u4f3c\u4e4e\u53ea\u80fd\u4ee5 UTF-32 \u6765\u5b58\u50a8\uff1f Python \u60f3\u5230\u4e86\u4e00\u4e2a\u5c0f\u5999\u62db\uff1a enum PyUnicodeType { PyUnicodeType_Latin1, PyUnicodeType_UCS2, PyUnicodeType_UCS4, }; struct PyUnicodeString { PyUnicodeType type; union { uint8_t *latin1; uint16_t *ucs2; uint32_t *ucs4; }; }; \u8fd9\u91cc\u7684 union \u662f\u4e00\u4e2a C \u8bed\u8a00\u7279\u6027\uff0c\u4ed6\u5141\u8bb8\u4f60\u628a\u591a\u4e2a\u6210\u5458\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u7a7a\u95f4\u3002\u901a\u5e38\u6765\u8bf4\u9700\u8981\u914d\u5408\u4e00\u4e2a enum \u8868\u793a\u7c7b\u578b\uff0c\u624d\u80fd\u5b89\u5168\u4f7f\u7528\u3002\u73b0\u4ee3 C++ \u7684 std::variant \u66f4\u5b89\u5168\u7684\u53d6\u4ee3\u4e86\u4ed6\uff0c\u800c\u4e14\u4e0d\u9700\u8981\u5916\u6302\u4e00\u4e2a enum \u3002CPython \u89e3\u91ca\u5668\u56e0\u4e3a\u662f C \u8bed\u8a00\u5b9e\u73b0\uff0c\u53ea\u80fd\u7528 union \u6a21\u62df std::variant \u7684\u6548\u679c\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFF \u7684\u5b57\u7b26\uff08Latin-1 \u8303\u56f4\uff09\u65f6\uff0c\u5b9e\u9645\u4e0a\u6ca1\u5fc5\u8981\u5168\u7528\u8d85\u5927\u7684 uint32_t \u6765\u5b58\u50a8\u3002\u5b8c\u5168\u53ef\u4ee5\u53ea\u53d6\u51fa\u4f4e 8 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a\u66f4\u7d27\u51d1\u7684 uint8_t \u6570\u7ec4\uff0c\u5c31\u50cf Latin-1 \u7f16\u7801\u4e00\u6837\u3002 \u4f46\u662f\u5f53\u6309\u7d22\u5f15\u8bfb\u53d6\u5143\u7d20\u65f6\uff0c\u4f1a\u5224\u65ad\u5f53\u524d union \u91cc\u88c5\u7684\u662f\u54ea\u79cd\u7c7b\u578b\uff0c\u5982\u679c\u662f Latin-1 \u7684\uff0c\u90a3\u5c31\u4f1a\u7528\u7ed3\u6784\u4f53\u91cc\u7684 uint8_t * \u6307\u9488\u6765\u7d22\u5f15\u3002 \u8fd9\u6837\uff0c\u5bf9\u4e8e\u5168 ASCII \u7684\u5b57\u7b26\u4e32\uff0c\u76f8\u6bd4\u8001\u8001\u5b9e\u5b9e\u5b58 UTF-32 \u5185\u5b58\u5360\u7528\u76f4\u63a5\u51cf\u5c11\u4e86 75%\uff01\u552f\u4e00\u7684\u4ee3\u4ef7\u662f\u6309\u7d22\u5f15\u8bfb\u5b57\u7b26\u5143\u7d20\u65f6\u9700\u8981\u505a\u4e2a if-else \u5224\u65ad\u3002\u540c\u65f6\u53c8\u4e0d\u5931\u53bb\u5b9a\u957f\u7f16\u7801\u7684\u4f18\u52bf\u3002 \u5f53\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\u53ea\u5305\u542b 0 \u5230 0xFFFF \u7684\u5b57\u7b26\uff08\u65e9\u671f Unicode \u8303\u56f4\uff09\u65f6\uff0c\u5219\u662f\u53d6\u51fa\u4f4e 16 \u4f4d\uff0c\u5b58\u5165\u4e00\u4e2a uint16_t \u7684\u6570\u7ec4\uff0c\u8fd9\u79cd\u5b58\u50a8\u65b9\u6848\u4e5f\u79f0\u4e3a UCS-2\u3002 \u6ce8\u610f UCS-2 \u5e76\u4e0d\u7b49\u540c\u4e8e UTF-16\uff0cUTF-16 \u662f\u80fd\u591f\u8868\u793a\u5b8c\u6574\u7684 Unicode \u7684\u53d8\u957f\u7f16\u7801\uff08\u6709\u4ee3\u7406\u5bf9\uff09\uff1b\u800c UCS-2 \u662f\u6ca1\u6709\u4ee3\u7406\u5bf9\u7684\u5b9a\u957f\u7f16\u7801\uff0c\u7f3a\u70b9\u662f\u53ea\u80fd\u8868\u793a 0xFFFF \u7684\u8303\u56f4\u3002 \u5bf9\u4e8e\u5927\u90e8\u5206\u4e2d\u6587\u5b57\u7b26\u4e32\uff0c\u5185\u5b58\u5360\u7528\u5c31\u51cf\u5c11\u4e86 50%\uff0c\u4e5f\u4e0d\u4e8f\u3002 \u5982\u679c\u5b57\u7b26\u4e32\u4e2d\u51fa\u73b0\u201c\ud883\udede\u201d\u8fd9\u6837\u7684\uff0c\u8d85\u8fc7 0xFFFF \u7684\u5b57\u7b26\uff0c\u624d\u4f1a\u91c7\u7528 uint32_t \u8001\u8001\u5b9e\u5b9e\u5b58\u50a8\u771f\u6b63\u7684 UTF-32\uff0c\u53c8\u79f0 UCS-4\u3002 \u8fd9\u4e24\u79cd\u53eb\u6cd5\u662f\u7b49\u4ef7\u7684\uff0c\u53cd\u6b63 Unicode \u4ece\u6765\u6ca1\u6709\u8d85\u8fc7 0xFFFFFFFF \u7684\uff0c\u73b0\u5728\u4ed6\u4eec\u90fd\u662f\u5b9a\u957f\u7f16\u7801\u3002 \u5bf9\u4e8e\u4e00\u4e2a\u7ecf\u5e38\u9700\u8981\u5904\u7406\u5b57\u7b26\u4e32\u7684\u865a\u62df\u673a\u8bed\u8a00\u6765\u8bf4\uff0c\u53cd\u6b63\u672c\u6765\u5c31\u4e0d\u5728\u4e4e if-else \u5206\u652f\u8fd9\u70b9\u5c0f\u5f00\u9500\uff0c\u8fd9\u786e\u5b9e\u662f\u6700\u597d\u7684\u65b9\u6848\u3002 \u7f3a\u70b9\u5c31\u662f\uff0c\u5f53\u4f60\u5f80\u4e00\u4e2a\u5b8c\u5168\u662f ASCII \u7684\u5b57\u7b26\u4e32\u4e2d\uff0c\u7a81\u7136\u63d2\u5165\u4e00\u4e2a\u201c\ud883\udede\u201d\u65f6\uff0c\u4f1a\u4ea7\u751f\u5de8\u5927\u7684\u5185\u5b58\u91cd\u5206\u914d\u3002\u867d\u7136\u53ea\u6709\u4e00\u4e2a\u201c\ud883\udede\u201d\uff0c\u4f46\u4e3a\u6b64\uff0c\u5176\u4ed6\u6240\u6709 ASCII \u5b57\u7b26\u90fd\u5f97\u4e3a\u4ed6\u6269\u5f20\u5230 32 \u4f4d\u7684\u5927\u5c0f\u3002\u800c UTF-8 \u548c\u4f20\u7edf\u7684 UTF-32 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u56e0\u6b64\u6211\u4e5f\u4e0d\u5efa\u8bae C++ \u7a0b\u5e8f\u5458\u81ea\u5df1\u624b\u6413\u4e2a\u8fd9\u6837\u7684 union \u5b57\u7b26\u4e32\u3002","title":"Python 3 str"},{"location":"unicode/#rust-str-string","text":"\u800c Rust \u5219\u91c7\u7528\u4e86\u5b57\u7b26\u4e32\u5168\u5458 UTF-8 \u7684\u7b56\u7565\uff0c\u8fd9\u662f\u56e0\u4e3a Rust \u6700\u5e38\u7528\u4e8e\u4e92\u8054\u7f51\u65b9\u9762\u7684\u5e95\u5c42\u7cfb\u7edf\u8f6f\u4ef6\uff0c\u4e92\u8054\u7f51\u6700\u5e38\u7528\u7684\u6587\u672c\u7f16\u7801\u5c31\u662f UTF-8\uff0c\u6ca1\u6709\u5927\u5c0f\u7aef\u95ee\u9898\uff0c\u4e14\u56fd\u9645\u901a\u7528\u3002\u9664\u6b64\u4e4b\u5916\uff0c\u4e92\u8054\u7f51\u57fa\u5efa\u6700\u5e38\u89c1\u7684\u5e73\u53f0\u5c31\u662f Linux\uff0c\u4f7f\u7528 UTF-8 \u5b58\u50a8\u5b57\u7b26\u4e32\uff0c\u8c03\u7528 Linux \u7cfb\u7edf API \u65e0\u9700\u4efb\u4f55\u8f6c\u6362\u3002\u4e14\u6587\u672c\u6587\u4ef6\u57fa\u672c\u90fd\u53ef\u4ee5\u5047\u5b9a\u662f UTF-8 \u7f16\u7801\uff0c\u5199\u5165\u65f6\u65e0\u9700\u4efb\u4f55\u8f6c\u6362\uff0c\u590d\u6742\u5ea6\u4f4e\u81f3 O(1) O(1) \u3002\u4f5c\u4e3a\u4ee3\u4ef7\uff0c\u8fd9\u5bfc\u81f4\u6587\u672c\u5904\u7406\u4e0a\u7684\u4e00\u4e9b\u56f0\u96be\uff0c\u4f8b\u5982\u5b57\u7b26\u4e32\u7684\u7d22\u5f15\uff0c\u9700\u8981\u533a\u5206\u662f\u6309\u5b57\u8282\u7d22\u5f15\u8fd8\u662f\u6309\u5b57\u7b26\u7d22\u5f15\uff0c\u5982\u679c\u786e\u5b9e\u9700\u8981\u6309\u5b57\u7b26\u7d22\u5f15\u7684\u8bdd\uff0c\u590d\u6742\u5ea6\u5c31\u4f1a\u662f O(N) O(N) \u4e86\u3002 \u65e0\u8bba\u5982\u4f55\uff0c\u5982\u679c\u4f60\u9009\u62e9\u4e86 UTF-8 \u6d41\u6d3e\u7684\u8bdd\uff0cRust \u5b57\u7b26\u4e32\u7684\u201c\u8fed\u4ee3\u5668\u53cc\u8f68\u5236\u201d\u786e\u5b9e\u503c\u5f97\u79f0\u9053\uff1a let s = \"\u4f60\u597d\uff0c\u4e16\u754c\"; for c in s.chars() { // \u6309\u5b57\u7b26\u8fed\u4ee3 println!(\"{}\", c); } for b in s.bytes() { // \u6309\u5b57\u8282\u8fed\u4ee3 println!(\"{:02X}\", b); // \u6253\u5370\uff1aE4 BD A0 E5 A5 BD EF BC 8C E4 B8 96 E7 95 8C }","title":"Rust &str \u548c String"},{"location":"unicode/#java-string","text":"Java \u4e5f\u662f UTF-16 \u7684\u53cc\u5b57\u8282\u7f16\u7801\u3002 \u4eae\u70b9\uff1a\u4ed6\u7684 String \u7c7b\u578b\u662f\u4e0d\u53ef\u53d8\u7684\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u4f60\u65e0\u6cd5\u5c31\u5730\u4fee\u6539\u4e00\u4e2a String \u5bf9\u8c61\uff0c\u6bcf\u6b21\u4ea7\u751f\u4f60\u8c03\u7528 += \u7684\u90fd\u662f\u4e00\u4e2a\u65b0 String\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u3002 \u4e5f\u5c31\u662f\u8bf4\uff1aJava \u7684 String \u867d\u7136\u662f\u201c\u5806\u201d\u4e2d\u7684\u5bf9\u8c61\uff0c\u5374\u65e0\u6cd5\u4ee5\u5f15\u7528\u4f20\u9012\u3002 \u8fd9\u907f\u514d\u4e86\u4ee5\u4e0b\u8fd9\u79cd\u60c5\u51b5\uff1a void registerStudent(String name) { name += \".txt\"; File file(name); file.write(...); } void myTransaction() { String name = \"\u5c0f\u5f6d\"; lib.registerStudent(name); office.registerStudent(name); // \u8fd9\u91cc name \u662f\u5426\u88ab\u4fee\u6539\uff1f } \u5982\u679c Java \u7684 String \u548c\u666e\u901a\u5bf9\u8c61\u4e00\u6837\uff0c\u88ab\u8c03\u7528\u8005\u7684\u4fee\u6539\u53ef\u4ee5\u5bf9\u5916\u90e8\u53ef\u89c1\uff0c\u90a3\u5c82\u4e0d\u662f\u6211\u6bcf\u6b21\u8c03\u7528\u4e00\u4e2a\u4ee5 String \u4e3a\u53c2\u6570\u7684\u51fd\u6570\u65f6\uff0c\u90fd\u9700\u8981\u64cd\u5fc3\uff1a\u8fd9\u4e2a\u51fd\u6570\u4f1a\u4e0d\u4f1a\u628a\u6211\u7684\u5b57\u7b26\u4e32\u4fee\u6539\u6389\uff1f \u6240\u4ee5\uff0cJava \u7ed9\u4ed6\u7684\u5bf9\u8c61\u6a21\u578b\u5f00\u4e86\u4e2a\u540e\u95e8\uff1a\u89c4\u5b9a\u6240\u6709\u5bf9\u8c61\u90fd\u662f\u6309\u5f15\u7528\u4f20\u9012\uff0c\u9664\u4e86 String \uff01\u5c31\u53ea\u6709 String \u662f\u4e0d\u53ef\u53d8\u5bf9\u8c61\uff0c\u88ab\u8c03\u7528\u8005\u5185\u90e8\u7684\u4fee\u6539\u5bf9\u5916\u4e0d\u53ef\u89c1\u3002 C++ \u548c Rust \u53ea\u9700\u8981\u52a0\u4e2a\u5e38\u5f15\u7528\u5c31\u884c\uff0c\u800c Java \u53d7\u5bb3\u8005\u8981\u8003\u8651\u7684\u5c31\u591a\u4e86\u3002 \u603b\u4e4b\uff0c\u8fd9\u5c31\u662f\u6ca1\u6709 const \u7684\u5783\u573e\u8bed\u8a00\u7684\u4e11\u6001\uff0c\u9700\u8981\u9760\u5404\u79cd\u8bed\u6cd5\u89c4\u5219\u4e0a\u5f00\u6d1e\u624d\u80fd\u5f25\u8865\u8bbe\u8ba1\u65f6\u8003\u8651\u4e0d\u5468\u7684\u7f3a\u9677\uff0c\u5c31\u4e3a\u4e86\u4f3a\u5019\u8fd9\u5e2e\u5f15\u7528\u90fd\u5f04\u4e0d\u660e\u767d\u7684\u5783\u573e\u5c0f\u767d\u3002 \u6211\u662f\u8bf4\u5783\u573e\u56de\u6536 (garbage-collect) \u8bed\u8a00\uff0c\u7b80\u79f0\u5783\u573e\u8bed\u8a00\u3002\u6ca1\u6709\u8bf4 Java \u5783\u573e\u7684\u610f\u601d\uff0c\u4f60\u8bf4\u5bf9\u5427\uff1f\u5783\u573e\u8bed\u8a00\u3002","title":"Java String"},{"location":"unicode/#cow","text":"\u62c5\u5fe7\uff1a\u90a3\u5c82\u4e0d\u662f\u6bcf\u6b21\u6211 += \u5b9e\u9645\u4e0a\u90fd\u767d\u767d\u6df1\u62f7\u8d1d\u4e86\u4e00\u4efd\u65b0\u7684 String \uff1f\u522b\u62c5\u5fc3\uff0c\u56e0\u4e3a\u5177\u4f53\u5b9e\u73b0\u4e0a\uff0cJava \u7684 String \u5728\u5e95\u5c42\u91c7\u7528\u4e86\u548c Qt \u7684 QString \u4e00\u6837\u7684 COW \u673a\u5236\uff1a \u5f53\u4e00\u4e2a QString \u88ab\u62f7\u8d1d\u6784\u9020\u65f6\uff0c\u5e76\u4e0d\u4f1a\u5bf9\u5176\u4e2d\u7684 QByteArray \u8fdb\u884c\u6df1\u62f7\u8d1d\uff0c\u800c\u662f\u5171\u4eab\u540c\u4e00\u7247\u5185\u5b58\u3002\u53ea\u6709\u5f53\u5176\u4e2d\u4e00\u4e2a QString \u88ab += \u7b49\u5e26\u6709\u526f\u4f5c\u7528\u64cd\u4f5c\u4fee\u6539\u65f6\uff0c\u624d\u4f1a\u6df1\u62f7\u8d1d\u4e00\u4efd\u65b0\u7684\uff0c\u8ba9\u4f60\u4fee\u6539\u3002\u8fd9\u6837\u5927\u5927\u964d\u4f4e\u4e86\u5185\u5b58\u5360\u7528\u548c\u6027\u80fd\u5f00\u9500\u3002 COW \u5b57\u7b26\u4e32\u7684\u7f3a\u70b9\u662f\uff1a\u5f53\u4f60\u5199\u591a\u7ebf\u7a0b\u5e76\u53d1\u65f6\uff0c\u672c\u6765\u591a\u7ebf\u7a0b\u53ea\u8bfb\u8bbf\u95ee\u540c\u4e00\u4e2a\u5b57\u7b26\u4e32\u662f\u5b89\u5168\u7684\uff0c\u4f46\u5982\u679c\u5b57\u7b26\u4e32\u6709 COW\uff0c\u8fde\u53ea\u8bfb\u8bbf\u95ee\u90fd\u4f1a\u4e0d\u5b89\u5168\u4e86\u3002\u4e4b\u540e\u6211\u4eec\u7684\u591a\u7ebf\u7a0b\u4e13\u9898\u8bfe\u4f1a\u8be6\u7ec6\u5206\u6790\u8fd9\u662f\u4e3a\u4ec0\u4e48\u3002 \u5176\u5b9e\u65e9\u671f\u7684 C++98 \u4e2d\uff0c std::string \u4e5f\u91c7\u7528\u4e86 COW \u673a\u5236\uff0c\u4f46\u540e\u6765\u56e0\u4e3a\u4e0d\u7b26\u5408\u7ebf\u7a0b\u5b89\u5168\u8981\u6c42\uff0c\u88ab\u8ffd\u6c42\u591a\u7ebf\u7a0b\u7684 C++11 \u8d23\u4ee4\u6539\u6b63\uff0c\u624d\u6709\u4e86\u540e\u6765 std::__cxx11::basic_string \u7684 ABI \u4e0d\u517c\u5bb9\u95ee\u9898\u3002\u6bd5\u7adf\u7ebf\u7a0b\u4e0d\u5b89\u5168\u5b9e\u5728\u592a\u4f24\u4e86\uff0c\u57fa\u672c\u610f\u5473\u7740\u591a\u7ebf\u7a0b\u5c31\u6ca1\u6cd5\u5171\u4eab std::string \u3002\u4e8b\u5b9e\u4e0a\uff0cQt \u6240\u6709\u7684\u5bf9\u8c61 QObject \uff0c\u5305\u62ec QString \u5728\u591a\u7ebf\u7a0b\u4e2d\u4f20\u9012\u65f6\uff0c\u5c31\u9700\u8981\u8c03\u7528 moveTo(QThread) \u8f6c\u79fb\u6240\u6709\u6743\uff0c\u624d\u80fd\u5b89\u5168\u5730\u4f20\u9012\u7ed9\u53e6\u4e00\u4e2a\u7ebf\u7a0b\uff0c\u5c31\u662f\u56e0\u4e3a Qt \u5927\u91cf\u4f7f\u7528\u4e86 COW \u673a\u5236\u3002","title":"COW \u5b57\u7b26\u4e32"},{"location":"unicode/#unicode_1","text":"","title":"Unicode \u77e5\u8bc6\u8fdb\u9636"},{"location":"unicode/#_23","text":"","title":"\u5b57\u7b26\u7684\u663e\u793a\u5bbd\u5ea6\u8ba1\u7b97"},{"location":"unicode/#grapheme","text":"TODO","title":"Grapheme"},{"location":"unicode/#_24","text":"TODO","title":"\u6b63\u89c4\u5316"},{"location":"unicode/#_25","text":"TODO","title":"\u96f6\u5bbd\u7a7a\u683c"},{"location":"unicode/#_26","text":"TODO","title":"\u7279\u6b8a\u63a7\u5236\u5b57\u7b26"},{"location":"unicode/#unicode_2","text":"TODO","title":"\u6839\u636e\u7f16\u53f7\u8f93\u5165 Unicode \u5b57\u7b26"},{"location":"unicode/#_27","text":"","title":"\u9ed1\u6697\u5c0f\u6280\u5de7"},{"location":"unicode/#latin-1_1","text":"Latin-1 \u662f\u4e00\u4e2a 8 \u4f4d\u7f16\u7801\uff0c\u80fd\u8868\u793a 256 \u4e2a\u5b57\u7b26\uff0c\u5305\u62ec\u4e86\u62c9\u4e01\u5b57\u6bcd\u3001\u963f\u62c9\u4f2f\u6570\u5b57\u3001\u6807\u70b9\u7b26\u53f7\u3001\u5e38\u7528\u7684\u897f\u6b27\u5b57\u7b26\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7279\u6b8a\u5b57\u7b26\u3002 \u56e0\u6b64\uff0c\u5982\u679c\u4f60\u9700\u8981\u628a\u4e00\u4e2a Latin-1 \u7f16\u7801\u7684 char \u5b57\u7b26\u4e32\u8f6c\u6362\u4e3a wchar_t \u5b57\u7b26\u4e32\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u63a5\u53e3\u6784\u9020 std::wstring \uff0c\u8fd9\u6837 char \u4f1a\u88ab\u9010\u4e2a\u8f6c\u6362\u4e3a wchar_t \u3002 std::string latin1 = \"I love P\\xE9ng\"; // 0xE9: \u00e9 std::wstring wstr(latin1.begin(), latin1.end()); std::wcout << wstr << '\\n'; \u8f93\u51fa\uff1a I love P\u00e9ng \u5e76\u4e0d\u6807\u51c6\u7684\u505a\u6cd5\uff0c\u8fd8\u662f\u5efa\u8bae\u7528 boost::locale::conv::to_utf(latin1, \"Latin-1\") \u3002","title":"Latin-1 \u7684\u8f6c\u6362"},{"location":"unicode/#latin-1_2","text":"\u7531\u4e8e Latin-1 \u8986\u76d6\u4e86\u6240\u6709\u7684 256 \u4e2a char \u7684\u53ef\u80fd\u503c\uff0c\u4efb\u4f55\u5b57\u8282\u6d41\u90fd\u53ef\u4ee5\u6210\u529f\u89e3\u7801\u3002 GBK \u548c UTF-8 \u6709\u81ea\u7ea0\u9519\u6027\uff0c\u6709\u4e9b\u8f93\u5165\u4f1a\u88ab\u584c\u7f29\u6210\u9519\u8bef\u201c\ufffd\u201d\u3002Latin-1 \u5c31\u6ca1\u6709\u8fd9\u4e2a\u95ee\u9898\uff0c\u4ed6\u7167\u5355\u5168\u6536\uff01 \u56e0\u6b64\u6709\u65f6\uff0c\u4eba\u4eec\u53ef\u4ee5\u6b3a\u9a97\u4e00\u4e2a\u7f16\u7801\u5668\u8bf4\u201c\u6211\u91c7\u7528\u7684\u5b57\u7b26\u7f16\u7801\u662f Latin-1\u201d\uff01\u8fd9\u6837\u7f16\u7801\u5668\u5c31\u4e0d\u4f1a\u5bf9\u8f93\u5165\u7684\u5b57\u8282\u6d41\u505a\u4efb\u4f55\u8f6c\u6362\uff0c\u4ece\u800c\u53ef\u4ee5\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u5f53\u6587\u672c\u6765\u4f20\uff0c\u89e3\u7801\u65f6\u4e5f\u6307\u5b9a Latin-1\uff0c\u539f\u539f\u672c\u672c\u7684\u53d6\u51fa\u6570\u636e\u3002","title":"Latin-1 \u7684\u5999\u7528"},{"location":"unicode/#base64","text":"\u5982\u679c\u8981\u628a\u4e00\u4e32\u4e2d\u6587\u8f93\u5165\u4e00\u4e2a\u4e0d\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\uff0c\u53d1\u9001\u8ba9\u5bf9\u65b9\u6536\u5230\uff0c\u600e\u4e48\u529e\uff1f \u53ef\u4ee5\u7528 Latin-1 \u7f16\u7801\uff0c\u9a97\u8fd9\u4e2a\u8f6f\u4ef6\uff0c\u8ba9\u4ed6\u4ee5\u4e3a\u81ea\u5df1\u6536\u5230\u7684\u662f Latin-1 \u5b57\u7b26\u4e32\uff0c\u53cd\u6b63\u4ed6\u4e5f\u4e0d\u770b\u5185\u5bb9\uff0c\u4ece\u800c\u8ba9\u4ed6\u4e0d\u8981\u505a\u4efb\u4f55\u8f6c\u6362\u64cd\u4f5c\u3002 \u4e0d\u8fc7\u6709\u65f6\u5019\uff0c\u6587\u672c\u6846\u65e0\u6cd5\u8f93\u5165\u90e8\u5206\u7279\u6b8a\u7684\u63a7\u5236\u5b57\u7b26\uff0c\u800c UTF-8 \u5b57\u7b26\u4e32\u7f16\u7801\u51fa\u6765\u7684\u6587\u672c\uff0c\u8d85\u8fc7 0x80 \u7684\u90e8\u5206\uff0c\u53ef\u80fd\u843d\u5165 Latin-1 \u7684\u63a7\u5236\u5b57\u7b26\u4e2d\uff0c\u88ab\u8fd9\u4e2a\u8f6f\u4ef6\u9519\u8bef\u5730\u505a\u4e86\u7279\u6b8a\u5904\u7406\u3002 \u4e3a\u4e86\u907f\u514d\u53ea\u517c\u5bb9\u4e86 ASCII \u7684\u843d\u540e\u8f6f\u4ef6\u7834\u574f\u6211\u4eec\u7684\u5b57\u7b26\uff0c\u5bf9\u4e8e\u8fd9\u79cd\u53ea\u652f\u6301 ASCII \u6587\u672c\u7684\u7f16\u8f91\u6846\uff0c\u6211\u4eec\u53ef\u4ee5\u7528 Base64 \u7f16\u7801\u5148\u628a\u4efb\u610f\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u3002 Base64 \u662f\u4e00\u79cd\u628a\u4e8c\u8fdb\u5236\u6570\u636e\u8f6c\u6362\u4e3a ASCII \u5b57\u7b26\u4e32\u7684\u7b97\u6cd5\uff0c\u539f\u7406\u5f88\u7b80\u5355\uff0c\u5c31\u662f\u628a\u6bcf 6 \u4e2a\u4e8c\u8fdb\u5236\u4f4d\u8f6c\u6362\u4e3a\u4e00\u4e2a\u53ef\u6253\u5370\u7684 ASCII \u5b57\u7b26\uff08\u7528 A-Z a-z 0-9 - / \u8fd9 64 \u4e2a\u5b57\u7b26\u8868\u793a\uff09\u3002\u56e0\u6b64\uff0cBase64 \u7f16\u7801\u540e\u7684\u6587\u672c\uff0c\u6bcf 4 \u4e2a\u5b57\u7b26\u5c31\u6709 3 \u4e2a\u662f\u6709\u6548\u5b57\u7b26\uff0c\u5269\u4e0b\u7684 1 \u4e2a\u5b57\u7b26\u662f\u586b\u5145\u5b57\u7b26 = \u3002 \u4f8b\u5982\uff0c\u5b57\u7b26\u4e32 \"\u5c0f\u5f6d\u8001\u5e08\" \uff0c\u4f60\u53ef\u80fd\u60f3\u8981\u628a\u5b83\u901a\u8fc7\u90ae\u4ef6\u53d1\u51fa\u53bb\u3002\u800c\u8fd9\u4e2a\u90ae\u4ef6\u670d\u52a1\u5668\u4e0d\u652f\u6301 UTF-8 \u4e5f\u4e0d\u652f\u6301 GBK\uff0c\u53ea\u652f\u6301 ASCII\uff01 \u9996\u5148\u6211\u4eec\u7528 UTF-8 \u7f16\u7801\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff1a 0xE5 0xB0 0x8F 0xE5 0xBD 0xAD 0xE8 0x80 0x81 0xE5 0xB8 0x88 \u7136\u540e\u518d\u7528 Base64 \u4e8c\u6b21\u7f16\u7801\u6210\u666e\u901a\u7684\u53ef\u6253\u5370\u5b57\u6bcd\u548c\u6570\u5b57\u5e8f\u5217\uff1a 5bCP5b2t6ICB5biI \u5bf9\u65b9\u6536\u5230\u8fd9\u4e32\u795e\u79d8\u5b57\u7b26\u540e\uff0c\u518d\u7528 base64 \u89e3\u7801\uff0c\u5f97\u5230\u4e8c\u8fdb\u5236\u5b57\u8282\u6d41\uff0c\u518d\u7528\u652f\u6301 UTF-8 \u7684\u8f6f\u4ef6\u89e3\u7801\uff0c\u5c31\u80fd\u770b\u5230\u672c\u6765\u7684\u4e2d\u6587\u4e86\u3002 # \u53d1\u9001\u8005\uff1a import base64 secret = base64.b64encode(\"\u5c0f\u5f6d\u8001\u5e08\".encode()) # \u63a5\u6536\u8005\uff1a base64.b64decode(secret).decode() \u8fd9\u4e2a\u65b9\u6cd5\u4e0d\u4ec5\u53ef\u4ee5\u7f16\u7801 UTF-8 \u5b57\u7b26\u4e32\uff0c\u8fd8\u53ef\u4ee5\u4f20\u8f93\u4efb\u610f\u975e\u6587\u672c\u7684\u6587\u4ef6\uff01\u4f8b\u5982\uff0c\u6709\u4eba\u5229\u7528 Base64 \u7f16\u7801\uff0c\u628a jpg \u56fe\u50cf\u6587\u4ef6\u76f4\u63a5\u5185\u5d4c\u5728 md \u6587\u4ef6\u91cc\uff01\uff08md \u6587\u4ef6\u53ea\u652f\u6301\u5305\u542b\u5408\u6cd5\u7684 UTF-8 \u6587\u672c\uff0c\u4e0d\u53ef\u80fd\u5305\u542b jpg \u7684\u4efb\u610f\u5b57\u8282\u6d41\uff0c\u56e0\u6b64\u53ea\u80fd\u7528 Base64 \u5148\u7f16\u7801\u6210 ASCII \u8303\u56f4\u5185\u7684\u5b57\u6bcd\u548c\u6570\u5b57\uff0c\u9632\u6b62 md \u7f16\u8bd1\u5668\u62a5 UTF-8 \u89e3\u7801\u9519\u8bef\uff09 \u603b\u4e4b\uff0c\u5982\u679c\u4f60\u8f93\u5165\u4e2d\u6587\u5b9e\u5728\u6709\u95ee\u9898\uff0c\u53ef\u4ee5\u8003\u8651\u5148 Base64 \u8f6c\u6362\u6210\u7eaf\u82f1\u6587\u8bd5\u8bd5\u770b\uff0c\u53cd\u6b63\u65e0\u8bba\u8c01\u90fd\u517c\u5bb9 ASCII\u3002\u5982\u679c\u8fd9\u4e2a\u6587\u672c\u6846\u4e0d\u533a\u5206\u5927\u5c0f\u5199\uff0c\u8fd8\u53ef\u4ee5\u8bd5\u8bd5\u770b\u53ea\u6709 A-Z 0-9 \u7684 Base32 \u7f16\u7801\u3002","title":"Base64 \u9632\u4e71\u7801"},{"location":"vartypes/","text":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) \u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) TODO","title":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5)"},{"location":"vartypes/#_1","text":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5) TODO","title":"\u53d8\u91cf\u4e0e\u7c7b\u578b (\u672a\u5b8c\u5de5)"}]} \ No newline at end of file diff --git a/unicode/index.html b/unicode/index.html index c48a238..e3c6a93 100644 --- a/unicode/index.html +++ b/unicode/index.html @@ -1192,7 +1192,7 @@

u8 字符串常量的作用

“你好” -运行字符集 (ANSI) +运行字符集 (默认是开发者电脑的“区域设置”,而不是客户的) const char [] @@ -1217,6 +1217,9 @@

u8 字符串常量的作用

+
+

只不过是开发者和客户往往处于同一个地区,所以 "你好" 看起来好像可以直接输入到 std::cout 中一样。实际上他只是开发者电脑的 ANSI,而不是客户电脑的 ANSI,如果直接拿来打印,会导致以 "" 常量形式写死的字符串会在客户电脑上出现乱码。除非这个字符串只包含 ASCII,因为所有 ANSI 都兼容 ASCII,才恰好避免了乱码。

+

选择你的阵营!

ANSI 阵营