{"id":34016,"date":"2025-04-28T22:09:37","date_gmt":"2025-04-28T14:09:37","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/34016.html"},"modified":"2025-04-28T22:09:37","modified_gmt":"2025-04-28T14:09:37","slug":"%e3%80%90c%e3%80%91-%e7%b2%be%e7%bb%86%e5%8c%96%e5%93%88%e5%b8%8c%e8%a1%a8%e6%9e%b6%e6%9e%84%ef%bc%9a%e7%90%86%e8%ae%ba%e4%b8%8e%e5%ae%9e%e8%b7%b5%e7%9a%84%e7%bb%bc%e5%90%88","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/34016.html","title":{"rendered":"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\u6790"},"content":{"rendered":"<h4 id=\"%E2%80%8B%E7%BC%96%E8%BE%91\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"726\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140933-680f8c1d7e43e.gif\" width=\"1200\" \/><\/h4>\n<p style=\"text-align:center\"><span style=\"color:#956fe7\">\u5148\u627e\u51fa\u4f60\u7684\u80fd\u529b\u5728\u54ea\u91cc&#xff0c;\u7136\u540e\u518d\u51b3\u5b9a\u4f60\u662f\u8c01\u3002<\/span><\/p>\n<p style=\"text-align:center\">\u2014\u2014 \u5854\u62c9\u00b7\u97e6\u65af\u7279\u5f17 \u300a\u4f60\u5f53\u50cf\u9e1f\u98de\u5f80\u4f60\u7684\u5c71\u300b<\/p>\n<hr \/>\n<p id=\"main-toc\">\u76ee\u5f55<\/p>\n<p id=\"1.%20C%2B%2B%20%E4%B8%8E%E5%93%88%E5%B8%8C%E8%A1%A8%EF%BC%9A%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%BC%95%E5%85%A5-toc\" style=\"margin-left:80px\">1. C&#043;&#043; \u4e0e\u54c8\u5e0c\u8868&#xff1a;\u6838\u5fc3\u6982\u5ff5\u4e0e\u5f15\u5165<\/p>\n<p id=\"2.%20%E5%93%88%E5%B8%8C%E8%A1%A8%E7%9A%84%E5%BA%95%E5%B1%82%E6%9C%BA%E5%88%B6%EF%BC%9A%E5%8E%9F%E7%90%86%E4%B8%8E%E6%8C%91%E6%88%98-toc\" style=\"margin-left:80px\">2. \u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u673a\u5236&#xff1a;\u539f\u7406\u4e0e\u6311\u6218<\/p>\n<p id=\"2.1%20%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD%E8%A7%A3%E6%9E%90%EF%BC%9A%E6%95%88%E7%8E%87%E4%B8%8E%E7%81%B5%E6%B4%BB%E6%80%A7%E7%9A%84%E5%B9%B3%E8%A1%A1-toc\" style=\"margin-left:120px\">2.1 \u6838\u5fc3\u529f\u80fd\u89e3\u6790&#xff1a;\u6548\u7387\u4e0e\u7075\u6d3b\u6027\u7684\u5e73\u8861<\/p>\n<p id=\"2.2%20%E5%93%88%E5%B8%8C%E5%86%B2%E7%AA%81%E7%9A%84%E6%9C%AC%E8%B4%A8%EF%BC%9A%E9%97%AE%E9%A2%98%E4%B8%8E%E5%BA%94%E5%AF%B9%E7%AD%96%E7%95%A5-toc\" style=\"margin-left:120px\">2.2 \u54c8\u5e0c\u51b2\u7a81\u7684\u672c\u8d28&#xff1a;\u95ee\u9898\u4e0e\u5e94\u5bf9\u7b56\u7565<\/p>\n<p id=\"2.3%20%E5%BC%80%E6%95%A3%E5%88%97%E4%B8%8E%E9%97%AD%E6%95%A3%E5%88%97%EF%BC%9A%E4%B8%A4%E5%A4%A7%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E7%9A%84%E6%AF%94%E8%BE%83-toc\" style=\"margin-left:120px\">2.3 \u5f00\u6563\u5217\u4e0e\u95ed\u6563\u5217&#xff1a;\u4e24\u5927\u89e3\u51b3\u65b9\u6848\u7684\u6bd4\u8f83<\/p>\n<p id=\"3.%20%E9%97%AD%E6%95%A3%E5%88%97%E7%9A%84%E7%B2%BE%E7%A1%AE%E5%AE%9E%E7%8E%B0%EF%BC%9A%E4%BB%8E%E8%AE%BE%E8%AE%A1%E5%88%B0%E4%BC%98%E5%8C%96-toc\" style=\"margin-left:80px\">3. \u95ed\u6563\u5217\u7684\u7cbe\u786e\u5b9e\u73b0&#xff1a;\u4ece\u8bbe\u8ba1\u5230\u4f18\u5316<\/p>\n<p id=\"3.1%20%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E8%AE%BE%E8%AE%A1%EF%BC%9A%E9%9D%A2%E5%90%91%E6%89%A9%E5%B1%95%E7%9A%84%E6%9E%B6%E6%9E%84-toc\" style=\"margin-left:120px\">3.1 \u6574\u4f53\u6846\u67b6\u8bbe\u8ba1&#xff1a;\u9762\u5411\u6269\u5c55\u7684\u67b6\u6784<\/p>\n<p id=\"3.2%20%E4%BB%BF%E5%87%BD%E6%95%B0%E7%9A%84%E7%81%B5%E6%B4%BB%E6%80%A7%EF%BC%9A%E9%AB%98%E6%95%88%E5%93%88%E5%B8%8C%E7%9A%84%E5%85%B3%E9%94%AE-toc\" style=\"margin-left:120px\">3.2 \u4eff\u51fd\u6570\u7684\u7075\u6d3b\u6027&#xff1a;\u9ad8\u6548\u54c8\u5e0c\u7684\u5173\u952e<\/p>\n<p id=\"3.3%20%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C%EF%BC%9A%E5%86%B2%E7%AA%81%E6%A3%80%E6%B5%8B%E4%B8%8E%E4%BD%8D%E7%BD%AE%E5%88%86%E9%85%8D-toc\" style=\"margin-left:120px\">3.3 \u63d2\u5165\u64cd\u4f5c&#xff1a;\u51b2\u7a81\u68c0\u6d4b\u4e0e\u4f4d\u7f6e\u5206\u914d<\/p>\n<p id=\"3.4%20%E6%9F%A5%E6%89%BE%E6%93%8D%E4%BD%9C%EF%BC%9A%E9%80%9F%E5%BA%A6%E4%B8%8E%E5%87%86%E7%A1%AE%E7%9A%84%E5%8F%8C%E9%87%8D%E4%BF%9D%E9%9A%9C-toc\" style=\"margin-left:120px\">3.4 \u67e5\u627e\u64cd\u4f5c&#xff1a;\u901f\u5ea6\u4e0e\u51c6\u786e\u7684\u53cc\u91cd\u4fdd\u969c<\/p>\n<p id=\"3.5%20%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%EF%BC%9A%E6%A0%87%E8%AE%B0%E4%B8%8E%E9%87%8D%E6%9E%84%E7%9A%84%E7%BB%86%E8%8A%82%E4%BC%98%E5%8C%96-toc\" style=\"margin-left:120px\">3.5 \u5220\u9664\u64cd\u4f5c&#xff1a;\u6807\u8bb0\u4e0e\u91cd\u6784\u7684\u7ec6\u8282\u4f18\u5316<\/p>\n<p id=\"4.%20%E5%BC%80%E6%95%A3%E5%88%97%E7%9A%84%E7%81%B5%E6%B4%BB%E5%AE%9E%E7%8E%B0%EF%BC%9A%E4%BB%8E%E5%9F%BA%E7%A1%80%E5%88%B0%E9%AB%98%E6%95%88-toc\" style=\"margin-left:80px\">4. \u5f00\u6563\u5217\u7684\u7075\u6d3b\u5b9e\u73b0&#xff1a;\u4ece\u57fa\u7840\u5230\u9ad8\u6548<\/p>\n<p id=\"4.1%20%E8%8A%82%E7%82%B9%E8%AE%BE%E8%AE%A1%EF%BC%9A%E6%8C%87%E9%92%88%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%9A%84%E5%B9%B3%E8%A1%A1%E8%89%BA%E6%9C%AF-toc\" style=\"margin-left:120px\">4.1 \u8282\u70b9\u8bbe\u8ba1&#xff1a;\u6307\u9488\u4e0e\u6570\u636e\u7684\u5e73\u8861\u827a\u672f<\/p>\n<p id=\"4.2%20%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E6%90%AD%E5%BB%BA%EF%BC%9A%E9%93%BE%E8%A1%A8%E4%B8%8E%E9%80%BB%E8%BE%91%E7%9A%84%E8%9E%8D%E5%90%88-toc\" style=\"margin-left:120px\">4.2 \u6574\u4f53\u6846\u67b6\u642d\u5efa&#xff1a;\u94fe\u8868\u4e0e\u903b\u8f91\u7684\u878d\u5408<\/p>\n<p id=\"4.3%20%E6%8F%92%E5%85%A5%E5%87%BD%E6%95%B0%EF%BC%9A%E9%93%BE%E8%A1%A8%E6%8B%93%E5%B1%95%E4%B8%8E%E5%93%88%E5%B8%8C%E5%88%86%E5%B8%83-toc\" style=\"margin-left:120px\">4.3 \u63d2\u5165\u51fd\u6570&#xff1a;\u94fe\u8868\u62d3\u5c55\u4e0e\u54c8\u5e0c\u5206\u5e03<\/p>\n<p id=\"4.4%20%E5%88%A0%E9%99%A4%E5%87%BD%E6%95%B0%EF%BC%9A%E8%8A%82%E7%82%B9%E6%B8%85%E7%90%86%E4%B8%8E%E7%A9%BA%E9%97%B4%E5%A4%8D%E7%94%A8-toc\" style=\"margin-left:120px\">4.4 \u5220\u9664\u51fd\u6570&#xff1a;\u8282\u70b9\u6e05\u7406\u4e0e\u7a7a\u95f4\u590d\u7528<\/p>\n<p id=\"4.5%20%E6%9F%A5%E6%89%BE%E6%93%8D%E4%BD%9C%EF%BC%9A%E5%AE%9A%E4%BD%8D%E6%95%88%E7%8E%87%E7%9A%84%E4%BC%98%E5%8C%96%E5%AE%9E%E8%B7%B5-toc\" style=\"margin-left:120px\">4.5 \u67e5\u627e\u64cd\u4f5c&#xff1a;\u5b9a\u4f4d\u6548\u7387\u7684\u4f18\u5316\u5b9e\u8df5<\/p>\n<p id=\"4.6%20%E5%8A%9F%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%9A%E5%A4%9A%E5%9C%BA%E6%99%AF%E9%AA%8C%E8%AF%81%E4%B8%8E%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90-toc\" style=\"margin-left:120px\">4.6 \u529f\u80fd\u6d4b\u8bd5&#xff1a;\u591a\u573a\u666f\u9a8c\u8bc1\u4e0e\u6027\u80fd\u5206\u6790<\/p>\n<hr id=\"hr-toc\" \/>\n<h2 id=\"1.%20C%2B%2B%20%E4%B8%8E%E5%93%88%E5%B8%8C%E8%A1%A8%EF%BC%9A%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5%E4%B8%8E%E5%BC%95%E5%85%A5\" style=\"background-color:transparent\">1\u3001C&#043;&#043; \u4e0e\u54c8\u5e0c\u8868&#xff1a;\u6838\u5fc3\u6982\u5ff5\u4e0e\u5f15\u5165<\/h2>\n<p><span style=\"color:#0d0016\">\u54c8\u5e0c\u8868&#xff08;Hash Table&#xff09;\u662f\u4e00\u79cd\u91cd\u8981\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u5b83\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5c06\u952e\u6620\u5c04\u5230\u8868\u4e2d\u7684\u7279\u5b9a\u4f4d\u7f6e&#xff0c;\u4ece\u800c\u5b9e\u73b0\u5bf9\u8bb0\u5f55\u7684\u5feb\u901f\u8bbf\u95ee&#xff0c;\u652f\u6301\u9ad8\u6548\u7684\u63d2\u5165\u548c\u67e5\u627e\u64cd\u4f5c\u3002<\/span><\/p>\n<p>\u54c8\u5e0c\u8868\u7684\u6982\u5ff5\u6700\u65e9\u7531H. P. Luhn\u4e8e1953\u5e74\u63d0\u51fa&#xff0c;\u4ed6\u9996\u6b21\u63cf\u8ff0\u4e86\u5229\u7528\u54c8\u5e0c\u51fd\u6570\u52a0\u901f\u6570\u636e\u68c0\u7d22\u7684\u8fc7\u7a0b\u3002\u81ea\u6b64&#xff0c;\u8fd9\u4e00\u601d\u60f3\u9010\u6b65\u6f14\u5316\u5e76\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6570\u636e\u5e93\u7ba1\u7406\u7cfb\u7edf\u548c\u7f16\u7a0b\u8bed\u8a00\u4e2d&#xff0c;\u6210\u4e3a\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\u7684\u91cd\u8981\u57fa\u7840\u4e4b\u4e00\u3002<\/p>\n<p>\u968f\u7740\u8ba1\u7b97\u673a\u786c\u4ef6\u6027\u80fd\u7684\u63d0\u5347\u548c\u6570\u636e\u91cf\u7684\u7206\u70b8\u6027\u589e\u957f&#xff0c;\u54c8\u5e0c\u8868\u7684\u4f5c\u7528\u6108\u53d1\u51f8\u663e\u3002\u4f5c\u4e3a\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u7ed3\u6784&#xff0c;\u54c8\u5e0c\u8868\u5728\u8f6f\u4ef6\u5de5\u7a0b\u3001\u6570\u636e\u5e93\u7cfb\u7edf\u3001\u7f51\u7edc\u641c\u7d22\u5f15\u64ce\u7b49\u9886\u57df\u626e\u6f14\u7740\u4e0d\u53ef\u6216\u7f3a\u7684\u89d2\u8272&#xff0c;\u5c24\u5176\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u548c\u9ad8\u9891\u7387\u67e5\u8be2\u65f6\u5c55\u73b0\u51fa\u5353\u8d8a\u7684\u6027\u80fd\u4f18\u52bf\u3002<\/p>\n<p><span style=\"color:#fe2c24\">\u5728C&#043;&#043;\u4e2d&#xff0c;\u54c8\u5e0c\u8868\u7684\u529f\u80fd\u4e3b\u8981\u901a\u8fc7unordered\u7cfb\u5217\u5173\u8054\u5f0f\u5bb9\u5668\u5b9e\u73b0<\/span>\u3002\u5728C&#043;&#043;98\u6807\u51c6\u4e2d&#xff0c;STL\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5e95\u5c42\u57fa\u4e8e\u7ea2\u9ed1\u6811\u7684\u5173\u8054\u5f0f\u5bb9\u5668&#xff0c;\u5982map\u548cset\u3002<span style=\"color:#0d0016\">\u8fd9\u4e9b\u5bb9\u5668\u5728\u6700\u574f\u60c5\u51b5\u4e0b\u7684\u67e5\u8be2\u590d\u6742\u5ea6\u4e3aO(log N)&#xff0c;\u5373\u9700\u8981\u8fdb\u884c\u4e0e\u7ea2\u9ed1\u6811\u9ad8\u5ea6\u6210\u6bd4\u4f8b\u7684\u6bd4\u8f83\u64cd\u4f5c\u3002\u5f53\u6811\u7684\u8282\u70b9\u6570\u91cf\u5e9e\u5927\u65f6&#xff0c;\u67e5\u8be2\u6548\u7387\u53ef\u80fd\u4f1a\u53d7\u5230\u663e\u8457\u5f71\u54cd\u3002<\/span><\/p>\n<p>\u4e3a\u4e86<span style=\"color:#0d0016\">\u8fdb\u4e00\u6b65\u63d0\u5347\u67e5\u8be2\u6027\u80fd<\/span>&#xff0c;C&#043;&#043;11\u5f15\u5165\u4e86\u56db\u79cdunordered\u7cfb\u5217\u7684\u5173\u8054\u5f0f\u5bb9\u5668&#xff0c;\u5305\u62ecunordered_map\u3001unordered_set\u3001unordered_multimap\u548cunordered_multiset\u3002<span style=\"color:#0d0016\">\u8fd9\u4e9b\u5bb9\u5668\u5728\u4f7f\u7528\u65b9\u5f0f\u4e0a\u4e0e\u4f20\u7edf\u7684\u7ea2\u9ed1\u6811\u5173\u8054\u5f0f\u5bb9\u5668\u76f8\u4f3c&#xff0c;\u4f46\u5176\u5e95\u5c42\u5b9e\u73b0\u57fa\u4e8e\u54c8\u5e0c\u8868\u3002\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u7684\u9ad8\u6548\u6620\u5c04&#xff0c;unordered\u5bb9\u5668\u5728\u5e73\u5747\u60c5\u51b5\u4e0b\u80fd\u591f\u5b9e\u73b0O(1)\u7684\u5e38\u6570\u7ea7\u67e5\u8be2\u590d\u6742\u5ea6<\/span>&#xff0c;\u6781\u5927\u5730\u63d0\u9ad8\u4e86\u6570\u636e\u8bbf\u95ee\u901f\u5ea6&#xff0c;\u5c24\u5176\u9002\u7528\u4e8e\u5bf9\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u3002<\/p>\n<p>\u603b\u4e4b&#xff0c;\u54c8\u5e0c\u8868\u53ca\u5176\u5728C&#043;&#043;\u4e2d\u7684\u5b9e\u73b0&#xff0c;\u4e0d\u4ec5\u4f18\u5316\u4e86\u6570\u636e\u5b58\u50a8\u4e0e\u68c0\u7d22\u6548\u7387&#xff0c;\u8fd8\u63a8\u52a8\u4e86\u73b0\u4ee3\u8f6f\u4ef6\u5f00\u53d1\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u7684\u80fd\u529b\u8fb9\u754c&#xff0c;\u6210\u4e3a\u8ba1\u7b97\u673a\u79d1\u5b66\u9886\u57df\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u6838\u5fc3\u6280\u672f\u4e4b\u4e00\u3002<\/p>\n<h2 id=\"2.%20%E5%93%88%E5%B8%8C%E8%A1%A8%E7%9A%84%E5%BA%95%E5%B1%82%E6%9C%BA%E5%88%B6%EF%BC%9A%E5%8E%9F%E7%90%86%E4%B8%8E%E6%8C%91%E6%88%98\" style=\"background-color:transparent\">2\u3001\u54c8\u5e0c\u8868\u7684\u5e95\u5c42\u673a\u5236&#xff1a;\u539f\u7406\u4e0e\u6311\u6218<\/h2>\n<h3 id=\"2.1%20%E6%A0%B8%E5%BF%83%E5%8A%9F%E8%83%BD%E8%A7%A3%E6%9E%90%EF%BC%9A%E6%95%88%E7%8E%87%E4%B8%8E%E7%81%B5%E6%B4%BB%E6%80%A7%E7%9A%84%E5%B9%B3%E8%A1%A1\" style=\"background-color:transparent\">2.1 \u6838\u5fc3\u529f\u80fd\u89e3\u6790&#xff1a;\u6548\u7387\u4e0e\u7075\u6d3b\u6027\u7684\u5e73\u8861<\/h3>\n<p>\u5728\u987a\u5e8f\u7ed3\u6784\u548c\u5e73\u8861\u6811\u4e2d&#xff0c;\u5143\u7d20\u7684\u5173\u952e\u7801\u4e0e\u5176\u5b58\u50a8\u4f4d\u7f6e\u4e4b\u95f4\u5e76\u6ca1\u6709\u76f4\u63a5\u7684\u5bf9\u5e94\u5173\u7cfb\u3002\u56e0\u6b64&#xff0c;\u5728\u67e5\u627e\u4e00\u4e2a\u5143\u7d20\u65f6&#xff0c;\u5fc5\u987b\u901a\u8fc7\u591a\u6b21\u6bd4\u8f83\u5176\u5173\u952e\u7801\u3002\u5728\u987a\u5e8f\u67e5\u627e\u4e2d&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a O(N&#xff09;&#xff0c;\u800c\u5728\u5e73\u8861\u6811\u4e2d&#xff0c;\u67e5\u627e\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u6811\u7684\u9ad8\u5ea6 O(log\u20612N))&#xff0c;\u641c\u7d22\u6548\u7387\u53d6\u51b3\u4e8e\u67e5\u627e\u8fc7\u7a0b\u4e2d\u5143\u7d20\u6bd4\u8f83\u7684\u6b21\u6570\u3002<\/p>\n<p>\u7406\u60f3\u7684\u641c\u7d22\u65b9\u6cd5&#xff1a;<span style=\"color:#fe2c24\">\u53ef\u4ee5\u4e0d\u7ecf\u8fc7\u4efb\u4f55\u6bd4\u8f83&#xff0c;\u4e00\u6b21\u76f4\u63a5\u4ece\u8868\u4e2d\u5f97\u5230\u8981\u641c\u7d22\u7684\u5143\u7d20<\/span>\u3002\u4e3a\u6b64&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6784\u9020\u4e00\u79cd\u7279\u6b8a\u7684\u5b58\u50a8\u7ed3\u6784&#xff0c;<span style=\"color:#0d0016\">\u901a\u8fc7\u4e00\u4e2a\u54c8\u5e0c\u51fd\u6570&#xff08;hashFunc&#xff09;\u5c06\u5143\u7d20\u7684\u5b58\u50a8\u4f4d\u7f6e\u4e0e\u5176\u5173\u952e\u7801&#xff08;key&#xff09;\u5efa\u7acb\u4e00\u4e00\u6620\u5c04\u5173\u7cfb<\/span>\u3002\u5728\u8fd9\u79cd\u7ed3\u6784\u4e2d&#xff1a;<\/p>\n<li>\u63d2\u5165\u5143\u7d20&#xff1a;\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u5f85\u63d2\u5165\u5143\u7d20\u7684\u5b58\u50a8\u4f4d\u7f6e&#xff0c;\u5e76\u5c06\u5143\u7d20\u5b58\u50a8\u5728\u8be5\u4f4d\u7f6e\u3002<\/li>\n<li>\u641c\u7d22\u5143\u7d20&#xff1a;\u8ba1\u7b97\u5143\u7d20\u7684\u5173\u952e\u7801\u5bf9\u5e94\u7684\u5b58\u50a8\u4f4d\u7f6e&#xff0c;\u76f4\u63a5\u8bbf\u95ee\u8be5\u4f4d\u7f6e\u3002\u5982\u679c\u8be5\u4f4d\u7f6e\u7684\u5143\u7d20\u5173\u952e\u7801\u4e0e\u5f85\u67e5\u627e\u7684\u5143\u7d20\u76f8\u540c&#xff0c;\u5219\u641c\u7d22\u6210\u529f\u3002<\/li>\n<h3 id=\"2.2%20%E5%93%88%E5%B8%8C%E5%86%B2%E7%AA%81%E7%9A%84%E6%9C%AC%E8%B4%A8%EF%BC%9A%E9%97%AE%E9%A2%98%E4%B8%8E%E5%BA%94%E5%AF%B9%E7%AD%96%E7%95%A5\" style=\"background-color:transparent\">2.2 \u54c8\u5e0c\u51b2\u7a81\u7684\u672c\u8d28&#xff1a;\u95ee\u9898\u4e0e\u5e94\u5bf9\u7b56\u7565<\/h3>\n<p>\u5bf9\u4e8e\u4e24\u4e2a\u6570\u636e\u5143\u7d20\u7684\u5173\u952e\u5b57 &#xff0c;\u5982\u679c\u5176\u4e0b\u6807\u4e0d\u540c\u200b&#xff0c;\u5bf9\u5e94\u7684\u5143\u7d20\u503c\u4e5f\u4e0d\u540c\u3002\u4f46\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u7ed3\u679c\u5374\u662f\u76f8\u540c&#xff0c;\u5373 Hash&#xff08;ki&#xff09;&#061;&#061;Hash&#xff08;kj\u200b&#xff09;&#xff0c;<span style=\"color:#fe2c24\">\u7b80\u5355\u8bf4\u4e24\u4e2a\u4e0d\u540c\u7684key\u53ef\u80fd\u4f1a\u6620\u5c04\u5230\u540c\u2f00\u4e2a\u4f4d\u7f6e\u53bb<\/span> \u8fd9\u79cd\u73b0\u8c61\u79f0\u4e3a\u54c8\u5e0c\u51b2\u7a81\u6216\u54c8\u5e0c\u78b0\u649e\u3002<\/p>\n<p>\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u53ef\u80fd\u662f\u7531\u4e8e\u54c8\u5e0c\u51fd\u6570\u7684\u8bbe\u8ba1\u95ee\u9898\u3002\u4e00\u4e2a\u597d\u7684\u54c8\u5e0c\u51fd\u6570\u5e94\u8be5\u6ee1\u8db3\u4ee5\u4e0b\u539f\u5219&#xff1a;<\/p>\n<li>\u5b9a\u4e49\u57df\u8986\u76d6\u6240\u6709\u5173\u952e\u5b57&#xff1a;\u54c8\u5e0c\u51fd\u6570\u7684\u5b9a\u4e49\u57df\u5fc5\u987b\u5305\u542b\u6240\u6709\u9700\u8981\u5b58\u50a8\u7684\u5173\u952e\u5b57&#xff1b;\u5982\u679c\u6563\u5217\u8868\u5141\u8bb8\u6709\u00a0 m\u00a0\u4e2a\u5730\u5740&#xff0c;\u5219\u54c8\u5e0c\u51fd\u6570\u7684\u503c\u57df\u5e94\u5f53\u5728 0\u00a0\u5230 m-1 \u4e4b\u95f4\u3002<\/li>\n<li>\u5747\u5300\u5206\u5e03&#xff1a;\u54c8\u5e0c\u51fd\u6570\u5e94\u80fd\u5c06\u5173\u952e\u5b57\u5747\u5300\u5730\u6620\u5c04\u5230\u6574\u4e2a\u5730\u5740\u7a7a\u95f4\u4e2d&#xff0c;\u51cf\u5c11\u51b2\u7a81\u7684\u6982\u7387\u3002<\/li>\n<li>\u8ba1\u7b97\u7b80\u5355&#xff1a;\u54c8\u5e0c\u51fd\u6570\u5e94\u8bbe\u8ba1\u5f97\u5c3d\u53ef\u80fd\u7b80\u5355&#xff0c;\u4ee5\u63d0\u9ad8\u8ba1\u7b97\u6548\u7387\u3002<\/li>\n<p>\u7136\u800c&#xff0c;\u7531\u4e8e\u5b58\u50a8\u7a7a\u95f4\u6709\u9650&#xff0c;\u54c8\u5e0c\u51fd\u6570\u96be\u4ee5\u5b8c\u5168\u907f\u514d\u54c8\u5e0c\u51b2\u7a81\u7684\u53d1\u751f\u3002\u56e0\u6b64&#xff0c;\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u65f6&#xff0c;\u7cfb\u7edf\u9700\u8981\u91c7\u53d6\u9002\u5f53\u7684\u5904\u7406\u65b9\u6cd5\u3002\u901a\u5e38&#xff0c;\u54c8\u5e0c\u51b2\u7a81\u7684\u89e3\u51b3\u65b9\u6848\u6709\u4e24\u79cd\u5e38\u89c1\u65b9\u6cd5&#xff1a;\u95ed\u6563\u5217&#xff08;Open Addressing&#xff09;\u548c\u5f00\u6563\u5217&#xff08;Chaining&#xff09;\u3002<\/p>\n<p>\u5728\u95ed\u6563\u5217\u4e2d&#xff0c;\u5f53\u53d1\u751f\u51b2\u7a81\u65f6&#xff0c;\u5bfb\u627e\u4e00\u4e2a\u7a7a\u4f4d\u5c06\u5143\u7d20\u5b58\u50a8\u5728\u6563\u5217\u8868\u4e2d\u3002\u8fd9\u901a\u5e38\u901a\u8fc7\u7ebf\u6027\u63a2\u6d4b\u3001\u4e8c\u6b21\u63a2\u6d4b\u6216\u53cc\u91cd\u6563\u5217\u7b49\u6280\u672f\u5b9e\u73b0\u3002<\/p>\n<p>\u5728\u5f00\u6563\u5217\u4e2d&#xff0c;\u5f53\u53d1\u751f\u51b2\u7a81\u65f6&#xff0c;\u591a\u4e2a\u5143\u7d20\u88ab\u5b58\u50a8\u5728\u540c\u4e00\u4e2a\u5730\u5740\u4f4d\u7f6e\u7684\u94fe\u8868\u4e2d&#xff0c;\u901a\u5e38\u91c7\u7528\u94fe\u8868\u6216\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u6765\u5b58\u50a8\u8fd9\u4e9b\u201c\u540c\u4e49\u8bcd\u201d\u3002<\/p>\n<p>\u5728\u8ba8\u8bba\u54c8\u5e0c\u51b2\u7a81\u7684\u5904\u7406\u65b9\u6cd5\u65f6&#xff0c;\u53e6\u4e00\u4e2a\u91cd\u8981\u7684\u6982\u5ff5\u662f\u8d1f\u8f7d\u56e0\u5b50&#xff08;Load Factor&#xff09;\u3002\u8d1f\u8f7d\u56e0\u5b50\u662f\u54c8\u5e0c\u8868\u4e2d\u5143\u7d20\u4e2a\u6570\u4e0e\u8868\u7684\u603b\u5bb9\u91cf\u4e4b\u95f4\u7684\u6bd4\u503c&#xff0c;\u901a\u5e38\u8868\u793a\u4e3a&#xff1a;<\/p>\n<p class=\"img-center\"><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"97\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140933-680f8c1df1ae2.png\" width=\"454\" \/><\/p>\n<p><span style=\"color:#fe2c24\">\u8d1f\u8f7d\u56e0\u5b50\u53cd\u6620\u4e86\u54c8\u5e0c\u8868\u7684\u201c\u5bc6\u96c6\u7a0b\u5ea6\u201d\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8f83\u9ad8\u65f6&#xff0c;\u610f\u5473\u7740\u54c8\u5e0c\u8868\u4e2d\u5b58\u50a8\u7684\u5143\u7d20\u63a5\u8fd1\u5176\u603b\u5bb9\u91cf&#xff0c;\u53d1\u751f\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u589e\u5927&#xff1b;\u76f8\u53cd&#xff0c;\u8d1f\u8f7d\u56e0\u5b50\u8f83\u4f4e\u65f6&#xff0c;\u54c8\u5e0c\u8868\u4e2d\u7684\u5143\u7d20\u8f83\u5c11&#xff0c;\u51b2\u7a81\u7684\u6982\u7387\u8f83\u5c0f\u3002<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u8d1f\u8f7d\u56e0\u5b50\u7684\u5e94\u7528\u4e0e\u5f71\u54cd&#xff1a;<\/span><\/p>\n<li>\n<p>\u5f71\u54cd\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387&#xff1a;\u8d1f\u8f7d\u56e0\u5b50\u8d8a\u5927&#xff0c;\u54c8\u5e0c\u8868\u4e2d\u7684\u5143\u7d20\u8d8a\u5bc6\u96c6&#xff0c;\u78b0\u649e\u7684\u6982\u7387\u4e5f\u8d8a\u9ad8\u3002\u4e3a\u4e86\u964d\u4f4e\u51b2\u7a81\u53d1\u751f\u7684\u6982\u7387&#xff0c;\u901a\u5e38\u5728\u8d1f\u8f7d\u56e0\u5b50\u8fbe\u5230\u4e00\u5b9a\u9608\u503c\u65f6&#xff0c;\u4f1a\u8fdb\u884c\u54c8\u5e0c\u8868\u7684\u6269\u5bb9&#xff0c;\u5c06\u54c8\u5e0c\u8868\u7684\u5bb9\u91cf\u589e\u5927&#xff0c;\u4ece\u800c\u964d\u4f4e\u8d1f\u8f7d\u56e0\u5b50\u5e76\u51cf\u5c11\u51b2\u7a81\u3002<\/p>\n<\/li>\n<li>\n<p>\u95ed\u6563\u5217\u4e2d\u7684\u8d1f\u8f7d\u56e0\u5b50&#xff1a;\u5728\u95ed\u6563\u5217\u6cd5\u4e2d&#xff0c;\u5f53\u8d1f\u8f7d\u56e0\u5b50\u589e\u5927\u65f6&#xff0c;\u67e5\u627e\u3001\u63d2\u5165\u7b49\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u589e\u52a0\u3002\u56e0\u4e3a\u54c8\u5e0c\u8868\u7684\u7a7a\u95f2\u4f4d\u7f6e\u51cf\u5c11&#xff0c;\u51b2\u7a81\u53d1\u751f\u540e\u9700\u8981\u8fdb\u884c\u63a2\u6d4b&#xff0c;\u53ef\u80fd\u5bfc\u81f4\u64cd\u4f5c\u53d8\u5f97\u4f4e\u6548\u3002\u4e3a\u907f\u514d\u8fd9\u79cd\u60c5\u51b5&#xff0c;\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u4e00\u5b9a\u503c&#xff08;\u901a\u5e38\u4e3a 0.7 \u6216 0.75&#xff09;\u65f6&#xff0c;\u4f1a\u89e6\u53d1\u6269\u5bb9\u64cd\u4f5c&#xff0c;\u5c06\u54c8\u5e0c\u8868\u7684\u5927\u5c0f\u7ffb\u500d&#xff0c;\u5e76\u91cd\u65b0\u8ba1\u7b97\u6bcf\u4e2a\u5143\u7d20\u7684\u54c8\u5e0c\u5730\u5740\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f00\u6563\u5217\u4e2d\u7684\u8d1f\u8f7d\u56e0\u5b50&#xff1a;\u5728\u5f00\u6563\u5217\u6cd5\u4e2d&#xff0c;\u8d1f\u8f7d\u56e0\u5b50\u7684\u589e\u5927\u4f1a\u5bfc\u81f4\u94fe\u8868\u7684\u957f\u5ea6\u589e\u52a0&#xff0c;\u8fdb\u800c\u5f71\u54cd\u67e5\u627e\u6548\u7387\u3002\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8fc7\u9ad8\u65f6&#xff0c;\u94fe\u8868\u53d8\u957f&#xff0c;\u67e5\u627e\u3001\u63d2\u5165\u548c\u5220\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4f1a\u9000\u5316\u4e3a\u7ebf\u6027\u65f6\u95f4 O(n)\u3002\u56e0\u6b64&#xff0c;\u5728\u5f00\u6563\u5217\u4e2d&#xff0c;\u901a\u5e38\u4e5f\u4f1a\u91c7\u53d6\u76f8\u4f3c\u7684\u7b56\u7565&#xff0c;\u76d1\u63a7\u8d1f\u8f7d\u56e0\u5b50\u7684\u53d8\u5316&#xff0c;\u5f53\u8d1f\u8f7d\u56e0\u5b50\u8d85\u8fc7\u67d0\u4e2a\u9608\u503c\u65f6&#xff0c;\u8fdb\u884c\u6269\u5bb9\u6216\u91cd\u65b0\u54c8\u5e0c\u3002<\/p>\n<\/li>\n<h3 id=\"2.3%20%E5%BC%80%E6%95%A3%E5%88%97%E4%B8%8E%E9%97%AD%E6%95%A3%E5%88%97%EF%BC%9A%E4%B8%A4%E5%A4%A7%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%E7%9A%84%E6%AF%94%E8%BE%83\" style=\"background-color:transparent\">2.3 \u5f00\u6563\u5217\u4e0e\u95ed\u6563\u5217&#xff1a;\u4e24\u5927\u89e3\u51b3\u65b9\u6848\u7684\u6bd4\u8f83<\/h3>\n<p>\u54c8\u5e0c&#xff08;\u6563\u5217&#xff09;\u65b9\u6cd5\u662f\u4e00\u79cd\u9ad8\u6548\u7684\u6570\u636e\u5b58\u50a8\u548c\u68c0\u7d22\u65b9\u5f0f&#xff0c;\u5176\u6838\u5fc3\u5728\u4e8e\u54c8\u5e0c\u51fd\u6570\u548c\u54c8\u5e0c\u8868\u7684\u6784\u5efa\u3002\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5c06\u6570\u636e\u6620\u5c04\u5230\u6570\u7ec4\u7d22\u5f15\u4e0a&#xff0c;\u53ef\u4ee5\u5feb\u901f\u5b9a\u4f4d\u5143\u7d20\u3002\u7136\u800c&#xff0c;\u5f53\u591a\u4e2a\u6570\u636e\u88ab\u6620\u5c04\u5230\u76f8\u540c\u7684\u7d22\u5f15&#xff08;\u5373\u54c8\u5e0c\u51b2\u7a81&#xff09;\u65f6&#xff0c;\u9700\u8981\u91c7\u53d6\u6709\u6548\u7684\u7b56\u7565\u8fdb\u884c\u5904\u7406\u3002\u6839\u636e\u89e3\u51b3\u51b2\u7a81\u7684\u65b9\u5f0f&#xff0c;\u54c8\u5e0c\u8868\u5206\u4e3a\u4e24\u79cd\u7c7b\u578b&#xff1a;\u95ed\u6563\u5217\u548c\u5f00\u6563\u5217\u3002<\/p>\n<p><span style=\"color:#0d0016\">\u95ed\u6563\u5217&#xff08;\u5f00\u653e\u5b9a\u5740\u6cd5&#xff09;<\/span><\/p>\n<p>\u95ed\u6563\u5217\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u51b2\u7a81\u7684\u5143\u7d20\u5b58\u653e\u5230\u54c8\u5e0c\u8868\u4e2d\u7684\u5176\u4ed6\u7a7a\u4f4d\u7f6e\u3002\u5176\u4e3b\u8981\u5b9e\u73b0\u65b9\u5f0f\u662f\u7ebf\u6027\u63a2\u6d4b&#xff1a;<\/p>\n<ul>\n<li>\u63d2\u5165&#xff1a;\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u5f97\u5230\u76ee\u6807\u4f4d\u7f6e\u3002\u5982\u679c\u8be5\u4f4d\u7f6e\u4e3a\u7a7a&#xff0c;\u5219\u76f4\u63a5\u63d2\u5165&#xff1b;\u82e5\u53d1\u751f\u51b2\u7a81&#xff0c;\u5219\u4ece\u51b2\u7a81\u4f4d\u7f6e\u5f00\u59cb&#xff0c;\u4f9d\u6b21\u5411\u540e\u63a2\u6d4b&#xff0c;\u76f4\u5230\u627e\u5230\u7a7a\u4f4d\u7f6e\u4e3a\u6b62&#xff0c;\u518d\u63d2\u5165\u5143\u7d20\u3002\u4f8b\u5982&#xff0c;\u82e5\u5143\u7d2044\u8ba1\u7b97\u51fa\u7684\u54c8\u5e0c\u5730\u5740\u4e3a4&#xff0c;\u4f46\u4f4d\u7f6e4\u5df2\u5b58\u6709\u5143\u7d204&#xff0c;\u5219\u7ee7\u7eed\u5411\u540e\u63a2\u6d4b&#xff0c;\u627e\u5230\u4e0b\u4e00\u4e2a\u7a7a\u4f4d\u7f6e\u5b58\u653e44\u3002<\/li>\n<li>\u5220\u9664&#xff1a;\u91c7\u7528\u4f2a\u5220\u9664\u6807\u8bb0\u4ee3\u66ff\u7269\u7406\u5220\u9664&#xff0c;\u4ee5\u514d\u5f71\u54cd\u5176\u4ed6\u5143\u7d20\u7684\u641c\u7d22\u8def\u5f84\u3002\u4f8b\u5982&#xff0c;\u82e5\u76f4\u63a5\u5220\u9664\u5143\u7d204&#xff0c;\u5219\u4f1a\u5bfc\u81f4\u540e\u7eed\u67e5\u627e44\u65f6\u8def\u5f84\u65ad\u88c2&#xff0c;\u56e0\u6b64\u91c7\u7528\u6807\u8bb0\u4f2a\u5220\u9664\u7684\u65b9\u6cd5\u3002<\/li>\n<\/ul>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5b9e\u73b0\u7b80\u5355&#xff0c;\u65e0\u9700\u989d\u5916\u6570\u636e\u7ed3\u6784\u652f\u6301\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u5bb9\u6613\u4ea7\u751f\u6570\u636e\u5806\u79ef&#xff08;\u53c8\u79f0\u201c\u805a\u96c6\u201d&#xff09;&#xff0c;\u5373\u591a\u4e2a\u51b2\u7a81\u5143\u7d20\u8fde\u7eed\u5360\u636e\u7a7a\u4f4d\u7f6e&#xff0c;\u5bfc\u81f4\u540e\u7eed\u63d2\u5165\u548c\u67e5\u627e\u7684\u6548\u7387\u663e\u8457\u4e0b\u964d\u3002<\/li>\n<li>\u7a7a\u95f4\u5229\u7528\u7387\u8f83\u4f4e&#xff0c;\u7279\u522b\u662f\u5728\u88c5\u8f7d\u56e0\u5b50\u8f83\u9ad8\u65f6&#xff0c;\u51b2\u7a81\u9891\u7387\u589e\u52a0&#xff0c;\u6027\u80fd\u9000\u5316\u660e\u663e\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u7f13\u89e3\u4e0a\u8ff0\u95ee\u9898&#xff0c;\u53ef\u4ee5\u4f7f\u7528\u4e8c\u6b21\u63a2\u6d4b\u6cd5\u6216\u5176\u4ed6\u6539\u8fdb\u65b9\u6cd5\u4f18\u5316\u51b2\u7a81\u5904\u7406\u3002<\/p>\n<p><span style=\"color:#0d0016\">\u5f00\u6563\u5217&#xff08;\u94fe\u5730\u5740\u6cd5&#xff09;<\/span><\/p>\n<p>\u5f00\u6563\u5217\u901a\u8fc7\u4e3a\u6bcf\u4e2a\u54c8\u5e0c\u5730\u5740\u7ef4\u62a4\u4e00\u4e2a\u94fe\u8868\u6765\u89e3\u51b3\u51b2\u7a81&#xff1a;<\/p>\n<ul>\n<li>\u63d2\u5165&#xff1a;\u8ba1\u7b97\u54c8\u5e0c\u5730\u5740\u540e&#xff0c;\u5c06\u51b2\u7a81\u5143\u7d20\u6dfb\u52a0\u5230\u5bf9\u5e94\u94fe\u8868\u7684\u672b\u5c3e\u3002<\/li>\n<li>\u5220\u9664&#xff1a;\u76f4\u63a5\u4ece\u94fe\u8868\u4e2d\u5220\u9664\u76ee\u6807\u5143\u7d20&#xff0c;\u94fe\u8868\u7ed3\u6784\u786e\u4fdd\u4e0d\u4f1a\u5f71\u54cd\u5176\u4ed6\u5143\u7d20\u7684\u67e5\u627e\u3002<\/li>\n<\/ul>\n<p>\u4f18\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u7a7a\u95f4\u5229\u7528\u7387\u9ad8&#xff0c;\u80fd\u66f4\u597d\u5730\u9002\u5e94\u9ad8\u88c5\u8f7d\u56e0\u5b50\u3002<\/li>\n<li>\u51b2\u7a81\u5904\u7406\u7075\u6d3b&#xff0c;\u4e0d\u4f1a\u4ea7\u751f\u6570\u636e\u5806\u79ef&#xff0c;\u67e5\u627e\u6548\u7387\u76f8\u5bf9\u7a33\u5b9a\u3002<\/li>\n<\/ul>\n<p>\u7f3a\u70b9&#xff1a;<\/p>\n<ul>\n<li>\u9700\u8981\u989d\u5916\u7684\u94fe\u8868\u5b58\u50a8\u7a7a\u95f4\u3002<\/li>\n<li>\u94fe\u8868\u64cd\u4f5c\u589e\u52a0\u4e86\u4e00\u5b9a\u7684\u590d\u6742\u6027\u3002<\/li>\n<\/ul>\n<p><span style=\"color:#0d0016\">\u6f14\u793a\u4e24\u79cd\u65b9\u6cd5 {19,30,5,36,13,20,21,12,24,96} \u8fd9\u2f00\u7ec4\u503c\u6620\u5c04\u5230M&#061;11\u7684\u8868\u4e2d&#xff0c;\u91c7\u7528\u7684\u54c8\u5e0c\u51fd\u6570&#xff1a;<\/span><\/p>\n<p><span style=\"color:#fe2c24\">\u9664\u6cd5\u6563\u5217\u6cd5\u4e5f\u53eb\u505a\u9664\u7559\u4f59\u6570\u6cd5&#xff0c;\u987e\u540d\u601d\u4e49&#xff0c;\u5047\u8bbe\u54c8\u5e0c\u8868\u7684\u5927\u5c0f\u4e3aM&#xff0c;\u90a3\u4e48\u901a\u8fc7key\u9664\u4ee5M\u7684\u4f59\u6570\u4f5c\u4e3a\u6620\u5c04\u4f4d\u7f6e\u7684\u4e0b\u6807&#xff0c;\u4e5f\u5c31\u662f\u54c8\u5e0c\u51fd\u6570\u4e3a&#xff1a;h(key) &#061; key % M\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1072\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140934-680f8c1e283b5.png\" width=\"1200\" \/><\/p>\n<h2 id=\"3.%20%E9%97%AD%E6%95%A3%E5%88%97%E7%9A%84%E7%B2%BE%E7%A1%AE%E5%AE%9E%E7%8E%B0%EF%BC%9A%E4%BB%8E%E8%AE%BE%E8%AE%A1%E5%88%B0%E4%BC%98%E5%8C%96\" style=\"background-color:transparent\">3\u3001\u00a0\u95ed\u6563\u5217\u7684\u7cbe\u786e\u5b9e\u73b0&#xff1a;\u4ece\u8bbe\u8ba1\u5230\u4f18\u5316<\/h2>\n<h3 id=\"3.1%20%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E8%AE%BE%E8%AE%A1%EF%BC%9A%E9%9D%A2%E5%90%91%E6%89%A9%E5%B1%95%E7%9A%84%E6%9E%B6%E6%9E%84\" style=\"background-color:transparent\">3.1 \u6574\u4f53\u6846\u67b6\u8bbe\u8ba1&#xff1a;\u9762\u5411\u6269\u5c55\u7684\u67b6\u6784<\/h3>\n<p><span style=\"color:#fe2c24\">\u9996\u5148\u6211\u4eec\u9700\u8981\u8fdb\u884c\u4e00\u4e2a\u7b80\u5355\u7684\u6846\u67b6\u642d\u5efa&#xff1a;<\/span><\/p>\n<li><span style=\"color:#0d0016\">\u6211\u4eec\u9700\u8981\u4e00\u4e2aHashData\u7c7b&#xff0c;\u6765\u50a8\u5b58\u6570\u636e<\/span><\/li>\n<li><span style=\"color:#0d0016\">HashTable\u7c7b\u5e95\u5c42\u662fvector\u5bb9\u5668<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u56e0\u4e3a\u4f1a\u6709\u4e0d\u540c\u7c7b\u578b\u7684key&#xff0c;\u6240\u4ee5\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u4eff\u51fd\u6570\u6765\u5c06\u4e0d\u540c\u7c7b\u578b\u8f6c\u6362\u4e3asize_t&#xff0c;\u8fd9\u6837\u624d\u652f\u6301\u54c8\u5e0c\u51fd\u6570\u6620\u5c04<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u56e0\u4e3a\u95ed\u6563\u5217\u7684\u5220\u9664\u4e0d\u80fd\u76f4\u63a5\u5220\u9664\u8282\u70b9&#xff0c;\u5426\u5219\u4f1a\u5bfc\u81f4\u7ebf\u6027\u63a2\u6d4b\u5931\u6548&#xff0c;\u6240\u4ee5HashData\u7c7b\u91cc\u9700\u8981\u8bb0\u5f55\u72b6\u6001<\/span><\/li>\n<p>\/\/\u7248\u672c\u4e00 &#8212; \u5f00\u653e\u5730\u5740\u6cd5&#xff08;\u95ed\u6563\u5217&#xff09;<br \/>\n#include&lt;utility&gt;<br \/>\n#include&lt;iostream&gt;<br \/>\n#include&lt;vector&gt;<br \/>\nusing namespace std;<\/p>\n<p>\/\/\u8282\u70b9\u72b6\u6001<br \/>\nenum status<br \/>\n{<br \/>\nEXIST,<br \/>\nEMPTY,<br \/>\nDELETE<br \/>\n};<br \/>\n\/\/\u8bbe\u8ba1\u8282\u70b9<br \/>\ntemplate&lt;class K, class V&gt;<br \/>\nstruct HashData<br \/>\n{<br \/>\n\/\/\u952e\u503c\u5bf9<br \/>\npair&lt;K, V&gt; _kv;<br \/>\n\/\/\u72b6\u6001<br \/>\nstatus _status;<br \/>\n};<br \/>\n\/\/ kv\u952e\u503c&#xff0c;\u4eff\u51fd\u6570\u89e3\u51b3\u4e0d\u540c\u7c7b\u578bkey\u8f6c\u6362\u4e3asize_t\u7c7b\u578b\u7684\u4e0b\u6807<br \/>\ntemplate&lt;class K,class V,class Hash&#061;HashFunc&lt;K&gt;&gt;<br \/>\nclass HashTable<br \/>\n{<br \/>\npublic:<br \/>\nHashTable()<br \/>\n{<br \/>\n_table.resize(10);<br \/>\n}<br \/>\nprivate:<br \/>\n\/\/\u5e95\u5c42\u662fvector\u5bb9\u5668<br \/>\nvector&lt;HashData K, V&gt;&gt; _table;<br \/>\nsize_t _n;\/\/\u6709\u6548\u6570\u636e\u4e2a\u6570<br \/>\n    Hash hs&#xff1b;\/\/\u4eff\u51fd\u6570<br \/>\n};<\/p>\n<h3 id=\"3.2%20%E4%BB%BF%E5%87%BD%E6%95%B0%E7%9A%84%E7%81%B5%E6%B4%BB%E6%80%A7%EF%BC%9A%E9%AB%98%E6%95%88%E5%93%88%E5%B8%8C%E7%9A%84%E5%85%B3%E9%94%AE\" style=\"background-color:transparent\">3.2 \u4eff\u51fd\u6570\u7684\u7075\u6d3b\u6027&#xff1a;\u9ad8\u6548\u54c8\u5e0c\u7684\u5173\u952e<\/h3>\n<p><span style=\"color:#0d0016\">\u4eff\u51fd\u6570\u7684\u4f5c\u7528\u662f\u5c06\u4e0d\u540c\u6570\u636e\u7c7b\u578b\u7684key\u8f6c\u6362\u4e3a\u53ef\u4ee5\u4f7f\u7528\u7684size_t\u7c7b\u578b&#xff0c;\u8fd9\u6837\u53ef\u4ee5\u624d\u80fd\u4e00 \u4e00\u6620\u5c04\u8fc7\u53bb<\/span> \u5bf9\u4e8e\u53ef\u4ee5\u76f4\u63a5\u663e\u793a\u7c7b\u578b\u8f6c\u6362\u7684\u7c7b\u578b\u76f4\u63a5\u8f6c\u6362\u5373\u53ef\u3002\u800c\u5bf9\u4e8e\u4e0d\u80fd\u76f4\u63a5\u8f6c\u6362\u7684\u7c7b\u578b&#xff08;\u6bd4\u5982string&#xff09;\u5c31\u8981\u8fdb\u884c\u7279\u6b8a\u5904\u7406\u4e86&#xff01;<\/p>\n<p>template&lt;class K&gt;<br \/>\nstruct HashFunc<br \/>\n{<br \/>\nsize_t operator()(const K&amp; key)<br \/>\n{<br \/>\nreturn (size_t)key;<br \/>\n}<br \/>\n};<\/p>\n<p>template&lt;&gt;<br \/>\nstruct HashFunc&lt;string&gt;<br \/>\n{<br \/>\nsize_t operator()(const string&amp; s)<br \/>\n{<br \/>\n\/\/ BKDR<br \/>\nsize_t hash &#061; 0;<br \/>\nfor (auto ch : s)<br \/>\n{<br \/>\nhash &#043;&#061; ch;<br \/>\nhash *&#061; 131;<br \/>\n}<\/p>\n<p>return hash;<br \/>\n}<br \/>\n};<\/p>\n<h3 id=\"3.3%20%E6%8F%92%E5%85%A5%E6%93%8D%E4%BD%9C%EF%BC%9A%E5%86%B2%E7%AA%81%E6%A3%80%E6%B5%8B%E4%B8%8E%E4%BD%8D%E7%BD%AE%E5%88%86%E9%85%8D\" style=\"background-color:transparent\">3.3 \u63d2\u5165\u64cd\u4f5c&#xff1a;\u51b2\u7a81\u68c0\u6d4b\u4e0e\u4f4d\u7f6e\u5206\u914d<\/h3>\n<li><span style=\"color:#0d0016\">\u9996\u5148\u63d2\u5165\u4e4b\u524d\u8981\u5148\u68c0\u67e5\u662f\u5426\u5728\u54c8\u5e0c\u8868\u4e2d\u5df2\u7ecf\u6709\u6570\u636e\u4e86<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u7136\u540e\u68c0\u67e5\u8be5\u6b21\u662f\u5426\u9700\u8981\u8fdb\u884c\u6269\u5bb9<\/span><\/li>\n<li><span style=\"color:#0d0016\">\u901a\u8fc7key\u503c\u9009\u53d6\u5408\u9002\u4f4d\u7f6e\u8fdb\u884c\u63d2\u5165&#xff0c;\u6709\u6548\u4e2a\u6570&#043;&#043;<\/span><\/li>\n<p>bool insert(const pair&lt;K, V&gt;&amp;kv)<br \/>\n{<br \/>\n\/\/\u63d2\u5165\u524d\u5148\u8fdb\u884c\u4e00\u4e2a\u68c0\u67e5<br \/>\nif (Find(kv.first)) return false;<br \/>\n\/\/\u662f\u5426\u9700\u8981\u6269\u5bb9<br \/>\nif (_n &gt;&#061; _table.size() * 0.7)<br \/>\n{<br \/>\n\/\/\u8fdb\u884c\u66ff\u6362<br \/>\nHashTable&lt;K, V&gt; newHT;<br \/>\nnewHT._table.resize(_table.size() * 2);<br \/>\n\/\/\u8fdb\u884c\u8d4b\u503c<br \/>\nfor (auto &amp;s : _table)<br \/>\n{<br \/>\nif (s._status &#061;&#061; EXIST)<br \/>\nnewHT.insert(s._kv);<br \/>\n}<br \/>\n\/\/\u8fdb\u884c\u66ff\u6362&#xff01;&#xff01;&#xff01;<br \/>\n_table.swap(newHT._table);<br \/>\n}<br \/>\n\/\/\u8fdb\u884c\u63d2\u5165<br \/>\n\/\/hash\u5730\u5740<br \/>\nsize_t hash0 &#061; kv.first % _table.size();<br \/>\nsize_t hashi &#061; hash0;<br \/>\nsize_t i &#061; 1;<br \/>\n\/\/\u5bfb\u627e\u5408\u9002\u4f4d\u7f6e\u8fdb\u884c\u63d2\u5165<br \/>\n\/\/ \u7ebf\u6027\u63a2\u6d4b<br \/>\nwhile (_table[hashi].status &#061;&#061; EXIST)<br \/>\n{<br \/>\nhashi &#061; (hash0 &#043; i) % _table.size(); \/\/\u53d6\u6a21\u89e3\u51b3\u56de\u7ed5\u95ee\u9898<br \/>\n&#043;&#043;i;<br \/>\n}<br \/>\n\/\/\u627e\u5230\u5408\u9002\u4f4d\u7f6e\u4e86\u8fdb\u884c\u63d2\u5165<br \/>\n_table[hashi]._kv &#061; kv;<br \/>\n_table[hashi].status &#061; EXIST;<br \/>\n_n&#043;&#043;;<br \/>\nreturn true;<br \/>\n} <\/p>\n<h3 id=\"3.4%20%E6%9F%A5%E6%89%BE%E6%93%8D%E4%BD%9C%EF%BC%9A%E9%80%9F%E5%BA%A6%E4%B8%8E%E5%87%86%E7%A1%AE%E7%9A%84%E5%8F%8C%E9%87%8D%E4%BF%9D%E9%9A%9C\" style=\"background-color:transparent\">3.4 \u67e5\u627e\u64cd\u4f5c&#xff1a;\u901f\u5ea6\u4e0e\u51c6\u786e\u7684\u53cc\u91cd\u4fdd\u969c<\/h3>\n<p>\u67e5\u627e\u903b\u8f91\u5f88\u7b80\u5355\u5bf9Key\u8fdb\u884c\u7ebf\u6027\u63a2\u6d4b\u5373\u53ef<\/p>\n<p>HashData&lt;K, V&gt;* Find(const K&amp; key)<br \/>\n{<br \/>\nsize_t hash0 &#061; hs(key) % _table.size();<br \/>\nsize_t hashi &#061; hash0;<br \/>\nsize_t i &#061; 1;<br \/>\nwhile (_table[hashi]._state !&#061; EMPTY)<br \/>\n{<br \/>\nif (_table[hashi]._state &#061;&#061; EXIST<br \/>\n&amp;&amp; _table[hashi]._kv.first &#061;&#061; key) \/\/\u8fd9\u91cc\u9700\u8981\u52a0\u5224\u65ad\u72b6\u6001\u8bed\u53e5 \u6211\u4eec\u5220\u9664\u53ea\u662f\u8be5\u72b6\u6001<br \/>\n{<br \/>\nreturn &amp;_table[hashi];<br \/>\n}<\/p>\n<p>\/\/ \u7ebf\u6027\u63a2\u6d4b<br \/>\nhashi &#061; (hash0 &#043; i) % _table.size();<br \/>\n&#043;&#043;i;<br \/>\n}<br \/>\nreturn nullptr;<br \/>\n} <\/p>\n<h3 id=\"3.5%20%E5%88%A0%E9%99%A4%E6%93%8D%E4%BD%9C%EF%BC%9A%E6%A0%87%E8%AE%B0%E4%B8%8E%E9%87%8D%E6%9E%84%E7%9A%84%E7%BB%86%E8%8A%82%E4%BC%98%E5%8C%96\" style=\"background-color:transparent\">3.5 \u5220\u9664\u64cd\u4f5c&#xff1a;\u6807\u8bb0\u4e0e\u91cd\u6784\u7684\u7ec6\u8282\u4f18\u5316<\/h3>\n<p>\u5220\u9664\u5148\u901a\u8fc7key\u627e\u5230\u9700\u8981\u5220\u9664\u7684\u6570\u636e \u7136\u540e\u5c06\u72b6\u6001\u8bbe\u7f6e\u4e3aDELETE\u00a0, \u6709\u6548\u4e2a\u6570- &#8211;<\/p>\n<p>\/\/\u5220\u9664<br \/>\nbool Erase(const K&amp; key)<br \/>\n{<br \/>\n\/\/size_t hash0 &#061; hs(key) % _tables.size();<br \/>\n\/\/size_t hashi &#061; hash0;<br \/>\n\/\/size_t i &#061; 1;<br \/>\n\/\/while (_table[hashi]._state !&#061; EMPTY)<br \/>\n\/\/{<br \/>\n\/\/if (_table[hashi]._state &#061;&#061; EXIST<br \/>\n\/\/&amp;&amp; _table[hashi]._kv.first &#061;&#061; key)<br \/>\n\/\/{<br \/>\n\/\/_table[hashi].status &#061; DELETE;<br \/>\n\/\/&#8211;_n;<br \/>\n\/\/return true;<br \/>\n\/\/}<br \/>\n\/\/\/\/ \u7ebf\u6027\u63a2\u6d4b<br \/>\n\/\/hashi &#061; (hash0 &#043; i) % _tables.size();<br \/>\n\/\/&#043;&#043;i;<br \/>\n\/\/}<br \/>\n\/\/return  false;<\/p>\n<p>\/\/\u590d\u7528 Find<br \/>\nHashData&lt;K, V&gt;* ret &#061; Find(key);<br \/>\nif (ret &#061;&#061; nullptr)<br \/>\n{<br \/>\nreturn false;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nret-&gt;_status &#061; DELETE;<br \/>\n&#8211;_n;<br \/>\nreturn true;<br \/>\n}<br \/>\n} <\/p>\n<p><span style=\"color:#0d0016\">\u8fd9\u91cc\u4e00\u5f00\u59cb\u7684\u7a7a\u95f4\u673a\u5236\u53ef\u4ee5\u4f18\u5316&#xff0c;\u6211\u4eec\u4f7f\u7528\u7684\u54c8\u5e0c\u51fd\u6570\u662f\u9664\u6cd5\u6563\u5217\u6cd5\u4e5f\u53eb\u505a\u9664\u7559\u4f59\u6570\u6cd5&#xff0c;\u5f53\u4f7f\u7528\u9664\u6cd5\u6563\u5217\u6cd5\u65f6&#xff0c;\u5efa\u8baeM\u53d6\u4e0d\u592a\u63a5\u8fd12\u7684\u6574\u6570\u6b21\u51a5\u7684\u2f00\u4e2a\u8d28\u6570(\u7d20\u6570)\u3002\u8fd9\u6837\u53ef\u4ee5\u6709\u6548\u907f\u514d\u54c8\u5e0c\u51b2\u7a81<\/span><\/p>\n<p><span style=\"color:#0d0016\">\u4f18\u5316\u4e00\u4e0b&#xff1a;\u91c7\u7528\u63a5\u8fd12\u500d\u6269\u5bb9\u7684\u7d20\u6570\u8868\u8fdb\u884c\u5f00\u8f9f\u7a7a\u95f4\u6269\u5bb9<\/span> \u00a0<\/p>\n<p>class HashTable<br \/>\n{<br \/>\npublic:<br \/>\nHashTable()<br \/>\n:_tables(__stl_next_prime(0))<br \/>\n, _n(0)<br \/>\n{}<\/p>\n<p>inline unsigned long __stl_next_prime(unsigned long n)<br \/>\n{<br \/>\n\/\/ Note: assumes long is at least 32 bits.<br \/>\nstatic const int __stl_num_primes &#061; 28;<br \/>\nstatic const unsigned long __stl_prime_list[__stl_num_primes] &#061; {<br \/>\n53, 97, 193, 389, 769,<br \/>\n1543, 3079, 6151, 12289, 24593,<br \/>\n49157, 98317, 196613, 393241, 786433,<br \/>\n1572869, 3145739, 6291469, 12582917, 25165843,<br \/>\n50331653, 100663319, 201326611, 402653189, 805306457,<br \/>\n1610612741, 3221225473, 4294967291<br \/>\n};<br \/>\nconst unsigned long* first &#061; __stl_prime_list;<br \/>\nconst unsigned long* last &#061; __stl_prime_list &#043; __stl_num_primes;<br \/>\nconst unsigned long* pos &#061; lower_bound(first, last, n);<br \/>\nreturn pos &#061;&#061; last ? *(last &#8211; 1) : *pos;<br \/>\n}<\/p>\n<p>bool Insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nif (Find(kv.first))<br \/>\nreturn 0;<\/p>\n<p>\/\/\u8d1f\u8f7d\u56e0\u5b50&gt;&#061;0.7 \u6269\u5bb9<br \/>\nif (_n*10 \/ _tables.size() &gt;&#061; 7)<br \/>\n{<br \/>\nHashTable&lt;K, V&gt;newht;<br \/>\nnewht._tables.resize(__stl_next_prime(_tables.size() &#043; 1));<br \/>\nfor (auto&amp; data : _tables)<br \/>\n{<br \/>\n\/\/ \u65e7\u8868\u7684\u6570\u636e\u6620\u5c04\u5230\u65b0\u8868<br \/>\nif (data._state &#061;&#061; EXIST)<br \/>\n{<br \/>\nnewht.Insert(data._kv);<br \/>\n}<br \/>\n}<br \/>\n_tables.swap(newht._tables);<\/p>\n<p>}<br \/>\nsize_t hash0 &#061; kv.first % _tables.size();<br \/>\nsize_t hashi &#061; hash0;<br \/>\nsize_t i &#061; 1;<br \/>\nint flag &#061; 1;<\/p>\n<p>while (_tables[hashi]._state &#061;&#061; EXIST)<br \/>\n{<br \/>\n\/\/ \u7ebf\u6027\u63a2\u6d4b<br \/>\nhashi &#061; (hash0 &#043; i) % _tables.size();<br \/>\n&#043;&#043;i;<\/p>\n<p>\/*hashi &#061; (hash0 &#043; (i*i*flag)) % _tables.size();<br \/>\nif (hashi &lt; _tables.size())<br \/>\nhashi &#043;&#061; _tables.size();<\/p>\n<p>if (flag &#061;&#061; 1)<br \/>\n{<br \/>\nflag &#061; -1;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n&#043;&#043;i;<br \/>\nflag &#061; 1;<br \/>\n}*\/<br \/>\n}<\/p>\n<p>_tables[hashi]._kv &#061; kv;<br \/>\n_tables[hashi]._state &#061; EXIST;<br \/>\n&#043;&#043;_n;<\/p>\n<p>return 1;<br \/>\n}<\/p>\n<p>HashData&lt;K, V&gt;* Find(const K&amp; key)<br \/>\n{<br \/>\nsize_t hash0 &#061; key % _tables.size();<br \/>\nsize_t hashi &#061; hash0;<br \/>\nsize_t i &#061; 1;<br \/>\nwhile (_tables[hashi]._state !&#061; EMPTY)<br \/>\n{<br \/>\nif (_tables[hashi]._state &#061;&#061; EXIST<br \/>\n&amp;&amp; _tables[hashi]._kv.first &#061;&#061; key)<br \/>\n{<br \/>\nreturn &amp;_tables[hashi];<br \/>\n}<\/p>\n<p>\/\/ \u7ebf\u6027\u63a2\u6d4b<br \/>\nhashi &#061; (hash0 &#043; i) % _tables.size();<br \/>\n&#043;&#043;i;<br \/>\n}<\/p>\n<p>return nullptr;<br \/>\n}<\/p>\n<p>bool Erase(const K&amp; key)<br \/>\n{<br \/>\nHashData&lt;K, V&gt;* ret &#061; Find(key);<br \/>\nif (ret &#061;&#061; nullptr)<br \/>\nreturn 0;<br \/>\nret-&gt;_state &#061; DELETE;<br \/>\nreturn 1;<\/p>\n<p>}<br \/>\nprivate:<br \/>\nvector&lt;HashData&lt;K, V&gt;&gt; _tables;<br \/>\nsize_t _n; \/\/\u6807\u8bc6\u6570\u636e\u4e2a\u6570<br \/>\n}; <\/p>\n<p><span style=\"color:#fe2c24\">\u8fd9\u6837\u6211\u4eec\u5c31\u5b9e\u73b0\u4e86\u5f00\u53d1\u5730\u5740\u6cd5&#xff08;\u95ed\u6563\u5217&#xff09;\u7684\u54c8\u5e0c\u8868&#xff01;&#xff01;&#xff01;<\/span><\/p>\n<h2 id=\"4.%20%E5%BC%80%E6%95%A3%E5%88%97%E7%9A%84%E7%81%B5%E6%B4%BB%E5%AE%9E%E7%8E%B0%EF%BC%9A%E4%BB%8E%E5%9F%BA%E7%A1%80%E5%88%B0%E9%AB%98%E6%95%88\" style=\"background-color:transparent\">4. \u5f00\u6563\u5217\u7684\u7075\u6d3b\u5b9e\u73b0&#xff1a;\u4ece\u57fa\u7840\u5230\u9ad8\u6548<\/h2>\n<p>\u6211\u4eec\u5df2\u7ecf\u5b9e\u73b0\u4e86\u95ed\u6563\u5217\u7248\u672c\u7684\u54c8\u5e0c\u8868&#xff0c;\u4eca\u5929\u8ba1\u5212\u5b9e\u73b0\u53e6\u4e00\u79cd\u54c8\u5e0c\u8868\u7684\u5b9e\u73b0\u65b9\u5f0f\u2014\u2014<span style=\"color:#fe2c24\">\u5f00\u6563\u5217\u7248\u672c&#xff08;\u4e5f\u79f0\u54c8\u5e0c\u6876&#xff09;<\/span>\u3002\u5728\u6df1\u5165\u5b9e\u73b0\u4e4b\u524d&#xff0c;\u8ba9\u6211\u4eec\u5148\u5206\u6790\u6240\u9700\u7684\u5de5\u4f5c\u3002<\/p>\n<p><span style=\"color:#0d0016\">\u5f00\u6563\u5217\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u54c8\u5e0c\u8868\u8bbe\u8ba1\u4e3a\u4e00\u4e2a\u6570\u7ec4&#xff0c;\u6bcf\u4e2a\u6570\u7ec4\u5143\u7d20\u5bf9\u5e94\u4e00\u4e2a\u6620\u5c04\u5730\u5740\u3002\u4e3a\u4e86\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81&#xff0c;\u5f00\u6563\u5217\u91c7\u7528\u94fe\u8868\u7684\u5f62\u5f0f\u5c06\u51b2\u7a81\u7684\u5143\u7d20\u94fe\u63a5\u8d77\u6765&#xff0c;\u4ece\u800c\u786e\u4fdd\u9ad8\u6548\u7684\u67e5\u627e\u548c\u63d2\u5165\u64cd\u4f5c\u3002<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"591\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140934-680f8c1ef205b.png\" width=\"1200\" \/><\/p>\n<p>\u7531\u4e8e\u6d89\u53ca\u5230\u94fe\u8868\u7684\u4f7f\u7528&#xff0c;\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5229\u7528 STL\u00a0\u5e93\u7684 list \u6570\u636e\u7ed3\u6784\u3002\u7136\u800c&#xff0c;list \u672c\u8d28\u4e0a\u662f\u4e00\u4e2a\u53cc\u5411\u5faa\u73af\u94fe\u8868&#xff0c;\u5bf9\u6211\u4eec\u8fd9\u6837\u7b80\u5355\u7684\u573a\u666f\u6765\u8bf4&#xff0c;\u53ef\u80fd\u663e\u5f97\u6709\u4e9b\u590d\u6742\u4e14\u4e0d\u591f\u9ad8\u6548\u3002\u4e3a\u4e86\u7b80\u5316\u5b9e\u73b0\u5e76\u8282\u7701\u5185\u5b58\u7a7a\u95f4&#xff0c;\u6211\u4eec\u53ef\u4ee5\u81ea\u884c\u6784\u9020\u4e00\u4e2a\u7b80\u5355\u7684\u5355\u5411\u975e\u5faa\u73af\u94fe\u8868&#xff0c;\u5b8c\u5168\u53ef\u4ee5\u6ee1\u8db3\u9700\u6c42&#xff0c;\u540c\u65f6\u8282\u7701\u7ea6\u4e00\u534a\u7684\u7a7a\u95f4\u3002<\/p>\n<p>\u901a\u8fc7\u8fd9\u4e00\u8bbe\u8ba1&#xff0c;\u6211\u4eec\u65e2\u53ef\u4ee5\u907f\u514d\u95ed\u6563\u5217\u4e2d\u53ef\u80fd\u51fa\u73b0\u7684\u8fc7\u591a\u63a2\u6d4b\u95ee\u9898&#xff0c;\u53c8\u80fd\u4ee5\u8f83\u4f4e\u7684\u5b9e\u73b0\u6210\u672c\u6784\u5efa\u4e00\u4e2a\u529f\u80fd\u5b8c\u5907\u4e14\u9ad8\u6548\u7684\u54c8\u5e0c\u8868\u3002<\/p>\n<h3 id=\"4.1%20%E8%8A%82%E7%82%B9%E8%AE%BE%E8%AE%A1%EF%BC%9A%E6%8C%87%E9%92%88%E4%B8%8E%E6%95%B0%E6%8D%AE%E7%9A%84%E5%B9%B3%E8%A1%A1%E8%89%BA%E6%9C%AF\">4.1 \u8282\u70b9\u8bbe\u8ba1&#xff1a;\u6307\u9488\u4e0e\u6570\u636e\u7684\u5e73\u8861\u827a\u672f<\/h3>\n<p>\u56e0\u4e3a\u6211\u4eec\u8981\u5b9e\u73b0\u5355\u94fe\u8868\u7ed3\u6784&#xff0c;\u80af\u5b9a\u8981\u6765\u5148\u8bbe\u8ba1\u4e00\u4e0b\u8282\u70b9\u6846\u67b6&#xff1a;<\/p>\n<p>\/\/\u8282\u70b9\u8bbe\u8ba1<br \/>\ntemplate&lt;class K, class V, class Hash &#061; HashFunc&lt;K&gt;&gt;<br \/>\nstruct HashNode<br \/>\n{<br \/>\n\/\/\u50a8\u5b58\u7684\u6570\u636e<br \/>\npair&lt;K, V&gt; _kv;<br \/>\n\/\/\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488<br \/>\nHashNode&lt;K, V&gt;* _next;<br \/>\nHashNode(const pair&lt;K, V&gt;&amp; kv)<br \/>\n:_kv(kv)<br \/>\n, _next(nullptr)<br \/>\n{}<br \/>\n}; <\/p>\n<h3 id=\"4.2%20%E6%95%B4%E4%BD%93%E6%A1%86%E6%9E%B6%E6%90%AD%E5%BB%BA%EF%BC%9A%E9%93%BE%E8%A1%A8%E4%B8%8E%E9%80%BB%E8%BE%91%E7%9A%84%E8%9E%8D%E5%90%88\" style=\"background-color:transparent\">4.2 \u6574\u4f53\u6846\u67b6\u642d\u5efa&#xff1a;\u94fe\u8868\u4e0e\u903b\u8f91\u7684\u878d\u5408<\/h3>\n<p>\u8bbe\u8ba1\u5b8c\u6210\u8282\u70b9\u540e&#xff0c;\u5c31\u53ef\u4ee5\u7740\u624b\u6784\u5efa\u6574\u4f53\u6846\u67b6\u4e86\u3002\u54c8\u5e0c\u6876\u7684\u5e95\u5c42\u7ed3\u6784\u901a\u5e38\u7531\u4e00\u4e2a\u6307\u9488\u6570\u7ec4\u6784\u6210&#xff0c;\u540c\u65f6\u9700\u8981\u5f15\u5165\u4e00\u4e2a\u53d8\u91cf\u7528\u4e8e\u8bb0\u5f55\u5f53\u524d\u6709\u6548\u5143\u7d20\u7684\u4e2a\u6570&#xff0c;\u4ee5\u4fbf\u5728<span style=\"color:#0d0016\">\u8d1f\u8f7d\u56e0\u5b50\u8fc7\u9ad8\u65f6\u53ca\u65f6\u89e6\u53d1\u6269\u5bb9\u64cd\u4f5c<\/span>\u3002<\/p>\n<p>\u5b9e\u73b0\u7684\u6838\u5fc3\u529f\u80fd\u5305\u62ec\u63d2\u5165\u3001\u5220\u9664\u548c\u67e5\u627e\u4e09\u4e2a\u57fa\u672c\u64cd\u4f5c\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f&#xff0c;\u4e0d\u540c\u7c7b\u578b\u7684\u6570\u636e\u5728\u63d2\u5165\u65f6\u9700\u8981\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u8f6c\u6362\u4e3a size_t \u7c7b\u578b&#xff0c;\u8fd9\u6837\u624d\u80fd\u5c06\u6570\u636e\u6b63\u786e\u6620\u5c04\u5230\u6570\u7ec4\u4e2d\u7684\u5bf9\u5e94\u4f4d\u7f6e\u3002<\/p>\n<p>\u5728\u5b9e\u73b0\u8fd9\u4e9b\u529f\u80fd\u65f6&#xff0c;\u9700\u8981\u91cd\u70b9\u5173\u6ce8\u4ee5\u4e0b\u51e0\u70b9&#xff1a;<\/p>\n<li>\u63d2\u5165\u64cd\u4f5c&#xff1a;\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u786e\u5b9a\u76ee\u6807\u4f4d\u7f6e&#xff0c;\u5904\u7406\u53ef\u80fd\u7684\u51b2\u7a81&#xff0c;\u5c06\u65b0\u5143\u7d20\u63d2\u5165\u5230\u5bf9\u5e94\u94fe\u8868\u4e2d\u3002<\/li>\n<li>\u5220\u9664\u64cd\u4f5c&#xff1a;\u67e5\u627e\u5230\u76ee\u6807\u4f4d\u7f6e\u7684\u94fe\u8868\u8282\u70b9\u5e76\u5220\u9664&#xff0c;\u540c\u65f6\u9700\u8981\u59a5\u5584\u5904\u7406\u94fe\u8868\u8fde\u63a5\u3002<\/li>\n<li>\u67e5\u627e\u64cd\u4f5c&#xff1a;\u6839\u636e\u54c8\u5e0c\u51fd\u6570\u5b9a\u4f4d\u5230\u76ee\u6807\u94fe\u8868&#xff0c;\u7136\u540e\u987a\u5e8f\u67e5\u627e\u76ee\u6807\u8282\u70b9\u3002<\/li>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u57fa\u672c\u529f\u80fd\u7684\u5b9e\u73b0&#xff0c;\u6211\u4eec\u53ef\u4ee5\u6784\u5efa\u4e00\u4e2a\u9ad8\u6548\u7684\u54c8\u5e0c\u6876\u7ed3\u6784&#xff0c;\u4e3a\u540e\u7eed\u529f\u80fd\u6269\u5c55\u548c\u4f18\u5316\u6253\u4e0b\u575a\u5b9e\u57fa\u7840\u3002<\/p>\n<p>namespace hash_bucket<br \/>\n{<br \/>\n    \/\/\u4eff\u51fd\u6570 \u8f6c\u6574\u5f62<br \/>\ntemplate&lt;class K&gt;<br \/>\nstruct HashFunc<br \/>\n{<br \/>\nsize_t operator()(const K&amp; key)<br \/>\n{<br \/>\nreturn (size_t)key;<br \/>\n}<br \/>\n};<\/p>\n<p>template&lt;&gt;<br \/>\nstruct HashFunc&lt;string&gt;<br \/>\n{<br \/>\nsize_t operator()(const string&amp; s)<br \/>\n{<br \/>\n\/\/ BKDR<br \/>\nsize_t hash &#061; 0;<br \/>\nfor (auto ch : s)<br \/>\n{<br \/>\nhash &#043;&#061; ch;<br \/>\nhash *&#061; 131;<br \/>\n}<\/p>\n<p>return hash;<br \/>\n}<br \/>\n};<\/p>\n<p>\/\/\u8282\u70b9\u8bbe\u8ba1<br \/>\ntemplate&lt;class K, class V, class Hash &#061; HashFunc&lt;K&gt;&gt;<br \/>\nstruct HashNode<br \/>\n{<br \/>\n\/\/\u50a8\u5b58\u7684\u6570\u636e<br \/>\npair&lt;K, V&gt; _kv;<br \/>\n\/\/\u4e0b\u4e00\u4e2a\u8282\u70b9\u7684\u6307\u9488<br \/>\nHashNode&lt;K, V&gt;* _next;<br \/>\nHashNode(const pair&lt;K, V&gt;&amp; kv)<br \/>\n:_kv(kv)<br \/>\n, _next(nullptr)<br \/>\n{}<br \/>\n};<\/p>\n<p>                \/\/\u5f00\u6563\u5217\u7684\u54c8\u5e0c\u8868<br \/>\n\/\/       key           value      \u4eff\u51fd\u6570&#xff08;\u8f6c\u6362\u4e3asize_t&#xff09;<br \/>\ntemplate&lt;class K, class V, class Hash &#061; HashFunc&lt;K&gt;&gt;<br \/>\nclass HashTable<br \/>\n{<br \/>\ntypedef HashNode&lt;K, V&gt; Node;<\/p>\n<p>inline unsigned long __stl_next_prime(unsigned long n)<br \/>\n{<br \/>\nstatic const int __stl_num_primes &#061; 28;<br \/>\nstatic const unsigned long __stl_prime_list[__stl_num_primes] &#061;<br \/>\n{<br \/>\n53, 97, 193, 389, 769,<br \/>\n1543, 3079, 6151, 12289, 24593,<br \/>\n49157, 98317, 196613, 393241, 786433,<br \/>\n1572869, 3145739, 6291469, 12582917, 25165843,<br \/>\n50331653, 100663319, 201326611, 402653189, 805306457,<br \/>\n1610612741, 3221225473, 4294967291<br \/>\n};<br \/>\nconst unsigned long* first &#061; __stl_prime_list;<br \/>\nconst unsigned long* last &#061; __stl_prime_list &#043;<br \/>\n__stl_num_primes;<br \/>\nconst unsigned long* pos &#061; lower_bound(first, last, n);<br \/>\nreturn pos &#061;&#061; last ? *(last &#8211; 1) : *pos;<br \/>\n}<br \/>\npublic:<br \/>\nHashTable()<br \/>\n:_tables(__stl_next_prime(0))<br \/>\n,_n(0)<br \/>\n{}<br \/>\nprivate:<br \/>\nvector&lt;Node*&gt; _tables; \/\/ \u6307\u9488\u6570\u7ec4<br \/>\n\/\/vector&lt;list&lt;pair&lt;K, V&gt;&gt;&gt; _tables;<br \/>\nsize_t _n &#061; 0; \/\/ \u8868\u4e2d\u5b58\u50a8\u6570\u636e\u4e2a\u6570<\/p>\n<p>\/\/struct Date<br \/>\n\/\/{<br \/>\n\/\/list&lt;pair&lt;K, V&gt;&gt;_list;<br \/>\n\/\/map&lt;pair&lt;K, V&gt;&gt;_map;<br \/>\n\/\/size_t len; \/\/ len &lt;&#061;8 _list &gt;8 \u5b58_map \u7ea2\u9ed1\u6811<br \/>\n\/\/};<br \/>\n\/\/vector&lt;Date&gt;_tables;<br \/>\n\/\/size_t  n &#061; 0;<br \/>\n};<\/p>\n<p>} <\/p>\n<h3 id=\"4.3%20%E6%8F%92%E5%85%A5%E5%87%BD%E6%95%B0%EF%BC%9A%E9%93%BE%E8%A1%A8%E6%8B%93%E5%B1%95%E4%B8%8E%E5%93%88%E5%B8%8C%E5%88%86%E5%B8%83\" style=\"background-color:transparent\">4.3 \u63d2\u5165\u51fd\u6570&#xff1a;\u94fe\u8868\u62d3\u5c55\u4e0e\u54c8\u5e0c\u5206\u5e03<\/h3>\n<p>\u5b9e\u73b0\u63d2\u5165\u51fd\u6570\u65f6&#xff0c;\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c&#xff1a;<\/p>\n<li>\u68c0\u67e5\u952e\u662f\u5426\u5b58\u5728&#xff1a;\u5728\u63d2\u5165\u65b0\u5143\u7d20\u4e4b\u524d&#xff0c;\u9996\u5148\u68c0\u67e5\u5f53\u524d\u952e\u662f\u5426\u5df2\u7ecf\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d&#xff0c;\u53ea\u6709\u5728\u952e\u4e0d\u5b58\u5728\u65f6\u624d\u8fdb\u884c\u63d2\u5165\u64cd\u4f5c\u3002<\/li>\n<li>\u68c0\u67e5\u662f\u5426\u9700\u8981\u6269\u5bb9&#xff1a;\u6839\u636e\u5f53\u524d\u7684\u8d1f\u8f7d\u56e0\u5b50&#xff08;\u6709\u6548\u5143\u7d20\u6570\u4e0e\u6876\u5bb9\u91cf\u7684\u6bd4\u503c&#xff09;&#xff0c;\u5224\u65ad\u662f\u5426\u9700\u8981\u6269\u5bb9\u4ee5\u4fdd\u8bc1\u64cd\u4f5c\u7684\u6548\u7387\u3002<\/li>\n<li>\u8ba1\u7b97\u54c8\u5e0c\u503c\u5e76\u5b9a\u4f4d\u6620\u5c04\u4f4d\u7f6e&#xff1a;\u5229\u7528\u4eff\u51fd\u6570\u8ba1\u7b97\u952e\u7684\u54c8\u5e0c\u503c hashi&#xff0c;\u4ece\u800c\u786e\u5b9a\u5176\u5728\u54c8\u5e0c\u8868\u4e2d\u7684\u6620\u5c04\u4f4d\u7f6e\u3002<\/li>\n<li>\u521b\u5efa\u5e76\u63d2\u5165\u65b0\u8282\u70b9&#xff1a;\u6784\u9020\u4e00\u4e2a\u65b0\u8282\u70b9&#xff0c;\u5e76\u91c7\u7528\u5934\u63d2\u6cd5\u5c06\u5176\u63d2\u5165\u5230\u5bf9\u5e94\u4f4d\u7f6e\u7684\u94fe\u8868\u4e2d\u3002<\/li>\n<p>\u5173\u4e8e\u6269\u5bb9\u903b\u8f91&#xff0c;\u9700\u8981\u7279\u522b\u6ce8\u610f\u4f18\u5316\u5b9e\u73b0\u3002\u6700\u76f4\u89c2\u7684\u65b9\u6cd5\u662f\u904d\u5386\u539f\u54c8\u5e0c\u8868&#xff0c;\u5c06\u6570\u636e\u91cd\u65b0\u63d2\u5165\u5230\u65b0\u7684\u54c8\u5e0c\u8868\u4e2d&#xff0c;\u5e76\u91ca\u653e\u65e7\u8282\u70b9\u3002\u7136\u800c&#xff0c;\u8fd9\u79cd\u65b9\u5f0f\u591a\u505a\u4e86\u65e0\u610f\u4e49\u7684\u8282\u70b9\u91ca\u653e\u4e0e\u91cd\u5efa\u64cd\u4f5c\u3002\u5b9e\u9645\u4e0a&#xff0c;<span style=\"color:#0d0016\">\u6211\u4eec\u53ef\u4ee5\u76f4\u63a5\u5c06\u539f\u6709\u7684\u8282\u70b9\u4ece\u65e7\u54c8\u5e0c\u8868\u4e2d\u8fc1\u79fb\u5230\u65b0\u54c8\u5e0c\u8868\u4e2d&#xff0c;\u65e0\u9700\u91ca\u653e\u548c\u91cd\u65b0\u5206\u914d&#xff0c;\u65e2\u8282\u7701\u4e86\u65f6\u95f4\u4e5f\u4f18\u5316\u4e86\u5185\u5b58\u4f7f\u7528\u3002<\/span><\/p>\n<p>bool Insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\n\/\/\u5148\u67e5\u627e&#xff0c;\u5df2\u7ecf\u6709\u4e86\u76f8\u540c\u6570\u636e\u63d2\u5165\u5931\u8d25<br \/>\nif (Find(kv.first))<br \/>\nreturn 0;<\/p>\n<p>Hash hash;<\/p>\n<p>\/\/ \u8d1f\u8f7d\u56e0\u5b50\u7b49\u4e8e1\u65f6\u5019 \u8fdb\u884c\u6269\u5bb9<br \/>\nif (_n&#061;&#061;_tables.size())<br \/>\n{<br \/>\n\/\/HashTable&lt;K, V&gt;newht;<br \/>\n\/\/newht._tables.resize(__stl_next_prime(_tables.size() &#043; 1));<br \/>\n\/\/for (size_t i &#061; 0; i &lt; _tables.size(); i&#043;&#043;)<br \/>\n\/\/{<br \/>\n\/\/Node* cur &#061; _tables[i];<br \/>\n\/\/while (cur)<br \/>\n\/\/{<br \/>\n\/\/newht.Insert(cur-&gt;_kv);<br \/>\n\/\/cur &#061; cur-&gt;_next;<br \/>\n\/\/}<br \/>\n\/\/}<br \/>\n\/\/_tables.swap(newht._tables);<br \/>\n\/\/ \/\/ \u591a\u6b21\u63d2\u5165 \u7e41\u7410<\/p>\n<p>\/\/ \u8fd9\u2fa5\u5982\u679c\u4f7f\u2f64\u4e0a\u2faf\u7684\u2f45\u6cd5&#xff0c;\u6269\u5bb9\u65f6\u521b\u5efa\u65b0\u7684\u7ed3\u70b9&#xff0c;\u540e\u2faf\u8fd8\u8981\u4f7f\u2f64\u65e7\u7ed3<br \/>\n\/\/\u70b9&#xff0c;\u6d6a\u8d39\u4e86<br \/>\n\/\/ \u4e0b\u2faf\u7684\u2f45\u6cd5&#xff0c;\u76f4\u63a5\u79fb\u52a8\u65e7\u8868\u7684\u7ed3\u70b9\u5230\u65b0\u8868&#xff0c;\u6548\u7387\u66f4\u597d<br \/>\nvector&lt;Node*&gt; newtable(__stl_next_prime(_tables.size() &#043; 1));<br \/>\nfor (size_t i &#061; 0; i &lt; _tables.size(); i&#043;&#043;)<br \/>\n{<\/p>\n<p>Node* cur &#061; _tables[i];<br \/>\nwhile (cur)<br \/>\n{<br \/>\nNode* next &#061; cur-&gt;_next;<br \/>\n\/\/ \u5934\u63d2\u5230\u65b0\u94fe\u8868<\/p>\n<p>size_t hashi &#061; hash(cur-&gt;_kv.first) % newtable.size();<br \/>\ncur-&gt;_next &#061; newtable[hashi];<br \/>\nnewtable[hashi] &#061; cur;<br \/>\ncur &#061; next;<\/p>\n<p>}<br \/>\n_tables[i] &#061; nullptr;<br \/>\n}<br \/>\n_tables.swap(newtable);<br \/>\n}<\/p>\n<p>size_t hashi &#061; hash(kv.first) % _tables.size();<br \/>\n\/\/ \u5934\u63d2<br \/>\nNode* newnode &#061; new Node(kv);<br \/>\nnewnode-&gt;_next &#061; _tables[hashi];<br \/>\n_tables[hashi] &#061; newnode;<br \/>\n&#043;&#043;_n;<br \/>\nreturn 1;<br \/>\n} <\/p>\n<h3 id=\"4.4%20%E5%88%A0%E9%99%A4%E5%87%BD%E6%95%B0%EF%BC%9A%E8%8A%82%E7%82%B9%E6%B8%85%E7%90%86%E4%B8%8E%E7%A9%BA%E9%97%B4%E5%A4%8D%E7%94%A8\">4.4 \u5220\u9664\u51fd\u6570&#xff1a;\u8282\u70b9\u6e05\u7406\u4e0e\u7a7a\u95f4\u590d\u7528<\/h3>\n<p>\u5220\u9664\u7684\u903b\u8f91\u662f\u6839\u636ekey\u503c\u627e\u5230\u5bf9\u5e94\u7684\u4f4d\u7f6e&#xff0c;\u5728\u8be5\u4f4d\u7f6e\u7684\u94fe\u8868\u4e2d\u68c0\u7d22\u662f\u5426\u6709\u76f8\u7b49\u7684\u6570\u503c\u3002\u5982\u679c\u6709\u5c31\u8fdb\u884c\u5220\u9664&#xff0c;\u5426\u5219\u8fd4\u56defalse<\/p>\n<p>bool Erase(const K&amp; key)<br \/>\n{<\/p>\n<p>if (Find(key) &#061;&#061; nullptr)<br \/>\nreturn 0;<\/p>\n<p>Hash hash;<br \/>\nsize_t hashi &#061; hash(key) % _tables.size();<br \/>\nNode* cur &#061; _tables[hashi];<br \/>\nNode* prev &#061; nullptr;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (cur-&gt;_kv.first &#061;&#061; key)<br \/>\n{<br \/>\nif (prev &#061;&#061; nullptr) \/\/\u53ea\u6709\u4e00\u4e2a\u8282\u70b9<br \/>\n{<br \/>\n_tables[hashi] &#061; cur-&gt;_next;<br \/>\n}<br \/>\nelse \/\/\u4e2d\u95f4\u8282\u70b9<br \/>\n{<br \/>\nprev-&gt;_next &#061; cur-&gt;_next;<br \/>\n}<br \/>\ndelete cur;<br \/>\n&#8211;_n;<br \/>\nreturn 1;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n            \/\/ \u5728\u94fe\u8868\u91cc\u904d\u5386\u5230\u5220\u9664\u7684\u6570\u636e<br \/>\nprev &#061; cur;<br \/>\ncur &#061; cur-&gt;_next;<br \/>\n}<br \/>\n}<br \/>\nreturn 0;<br \/>\n} <\/p>\n<h3 id=\"4.5%20%E6%9F%A5%E6%89%BE%E6%93%8D%E4%BD%9C%EF%BC%9A%E5%AE%9A%E4%BD%8D%E6%95%88%E7%8E%87%E7%9A%84%E4%BC%98%E5%8C%96%E5%AE%9E%E8%B7%B5\">4.5 \u67e5\u627e\u64cd\u4f5c&#xff1a;\u5b9a\u4f4d\u6548\u7387\u7684\u4f18\u5316\u5b9e\u8df5<\/h3>\n<p>\u67e5\u627e\u7684\u903b\u8f91\u548c\u5220\u9664\u7c7b\u4f3c&#xff0c;\u6839\u636ekey\u503c\u627e\u5230\u6620\u5c04\u4f4d\u7f6e&#xff0c;\u518d\u5728\u8be5\u94fe\u8868\u4e2d\u8fdb\u884c\u68c0\u7d22&#xff0c;\u627e\u5230\u8fd4\u56de\u8282\u70b9\u6307\u9488&#xff0c;\u53cd\u4e4b\u8fd4\u56de\u7a7a\u6307\u9488\u3002<\/p>\n<p>Node* Find(const K&amp; key)<br \/>\n{<br \/>\nHash hash;<br \/>\nsize_t hashi &#061; hash(key) % _tables.size();<br \/>\nNode* cur &#061; _tables[hashi];<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (cur-&gt;_kv.first &#061;&#061; key)<br \/>\n{<br \/>\nreturn cur;<br \/>\n}<br \/>\ncur &#061; cur-&gt;_next;<br \/>\n}<br \/>\nreturn __nullptr;<br \/>\n} <\/p>\n<h3 id=\"4.6%20%E5%8A%9F%E8%83%BD%E6%B5%8B%E8%AF%95%EF%BC%9A%E5%A4%9A%E5%9C%BA%E6%99%AF%E9%AA%8C%E8%AF%81%E4%B8%8E%E6%80%A7%E8%83%BD%E5%88%86%E6%9E%90\">4.6 \u529f\u80fd\u6d4b\u8bd5&#xff1a;\u591a\u573a\u666f\u9a8c\u8bc1\u4e0e\u6027\u80fd\u5206\u6790<\/h3>\n<p>\u8fd9\u91cc\u6211\u4eec\u5206\u522b\u6d4b\u8bd5\u63d2\u5165\u5220\u9664&#xff0c;\u63d2\u5165\u5bfb\u627e&#xff0c;\u5b57\u7b26\u4e32\u7684\u5904\u7406&#xff1a;<\/p>\n<p>int main()<br \/>\n{<br \/>\nint a[] &#061; { 19,30,5,36,13,20,21,12,24,96 };<\/p>\n<p>hash_bucket::HashTable&lt;int, int&gt; ht2;<br \/>\nfor (auto e : a)<br \/>\n{<br \/>\nht2.Insert({ e,e });<br \/>\n}<\/p>\n<p>cout &lt;&lt; ht2.Find(96) &lt;&lt; endl;<br \/>\ncout &lt;&lt; ht2.Find(30) &lt;&lt; endl;<br \/>\ncout &lt;&lt; ht2.Find(19) &lt;&lt; endl &lt;&lt; endl;<\/p>\n<p>ht2.Erase(96);<br \/>\nht2.Erase(19);<br \/>\nht2.Erase(30);<\/p>\n<p>cout &lt;&lt; ht2.Find(96) &lt;&lt; endl;<br \/>\ncout &lt;&lt; ht2.Find(30) &lt;&lt; endl;<br \/>\ncout &lt;&lt; ht2.Find(19) &lt;&lt; endl &lt;&lt; endl;<\/p>\n<p>hash_bucket::HashTable&lt;string, string&gt; ht3;<br \/>\nconst char* a2[] &#061; { &#034;abcd&#034;,&#034;sort&#034;,&#034;insert&#034; };<br \/>\nfor (auto&amp; e : a2)<br \/>\n{<br \/>\nht3.Insert({ e,e });<br \/>\n}<br \/>\ncout &lt;&lt; ht3.Find(&#034;abcd&#034;) &lt;&lt; endl;<\/p>\n<p>cout &lt;&lt; ht3.Erase(&#034;abcd&#034;) &lt;&lt; endl;<\/p>\n<p>return 0;<br \/>\n} <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"873\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140935-680f8c1f83bc4.png\" width=\"1200\" \/><\/p>\n<p>\u770b\u8d77\u6765\u6ca1\u6709\u95ee\u9898&#xff0c;\u8c03\u8bd5\u770b\u770b\u5206\u5e03&#xff1a;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"728\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140936-680f8c20372dc.png\" width=\"1200\" \/><\/p>\n<p><span style=\"color:#0d0016\">\u901a\u8fc7\u5bf9\u76d1\u89c6\u7a97\u53e3\u7684\u67e5\u770b&#xff0c;\u6211\u4eec\u53ef\u4ee5\u9a8c\u8bc1\u6211\u4eec\u7684\u4ee3\u7801\u6b63\u5e38\u8fd0\u884c\u7684&#xff01;<\/span><\/p>\n<h2>Thanks\u8c22\u8c22\u9605\u8bfb&#xff01;&#xff01;&#xff01;<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.2k\u6b21\uff0c\u70b9\u8d5e84\u6b21\uff0c\u6536\u85cf81\u6b21\u3002\u5148\u627e\u51fa\u4f60\u7684\u80fd\u529b\u5728\u54ea\u91cc\uff0c\u7136\u540e\u518d\u51b3\u5b9a\u4f60\u662f\u8c01\u3002\u2014\u2014 \u5854\u62c9\u00b7\u97e6\u65af\u7279\u5f17 \u300a\u4f60\u5f53\u50cf\u9e1f\u98de\u5f80\u4f60\u7684\u5c71\u300b<\/p>\n","protected":false},"author":2,"featured_media":34010,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[55,2872,188],"topic":[],"class_list":["post-34016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-c","tag-2872","tag-188"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\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\/34016.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.2k\u6b21\uff0c\u70b9\u8d5e84\u6b21\uff0c\u6536\u85cf81\u6b21\u3002\u5148\u627e\u51fa\u4f60\u7684\u80fd\u529b\u5728\u54ea\u91cc\uff0c\u7136\u540e\u518d\u51b3\u5b9a\u4f60\u662f\u8c01\u3002\u2014\u2014 \u5854\u62c9\u00b7\u97e6\u65af\u7279\u5f17 \u300a\u4f60\u5f53\u50cf\u9e1f\u98de\u5f80\u4f60\u7684\u5c71\u300b\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/34016.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2025-04-28T14:09:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140933-680f8c1d7e43e.gif\" \/>\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=\"10 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34016.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/34016.html\",\"name\":\"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2025-04-28T14:09:37+00:00\",\"dateModified\":\"2025-04-28T14:09:37+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/34016.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/34016.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/34016.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\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":"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\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\/34016.html","og_locale":"zh_CN","og_type":"article","og_title":"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u6587\u7ae0\u6d4f\u89c8\u9605\u8bfb5.2k\u6b21\uff0c\u70b9\u8d5e84\u6b21\uff0c\u6536\u85cf81\u6b21\u3002\u5148\u627e\u51fa\u4f60\u7684\u80fd\u529b\u5728\u54ea\u91cc\uff0c\u7136\u540e\u518d\u51b3\u5b9a\u4f60\u662f\u8c01\u3002\u2014\u2014 \u5854\u62c9\u00b7\u97e6\u65af\u7279\u5f17 \u300a\u4f60\u5f53\u50cf\u9e1f\u98de\u5f80\u4f60\u7684\u5c71\u300b","og_url":"https:\/\/www.wsisp.com\/helps\/34016.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2025-04-28T14:09:37+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2025\/04\/20250428140933-680f8c1d7e43e.gif"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"10 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/34016.html","url":"https:\/\/www.wsisp.com\/helps\/34016.html","name":"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\u6790 - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2025-04-28T14:09:37+00:00","dateModified":"2025-04-28T14:09:37+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/34016.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/34016.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/34016.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u3010C++\u3011\u2014\u2014\u7cbe\u7ec6\u5316\u54c8\u5e0c\u8868\u67b6\u6784\uff1a\u7406\u8bba\u4e0e\u5b9e\u8df5\u7684\u7efc\u5408\u5206\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\/34016","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=34016"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/34016\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/34010"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=34016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=34016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=34016"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=34016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}