{"id":66996,"date":"2026-01-27T23:41:18","date_gmt":"2026-01-27T15:41:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/66996.html"},"modified":"2026-01-27T23:41:18","modified_gmt":"2026-01-27T15:41:18","slug":"c%e4%b8%89%e5%a4%a7%e5%85%b3%e8%81%94%e5%ae%b9%e5%99%a8%e5%85%a8%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/66996.html","title":{"rendered":"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790"},"content":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u8be6\u7ec6\u89e3\u6790 C&#043;&#043; \u4e2d\u7684\u4e09\u79cd\u91cd\u8981\u5173\u8054\u5f0f\u5bb9\u5668&#xff1a;std::map, std::set, \u548c std::unordered_map\u3002\u5173\u8054\u5f0f\u5bb9\u5668\u5b58\u50a8\u7684\u662f\u952e\u503c\u5bf9&#xff08;Key-Value&#xff09;\u6216\u5355\u72ec\u7684\u952e&#xff08;Key&#xff09;&#xff0c;\u5e76\u5141\u8bb8\u57fa\u4e8e\u952e\u8fdb\u884c\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002<\/p>\n<h4>1. std::map (\u6709\u5e8f\u6620\u5c04)<\/h4>\n<p>\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>\u5e95\u5c42\u7ed3\u6784&#xff1a; \u901a\u5e38\u57fa\u4e8e\u7ea2\u9ed1\u6811&#xff08;Red-Black Tree&#xff09;\u5b9e\u73b0\u3002\u7ea2\u9ed1\u6811\u662f\u4e00\u79cd\u81ea\u5e73\u8861\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff08;BST&#xff09;\u3002<\/li>\n<li>\u5173\u952e\u7279\u6027&#xff1a;\n<ul>\n<li>\u6392\u5e8f&#xff1a; \u5143\u7d20&#xff08;\u952e\u503c\u5bf9&#xff09;\u59cb\u7ec8\u6309\u952e&#xff08;Key&#xff09;\u7684\u5347\u5e8f&#xff08;\u9ed8\u8ba4&#xff09;\u6216\u6307\u5b9a\u7684\u6bd4\u8f83\u89c4\u5219\u4e25\u683c\u6392\u5e8f\u3002\u63d2\u5165\u65b0\u5143\u7d20\u65f6&#xff0c;\u5bb9\u5668\u4f1a\u81ea\u52a8\u5c06\u5176\u653e\u7f6e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u4ee5\u7ef4\u6301\u6392\u5e8f\u3002<\/li>\n<li>\u552f\u4e00\u952e&#xff1a; \u5bb9\u5668\u4e2d\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5c1d\u8bd5\u63d2\u5165\u5177\u6709\u76f8\u540c\u952e\u7684\u5143\u7d20\u4f1a\u88ab\u5ffd\u7565&#xff08;\u6216\u8986\u76d6\u503c&#xff0c;\u53d6\u51b3\u4e8e\u63d2\u5165\u65b9\u6cd5&#xff09;\u3002<\/li>\n<li>\u64cd\u4f5c\u590d\u6742\u5ea6&#xff1a; \u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u548c\u6700\u574f\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a $O(\\\\log n)$&#xff0c;\u5176\u4e2d $n$ \u662f\u5bb9\u5668\u4e2d\u5143\u7d20\u7684\u6570\u91cf\u3002\u8fd9\u662f\u56e0\u4e3a\u7ea2\u9ed1\u6811\u7684\u9ad8\u5ea6\u5927\u81f4\u662f $\\\\log n$\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u7ea2\u9ed1\u6811\u7279\u6027&#xff08;\u7b80\u8ff0&#xff09;&#xff1a; \u7ea2\u9ed1\u6811\u901a\u8fc7\u7279\u5b9a\u7684\u7740\u8272\u89c4\u5219\u548c\u65cb\u8f6c\u64cd\u4f5c\u6765\u7ef4\u6301\u8fd1\u4f3c\u5e73\u8861&#xff0c;\u786e\u4fdd\u6811\u7684\u9ad8\u5ea6\u4e0d\u4f1a\u9000\u5316\u4e3a\u7ebf\u6027&#xff0c;\u4ece\u800c\u4fdd\u8bc1\u4e86 $O(\\\\log n)$ \u7684\u64cd\u4f5c\u6548\u7387\u3002\u5b83\u6ee1\u8db3\u4ee5\u4e0b\u7ea6\u675f&#xff1a;\n<li>\u6bcf\u4e2a\u8282\u70b9\u975e\u7ea2\u5373\u9ed1\u3002<\/li>\n<li>\u6839\u8282\u70b9\u662f\u9ed1\u7684\u3002<\/li>\n<li>\u6bcf\u4e2a\u53f6\u5b50\u8282\u70b9&#xff08;NIL\u8282\u70b9&#xff09;\u662f\u9ed1\u7684\u3002<\/li>\n<li>\u5982\u679c\u4e00\u4e2a\u8282\u70b9\u662f\u7ea2\u7684&#xff0c;\u5219\u5176\u5b50\u8282\u70b9\u5fc5\u987b\u662f\u9ed1\u7684\u3002<\/li>\n<li>\u4ece\u4efb\u4e00\u8282\u70b9\u5230\u5176\u6bcf\u4e2a\u53f6\u5b50\u8282\u70b9\u7684\u6240\u6709\u8def\u5f84\u4e0a\u5305\u542b\u76f8\u540c\u6570\u76ee\u7684\u9ed1\u8282\u70b9\u3002<\/li>\n<\/li>\n<\/ul>\n<p>\u5e94\u7528&#xff1a;<\/p>\n<ul>\n<li>\u9700\u8981\u6309\u952e\u6392\u5e8f\u904d\u5386\u5143\u7d20\u7684\u573a\u666f\u3002<\/li>\n<li>\u9700\u8981\u9891\u7e41\u6309\u952e\u8fdb\u884c\u7cbe\u786e\u67e5\u627e&#xff08;find()&#xff09;\u3001\u63d2\u5165\u548c\u5220\u9664\u7684\u573a\u666f\u3002<\/li>\n<li>\u5b58\u50a8\u552f\u4e00\u952e\u53ca\u5176\u5173\u8054\u503c\u7684\u6620\u5c04\u5173\u7cfb\u3002\u4f8b\u5982&#xff1a;\u5b66\u751fID\u6620\u5c04\u5230\u5b66\u751f\u4fe1\u606f&#xff0c;\u5355\u8bcd\u6620\u5c04\u5230\u5176\u51fa\u73b0\u9891\u7387\u3002<\/li>\n<li>\u9700\u8981\u83b7\u53d6\u67d0\u4e2a\u952e\u8303\u56f4\u5185\u7684\u5143\u7d20&#xff08;\u5982 lower_bound(), upper_bound()&#xff09;\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;map&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a map&#xff0c;\u952e\u662f string&#xff0c;\u503c\u662f int<br \/>\n    std::map&lt;std::string, int&gt; studentScores;<\/p>\n<p>    \/\/ \u63d2\u5165\u5143\u7d20<br \/>\n    studentScores[&#034;Alice&#034;] &#061; 95;<br \/>\n    studentScores[&#034;Bob&#034;] &#061; 87;<br \/>\n    studentScores.insert({&#034;Charlie&#034;, 92}); \/\/ \u53e6\u4e00\u79cd\u63d2\u5165\u65b9\u5f0f<\/p>\n<p>    \/\/ \u67e5\u627e\u5143\u7d20<br \/>\n    auto it &#061; studentScores.find(&#034;Bob&#034;);<br \/>\n    if (it !&#061; studentScores.end()) {<br \/>\n        std::cout &lt;&lt; &#034;Bob&#039;s score: &#034; &lt;&lt; it-&gt;second &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u904d\u5386 (\u6309\u952e\u6392\u5e8f)<br \/>\n    for (const auto&amp; pair : studentScores) {<br \/>\n        std::cout &lt;&lt; pair.first &lt;&lt; &#034;: &#034; &lt;&lt; pair.second &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>2. std::set (\u6709\u5e8f\u96c6\u5408)<\/h4>\n<p>\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>\u5e95\u5c42\u7ed3\u6784&#xff1a; \u4e0e std::map \u7c7b\u4f3c&#xff0c;\u901a\u5e38\u57fa\u4e8e\u7ea2\u9ed1\u6811\u5b9e\u73b0\u3002<\/li>\n<li>\u5173\u952e\u7279\u6027&#xff1a;\n<ul>\n<li>\u6392\u5e8f&#xff1a; \u5143\u7d20&#xff08;\u952e\u672c\u8eab&#xff09;\u59cb\u7ec8\u6309\u5347\u5e8f&#xff08;\u9ed8\u8ba4&#xff09;\u6216\u6307\u5b9a\u7684\u6bd4\u8f83\u89c4\u5219\u4e25\u683c\u6392\u5e8f\u3002\u63d2\u5165\u65b0\u5143\u7d20\u65f6&#xff0c;\u5bb9\u5668\u4f1a\u81ea\u52a8\u5c06\u5176\u653e\u7f6e\u5728\u6b63\u786e\u7684\u4f4d\u7f6e\u4ee5\u7ef4\u6301\u6392\u5e8f\u3002<\/li>\n<li>\u552f\u4e00\u952e&#xff1a; \u5bb9\u5668\u4e2d\u7684\u952e&#xff08;\u5143\u7d20&#xff09;\u5fc5\u987b\u662f\u552f\u4e00\u7684\u3002\u5c1d\u8bd5\u63d2\u5165\u91cd\u590d\u5143\u7d20\u4f1a\u88ab\u5ffd\u7565\u3002<\/li>\n<li>\u64cd\u4f5c\u590d\u6742\u5ea6&#xff1a; \u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u548c\u6700\u574f\u65f6\u95f4\u590d\u6742\u5ea6\u901a\u5e38\u4e3a $O(\\\\log n)$\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u4e0e map \u7684\u533a\u522b&#xff1a; std::set \u53ea\u5b58\u50a8\u952e&#xff08;Key&#xff09;&#xff0c;\u4e0d\u5b58\u50a8\u663e\u5f0f\u7684\u503c&#xff08;Value&#xff09;\u3002\u952e\u672c\u8eab\u88ab\u89c6\u4e3a\u503c\u3002\u4f60\u53ef\u4ee5\u5c06\u5176\u770b\u4f5c\u4e00\u4e2a\u952e\u552f\u4e00\u7684\u3001\u81ea\u52a8\u6392\u5e8f\u7684\u96c6\u5408\u3002<\/li>\n<\/ul>\n<p>\u5e94\u7528&#xff1a;<\/p>\n<ul>\n<li>\u9700\u8981\u5b58\u50a8\u552f\u4e00\u5143\u7d20\u96c6\u5408\u5e76\u6309\u952e\u6392\u5e8f\u904d\u5386\u7684\u573a\u666f\u3002<\/li>\n<li>\u9700\u8981\u9891\u7e41\u68c0\u67e5\u4e00\u4e2a\u5143\u7d20\u662f\u5426\u5b58\u5728\u4e8e\u96c6\u5408\u4e2d&#xff08;find(), count()&#xff09;\u3002<\/li>\n<li>\u9700\u8981\u96c6\u5408\u8fd0\u7b97\u7684\u573a\u666f&#xff08;\u867d\u7136\u6807\u51c6\u5e93\u6ca1\u6709\u76f4\u63a5\u63d0\u4f9b&#xff0c;\u4f46\u53ef\u4ee5\u901a\u8fc7\u8fed\u4ee3\u5668\u64cd\u4f5c\u5b9e\u73b0\u4ea4\u96c6\u3001\u5e76\u96c6\u7b49&#xff09;\u3002<\/li>\n<li>\u4f8b\u5982&#xff1a;\u5b58\u50a8\u7528\u6237ID\u5217\u8868\u3001\u5b58\u50a8\u8bcd\u5178\u4e2d\u7684\u6240\u6709\u552f\u4e00\u5355\u8bcd\u3001\u5b58\u50a8\u8fdb\u7a0bID\u5217\u8868\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;set&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a set&#xff0c;\u5b58\u50a8\u6574\u6570<br \/>\n    std::set&lt;int&gt; uniqueNumbers;<\/p>\n<p>    \/\/ \u63d2\u5165\u5143\u7d20<br \/>\n    uniqueNumbers.insert(42);<br \/>\n    uniqueNumbers.insert(17);<br \/>\n    uniqueNumbers.insert(42); \/\/ \u91cd\u590d&#xff0c;\u4f1a\u88ab\u5ffd\u7565<\/p>\n<p>    \/\/ \u68c0\u67e5\u5143\u7d20\u662f\u5426\u5b58\u5728<br \/>\n    if (uniqueNumbers.find(17) !&#061; uniqueNumbers.end()) {<br \/>\n        std::cout &lt;&lt; &#034;17 is in the set.&#034; &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u904d\u5386 (\u6309\u952e\u6392\u5e8f)<br \/>\n    for (int num : uniqueNumbers) {<br \/>\n        std::cout &lt;&lt; num &lt;&lt; &#034; &#034;;<br \/>\n    }<br \/>\n    std::cout &lt;&lt; std::endl;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>3. std::unordered_map (\u65e0\u5e8f\u6620\u5c04\/\u54c8\u5e0c\u8868)<\/h4>\n<p>\u539f\u7406&#xff1a;<\/p>\n<ul>\n<li>\u5e95\u5c42\u7ed3\u6784&#xff1a; \u57fa\u4e8e\u54c8\u5e0c\u8868&#xff08;Hash Table&#xff09;\u5b9e\u73b0\u3002<\/li>\n<li>\u5173\u952e\u7279\u6027&#xff1a;\n<ul>\n<li>\u65e0\u5e8f&#xff1a; \u5143\u7d20\u4e0d\u6309\u4efb\u4f55\u7279\u5b9a\u987a\u5e8f\u5b58\u50a8\u3002\u5143\u7d20\u7684\u987a\u5e8f\u53d6\u51b3\u4e8e\u54c8\u5e0c\u51fd\u6570\u548c\u89e3\u51b3\u51b2\u7a81\u7684\u65b9\u5f0f&#xff0c;\u901a\u5e38\u662f\u4e0d\u53ef\u9884\u6d4b\u7684\u3002<\/li>\n<li>\u552f\u4e00\u952e&#xff1a; \u5bb9\u5668\u4e2d\u7684\u952e\u5fc5\u987b\u662f\u552f\u4e00\u7684&#xff08;\u9664\u975e\u4f7f\u7528 unordered_multimap&#xff09;\u3002<\/li>\n<li>\u64cd\u4f5c\u590d\u6742\u5ea6&#xff1a; \u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u64cd\u4f5c\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u5728\u7406\u60f3\u60c5\u51b5\u4e0b&#xff08;\u54c8\u5e0c\u51fd\u6570\u5206\u5e03\u5747\u5300&#xff0c;\u51b2\u7a81\u5c11&#xff09;\u4e3a $O(1)$&#xff08;\u5e38\u6570\u65f6\u95f4&#xff09;\u3002\u4f46\u5728\u6700\u574f\u60c5\u51b5\u4e0b&#xff08;\u6240\u6709\u5143\u7d20\u54c8\u5e0c\u5230\u540c\u4e00\u4e2a\u6876&#xff09;&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u5230 $O(n)$\u3002\u5b9e\u9645\u6027\u80fd\u9ad8\u5ea6\u4f9d\u8d56\u4e8e\u54c8\u5e0c\u51fd\u6570\u7684\u8d28\u91cf\u548c\u8d1f\u8f7d\u56e0\u5b50\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u54c8\u5e0c\u8868\u5de5\u4f5c\u539f\u7406&#xff1a;\n<li>\u54c8\u5e0c\u51fd\u6570&#xff1a; \u5bf9\u952e&#xff08;Key&#xff09;\u5e94\u7528\u54c8\u5e0c\u51fd\u6570&#xff0c;\u8ba1\u7b97\u51fa\u4e00\u4e2a\u54c8\u5e0c\u503c&#xff08;\u901a\u5e38\u662f\u4e00\u4e2a size_t \u7c7b\u578b\u7684\u6574\u6570&#xff09;\u3002<\/li>\n<li>\u6876\u6620\u5c04&#xff1a; \u6839\u636e\u54c8\u5e0c\u503c&#xff08;\u901a\u5e38\u53d6\u6a21\u6876\u7684\u6570\u91cf&#xff09;\u786e\u5b9a\u8be5\u952e\u503c\u5bf9\u5e94\u8be5\u5b58\u50a8\u5728\u54ea\u4e2a\u201c\u6876\u201d&#xff08;bucket&#xff09;\u4e2d\u3002<\/li>\n<li>\u51b2\u7a81\u89e3\u51b3&#xff1a; \u4e0d\u540c\u7684\u952e\u53ef\u80fd\u54c8\u5e0c\u5230\u540c\u4e00\u4e2a\u6876&#xff08;\u54c8\u5e0c\u51b2\u7a81&#xff09;\u3002C&#043;&#043; \u6807\u51c6\u5e93\u7684 unordered_map \u901a\u5e38\u4f7f\u7528\u94fe\u5730\u5740\u6cd5&#xff08;Separate Chaining&#xff09;\u89e3\u51b3\u51b2\u7a81&#xff1a;\u6bcf\u4e2a\u6876\u5185\u90e8\u7ef4\u62a4\u4e00\u4e2a\u94fe\u8868&#xff08;\u6216\u7c7b\u4f3c\u7ed3\u6784&#xff09;&#xff0c;\u6240\u6709\u54c8\u5e0c\u5230\u540c\u4e00\u4e2a\u6876\u7684\u5143\u7d20\u90fd\u5b58\u50a8\u5728\u8fd9\u4e2a\u94fe\u8868\u4e2d\u3002<\/li>\n<li>\u8d1f\u8f7d\u56e0\u5b50\u4e0e\u91cd\u65b0\u54c8\u5e0c&#xff1a; \u8d1f\u8f7d\u56e0\u5b50&#xff08;Load Factor&#xff09;\u5b9a\u4e49\u4e3a\u5143\u7d20\u6570\u91cf\u9664\u4ee5\u6876\u6570\u91cf\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u67d0\u4e2a\u9608\u503c&#xff08;\u9ed8\u8ba4\u901a\u5e38\u4e3a 1.0&#xff09;\u65f6&#xff0c;\u5bb9\u5668\u4f1a\u81ea\u52a8\u8fdb\u884c\u201c\u91cd\u65b0\u54c8\u5e0c\u201d&#xff08;Rehashing&#xff09;&#xff1a;\u521b\u5efa\u66f4\u591a\u7684\u65b0\u6876&#xff0c;\u91cd\u65b0\u8ba1\u7b97\u6240\u6709\u5143\u7d20\u7684\u54c8\u5e0c\u503c\u5e76\u5c06\u5b83\u4eec\u5206\u914d\u5230\u65b0\u7684\u6876\u4e2d\u3002\u8fd9\u4e2a\u8fc7\u7a0b\u5f00\u9500\u8f83\u5927&#xff0c;\u4f46\u6709\u52a9\u4e8e\u4fdd\u6301\u5e73\u5747 $O(1)$ \u7684\u64cd\u4f5c\u6027\u80fd\u3002<\/li>\n<\/li>\n<\/ul>\n<p>\u5e94\u7528&#xff1a;<\/p>\n<ul>\n<li>\u5bf9\u5143\u7d20\u7684\u5b58\u50a8\u987a\u5e8f\u6ca1\u6709\u8981\u6c42\u3002<\/li>\n<li>\u9700\u8981\u6781\u9ad8\u7684\u6309\u952e\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664\u901f\u5ea6\u7684\u573a\u666f&#xff0c;\u5c24\u5176\u662f\u5f53\u6570\u636e\u91cf\u5f88\u5927\u4e14\u54c8\u5e0c\u51fd\u6570\u826f\u597d\u65f6\u3002<\/li>\n<li>\u7f13\u5b58\u5b9e\u73b0\u3002<\/li>\n<li>\u5b58\u50a8\u952e\u503c\u5bf9\u6620\u5c04\u5173\u7cfb&#xff0c;\u4f46\u4e0d\u5173\u5fc3\u987a\u5e8f\u3002\u4f8b\u5982&#xff1a;\u5b58\u50a8URL\u5230\u7f51\u9875\u5185\u5bb9\u7684\u7f13\u5b58&#xff0c;\u5b58\u50a8\u7528\u6237\u540d\u5230\u7528\u6237\u914d\u7f6e\u7684\u6620\u5c04\u3002<\/li>\n<\/ul>\n<p>\u793a\u4f8b&#xff1a;<\/p>\n<p>#include &lt;iostream&gt;<br \/>\n#include &lt;unordered_map&gt;<br \/>\n#include &lt;string&gt;<\/p>\n<p>int main() {<br \/>\n    \/\/ \u521b\u5efa\u4e00\u4e2a unordered_map&#xff0c;\u952e\u662f string&#xff0c;\u503c\u662f int<br \/>\n    std::unordered_map&lt;std::string, int&gt; wordFrequency;<\/p>\n<p>    \/\/ \u63d2\u5165\u5143\u7d20<br \/>\n    wordFrequency[&#034;hello&#034;] &#061; 3;<br \/>\n    wordFrequency[&#034;world&#034;] &#061; 5;<br \/>\n    wordFrequency[&#034;hello&#034;]&#043;&#043;; \/\/ \u66f4\u65b0\u503c<\/p>\n<p>    \/\/ \u67e5\u627e\u5143\u7d20 (\u5e73\u5747 O(1))<br \/>\n    if (wordFrequency.find(&#034;world&#034;) !&#061; wordFrequency.end()) {<br \/>\n        std::cout &lt;&lt; &#034;Frequency of &#039;world&#039;: &#034; &lt;&lt; wordFrequency[&#034;world&#034;] &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    \/\/ \u904d\u5386 (\u987a\u5e8f\u4e0d\u786e\u5b9a)<br \/>\n    for (const auto&amp; pair : wordFrequency) {<br \/>\n        std::cout &lt;&lt; pair.first &lt;&lt; &#034;: &#034; &lt;&lt; pair.second &lt;&lt; std::endl;<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>\u5173\u952e\u5bf9\u6bd4\u603b\u7ed3<\/h4>\n<table>\n<tr>\u7279\u6027std::map (\u6709\u5e8f)std::set (\u6709\u5e8f)std::unordered_map (\u65e0\u5e8f)<\/tr>\n<tbody>\n<tr>\n<td style=\"text-align:left\">\u5e95\u5c42\u5b9e\u73b0<\/td>\n<td style=\"text-align:left\">\u7ea2\u9ed1\u6811 (\u5e73\u8861 BST)<\/td>\n<td style=\"text-align:left\">\u7ea2\u9ed1\u6811 (\u5e73\u8861 BST)<\/td>\n<td style=\"text-align:left\">\u54c8\u5e0c\u8868 (\u6876\u6570\u7ec4 &#043; \u94fe\u8868)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u5143\u7d20\u6392\u5e8f<\/td>\n<td style=\"text-align:left\">\u6309\u952e\u4e25\u683c\u6392\u5e8f (\u5347\u5e8f)<\/td>\n<td style=\"text-align:left\">\u6309\u952e(\u5143\u7d20)\u4e25\u683c\u6392\u5e8f (\u5347\u5e8f)<\/td>\n<td style=\"text-align:left\">\u65e0\u5e8f (\u4f9d\u8d56\u54c8\u5e0c)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u952e\u552f\u4e00\u6027<\/td>\n<td style=\"text-align:left\">\u552f\u4e00<\/td>\n<td style=\"text-align:left\">\u552f\u4e00<\/td>\n<td style=\"text-align:left\">\u552f\u4e00<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u5e73\u5747\u67e5\u627e\u65f6\u95f4<\/td>\n<td style=\"text-align:left\">$O(\\\\log n)$<\/td>\n<td style=\"text-align:left\">$O(\\\\log n)$<\/td>\n<td style=\"text-align:left\">$O(1)$<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u6700\u574f\u67e5\u627e\u65f6\u95f4<\/td>\n<td style=\"text-align:left\">$O(\\\\log n)$<\/td>\n<td style=\"text-align:left\">$O(\\\\log n)$<\/td>\n<td style=\"text-align:left\">$O(n)$<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u9700\u8981\u54c8\u5e0c\u51fd\u6570<\/td>\n<td style=\"text-align:left\">\u5426 (\u9700\u8981\u6bd4\u8f83\u51fd\u6570)<\/td>\n<td style=\"text-align:left\">\u5426 (\u9700\u8981\u6bd4\u8f83\u51fd\u6570)<\/td>\n<td style=\"text-align:left\">\u662f (\u9700\u8981std::hash)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u9700\u8981&lt;\u6bd4\u8f83<\/td>\n<td style=\"text-align:left\">\u662f<\/td>\n<td style=\"text-align:left\">\u662f<\/td>\n<td style=\"text-align:left\">\u5426<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u5185\u5b58\u4f7f\u7528<\/td>\n<td style=\"text-align:left\">\u901a\u5e38\u8f83\u9ad8 (\u6811\u8282\u70b9\u5f00\u9500)<\/td>\n<td style=\"text-align:left\">\u901a\u5e38\u8f83\u9ad8 (\u6811\u8282\u70b9\u5f00\u9500)<\/td>\n<td style=\"text-align:left\">\u901a\u5e38\u8f83\u4f4e (\u4f46\u6876\u6709\u5f00\u9500)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u8fed\u4ee3\u987a\u5e8f<\/td>\n<td style=\"text-align:left\">\u6309\u952e\u6392\u5e8f<\/td>\n<td style=\"text-align:left\">\u6309\u952e(\u5143\u7d20)\u6392\u5e8f<\/td>\n<td style=\"text-align:left\">\u672a\u5b9a\u4e49 (\u4f9d\u8d56\u54c8\u5e0c)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align:left\">\u9002\u7528\u573a\u666f<\/td>\n<td style=\"text-align:left\">\u9700\u6392\u5e8f\/\u8303\u56f4\u67e5\u8be2<\/td>\n<td style=\"text-align:left\">\u9700\u6392\u5e8f\/\u5b58\u5728\u6027\u68c0\u67e5<\/td>\n<td style=\"text-align:left\">\u9700\u6781\u5feb\u67e5\u627e\/\u4e0d\u5173\u5fc3\u987a\u5e8f<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>\u9009\u62e9\u6307\u5357<\/h4>\n<ul>\n<li>\u9700\u8981\u5143\u7d20\u6309\u952e\u6392\u5e8f\u3001\u8303\u56f4\u67e5\u8be2\u6216\u6709\u5e8f\u904d\u5386&#xff1f; \u9009\u62e9 std::map \u6216 std::set\u3002<\/li>\n<li>\u53ea\u9700\u8981\u5b58\u50a8\u552f\u4e00\u952e&#xff0c;\u4e0d\u9700\u8981\u5173\u8054\u503c&#xff1f; \u9009\u62e9 std::set\u3002<\/li>\n<li>\u9700\u8981\u952e\u503c\u5bf9\u6620\u5c04&#xff0c;\u4f46\u987a\u5e8f\u65e0\u5173\u7d27\u8981&#xff0c;\u4e14\u8ffd\u6c42\u6700\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u3001\u5220\u9664&#xff1f; \u9009\u62e9 std::unordered_map\u3002\u6ce8\u610f&#xff1a; \u786e\u4fdd\u4f60\u7684\u952e\u7c7b\u578b\u6709\u826f\u597d\u7684 std::hash \u7279\u5316\u6216\u81ea\u5b9a\u4e49\u54c8\u5e0c\u51fd\u6570\u3002<\/li>\n<li>\u5185\u5b58\u975e\u5e38\u7d27\u5f20&#xff1f; std::unordered_map \u53ef\u80fd\u66f4\u8282\u7701&#xff08;\u4f46\u9700\u8003\u8651\u6876\u7684\u5f00\u9500&#xff09;&#xff0c;std::map\/std::set \u7684\u8282\u70b9\u5f00\u9500\u66f4\u5927\u3002<\/li>\n<li>\u62c5\u5fc3\u6700\u574f\u60c5\u51b5\u6027\u80fd&#xff1f; std::map\/std::set \u7684 $O(\\\\log n)$ \u6700\u574f\u60c5\u51b5\u6bd4 std::unordered_map \u7684 $O(n)$ \u66f4\u53ef\u9884\u6d4b\u3002<\/li>\n<\/ul>\n<p>\u7406\u89e3\u8fd9\u4e9b\u5bb9\u5668\u7684\u5e95\u5c42\u539f\u7406\u548c\u7279\u6027\u5dee\u5f02&#xff0c;\u5bf9\u4e8e\u5728 C&#043;&#043; \u7a0b\u5e8f\u4e2d\u9ad8\u6548\u3001\u6b63\u786e\u5730\u4f7f\u7528\u5b83\u4eec\u81f3\u5173\u91cd\u8981\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u8be6\u7ec6\u89e3\u6790 C \u4e2d\u7684\u4e09\u79cd\u91cd\u8981\u5173\u8054\u5f0f\u5bb9\u5668&#xff1a;std::map, std::set, \u548c std::unordered_map\u3002\u5173\u8054\u5f0f\u5bb9\u5668\u5b58\u50a8\u7684\u662f\u952e\u503c\u5bf9&#xff08;Key-Value&#xff09;\u6216\u5355\u72ec\u7684\u952e&#xff08;Key&#xff09;&#xff0c;\u5e76\u5141\u8bb8\u57fa\u4e8e\u952e\u8fdb\u884c\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002<br \/>\n1. std::map (\u6709\u5e8f\u6620\u5c04)<br \/>\n\u539f\u7406&#xff1a;<br \/>\n\u5e95\u5c42\u7ed3\u6784&#xff1a; \u901a\u5e38\u57fa\u4e8e\u7ea2\u9ed1\u6811&#xff08;Red-Bla<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55],"topic":[],"class_list":["post-66996","post","type-post","status-publish","format-standard","hentry","category-server","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/66996.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u8be6\u7ec6\u89e3\u6790 C \u4e2d\u7684\u4e09\u79cd\u91cd\u8981\u5173\u8054\u5f0f\u5bb9\u5668&#xff1a;std::map, std::set, \u548c std::unordered_map\u3002\u5173\u8054\u5f0f\u5bb9\u5668\u5b58\u50a8\u7684\u662f\u952e\u503c\u5bf9&#xff08;Key-Value&#xff09;\u6216\u5355\u72ec\u7684\u952e&#xff08;Key&#xff09;&#xff0c;\u5e76\u5141\u8bb8\u57fa\u4e8e\u952e\u8fdb\u884c\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002 1. std::map (\u6709\u5e8f\u6620\u5c04) \u539f\u7406&#xff1a; \u5e95\u5c42\u7ed3\u6784&#xff1a; \u901a\u5e38\u57fa\u4e8e\u7ea2\u9ed1\u6811&#xff08;Red-Bla\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/66996.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-27T15:41:18+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66996.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/66996.html\",\"name\":\"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-27T15:41:18+00:00\",\"dateModified\":\"2026-01-27T15:41:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/66996.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/66996.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/66996.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/66996.html","og_locale":"zh_CN","og_type":"article","og_title":"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u597d\u7684&#xff0c;\u6211\u4eec\u6765\u8be6\u7ec6\u89e3\u6790 C \u4e2d\u7684\u4e09\u79cd\u91cd\u8981\u5173\u8054\u5f0f\u5bb9\u5668&#xff1a;std::map, std::set, \u548c std::unordered_map\u3002\u5173\u8054\u5f0f\u5bb9\u5668\u5b58\u50a8\u7684\u662f\u952e\u503c\u5bf9&#xff08;Key-Value&#xff09;\u6216\u5355\u72ec\u7684\u952e&#xff08;Key&#xff09;&#xff0c;\u5e76\u5141\u8bb8\u57fa\u4e8e\u952e\u8fdb\u884c\u9ad8\u6548\u7684\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u3002 1. std::map (\u6709\u5e8f\u6620\u5c04) \u539f\u7406&#xff1a; \u5e95\u5c42\u7ed3\u6784&#xff1a; \u901a\u5e38\u57fa\u4e8e\u7ea2\u9ed1\u6811&#xff08;Red-Bla","og_url":"https:\/\/www.wsisp.com\/helps\/66996.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-27T15:41:18+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"3 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/66996.html","url":"https:\/\/www.wsisp.com\/helps\/66996.html","name":"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-27T15:41:18+00:00","dateModified":"2026-01-27T15:41:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/66996.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/66996.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/66996.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"C++\u4e09\u5927\u5173\u8054\u5bb9\u5668\u5168\u89e3\u6790"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/66996","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=66996"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/66996\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=66996"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=66996"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=66996"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=66996"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}