{"id":221166,"date":"2020-11-25T08:52:08","date_gmt":"2020-11-25T00:52:08","guid":{"rendered":"http:\/\/4563.org\/?p=221166"},"modified":"2020-11-25T08:52:08","modified_gmt":"2020-11-25T00:52:08","slug":"c-stl-%e4%b8%ad%e6%9f%a5%e6%89%be%e9%80%9f%e5%ba%a6%e6%9c%80%e5%bf%ab%e7%9a%84%e6%98%af%e4%bb%80%e4%b9%88%e6%95%b0%e6%8d%ae%e7%bb%93%e6%9e%84%ef%bc%9f","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=221166","title":{"rendered":"C++ STL \u4e2d\u67e5\u627e\u901f\u5ea6\u6700\u5feb\u7684\u662f\u4ec0\u4e48\u6570\u636e\u7ed3\u6784\uff1f"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  C++ STL \u4e2d\u67e5\u627e\u901f\u5ea6\u6700\u5feb\u7684\u662f\u4ec0\u4e48\u6570\u636e\u7ed3\u6784\uff1f               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <span><i><\/i> 9<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u5982\u9898\uff0cC++\u4e0d\u592a\u719f\uff0c\u6700\u8fd1\u6709\u4e2a\u9700\u6c42\u662f\u8981\u628a python \u79d1\u5b66\u8ba1\u7b97\u4ee3\u7801\u7528 C++\u52a0\u901f\uff0c\u5199\u7684\u8fc7\u7a0b\u4e2d\u9047\u5230\u95ee\u9898\u5c31\u6765\u95ee V \u53cb\u4e86\u3002<\/p>\n<p>\u76ee\u524d\u8981\u5b9e\u73b0\u4e00\u4e2a\u7c7b\u4f3c python \u4e2d\u5b57\u5178\u6620\u5c04\u7684\u64cd\u4f5c\uff0c\u5373\u6bd4\u5982\u8f93\u5165 token = &#8220;sam&#8221; \uff0c\u8981\u6c42\u80fd\u591f\u5feb\u901f\u7684\u83b7\u53d6\u5230\u67d0\u6570\u636e\u7ed3\u6784\uff08\u6bd4\u5982\u8bf4\u4e00\u4e2a vector \uff09\u4e2d sam \u5bf9\u5e94\u7684\u6307\u5b9a\u9879\u3002\u636e\u6211\u6240\u77e5 vector \u539f\u751f\u53ef\u4ee5\u7528\u89d2\u6807\u63d0\u53d6\uff0c\u4f46\u5e94\u8be5\u662f\u4e0d\u652f\u6301\u7528\u5b57\u7b26\u4e32\u63d0\u53d6\u8fd9\u79cd\u7684\uff0c\u5373 v[0]\u8fd9\u79cd\u662f OK \u7684\u4f46\u662f v[&#8220;sam&#8221;]\u8fd9\u79cd\u5e94\u8be5\u662f\u4e0d\u884c\u7684\u3002<\/p>\n<p>\u7531\u4e8e\u6211\u4e0d\u6e05\u695a stl \u5bb9\u5668\u5177\u4f53\u7684\u641c\u7d22\u5b9e\u73b0\uff0c\u5e76\u4e0d\u80fd\u786e\u5b9a\u7528\u54ea\u79cd\u65b9\u5f0f\u4f1a\u66f4\u5feb\u3002\u56e0\u4e3a\u6211\u77e5\u9053 py \u7684\u5b57\u5178\u67e5\u627e\u7ecf\u8fc7\u4e86 hash \u4f18\u5316\uff0c\u5982\u679c c++\u4e0d\u597d\u597d\u9009\u62e9\u7b97\u6cd5\u7684\u8bdd\uff0c\u4e00\u4e2a\u641e\u4e0d\u597d\u6bd4 python \u66f4\u6162\u4e5f\u662f\u6709\u53ef\u80fd\u7684\u3002<\/p>\n<p>\u7f51\u4e0a\u641c\u5230\u4e00\u4e9b\u8d44\u6599\u663e\u793a vector \u6bd4 set \u67e5\u627e\u66f4\u5feb\uff0c\u800c\u53e6\u4e00\u4e9b\u8d44\u6599\u5219\u663e\u793a\u76f8\u53cd\uff0c\u4e92\u76f8\u77db\u76fe\uff0c\u770b\u4e0d\u592a\u61c2\u3002\u6c42\u95ee\u4e00\u4e0b c++\u5e26\u4f6c\uff0c\u8fd9\u79cd\u5e38\u89c1\u6570\u636e\u7ed3\u6784\u5728 c++\u91cc\u7684\u6700\u4f73\u5b9e\u8df5\u662f\u4ec0\u4e48\uff1f<\/p>\n<p>===========================<\/p>\n<p>\u53e6\u5916\u4e4b\u524d v2 \u4e00\u4e2a\u8001\u54e5\u4f3c\u4e4e\u8bf4\u8fc7 map \u67e5\u627e\u901f\u5ea6\u8d85\u7ea7\u6162<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>12<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"4460384\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : lcdtyph <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             unordered_map                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460385\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : agagega <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4f60\u60f3\u8981\u7684\u662f unordered_map                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460386\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : DGideas <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e0a\u6b63\u89e3\uff0c\u53e6\u5916\u4e5f\u522b\u8fd9\u4e48\u9ed1 map \u554a\u3002\u3002\u3002\u5145\u5176\u91cf\u67e5\u627e\u901f\u5ea6\u8fd8\u662f\u5bf9\u6570\u65f6\u95f4\u590d\u6742\u5ea6\u5462\u3002\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460387\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : secondwtq <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             vector \u53ea\u80fd\u6574\u6570 index \u8fd9\u53ea\u662f\u4e2a\u63a5\u53e3\u95ee\u9898\u800c\u5df2\uff0c\u4f60\u81ea\u5df1\u5f80\u91cc\u9762\u5b58\u4e2a pair \u81ea\u5df1\u5199\u51fd\u6570\u67e5\u4e5f\u53ef\u4ee5<br \/>C++ \u793e\u533a\u7684\u610f\u601d\u662f\uff0c\u6240\u6709\u6d89\u53ca\u5230 pointer chasing \u7684\u90fd\u6162\uff0c\u6240\u4ee5 std::list \u662f\u5783\u573e\uff0cvector FTW<br \/>\u6240\u4ee5\u5982\u679c\u6570\u636e\u5c0f\u7684\u8bdd\uff0c\u5c31\u76f4\u63a5\u7528 vector \u7ebf\u6027\u67e5\uff0c\u53ef\u80fd\u6bd4\u5176\u4ed6\u6570\u636e\u7ed3\u6784\u8fd8\u8981\u5feb\uff0c\u8fd9\u4e2a\u53eb flat map \uff08\u4e0d\u662f Monad bind \u7684\u90a3\u4e2a flatmap \uff09<br \/>\u53e6\u5916\uff0cvector \u8fd8\u4f1a\u52a0\u4e00\u4e2a SBO \u7684\u4f18\u5316\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u5b8c\u5168\u907f\u514d\u5185\u5b58\u5206\u914d\uff0c\u8fd9\u4e2a\u53eb small vector<\/p>\n<p>unordered_map \u5728\u6027\u80fd\u65b9\u9762\u7684\u95ee\u9898\u5728\u4e8e\u5b83\u662f chaining \u5b9e\u73b0\u7684\uff0c\u5185\u5b58\u6027\u80fd\u4e00\u822c\u4e0d\u5982 open addressing\uff0c\u6240\u4ee5\u6709\u65f6\u5019\u4f1a\u7528 open_addressing \u7684\u5b9e\u73b0\uff0c\u8fd9\u4e2a\u53eb dense_map<br \/>unordered_map \u7684\u4f18\u52bf\u5728\u4e8e\u8fed\u4ee3\u5668\u7a33\u5b9a\uff0cmap \u7684\u4f18\u52bf\u5728\u4e8e ordered access<\/p>\n<p>\u81f3\u4e8e\u67e5\u5b57\u7b26\u4e32\u8fd8\u6709\u4e13\u95e8\u7684\u6570\u636e\u7ed3\u6784<\/p>\n<p>\u81f3\u4e8e\u4e3b\u8fd9\u4e2a\uff0c\u4f60\u5f97\u81ea\u5df1 benchmark                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460388\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @secondwtq \u5927\u4f6c\u518d\u95ee\u4e2a\u4e8b\uff0c\u6709\u5173\u63d2\u5165\uff0c\u8bfb\u53d6\u548c\u5220\u9664\u7684\u6548\u7387\u3002\u76ee\u524d\u9700\u8981\u5bf9\u4e00\u4e2a\u8868\u7c7b\u6570\u636e\u7ed3\u6784\u9891\u7e41\u64cd\u4f5c\uff0c\u5bf9\u5e94\u7684\u662f py \u7684 list\uff0c\u63d2\u5165\uff0c\u8bfb\u53d6\uff0c\u5220\u9664\u6211\u7c97\u7565\u4f30\u8ba1\u5728 5:10:3 \u8fd9\u6837\u7684\u6bd4\u4f8b\uff0c\u7528 vector \u662f\u6b63\u786e\u9009\u62e9\u5417\uff1f\u56e0\u4e3a\u542c\u8bf4 vec \u8bfb\u53d6\u5f88\u5feb\uff0c\u4f46\u662f\u5220\u9664\u5f00\u9500\u5f88\u9ad8\u3002\u5982\u679c\u7528\u94fe\u8868\u7684\u8bdd\u54ea\u4e2a\u66f4\u5408\u9002\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460389\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : QBugHunter <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @black11black <br \/>vector \u65f6\u8fd9\u6837\u7684\uff0c\u6bd4\u5982\u4f60\u8981\u50a8\u5b58 10000 \u4e2a\u5bf9\u8c61\uff0c\u7136\u540e vector \u4f1a\u7533\u8bf7\u4e00\u5757\u8fde\u7eed\u7684\u5185\u5b58\uff0c\u53ef\u80fd\u53ef\u4ee5\u5b58\u653e 20000 \u4e2a\uff08\u8fd9\u4e2a\u503c\u4e0d\u540c\u7248\u672c\u7684 STL \u6709\u6240\u4e0d\u540c\uff09\uff0c\u7136\u540e\u4f60\u8981\u5728\u5c3e\u90e8\u63d2\u5165 10 \u4e2a\uff0c\u6216\u8005\u5220\u9664 10 \u4e2a\uff0c\u901f\u5ea6\u5f88\u5feb<br \/>\u4f46\u5982\u679c\u4f60\u8981\u5728\u5c3e\u90e8\u518d\u63d2\u5165 10000 \u4e2a\u5bf9\u8c61\uff0cvector \u5c31\u4f1a\u518d\u7533\u8bf7\u4e00\u5757 40000 \u4e2a\u5bf9\u8c61\u957f\u5ea6\u7684\u5185\u5b58\uff0c\u7136\u540e\u628a 20000 \u4e2a\u5bf9\u8c61\u590d\u5236\u8fdb\u53bb                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460390\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wctml <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u5e74\u8f7b\u4eba \u4f60\u4e0d\u8bb2\u6b66\u5fb7\uff0c\u4e3a\u4ec0\u4e48\u67e5\u540c\u4e00\u4f4d\u7f6e\uff1b<\/p>\n<p>&#8220;`<br \/> unordered_map&lt;int, float&gt; mapValue;<br \/> clock_t timeStart = clock();<br \/> for (size_t index(0); index &lt; 10000000; ++index)<br \/> {<br \/> mapValue[index] = 1000000 &#8211; index;<br \/> }<br \/> std::cout &lt;&lt; &#8220;make time &#8221; &lt;&lt; (clock() &#8211; timeStart) &lt;&lt; std::endl;<br \/> double sum(0);<br \/> timeStart = clock();<br \/> for (size_t index(0); index &lt; 10000000; ++index)<br \/> {<br \/> sum += mapValue[9999999];<br \/> }<br \/> std::cout &lt;&lt; &#8220;find time &#8221; &lt;&lt; (clock() &#8211; timeStart) &lt;&lt; std::endl;<\/p>\n<p> sum = 0;<br \/> timeStart = clock();<br \/> for (size_t index(0); index &lt; 10000000; ++index)<br \/> {<br \/> const auto&amp; iter = mapValue.find(9999999);<br \/> if (iter != mapValue.end())<br \/> {<br \/> sum += iter-&gt;second;<br \/> }<br \/> }<br \/> std::cout &lt;&lt; &#8220;find time &#8221; &lt;&lt; (clock() &#8211; timeStart) &lt;&lt; std::endl;<br \/>&#8220;`<\/p>\n<p>make time 3432<br \/>find time 39<br \/>find time 7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460391\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @wctml \u8fd9\u65b9\u4ee3\u7801\u5565\u610f\u601d\u5927\u4f6c\uff0cuo_map \u7684\u641c\u7d22\u6bd4\u7d22\u9690\u5feb\u7684\u591a\u7684\u610f\u601d\u5417\uff1f                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460392\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : black11black <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u6709\u9700\u8981\u5927\u91cf\u7528\u5230\u6392\u5e8f\u7684\u6570\u636e\u7ed3\u6784\uff0c\u7f51\u4e0a\u67e5\u4e86\u67e5\u4f3c\u4e4e\u662f deque \u6700\u5feb\uff0c\u7136\u800c\u5b9e\u6d4b\u8fd8\u662f vector \u5feb\uff0c\u5e8f\u5217\u957f\u5ea6\u5728\u4e00\u4e07\u5de6\u53f3\u3002\u4e5f\u662f\u795e\u79d8                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460393\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wctml <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @black11black <br \/>1 \u3001\u8fd9\u662f C++\u5751\u7684\u5730\u65b9\uff0c\u5728\u4e0d\u540c\u7684\u7528\u6cd5\uff0c\u6709\u4e0d\u540c\u7684\u533a\u522b\u3002\u8fd9\u70b9\u4e0d\u50cf python \u505a\u4e00\u4e2a\u4e8b\u60c5\u53ef\u80fd\u53ea\u6709\u4e00\u4e2a\u6700\u4f18\u89e3\uff0c\u4f46\u662f C++\u53ef\u80fd\u6709 N \u4e2a\u89e3\u6cd5\u5f97\u4f60\u53bb\u8e29\u5751\u3002<br \/>2 \u3001\u53ef\u4ee5\u4e86\u89e3 C ++\u4e2d\u7684 map []\u548c map.at \u7684\u533a\u522b\uff1b<br \/>3 \u3001\u53e6\u5916\u67e5\u627e\u540c\u4e00\u4e2a\u4f4d\u7f6e\u53ef\u80fd\u7f16\u8bd1\u5668\u4f1a\u6709\u4f18\u5316\u7684\uff0c\u53ef\u4ee5\u8bd5\u8bd5\u4f2a\u968f\u673a\u641c\u7d22\u6548\u7387\u6d4b\u8bd5\u3002                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460394\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wctml <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             sum = 0;<br \/> timeStart = clock();<br \/> for (size_t index(0); index &lt; 10000000; ++index)<br \/> {<br \/> sum += mapValue.at(9999999);<br \/> }<br \/> std::cout &lt;&lt; &#8220;find time &#8221; &lt;&lt; (clock() &#8211; timeStart) &lt;&lt; std::endl;<\/p>\n<p>find time 7                                                            <\/p><\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"4460395\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : wctml <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             &#8220;`<br \/> sum = 0;<br \/> timeStart = clock();<br \/> for (size_t index(0); index &lt; 10000000; ++index)<br \/> {<br \/> sum += mapValue \u3002at(9999999);<br \/> }<br \/> std::cout &lt;&lt; &#8220;find time &#8221; &lt;&lt; (clock() &#8211; timeStart) &lt;&lt; std::endl;<\/p>\n<p>find time 7<br \/>&#8220;`<\/p>\n<p>\u8bf7\u4e0d\u8981\u5728\u6bcf\u4e00\u4e2a\u56de\u590d\u4e2d\u90fd\u5305\u62ec\u5916\u94fe\uff0c\u8fd9\u770b\u8d77\u6765\u50cf\u662f\u5728 spamming<br \/>\u6211\u53ea\u80fd\u628a\u70b9\u6362\u6210\u53e5\u53f7                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>C++ STL \u4e2d\u67e5\u627e\u901f\u5ea6\u6700\u5feb\u7684\u662f&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221166"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=221166"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/221166\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=221166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=221166"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=221166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}