{"id":61116,"date":"2026-01-17T03:05:32","date_gmt":"2026-01-16T19:05:32","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/61116.html"},"modified":"2026-01-17T03:05:32","modified_gmt":"2026-01-16T19:05:32","slug":"%e7%bb%93%e5%90%88stl%ef%bc%8c%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%a1%b9%e7%9b%ae%e8%a7%a3%e6%9e%90vetcor-map-unordered_map","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/61116.html","title":{"rendered":"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map"},"content":{"rendered":"<h2>\u4e00\u3001STL \u7684\u516d\u5927\u6838\u5fc3\u7ec4\u4ef6<\/h2>\n<p>STL&#xff08;Standard Template Library&#xff0c;\u6807\u51c6\u6a21\u677f\u5e93&#xff09;\u662f C&#043;&#043; \u7684\u6807\u51c6\u5e93\u6838\u5fc3&#xff0c;\u662f\u6cdb\u578b\u7f16\u7a0b\u601d\u60f3\u7684\u6781\u81f4\u5b9e\u73b0&#xff0c;STL \u7684\u6240\u6709\u5185\u5bb9\u88ab\u4e25\u683c\u5212\u5206\u4e3a\u516d\u5927\u76f8\u4e92\u72ec\u7acb\u53c8\u7d27\u5bc6\u914d\u5408\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u6240\u6709\u5bb9\u5668 \/ \u7b97\u6cd5 \/ \u8fed\u4ee3\u5668\u90fd\u662f\u57fa\u4e8e\u8fd9\u516d\u5927\u7ec4\u4ef6\u5b9e\u73b0&#xff0c;\u7f3a\u4e00\u4e0d\u53ef\u3002<\/p>\n<ul>\n<li>\u5bb9\u5668&#xff08;Container&#xff09; &#xff1a;\u5b58\u653e\u6570\u636e\u7684\u5bb9\u5668&#xff0c;\u662f STL \u7684\u6838\u5fc3\u8f7d\u4f53&#xff0c;\u6bd4\u5982vector\/list\/map\/unordered_map\/set&#xff0c;\u5206\u4e3a\u300c\u5e8f\u5217\u5f0f\u5bb9\u5668\u300d\u548c\u300c\u5173\u8054\u5f0f\u5bb9\u5668\u300d\u4e24\u5927\u7c7b&#xff1b;<\/li>\n<li>\u7b97\u6cd5&#xff08;Algorithm&#xff09; &#xff1a;\u901a\u7528\u7684\u7b97\u6cd5\u51fd\u6570&#xff0c;\u4e0e\u5bb9\u5668\u89e3\u8026&#xff0c;\u6bd4\u5982\u6392\u5e8fsort\u3001\u67e5\u627efind\u3001\u904d\u5386for_each\u3001\u62f7\u8d1dcopy&#xff0c;\u7b97\u6cd5\u901a\u8fc7\u8fed\u4ee3\u5668\u8bbf\u95ee\u5bb9\u5668\u6570\u636e&#xff1b;<\/li>\n<li>\u8fed\u4ee3\u5668&#xff08;Iterator&#xff09; &#xff1a;\u5bb9\u5668\u548c\u7b97\u6cd5\u7684\u6865\u6881&#xff0c;\u662f\u300c\u6cdb\u5316\u7684\u6307\u9488\u300d&#xff0c;\u5c01\u88c5\u4e86\u5bb9\u5668\u7684\u904d\u5386\u903b\u8f91&#xff0c;\u6240\u6709 STL \u7b97\u6cd5\u90fd\u901a\u8fc7\u8fed\u4ee3\u5668\u64cd\u4f5c\u5bb9\u5668&#xff0c;\u5c4f\u853d\u4e86\u4e0d\u540c\u5bb9\u5668\u7684\u5e95\u5c42\u5dee\u5f02&#xff1b;<\/li>\n<li>\u4eff\u51fd\u6570&#xff08;Functor&#xff09; &#xff1a;\u91cd\u8f7d\u4e86()\u8fd0\u7b97\u7b26\u7684\u7c7b \/ \u7ed3\u6784\u4f53&#xff0c;\u884c\u4e3a\u50cf\u51fd\u6570&#xff0c;\u4f5c\u4e3a\u7b97\u6cd5\u7684\u81ea\u5b9a\u4e49\u903b\u8f91\u5165\u53c2&#xff08;\u6bd4\u5982\u81ea\u5b9a\u4e49\u6392\u5e8f\u89c4\u5219&#xff09;&#xff0c;\u662f Lambda \u8868\u8fbe\u5f0f\u7684\u524d\u8eab&#xff1b;<\/li>\n<li>\u9002\u914d\u5668&#xff08;Adaptor&#xff09; &#xff1a;\u300c\u5305\u88c5\u5668\u300d&#xff0c;\u5bf9\u73b0\u6709\u7ec4\u4ef6\u505a\u9002\u914d\u6539\u9020&#xff0c;\u4e0d\u6539\u53d8\u539f\u7ec4\u4ef6\u529f\u80fd&#xff0c;\u53ea\u6539\u53d8\u63a5\u53e3\u5f62\u6001\u3002\u6bd4\u5982stack\/queue\u662f\u5bf9deque\u7684\u9002\u914d\u5668\u3001priority_queue\u662f\u5806\u9002\u914d\u5668\u3001reverse_iterator\u662f\u53cd\u5411\u8fed\u4ee3\u5668\u9002\u914d\u5668&#xff1b;<\/li>\n<li>\u7a7a\u95f4\u914d\u7f6e\u5668&#xff08;Allocator&#xff09; &#xff1a;STL \u7684\u300c\u5185\u5b58\u7ba1\u7406\u5668\u300d&#xff0c;\u8d1f\u8d23\u5bb9\u5668\u7684\u5185\u5b58\u7533\u8bf7 \/ \u91ca\u653e&#xff0c;\u5e95\u5c42\u5c01\u88c5\u4e86malloc\/free&#xff0c;\u505a\u4e86\u5185\u5b58\u6c60\u3001\u5185\u5b58\u5bf9\u9f50\u7b49\u4f18\u5316&#xff0c;\u6240\u6709\u5bb9\u5668\u7684\u5185\u5b58\u64cd\u4f5c\u90fd\u7531\u914d\u7f6e\u5668\u63a5\u7ba1&#xff0c;\u4fdd\u8bc1\u5185\u5b58\u9ad8\u6548\u4f7f\u7528\u3002<\/li>\n<\/ul>\n<h2>\u4e8c\u3001std::vector \u7684\u5e95\u5c42\u5b9e\u73b0 &#043; \u5b8c\u6574\u6269\u5bb9\u673a\u5236<\/h2>\n<h5>2.1 std::vector \u5e95\u5c42\u5b9e\u73b0<\/h5>\n<p>std::vector\u662f STL \u7684\u52a8\u6001\u8fde\u7eed\u6570\u7ec4&#xff0c;\u5c5e\u4e8e\u5e8f\u5217\u5f0f\u5bb9\u5668&#xff0c;\u5e95\u5c42\u6838\u5fc3\u662f&#xff1a;\u4e00\u5757\u8fde\u7eed\u7684\u5806\u5185\u5b58\u7f13\u51b2\u533a &#043; \u4e09\u4e2a\u8fed\u4ee3\u5668\u6307\u9488&#xff08;_start\/_finish\/_end_of_storage&#xff09;<\/p>\n<ul>\n<li>\u5185\u5b58\u7279\u6027&#xff1a;\u7269\u7406\u5185\u5b58\u7edd\u5bf9\u8fde\u7eed&#xff0c;\u652f\u6301\u968f\u673a\u8bbf\u95ee&#xff08;\u901a\u8fc7\u4e0b\u6807[]\u8bbf\u95ee&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6O(1)&#xff09;&#xff1b;<\/li>\n<li>\u4f18\u7f3a\u70b9&#xff1a;\u968f\u673a\u8bbf\u95ee\u6548\u7387\u6781\u9ad8&#xff0c;\u5c3e\u90e8\u63d2\u5165 \/ \u5220\u9664\u6548\u7387\u9ad8&#xff0c;\u5934\u90e8 \/ \u4e2d\u95f4\u63d2\u5165 \/ \u5220\u9664\u6548\u7387\u4f4e&#xff08;\u9700\u8981\u79fb\u52a8\u5143\u7d20&#xff0c;O (n)&#xff09; &#xff0c;\u6838\u5fc3\u75db\u70b9\u662f\u300c\u6269\u5bb9\u4f1a\u6709\u6027\u80fd\u5f00\u9500\u300d\u3002<\/li>\n<\/ul>\n<h5>2.2 std::vector \u5b8c\u6574\u6269\u5bb9\u673a\u5236<\/h5>\n<p>vector \u662f\u52a8\u6001\u6570\u7ec4&#xff0c;\u521d\u59cb\u5316\u65f6\u4f1a\u7533\u8bf7\u4e00\u5757\u300c\u521d\u59cb\u5bb9\u91cf&#xff08;capacity&#xff09;\u300d\u7684\u5185\u5b58&#xff0c;\u5f53\u5b58\u5165\u7684\u5143\u7d20\u4e2a\u6570&#xff08;size&#xff09;\u7b49\u4e8e\u5f53\u524d\u5bb9\u91cf&#xff08;capacity&#xff09;\u65f6&#xff0c;\u7ee7\u7eed\u63d2\u5165\u5143\u7d20\u5c31\u4f1a\u89e6\u53d1\u81ea\u52a8\u6269\u5bb9&#xff0c;\u8fd9\u662f vector \u7684\u6838\u5fc3\u7279\u6027&#xff0c;\u6269\u5bb9\u673a\u5236\u662f\u56fa\u5b9a\u7684\u6807\u51c6\u903b\u8f91&#xff0c;\u6240\u6709\u7f16\u8bd1\u5668&#xff08;GCC\/VS&#xff09;\u4e00\u81f4&#xff0c;\u7ec6\u8282\u5982\u4e0b&#xff1a;<\/p>\n<li>\u89e6\u53d1\u6761\u4ef6&#xff1a;\u5f53\u6267\u884cpush_back\/emplace_back\u63d2\u5165\u5143\u7d20\u65f6&#xff0c;\u5224\u65ad size() &#061;&#061; capacity() \u2192 \u89e6\u53d1\u6269\u5bb9&#xff1b;<\/li>\n<li>\u6269\u5bb9\u500d\u6570&#xff1a;\u6807\u51c6\u6269\u5bb9\u500d\u6570\u662f 1.5 \u500d&#xff08;GCC&#xff09;\/ 2 \u500d&#xff08;VS&#xff09;<\/li>\n<li>\u6269\u5bb9\u6838\u5fc3\u64cd\u4f5c&#xff1a;\u7533\u8bf7\u65b0\u5185\u5b58 \u2192 \u62f7\u8d1d \/ \u79fb\u52a8\u539f\u6570\u636e \u2192 \u91ca\u653e\u539f\u5185\u5b58 \u2192 \u6307\u5411\u65b0\u5185\u5b58\n<ul>\n<li>\u7b2c\u4e00\u6b65&#xff1a;\u5411\u7a7a\u95f4\u914d\u7f6e\u5668\u7533\u8bf7\u4e00\u5757\u300c\u539f\u5bb9\u91cf \u00d7 1.5 \u500d\u300d\u7684\u65b0\u7684\u66f4\u5927\u7684\u8fde\u7eed\u5806\u5185\u5b58&#xff1b;<\/li>\n<li>\u7b2c\u4e8c\u6b65&#xff1a;\u5c06 vector \u4e2d\u539f\u6709\u7684\u6240\u6709\u5143\u7d20&#xff0c;\u79fb\u52a8&#xff08;C&#043;&#043;11 \u540e&#xff09;\/ \u62f7\u8d1d\u5230\u65b0\u5185\u5b58\u4e2d&#xff1b;<\/li>\n<li>\u7b2c\u4e09\u6b65&#xff1a;\u8c03\u7528\u6790\u6784\u51fd\u6570&#xff0c;\u9500\u6bc1\u539f\u5185\u5b58\u4e2d\u7684\u5143\u7d20&#xff0c;\u5e76\u91ca\u653e\u539f\u5185\u5b58\u7a7a\u95f4&#xff1b;<\/li>\n<li>\u7b2c\u56db\u6b65&#xff1a;\u5c06 vector \u7684\u4e09\u4e2a\u6838\u5fc3\u6307\u9488\u6307\u5411\u65b0\u5185\u5b58\u7684\u5bf9\u5e94\u4f4d\u7f6e&#xff0c;\u66f4\u65b0 size \u548c capacity&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u6269\u5bb9\u7684\u6027\u80fd\u5f00\u9500&#xff1a;\u6269\u5bb9\u662f\u91cd\u91cf\u7ea7\u64cd\u4f5c&#xff0c;\u6d89\u53ca\u300c\u5185\u5b58\u7533\u8bf7 &#043; \u6570\u636e\u62f7\u8d1d &#043; \u5185\u5b58\u91ca\u653e\u300d&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6O(n)&#xff0c;\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d\u8981\u5c3d\u91cf\u907f\u514d\u9891\u7e41\u6269\u5bb9&#xff1b;<\/li>\n<li>\u5173\u952e\u4f18\u5316\u624b\u6bb5&#xff1a;\u63d0\u524d\u9884\u5206\u914d\u5bb9\u91cf&#xff0c;\u4f7f\u7528 vector.reserve(n) \u624b\u52a8\u6307\u5b9a\u5bb9\u91cf&#xff0c;\u4e00\u6b21\u6027\u7533\u8bf7\u8db3\u591f\u7684\u5185\u5b58&#xff0c;\u5f7b\u5e95\u907f\u514d\u540e\u7eed\u7684\u81ea\u52a8\u6269\u5bb9&#xff0c;\u8fd9\u662f\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u4e2d vector \u7684\u6807\u914d\u5199\u6cd5\u3002<\/li>\n<ul>\n<li>Q1&#xff1a;vector.size() \u548c vector.capacity() \u7684\u533a\u522b&#xff1f;<\/li>\n<li>A&#xff1a;size() \u662f\u5f53\u524d\u5df2\u5b58\u50a8\u7684\u5143\u7d20\u4e2a\u6570&#xff1b;capacity() \u662f\u5f53\u524d\u7533\u8bf7\u7684\u603b\u5185\u5b58\u80fd\u5bb9\u7eb3\u7684\u5143\u7d20\u4e2a\u6570&#xff1b;capacity() &gt;&#061; size() \u6c38\u8fdc\u6210\u7acb&#xff0c;\u5dee\u503c\u662f\u300c\u7a7a\u95f2\u5185\u5b58\u300d\u3002<\/li>\n<li>Q2&#xff1a;C&#043;&#043;11 \u5bf9 vector \u6269\u5bb9\u7684\u4f18\u5316&#xff1f;<\/li>\n<li>A&#xff1a;C&#043;&#043;11 \u5f15\u5165\u79fb\u52a8\u8bed\u4e49&#xff0c;\u6269\u5bb9\u65f6\u4f1a\u8c03\u7528\u5143\u7d20\u7684\u300c\u79fb\u52a8\u6784\u9020\u51fd\u6570\u300d&#xff0c;\u800c\u975e\u62f7\u8d1d\u6784\u9020&#xff0c;\u628a\u539f\u6570\u636e\u7684\u300c\u8d44\u6e90\u6240\u6709\u6743\u8f6c\u79fb\u300d\u5230\u65b0\u5185\u5b58&#xff0c;\u800c\u975e\u62f7\u8d1d\u6570\u636e&#xff0c;\u5927\u5e45\u964d\u4f4e\u6269\u5bb9\u7684 CPU \u5f00\u9500\u3002<\/li>\n<\/ul>\n<h2>\u4e09\u3001std::map \u548c std::unordered_map \u5e95\u5c42\u5b9e\u73b0 &#043; \u6838\u5fc3\u533a\u522b &#043; \u9002\u7528\u573a\u666f<\/h2>\n<p>\u4e24\u8005\u90fd\u662f\u5173\u8054\u5f0f\u5bb9\u5668&#xff0c;\u6838\u5fc3\u4f5c\u7528\u662f\u300c\u5b58\u50a8\u952e\u503c\u5bf9&#xff08;key-value&#xff09;\u3001\u901a\u8fc7 key \u5feb\u901f\u67e5\u627e value\u300d&#xff0c;\u662f\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u4e2d\u6700\u5e38\u7528\u7684\u4e24\u7c7b\u5bb9\u5668&#xff0c;\u533a\u522b\u5de8\u5927\u3001\u9002\u7528\u573a\u666f\u5b8c\u5168\u4e0d\u540c<\/p>\n<h5>3.1 std::map \u5e95\u5c42\u5b9e\u73b0<\/h5>\n<li>\u5e95\u5c42\u6570\u636e\u7ed3\u6784&#xff1a;\u7ea2\u9ed1\u6811&#xff08;\u81ea\u5e73\u8861\u7684\u4e8c\u53c9\u641c\u7d22\u6811&#xff09; &#xff0c;\u4e00\u9897\u975e\u4e25\u683c\u7684\u5e73\u8861\u4e8c\u53c9\u6811&#xff1b;<\/li>\n<li>\u6838\u5fc3\u7279\u6027&#xff1a;\n<ul>\n<li>\u5b58\u50a8\u7684\u952e\u503c\u5bf9\u6309 key \u7684\u5927\u5c0f\u81ea\u52a8\u6392\u5e8f&#xff08;\u5347\u5e8f&#xff09;&#xff0c;key \u5fc5\u987b\u652f\u6301&lt;\u6bd4\u8f83\u8fd0\u7b97\u7b26&#xff1b;<\/li>\n<li>\u67e5\u627e \/ \u63d2\u5165 \/ \u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O(log n)&#xff0c;n \u662f\u5143\u7d20\u4e2a\u6570&#xff1b;<\/li>\n<li>key \u662f\u552f\u4e00\u7684&#xff0c;\u4e0d\u5141\u8bb8\u91cd\u590d\u63d2\u5165\u76f8\u540c\u7684 key&#xff0c;\u91cd\u590d\u63d2\u5165\u4f1a\u8986\u76d6\u539f value&#xff1b;<\/li>\n<li>\u5e95\u5c42\u662f\u6811\u7ed3\u6784&#xff0c;\u5185\u5b58\u662f\u975e\u8fde\u7eed\u7684&#xff0c;\u652f\u6301\u9ad8\u6548\u7684\u8303\u56f4\u67e5\u627e\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>3.2 std::unordered_map \u5e95\u5c42\u5b9e\u73b0<\/h5>\n<li>\u5e95\u5c42\u6570\u636e\u7ed3\u6784&#xff1a;\u54c8\u5e0c\u8868&#xff08;\u6563\u5217\u8868&#xff09; &#043; \u94fe\u8868 \/ \u7ea2\u9ed1\u6811 &#xff08;\u62c9\u94fe\u6cd5\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81&#xff09;&#xff1b;\n<ul>\n<li>\u6838\u5fc3\u903b\u8f91&#xff1a;\u901a\u8fc7\u54c8\u5e0c\u51fd\u6570\u5c06 key \u6620\u5c04\u5230\u54c8\u5e0c\u8868\u7684\u6307\u5b9a\u6876&#xff08;bucket&#xff09;\u4f4d\u7f6e&#xff1b;<\/li>\n<li>\u54c8\u5e0c\u51b2\u7a81&#xff1a;\u5f53\u4e24\u4e2a\u4e0d\u540c\u7684 key \u6620\u5c04\u5230\u540c\u4e00\u4e2a\u6876\u65f6&#xff0c;\u7528\u300c\u94fe\u8868\u300d\u6302\u8f7d\u51b2\u7a81\u5143\u7d20&#xff1b;\u5f53\u94fe\u8868\u957f\u5ea6\u8d85\u8fc7\u9608\u503c&#xff08;\u9ed8\u8ba4 8&#xff09;&#xff0c;\u4f1a\u81ea\u52a8\u8f6c\u4e3a\u300c\u7ea2\u9ed1\u6811\u300d&#xff0c;\u4f18\u5316\u67e5\u627e\u6548\u7387&#xff1b;<\/li>\n<\/ul>\n<\/li>\n<li>\u6838\u5fc3\u7279\u6027&#xff1a;\n<ul>\n<li>\u5b58\u50a8\u7684\u952e\u503c\u5bf9\u65e0\u5e8f&#xff0c;\u5b8c\u5168\u4e0d\u4fdd\u8bc1\u987a\u5e8f&#xff0c;\u4e0e\u63d2\u5165\u987a\u5e8f\u65e0\u5173&#xff1b;<\/li>\n<li>\u67e5\u627e \/ \u63d2\u5165 \/ \u5220\u9664\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6&#xff1a;O (1) &#xff0c;\u8fd9\u662f\u54c8\u5e0c\u8868\u7684\u6838\u5fc3\u4f18\u52bf&#xff1b;\u6700\u574f\u60c5\u51b5&#xff08;\u54c8\u5e0c\u51b2\u7a81\u4e25\u91cd&#xff09;\u662fO(log n)&#xff1b;<\/li>\n<li>key \u662f\u552f\u4e00\u7684&#xff0c;\u4e0d\u5141\u8bb8\u91cd\u590d&#xff0c;\u91cd\u590d\u63d2\u5165\u4f1a\u8986\u76d6\u539f value&#xff1b;<\/li>\n<li>\u5e95\u5c42\u662f\u54c8\u5e0c\u8868&#xff0c;\u5185\u5b58\u662f\u975e\u8fde\u7eed\u7684&#xff0c;\u9700\u8981\u989d\u5916\u7684\u54c8\u5e0c\u51fd\u6570\u8ba1\u7b97\u5f00\u9500\u3002<\/li>\n<\/ul>\n<\/li>\n<h5>3.3 map \u548c unordered_map \u6838\u5fc3\u533a\u522b<\/h5>\n<table border=\"1\" cellpadding=\"1\" cellspacing=\"1\" style=\"width:650px\">\n<tbody>\n<tr>\n<td>\u5bf9\u6bd4\u7ef4\u5ea6<\/td>\n<td>std::map<\/td>\n<td>std::unordered_map<\/td>\n<\/tr>\n<tr>\n<td>\u5e95\u5c42\u5b9e\u73b0<\/td>\n<td>\u7ea2\u9ed1\u6811<\/td>\n<td>\u54c8\u5e0c\u8868 &#043; \u62c9\u94fe\u6cd5<\/td>\n<\/tr>\n<tr>\n<td>\u6709\u5e8f\u6027<\/td>\n<td>\u6709\u5e8f&#xff08;key \u5347\u5e8f&#xff09;<\/td>\n<td>\u65e0\u5e8f<\/td>\n<\/tr>\n<tr>\n<td>\u67e5\u627e\u6548\u7387<\/td>\n<td>O(log n)<\/td>\n<td>O (1) \u5e73\u5747&#xff0c;O (log n) \u6700\u574f<\/td>\n<\/tr>\n<tr>\n<td>\u63d2\u5165 \/ \u5220\u9664\u6548\u7387<\/td>\n<td>O(log n)<\/td>\n<td>O (1) \u5e73\u5747&#xff0c;O (log n) \u6700\u574f<\/td>\n<\/tr>\n<tr>\n<td>key \u7684\u8981\u6c42<\/td>\n<td>\u652f\u6301&lt;\u6bd4\u8f83\u8fd0\u7b97\u7b26\u5373\u53ef&#xff0c;\u65e0\u9700\u54c8\u5e0c\u51fd\u6570<\/td>\n<td>\u5fc5\u987b\u652f\u6301\u54c8\u5e0c\u51fd\u6570 &#043; &#061;&#061;\u6bd4\u8f83\u8fd0\u7b97\u7b26<\/td>\n<\/tr>\n<tr>\n<td>\u5185\u5b58\u5360\u7528<\/td>\n<td>\u5185\u5b58\u5360\u7528\u5c0f&#xff0c;\u65e0\u5197\u4f59<\/td>\n<td>\u5185\u5b58\u5360\u7528\u5927&#xff0c;\u54c8\u5e0c\u8868\u6709\u6876\u7684\u5197\u4f59\u7a7a\u95f4<\/td>\n<\/tr>\n<tr>\n<td>\u8fed\u4ee3\u5668\u7a33\u5b9a\u6027<\/td>\n<td>\u63d2\u5165 \/ \u5220\u9664\u5143\u7d20\u540e&#xff0c;\u8fed\u4ee3\u5668\u4e0d\u4f1a\u5931\u6548<\/td>\n<td>\u63d2\u5165\u5143\u7d20\u53ef\u80fd\u89e6\u53d1\u6269\u5bb9&#xff0c;\u8fed\u4ee3\u5668\u5168\u90e8\u5931\u6548&#xff1b;\u5220\u9664\u5143\u7d20\u4ec5\u5f53\u524d\u8fed\u4ee3\u5668\u5931\u6548<\/td>\n<\/tr>\n<tr>\n<td>\u9002\u7528\u573a\u666f<\/td>\n<td>\u6709\u5e8f\u67e5\u627e\u3001\u8303\u56f4\u67e5\u8be2\u3001key \u65e0\u54c8\u5e0c\u51fd\u6570<\/td>\n<td>\u9ad8\u9891\u5355\u70b9\u67e5\u627e\u3001\u65e0\u5e8f\u573a\u666f\u3001\u8ffd\u6c42\u6781\u81f4\u67e5\u8be2\u6548\u7387<\/td>\n<\/tr>\n<tr>\n<td>C&#043;&#043; \u6807\u51c6<\/td>\n<td>C&#043;&#043;98 \u539f\u751f\u652f\u6301<\/td>\n<td>C&#043;&#043;11 \u65b0\u589e\u7279\u6027<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h5>3.4 \u4e24\u8005\u7684\u9002\u7528\u573a\u666f<\/h5>\n<p>std::map \u9002\u7528\u573a\u666f<\/p>\n<p>\u9700\u8981\u6709\u5e8f\u904d\u5386 \/ \u8303\u56f4\u67e5\u8be2\u7684\u4e1a\u52a1&#xff0c;\u6bd4\u5982&#xff1a;\u6309 key \u7684\u533a\u95f4\u67e5\u627e\u3001\u6392\u884c\u699c\u6392\u5e8f\u3001\u904d\u5386\u8981\u6c42\u6709\u5e8f\u7684\u573a\u666f&#xff1b;\u6216\u8005\u4e1a\u52a1\u4e2d\u7684 key\u65e0\u6cd5\u5b9e\u73b0\u54c8\u5e0c\u51fd\u6570&#xff08;\u6bd4\u5982\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u65e0\u54c8\u5e0c&#xff09;&#xff0c;\u53ea\u80fd\u63d0\u4f9b&lt;\u6bd4\u8f83\u8fd0\u7b97\u7b26\u65f6\u3002<\/p>\n<p>\u4f8b&#xff1a;\u6309\u7528\u6237 ID \u533a\u95f4\u67e5\u8be2\u3001\u6309\u65f6\u95f4\u6233\u6392\u5e8f\u7684\u65e5\u5fd7\u5217\u8868\u3002<\/p>\n<p>std::unordered_map \u9002\u7528\u573a\u666f<\/p>\n<p>\u6240\u6709\u8ffd\u6c42\u6781\u81f4\u67e5\u8be2\u6548\u7387\u7684\u9ad8\u9891\u5355\u70b9\u67e5\u627e\u573a\u666f&#xff08;99% \u7684\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u573a\u666f&#xff09;&#xff0c;\u6bd4\u5982&#xff1a;\u901a\u8fc7\u552f\u4e00 key \u5feb\u901f\u67e5\u627e\u5bf9\u5e94 value\u3001\u65e0\u6392\u5e8f\u8981\u6c42\u7684\u952e\u503c\u5bf9\u5b58\u50a8&#xff0c;\u8fd9\u662f\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u7684\u9996\u9009&#xff0c;\u56e0\u4e3a O (1) \u7684\u67e5\u8be2\u6548\u7387\u80fd\u6781\u5927\u964d\u4f4e CPU \u5f00\u9500\u3002<\/p>\n<p>\u4f8b&#xff1a;\u901a\u8fc7\u8fde\u63a5 fd \u67e5\u627e TcpConnection \u5bf9\u8c61\u3001\u901a\u8fc7\u7528\u6237 ID \u67e5\u627e UserSession \u5bf9\u8c61\u3002<\/p>\n<h2>\u56db\u3001\u9ad8\u6027\u80fd\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d&#xff0c;STL \u5bb9\u5668\u7684\u5177\u4f53\u843d\u5730\u573a\u666f<\/h2>\n<p>\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u5bf9 STL \u5bb9\u5668\u7684\u9009\u578b\u539f\u5219\u662f\u300c\u6781\u81f4\u6027\u80fd &#043; \u8d34\u5408\u4e1a\u52a1\u573a\u666f\u300d&#xff0c;90% \u7684\u573a\u666f\u4f18\u5148\u7528 C&#043;&#043;11 \u65b0\u589e\u7684\u5bb9\u5668&#xff08;unordered_map\/unordered_set&#xff09; &#xff0c;\u8fd9\u4e5f\u662f\u9762\u8bd5\u5b98\u60f3\u542c\u5230\u7684\u6838\u5fc3\u70b9&#xff01;<\/p>\n<h5>\u573a\u666f 1&#xff1a;std::unordered_map<\/h5>\n<p>\u8fde\u63a5\u7ba1\u7406\u6838\u5fc3&#xff1a;\u5b58\u50a8\u300csocket fd \u2192 TcpConnection \u667a\u80fd\u6307\u9488\u300d\u7684\u6620\u5c04\u5173\u7cfb<\/p>\n<ul>\n<li>\u573a\u666f&#xff1a;\u670d\u52a1\u5668\u7684 IO \u7ebf\u7a0b\u901a\u8fc7 epoll \u62ff\u5230\u5c31\u7eea\u7684 fd \u540e&#xff0c;\u9700\u8981\u7acb\u523b\u627e\u5230\u5bf9\u5e94\u7684 TcpConnection \u5bf9\u8c61\u5904\u7406\u8bfb\u5199\u4e8b\u4ef6&#xff0c;\u8fd9\u662f\u767e\u4e07\u5e76\u53d1\u7684\u6838\u5fc3\u94fe\u8def&#xff0c;\u8981\u6c42\u6781\u81f4\u7684\u67e5\u8be2\u6548\u7387&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;\u5355\u70b9\u67e5\u627e\u3001\u65e0\u6392\u5e8f\u8981\u6c42&#xff0c;O (1) \u7684\u67e5\u8be2\u6548\u7387\u662f\u521a\u9700&#xff0c;\u8fd9\u662f\u670d\u52a1\u5668\u4e2d\u4f7f\u7528\u9891\u7387\u6700\u9ad8\u7684 STL \u5bb9\u5668&#xff1b;<\/li>\n<li>\u4ee3\u7801\u793a\u4f8b<\/li>\n<\/ul>\n<p>\/\/ \u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u6838\u5fc3&#xff1a;fd\u5230\u8fde\u63a5\u7684\u6620\u5c04\u8868&#xff0c;\u7ebf\u7a0b\u5b89\u5168\u9700\u52a0\u9501<br \/>\nstd::unordered_map&lt;int, std::shared_ptr&lt;TcpConnection&gt;&gt; conn_map_;<br \/>\nstd::mutex conn_mutex_;<\/p>\n<p>\/\/ \u6838\u5fc3\u64cd\u4f5c&#xff1a;\u901a\u8fc7fd\u5feb\u901f\u67e5\u627e\u8fde\u63a5<br \/>\nstd::shared_ptr&lt;TcpConnection&gt; getConnByFd(int fd) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(conn_mutex_);<br \/>\n    auto it &#061; conn_map_.find(fd);<br \/>\n    return it !&#061; conn_map_.end() ? it-&gt;second : nullptr;<br \/>\n}<\/p>\n<p>\u4e1a\u52a1\u5c42\u6570\u636e\u5b58\u50a8&#xff1a;\u5b58\u50a8\u300c\u7528\u6237 ID \u2192 UserSession\u300d\u3001\u300cToken \u2192 \u7528\u6237\u4fe1\u606f\u300d\u7684\u6620\u5c04\u5173\u7cfb<\/p>\n<ul>\n<li>\u573a\u666f&#xff1a;\u767b\u5f55\u9a8c\u8bc1\u3001\u7528\u6237\u4f1a\u8bdd\u7ba1\u7406&#xff0c;\u901a\u8fc7\u552f\u4e00 ID\/Token \u5feb\u901f\u67e5\u627e\u7528\u6237\u6570\u636e&#xff0c;\u65e0\u6392\u5e8f\u8981\u6c42&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;O (1) \u67e5\u8be2\u6548\u7387&#xff0c;\u652f\u6491\u767e\u4e07\u7ea7\u7528\u6237\u7684\u5feb\u901f\u9a8c\u8bc1\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48\u4e0d\u7528 map&#xff1f;<\/p>\n<p>\u5982\u679c\u7528 map&#xff0c;\u67e5\u8be2\u6548\u7387\u662f O (log n)&#xff0c;\u767e\u4e07\u7ea7\u8fde\u63a5\u4e0b&#xff0c;\u6bcf\u6b21 fd \u67e5\u627e\u7684\u8017\u65f6\u4f1a\u662f unordered_map \u7684\u6570\u500d&#xff0c;CPU \u5f00\u9500\u4f1a\u88ab\u65e0\u9650\u653e\u5927&#xff0c;\u76f4\u63a5\u5bfc\u81f4\u670d\u52a1\u5668\u7684\u5e76\u53d1\u80fd\u529b\u4e0b\u964d&#xff0c;\u8fd9\u662f\u7edd\u5bf9\u7684\u6027\u80fd\u5927\u5fcc\u3002<\/p>\n<h5>\u573a\u666f 2&#xff1a;std::vector<\/h5>\n<p>\u7f51\u7edc\u6570\u636e\u7f13\u51b2\u533a&#xff1a;\u5b58\u50a8\u5f85\u53d1\u9001\u7684\u4e8c\u8fdb\u5236\u6570\u636e\u3001\u89e3\u6790\u540e\u7684\u534f\u8bae\u5305<\/p>\n<ul>\n<li>\u573a\u666f&#xff1a;\u6bcf\u4e2a TcpConnection \u5bf9\u8c61\u5185\u90e8\u90fd\u6709\u300c\u8bfb\u7f13\u51b2\u533a\u300d\u548c\u300c\u5199\u7f13\u51b2\u533a\u300d&#xff0c;\u7528 vector&lt;char&gt;\u5b58\u50a8\u4e8c\u8fdb\u5236\u6570\u636e&#xff0c;\u652f\u6301\u968f\u673a\u8bbf\u95ee\u3001\u5c3e\u90e8\u8ffd\u52a0&#xff0c;\u6548\u7387\u6781\u9ad8&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;\u8fde\u7eed\u5185\u5b58\u3001\u968f\u673a\u8bbf\u95ee O (1)\u3001\u5c3e\u90e8\u63d2\u5165emplace_back\u6548\u7387\u9ad8&#xff0c;\u4e14 C&#043;&#043;11 \u7684\u79fb\u52a8\u8bed\u4e49\u80fd\u5927\u5e45\u964d\u4f4e\u62f7\u8d1d\u5f00\u9500&#xff1b;\u5fc5\u505a\u4f18\u5316&#xff1a;\u63d0\u524d\u8c03\u7528reserve(4096)\u9884\u5206\u914d\u7f13\u51b2\u533a\u5927\u5c0f&#xff0c;\u907f\u514d\u9891\u7e41\u6269\u5bb9&#xff1b;<\/li>\n<li>\u4ee3\u7801\u793a\u4f8b<\/li>\n<\/ul>\n<p>class TcpConnection {<br \/>\nprivate:<br \/>\n    std::vector&lt;char&gt; read_buf_;  \/\/ \u8bfb\u7f13\u51b2\u533a<br \/>\n    std::vector&lt;char&gt; write_buf_; \/\/ \u5199\u7f13\u51b2\u533a<br \/>\npublic:<br \/>\n    TcpConnection() {<br \/>\n        \/\/ \u9884\u5206\u914d4K\u5185\u5b58&#xff0c;\u907f\u514d\u6269\u5bb9&#xff0c;\u670d\u52a1\u5668\u6838\u5fc3\u4f18\u5316\u70b9<br \/>\n        read_buf_.reserve(4096);<br \/>\n        write_buf_.reserve(4096);<br \/>\n    }<br \/>\n};<\/p>\n<ul>\n<li>\u5b58\u50a8\u6279\u91cf\u4e1a\u52a1\u6570\u636e&#xff1a;\u6bd4\u5982\u6279\u91cf\u7684\u65e5\u5fd7\u6570\u636e\u3001\u6279\u91cf\u7684\u8bf7\u6c42\u7ed3\u679c\u3001\u5b9a\u65f6\u5668\u4efb\u52a1\u5217\u8868\n<ul>\n<li>\u573a\u666f&#xff1a;\u670d\u52a1\u5668\u7684\u65e5\u5fd7\u6a21\u5757\u6536\u96c6\u6279\u91cf\u65e5\u5fd7\u540e&#xff0c;\u7528 vector \u5b58\u50a8&#xff0c;\u6279\u91cf\u5199\u5165\u6587\u4ef6&#xff1b;\u5b9a\u65f6\u5668\u7ebf\u7a0b\u7528 vector \u5b58\u50a8\u6240\u6709\u5b9a\u65f6\u4efb\u52a1&#xff0c;\u904d\u5386\u6267\u884c&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;\u904d\u5386\u6548\u7387\u6781\u9ad8&#xff0c;\u5185\u5b58\u8fde\u7eed&#xff0c;CPU \u7f13\u5b58\u547d\u4e2d\u7387\u9ad8&#xff0c;\u9002\u5408\u6279\u91cf\u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u5b58\u50a8\u5f31\u5f15\u7528\u8fde\u63a5\u5217\u8868&#xff1a;EventLoop \u4e2d\u5b58\u50a8std::vector&lt;std::weak_ptr&lt;TcpConnection&gt;&gt;\n<ul>\n<li>\u573a\u666f&#xff1a;\u5fc3\u8df3\u68c0\u6d4b\u65f6\u904d\u5386\u6240\u6709\u8fde\u63a5&#xff0c;\u901a\u8fc7 weak_ptr::lock () \u5224\u65ad\u8fde\u63a5\u662f\u5426\u5b58\u6d3b&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;vector \u904d\u5386\u6548\u7387\u6700\u9ad8&#xff0c;\u9002\u5408\u6279\u91cf\u5904\u7406\u7684\u573a\u666f\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<h5>\u573a\u666f 3&#xff1a;std::map<\/h5>\n<p>\u6709\u5e8f\u7684\u4e1a\u52a1\u6570\u636e\u5b58\u50a8&#xff1a;\u6bd4\u5982\u6309\u65f6\u95f4\u6233\u6392\u5e8f\u7684\u7cfb\u7edf\u65e5\u5fd7\u3001\u6309\u4f18\u5148\u7ea7\u6392\u5e8f\u7684\u4efb\u52a1\u961f\u5217<\/p>\n<ul>\n<li>\u573a\u666f&#xff1a;\u670d\u52a1\u5668\u7684\u76d1\u63a7\u6a21\u5757&#xff0c;\u6309\u65f6\u95f4\u6233\u5b58\u50a8\u6027\u80fd\u6307\u6807&#xff08;QPS\u3001\u8017\u65f6&#xff09;&#xff0c;\u9700\u8981\u6309\u65f6\u95f4\u8303\u56f4\u67e5\u8be2&#xff0c;\u6b64\u65f6\u5fc5\u987b\u7528 map&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;\u5929\u7136\u6709\u5e8f&#xff0c;\u652f\u6301lower_bound\/upper_bound\u7684\u8303\u56f4\u67e5\u627e&#xff0c;\u8fd9\u662f unordered_map \u65e0\u6cd5\u66ff\u4ee3\u7684&#xff1b;<\/li>\n<li>\u793a\u4f8b&#xff1a;std::map&lt;uint64_t, MetricData&gt; metric_map_; &#xff08;uint64_t \u662f\u65f6\u95f4\u6233&#xff0c;\u6709\u5e8f\u5b58\u50a8&#xff09;\u3002<\/li>\n<\/ul>\n<p>key \u65e0\u6cd5\u54c8\u5e0c\u7684\u573a\u666f&#xff1a;\u6bd4\u5982\u81ea\u5b9a\u4e49\u7ed3\u6784\u4f53\u4f5c\u4e3a key&#xff0c;\u53ea\u80fd\u63d0\u4f9b&lt;\u6bd4\u8f83\u8fd0\u7b97\u7b26&#xff0c;\u65e0\u6cd5\u5b9e\u73b0\u54c8\u5e0c\u51fd\u6570&#xff0c;\u6b64\u65f6\u53ea\u80fd\u7528 map\u3002<\/p>\n<h5>\u573a\u666f 4&#xff1a;std::unordered_set<\/h5>\n<p>\u5b58\u50a8\u300c\u5df2\u767b\u5f55\u7684\u7528\u6237 ID\u300d\u3001\u300c\u9ed1\u540d\u5355 IP\u300d\u3001\u300c\u6d3b\u8dc3\u7684\u8fde\u63a5 fd\u300d&#xff0c;\u5b9e\u73b0\u5feb\u901f\u5224\u91cd \/ \u5feb\u901f\u67e5\u8be2\u662f\u5426\u5b58\u5728\u3002<\/p>\n<ul>\n<li>\u573a\u666f&#xff1a;\u670d\u52a1\u5668\u7684\u5b89\u5168\u6a21\u5757&#xff0c;\u5224\u65ad\u67d0\u4e2a IP \u662f\u5426\u5728\u9ed1\u540d\u5355\u4e2d&#xff1b;\u5224\u65ad\u7528\u6237\u662f\u5426\u5df2\u767b\u5f55&#xff0c;\u907f\u514d\u91cd\u590d\u767b\u5f55&#xff1b;<\/li>\n<li>\u9009\u578b\u539f\u56e0&#xff1a;\u5224\u65ad\u5143\u7d20\u662f\u5426\u5b58\u5728\u7684\u65f6\u95f4\u590d\u6742\u5ea6 O (1)&#xff0c;\u6bd4 vector \u7684 find (O (n)) \u9ad8\u6548\u767e\u500d&#xff0c;\u662f\u53bb\u91cd\u573a\u666f\u7684\u6700\u4f18\u89e3&#xff1b;<\/li>\n<li>\u793a\u4f8b&#xff1a;std::unordered_set&lt;std::string&gt; black_ip_set_; \u5feb\u901f\u5224\u65ad IP \u662f\u5426\u5728\u9ed1\u540d\u5355\u3002<\/li>\n<\/ul>\n<h5>\u573a\u666f 5&#xff1a;std::queue<\/h5>\n<p>\u670d\u52a1\u5668\u7684\u7ebf\u7a0b\u6c60\u4efb\u52a1\u961f\u5217\u3001\u7f51\u7edc\u6a21\u5757\u7684\u5f85\u53d1\u9001\u6d88\u606f\u961f\u5217&#xff0c;\u662f\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u7684\u6838\u5fc3\u7ec4\u4ef6\u3002<\/p>\n<ul>\n<li>\u5e95\u5c42\u5b9e\u73b0&#xff1a;queue \u662f\u9002\u914d\u5668&#xff0c;\u9ed8\u8ba4\u5c01\u88c5 std::deque \u5b9e\u73b0&#xff0c;\u652f\u6301\u961f\u5934\u51fa\u961f\u3001\u961f\u5c3e\u5165\u961f&#xff0c;\u65f6\u95f4\u590d\u6742\u5ea6 O (1)&#xff1b;<\/li>\n<li>\u573a\u666f&#xff1a;IO \u7ebf\u7a0b\u63a5\u6536\u5230\u8bf7\u6c42\u540e&#xff0c;\u5c06\u4e1a\u52a1\u4efb\u52a1\u5c01\u88c5\u6210\u51fd\u6570\u5bf9\u8c61&#xff0c;\u63a8\u5165 queue&#xff0c;\u5de5\u4f5c\u7ebf\u7a0b\u4ece queue \u4e2d\u53d6\u4efb\u52a1\u6267\u884c&#xff0c;\u5b9e\u73b0\u300cIO \u7ebf\u7a0b\u4e0e\u5de5\u4f5c\u7ebf\u7a0b\u7684\u89e3\u8026\u300d&#xff1b;<\/li>\n<\/ul>\n<p>\/\/ \u7ebf\u7a0b\u6c60\u7684\u4efb\u52a1\u961f\u5217<br \/>\nstd::queue&lt;std::function&lt;void()&gt;&gt; task_queue_;<br \/>\nstd::mutex task_mutex_;<br \/>\nstd::condition_variable task_cv_;<\/p>\n<p>\/\/ IO\u7ebf\u7a0b\u6295\u9012\u4efb\u52a1<br \/>\nvoid postTask(std::function&lt;void()&gt; task) {<br \/>\n    std::lock_guard&lt;std::mutex&gt; lock(task_mutex_);<br \/>\n    task_queue_.push(std::move(task));<br \/>\n    task_cv_.notify_one();<br \/>\n}<\/p>\n<h5>\u5176\u4ed6\u5bb9\u5668\u7684\u843d\u5730\u573a\u666f<\/h5>\n<ul>\n<li>std::deque&#xff1a;\u53cc\u7aef\u961f\u5217&#xff0c;\u4f5c\u4e3a std::queue\/std::stack \u7684\u5e95\u5c42\u5b9e\u73b0&#xff0c;\u670d\u52a1\u5668\u4e2d\u5f88\u5c11\u76f4\u63a5\u4f7f\u7528&#xff1b;<\/li>\n<li>std::set\/unordered_set&#xff1a;\u4e0e map\/unordered_map \u7684\u533a\u522b\u662f\u300c\u53ea\u5b58 key&#xff0c;\u4e0d\u5b58 value\u300d&#xff0c;\u9002\u7528\u7eaf\u53bb\u91cd \/ \u5224\u5b58\u5728\u7684\u573a\u666f&#xff1b;<\/li>\n<li>std::string&#xff1a;\u672c\u8d28\u662f\u300c\u5b57\u7b26\u7684 vector\u300d&#xff0c;\u4e5f\u662f STL \u5bb9\u5668&#xff0c;\u670d\u52a1\u5668\u4e2d\u5b58\u50a8\u5b57\u7b26\u4e32&#xff08;\u6bd4\u5982 URL\u3001\u8bf7\u6c42\u5934\u3001\u534f\u8bae\u5b57\u6bb5&#xff09;\u7684\u6838\u5fc3\u5bb9\u5668\u3002<\/li>\n<\/ul>\n<h2>\u4e94\u3001\u5ef6\u4f38<\/h2>\n<h5>Q1&#xff1a;\u9ad8\u6027\u80fd\u670d\u52a1\u5668\u4e2d&#xff0c;\u4f7f\u7528 vector \u65f6&#xff0c;\u4e3a\u4ec0\u4e48\u4e00\u5b9a\u8981\u8c03\u7528 reserve ()&#xff1f;\u4e0d\u8c03\u7528\u4f1a\u6709\u4ec0\u4e48\u95ee\u9898&#xff1f;<\/h5>\n<p>A&#xff1a;\u6838\u5fc3\u539f\u56e0\u662f\u907f\u514d\u9891\u7e41\u6269\u5bb9\u7684\u6027\u80fd\u5f00\u9500\u3002vector \u7684\u6269\u5bb9\u662f\u91cd\u91cf\u7ea7\u64cd\u4f5c&#xff08;\u7533\u8bf7\u65b0\u5185\u5b58 &#043; \u62f7\u8d1d\u6570\u636e &#043; \u91ca\u653e\u539f\u5185\u5b58&#xff09;&#xff0c;\u5982\u679c\u4e0d\u8c03\u7528 reserve ()&#xff0c;\u9ed8\u8ba4\u521d\u59cb\u5bb9\u91cf\u5f88\u5c0f&#xff08;\u6bd4\u5982 16&#xff09;&#xff0c;\u63d2\u5165\u5927\u91cf\u6570\u636e\u65f6\u4f1a\u89e6\u53d1\u591a\u6b21\u6269\u5bb9&#xff0c;\u6bcf\u6b21\u6269\u5bb9\u90fd\u6709 O (n) \u7684\u5f00\u9500&#xff0c;\u9ad8\u5e76\u53d1\u4e0b\u4f1a\u76f4\u63a5\u62d6\u6162\u670d\u52a1\u5668\u7684\u5904\u7406\u901f\u5ea6&#xff1b;\u8c03\u7528 reserve (n) \u540e&#xff0c;\u4e00\u6b21\u6027\u7533\u8bf7\u8db3\u591f\u7684\u5185\u5b58&#xff0c;\u5f7b\u5e95\u907f\u514d\u540e\u7eed\u6269\u5bb9&#xff0c;\u8fd9\u662f vector \u5728\u670d\u52a1\u5668\u4e2d\u7684\u5fc5\u505a\u4f18\u5316&#xff0c;\u65e0\u4efb\u4f55\u526f\u4f5c\u7528\u3002<\/p>\n<h5>Q2&#xff1a;unordered_map \u7684\u54c8\u5e0c\u51b2\u7a81\u4f1a\u5f71\u54cd\u670d\u52a1\u5668\u6027\u80fd\u5417&#xff1f;\u600e\u4e48\u89e3\u51b3&#xff1f;<\/h5>\n<p>A&#xff1a;\u54c8\u5e0c\u51b2\u7a81\u4f1a\u8f7b\u5fae\u5f71\u54cd\u6027\u80fd&#xff0c;\u4f46\u751f\u4ea7\u73af\u5883\u4e2d\u51e0\u4e4e\u53ef\u4ee5\u5ffd\u7565\u3002\u89e3\u51b3\u54c8\u5e0c\u51b2\u7a81\u7684\u65b9\u6848\u6709 2 \u4e2a&#xff1a;<\/p>\n<li>\u670d\u52a1\u5668\u4e2d\u5b58\u50a8\u7684 key&#xff08;fd\u3001\u7528\u6237 ID\u3001IP&#xff09;\u90fd\u662f\u6574\u578b \/ \u5b57\u7b26\u4e32&#xff0c;STL \u5bf9\u8fd9\u4e9b\u7c7b\u578b\u7684\u54c8\u5e0c\u51fd\u6570\u505a\u4e86\u6781\u81f4\u4f18\u5316&#xff0c;\u54c8\u5e0c\u51b2\u7a81\u7684\u6982\u7387\u6781\u4f4e&#xff1b;<\/li>\n<li>unordered_map \u7684\u5e95\u5c42\u4f1a\u81ea\u52a8\u5904\u7406\u51b2\u7a81&#xff1a;\u51b2\u7a81\u5143\u7d20\u7528\u94fe\u8868\u6302\u8f7d&#xff0c;\u94fe\u8868\u8fc7\u957f\u65f6\u81ea\u52a8\u8f6c\u4e3a\u7ea2\u9ed1\u6811&#xff0c;\u628a\u67e5\u627e\u6548\u7387\u4ece O (n) \u4f18\u5316\u5230 O (log n)\u3002\u2714 \u8865\u5145&#xff1a;\u670d\u52a1\u5668\u4e2d\u65e0\u9700\u624b\u52a8\u5904\u7406\u54c8\u5e0c\u51b2\u7a81&#xff0c;STL \u7684\u5b9e\u73b0\u8db3\u591f\u5065\u58ee\u3002<\/li>\n<h5>Q3&#xff1a;\u9ad8\u5e76\u53d1\u670d\u52a1\u5668\u4e2d&#xff0c;\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u64cd\u4f5c unordered_map \u9700\u8981\u52a0\u9501\u5417&#xff1f;\u4e3a\u4ec0\u4e48&#xff1f;<\/h5>\n<p>A&#xff1a;\u5fc5\u987b\u52a0\u9501\u3002STL \u7684\u6240\u6709\u5bb9\u5668&#xff08;\u5305\u62ec unordered_map\/map\/vector&#xff09;\u90fd\u662f\u975e\u7ebf\u7a0b\u5b89\u5168\u7684&#xff0c;\u6ca1\u6709\u4efb\u4f55\u5e76\u53d1\u63a7\u5236\u3002\u5982\u679c\u591a\u4e2a\u7ebf\u7a0b\u540c\u65f6\u5bf9\u540c\u4e00\u4e2a\u5bb9\u5668\u505a\u300c\u63d2\u5165 \/ \u5220\u9664 \/ \u4fee\u6539\u300d\u64cd\u4f5c&#xff0c;\u4f1a\u5bfc\u81f4\u5bb9\u5668\u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784&#xff08;\u54c8\u5e0c\u8868 \/ \u7ea2\u9ed1\u6811&#xff09;\u88ab\u7834\u574f&#xff0c;\u51fa\u73b0\u8fed\u4ee3\u5668\u5931\u6548\u3001\u6570\u636e\u9519\u4e71\u3001\u7a0b\u5e8f\u5d29\u6e83\u7684\u95ee\u9898&#xff1b;\u53ea\u8bfb\u64cd\u4f5c\u65e0\u9700\u52a0\u9501\u3002\u2714 \u670d\u52a1\u5668\u4e2d\u7684\u6807\u51c6\u5199\u6cd5&#xff1a;\u7528std::mutex &#043; std::lock_guard\u505a\u7ebf\u7a0b\u5b89\u5168\u4fdd\u62a4&#xff0c;\u5982\u4e0a\u6587\u7684 conn_map_\u793a\u4f8b\u3002<\/p>\n<h5>Q4&#xff1a;map \u548c unordered_map \u7684\u8fed\u4ee3\u5668\u5931\u6548\u95ee\u9898&#xff0c;\u5728\u670d\u52a1\u5668\u4e2d\u9700\u8981\u6ce8\u610f\u4ec0\u4e48&#xff1f;<\/h5>\n<p>A&#xff1a;1. map \u7684\u8fed\u4ee3\u5668&#xff1a;\u63d2\u5165 \/ \u5220\u9664\u5143\u7d20\u540e&#xff0c;\u8fed\u4ee3\u5668\u4e0d\u4f1a\u5931\u6548&#xff0c;\u53ea\u4f1a\u8ba9\u88ab\u5220\u9664\u5143\u7d20\u7684\u8fed\u4ee3\u5668\u5931\u6548&#xff0c;\u8fd9\u662f\u7ea2\u9ed1\u6811\u7684\u7279\u6027&#xff0c;\u670d\u52a1\u5668\u4e2d\u53ef\u4ee5\u653e\u5fc3\u904d\u5386&#xff1b;2. unordered_map \u7684\u8fed\u4ee3\u5668&#xff1a;\u63d2\u5165\u5143\u7d20\u53ef\u80fd\u89e6\u53d1\u6269\u5bb9&#xff0c;\u6b64\u65f6\u6240\u6709\u8fed\u4ee3\u5668\u5168\u90e8\u5931\u6548&#xff1b;\u5220\u9664\u5143\u7d20\u4ec5\u5f53\u524d\u8fed\u4ee3\u5668\u5931\u6548\u3002\u670d\u52a1\u5668\u4e2d\u904d\u5386 unordered_map \u65f6&#xff0c;\u5c3d\u91cf\u907f\u514d\u8fb9\u904d\u5386\u8fb9\u63d2\u5165&#xff0c;\u5982\u9700\u63d2\u5165\u53ef\u5148\u6536\u96c6\u6570\u636e&#xff0c;\u904d\u5386\u5b8c\u6210\u540e\u518d\u6279\u91cf\u63d2\u5165\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001STL \u7684\u516d\u5927\u6838\u5fc3\u7ec4\u4ef6STL&#xff08;Standard Template Library&#xff0c;\u6807\u51c6\u6a21\u677f\u5e93&#xff09;\u662f C \u7684\u6807\u51c6\u5e93\u6838\u5fc3&#xff0c;\u662f\u6cdb\u578b\u7f16\u7a0b\u601d\u60f3\u7684\u6781\u81f4\u5b9e\u73b0&#xff0c;STL \u7684\u6240\u6709\u5185\u5bb9\u88ab\u4e25\u683c\u5212\u5206\u4e3a\u516d\u5927\u76f8\u4e92\u72ec\u7acb\u53c8\u7d27\u5bc6\u914d\u5408\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u6240\u6709\u5bb9\u5668 \/ \u7b97\u6cd5 \/ \u8fed\u4ee3\u5668\u90fd\u662f\u57fa\u4e8e\u8fd9\u516d\u5927\u7ec4\u4ef6\u5b9e\u73b0&#xff0c;\u7f3a\u4e00\u4e0d\u53ef\u3002\u5bb9\u5668&#xff08;Container&#xff09; &#xff1a;\u5b58\u653e\u6570\u636e\u7684\u5bb9\u5668&#xff0c;\u662f S<\/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,190],"topic":[],"class_list":["post-61116","post","type-post","status-publish","format-standard","hentry","category-server","tag-c","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \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\/61116.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001STL \u7684\u516d\u5927\u6838\u5fc3\u7ec4\u4ef6STL&#xff08;Standard Template Library&#xff0c;\u6807\u51c6\u6a21\u677f\u5e93&#xff09;\u662f C \u7684\u6807\u51c6\u5e93\u6838\u5fc3&#xff0c;\u662f\u6cdb\u578b\u7f16\u7a0b\u601d\u60f3\u7684\u6781\u81f4\u5b9e\u73b0&#xff0c;STL \u7684\u6240\u6709\u5185\u5bb9\u88ab\u4e25\u683c\u5212\u5206\u4e3a\u516d\u5927\u76f8\u4e92\u72ec\u7acb\u53c8\u7d27\u5bc6\u914d\u5408\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u6240\u6709\u5bb9\u5668 \/ \u7b97\u6cd5 \/ \u8fed\u4ee3\u5668\u90fd\u662f\u57fa\u4e8e\u8fd9\u516d\u5927\u7ec4\u4ef6\u5b9e\u73b0&#xff0c;\u7f3a\u4e00\u4e0d\u53ef\u3002\u5bb9\u5668&#xff08;Container&#xff09; &#xff1a;\u5b58\u653e\u6570\u636e\u7684\u5bb9\u5668&#xff0c;\u662f S\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/61116.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-16T19:05:32+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=\"6 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61116.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/61116.html\",\"name\":\"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-16T19:05:32+00:00\",\"dateModified\":\"2026-01-16T19:05:32+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/61116.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/61116.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/61116.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map\"}]},{\"@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":"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \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\/61116.html","og_locale":"zh_CN","og_type":"article","og_title":"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001STL \u7684\u516d\u5927\u6838\u5fc3\u7ec4\u4ef6STL&#xff08;Standard Template Library&#xff0c;\u6807\u51c6\u6a21\u677f\u5e93&#xff09;\u662f C \u7684\u6807\u51c6\u5e93\u6838\u5fc3&#xff0c;\u662f\u6cdb\u578b\u7f16\u7a0b\u601d\u60f3\u7684\u6781\u81f4\u5b9e\u73b0&#xff0c;STL \u7684\u6240\u6709\u5185\u5bb9\u88ab\u4e25\u683c\u5212\u5206\u4e3a\u516d\u5927\u76f8\u4e92\u72ec\u7acb\u53c8\u7d27\u5bc6\u914d\u5408\u7684\u6838\u5fc3\u7ec4\u4ef6&#xff0c;\u6240\u6709\u5bb9\u5668 \/ \u7b97\u6cd5 \/ \u8fed\u4ee3\u5668\u90fd\u662f\u57fa\u4e8e\u8fd9\u516d\u5927\u7ec4\u4ef6\u5b9e\u73b0&#xff0c;\u7f3a\u4e00\u4e0d\u53ef\u3002\u5bb9\u5668&#xff08;Container&#xff09; &#xff1a;\u5b58\u653e\u6570\u636e\u7684\u5bb9\u5668&#xff0c;\u662f S","og_url":"https:\/\/www.wsisp.com\/helps\/61116.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-16T19:05:32+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"6 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/61116.html","url":"https:\/\/www.wsisp.com\/helps\/61116.html","name":"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-16T19:05:32+00:00","dateModified":"2026-01-16T19:05:32+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/61116.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/61116.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/61116.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u7ed3\u5408STL\uff0c\u670d\u52a1\u5668\u9879\u76ee\u89e3\u6790vetcor map unordered_map"}]},{"@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\/61116","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=61116"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/61116\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=61116"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=61116"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=61116"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=61116"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}