{"id":78,"date":"2021-09-15T22:44:43","date_gmt":"2021-09-15T14:44:43","guid":{"rendered":"http:\/\/cococat.top\/?p=78"},"modified":"2021-09-15T22:44:43","modified_gmt":"2021-09-15T14:44:43","slug":"c-hash-table-and-stuff","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2021\/09\/15\/c-hash-table-and-stuff\/","title":{"rendered":"C++ hash table and stuff"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"c-hash-table-and-related-stuff\">C++ hash table and related stuff<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-%E5%BC%95%E5%85%A5\">1. \u5f15\u5165<\/h2>\n\n\n\n<p>\u4e3b\u8981\u6d89\u53ca\u7684\u6a21\u677f\u7c7b\u6709set\/unordered_set\/map\/unordered_map\u3002\u5176\u4e2d\uff0c\u65e0unordered\u524d\u7f00\u7684\u7248\u672c\uff08\u5373set\u548cmap\uff09\u5747\u5185\u90e8\u5b9e\u73b0\u7ea2\u9ed1\u6811\uff0c\u800cunordered_set\u548cunordered_map\u5185\u90e8\u5b9e\u73b0\u54c8\u5e0c\u8868\u3002\u6b64\u5916\uff0cmap\u7ec4\u4e0eset\u7ec4\u4e4b\u95f4\u533a\u522b\u5728\u4e8e\u5185\u90e8\u6570\u636e\u5f62\u5f0f\uff0cset\u7ec4\u5747\u4e3a\u5355\u4e00\u6570\u636e\uff0c\u800cmap\u7ec4\u5219\u4e3astd::pair\u683c\u5f0f\u7684\u952e\u503c\u5bf9\uff0c\u5148key\uff0c\u518dvalue\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E6%8F%92%E5%85%A5%E9%83%A8%E5%88%86stdpair%E7%9A%84%E7%AE%80%E5%8D%95%E7%94%A8%E6%B3%95\">\u63d2\u5165\u90e8\u5206\uff1astd::pair\u7684\u7b80\u5355\u7528\u6cd5<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4e3b\u6210\u5458\u53d8\u91cffirst\u548csecond\u662f<strong>\u6210\u5458\u53d8\u91cf<\/strong>\uff0c\u4e0d\u662f\u6210\u5458\u51fd\u6570\uff0c\u4e0d\u7528\u52a0\u62ec\u53f7<\/li><li>\u6784\u9020\u8bed\u53e5\u5199\u6cd5\u548cvector\u5b8c\u5168\u4e00\u6837\uff0c\u53ef\u4ee5\u6709\u5982\u4e0b\u65b9\u5f0f\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>std::pair&lt;int, int&gt; ppp = {1, 2};  \nstd::pair&lt;int, int&gt; ppp(1, 2);  \nstd::pair&lt;int, int&gt; ppp{1, 2};  \n<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f7f\u7528make_pair\u6784\u9020\u53f3\u503cpair\u65f6\uff0c<strong>\u65e0\u9700<\/strong>\u4f20\u5165\u6a21\u677f\u7c7b\u578b\uff0c\u5373\u76f4\u63a5uMap.insert(make_pair(1, \"str\"))\u5373\u53ef\u3002<\/li><li>\u5f53\u7136\u4f7f\u7528\u6b63\u5e38\u7684\u53f3\u503cpair\u6784\u9020\u65f6\u5c31\u9700\u8981\u4f20\u5165\u6a21\u677f\u7c7b\u578b\uff0c\u5982uMap.insert(pair&lt;int, bool&gt;(1, true));<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-%E7%BA%A2%E9%BB%91%E6%A0%91%E4%B8%8E%E5%93%88%E5%B8%8C%E8%A1%A8%E5%AE%B9%E5%99%A8%E7%9A%84%E7%AE%80%E5%8D%95%E5%AF%B9%E6%AF%94\">2. \u7ea2\u9ed1\u6811\u4e0e\u54c8\u5e0c\u8868\u5bb9\u5668\u7684\u7b80\u5355\u5bf9\u6bd4<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E7%BA%A2%E9%BB%91%E6%A0%91mapset\">\u7ea2\u9ed1\u6811map\/set\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f18\u70b9\uff1a<ul><li><strong>\u6709\u5e8f\u6027<\/strong>\uff0c\u6700\u5927\u7684\u4f18\u70b9\uff0c\u5176\u5143\u7d20\u7684\u6709\u5e8f\u6027\u5728\u5f88\u591a\u5e94\u7528\u4e2d\u90fd\u4f1a\u7b80\u5316\u5f88\u591a\u7684\u64cd\u4f5c<\/li><li>\u9ad8\u6548\u4e14\u7a33\u5b9a\uff0c\u67e5\u8be2\u3001\u63d2\u5165\u64cd\u4f5c\u65f6\u95f4\u590d\u6742\u5ea6\u7a33\u5b9a\u4e3aO(logN)<\/li><\/ul><\/li><li>\u7f3a\u70b9\uff1a<ul><li>\u7a7a\u95f4\u5360\u7528\u7387\u9ad8\uff0c\u4ee5\u7ea2\u9ed1\u6811\u4f5c\u4e3a\u5185\u90e8\u5b9e\u73b0\u65f6\uff0c\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u9700\u8981\u989d\u5916\u4fdd\u5b58\u7236\u8282\u70b9\u3001\u5b69\u5b50\u8282\u70b9\u548c\u7ea2\/\u9ed1\u6027\u8d28\u7b49\u6570\u636e\u5b57\u6bb5\uff0c\u4f7f\u5f97\u6bcf\u4e00\u4e2a\u8282\u70b9\u90fd\u5360\u7528\u5927\u91cf\u7684\u7a7a\u95f4<\/li><li>\u67e5\u627e\u7684\u5e73\u5747\u65f6\u95f4\u4e0d\u5982\u54c8\u5e0c\u8868<\/li><\/ul><\/li><li>\u9002\u7528\u5904\uff1a \u5bf9\u6570\u636e\u6392\u5e8f\u6709\u987a\u5e8f\u8981\u6c42\u7684\u573a\u666f<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%93%88%E5%B8%8C%E8%A1%A8-unorderedmapunorderedset\">\u54c8\u5e0c\u8868 unordered_map\/unordered_set\uff1a<\/h3>\n\n\n\n<ul class=\"wp-block-list\"><li>\u4f18\u70b9\uff1a<ul><li>\u6548\u7387\u6781\u9ad8\uff0c\u67e5\u8be2\u548c\u63d2\u5165\u7684\u5e73\u5747\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)<\/li><\/ul><\/li><li>\u7f3a\u70b9\uff1a<ul><li><strong>\u65e0\u5e8f\u6027<\/strong><\/li><li>\u54c8\u5e0c\u8868\u7684\u5efa\u7acb\u6bd4\u8f83\u8017\u65f6<\/li><li>\u5728\u90e8\u5206\u60c5\u51b5\u4e0b\uff0c\u6027\u80fd\u4e0d\u7a33\u5b9a\u3002\u6bd4\u5982\u5f53\u5927\u91cf\u63d2\u5165\u6570\u636e\u65f6\uff0c\u53ef\u80fd\u89e6\u53d1\u5927\u91cfrehash\uff1b\u5f53\u54c8\u5e0c\u51fd\u6570\u8bbe\u7f6e\u4e0d\u5408\u7406\u65f6\uff0c\u51b2\u7a81\u8fc7\u591a\uff0c\u5355\u6b21\u67e5\u8be2\/\u63d2\u5165\u64cd\u4f5c\u6700\u574f\u65f6\u95f4\u590d\u6742\u5ea6\u8fbe\u5230O(N)<\/li><\/ul><\/li><li>\u9002\u7528\u5904\uff1a \u9ad8\u6548\u7387\u7684\u67e5\u627e\u573a\u666f<\/li><\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%AF%B9%E6%AF%94%E5%B0%8F%E7%BB%93\">\u5bf9\u6bd4\u5c0f\u7ed3\uff1a<\/h3>\n\n\n\n<p>\u5728\u9700\u8981\u5143\u7d20\u6709\u5e8f\u6027\u6216\u8005\u5bf9\u5355\u6b21\u67e5\u8be2\u6027\u80fd\u8981\u6c42\u8f83\u4e3a\u654f\u611f\u65f6\uff0c\u8bf7\u4f7f\u7528map\/set\uff0c\u5176\u4f59\u60c5\u51b5\u4e0b\u5e94\u4f7f\u7528unordered_map\/unorderer_set\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"%E5%88%B7%E9%A2%98%E8%B8%A9%E7%9A%84%E5%9D%91\">\u5237\u9898\u8e29\u7684\u5751<\/h3>\n\n\n\n<p>\u54c8\u5e0c\u8868\u597d\u5f52\u597d\uff0c\u4f46\u662f\u9700\u8981\u683c\u5916\u6ce8\u610f\u5176\u65e0\u5e8f\u6027\u3002\u82e5\u9700\u8981\u7528\u5230\u54c8\u5e0c\u8868\u5c06\u4e00\u4e2a\u6570\u7ec4\u8fdb\u884c\u6563\u5217\u5316\uff0c\u7531\u4e8e\u54c8\u5e0c\u8868\u5185\u90e8\u7684\u65e0\u5e8f\uff0c\u5f80\u5f80\u9700\u8981\u7528unordered_map\u4fdd\u5b58\u5143\u7d20\u7684\u539f\u59cb\u6570\u7ec4\u4e0b\u6807\u3002\u5373\uff1a\u952e\u503c\u5bf9\u4e2d\uff0ckey\u4e3anums[i]\uff0cvalue\u4e3ai\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-%E5%B8%B8%E7%94%A8%E6%88%90%E5%91%98%E5%87%BD%E6%95%B0\">3. \u5e38\u7528\u6210\u5458\u51fd\u6570<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li>insert \u63d2\u5165\u6570\u636e\u3002\u8fd4\u56de\u503c\u662f\u4e00\u4e2apair\uff0c\u5176second\u503c\u662f\u4e00\u4e2abool\u53d8\u91cf\uff0c\u4ee3\u8868\u63d2\u5165\u662f\u5426\u51b2\u7a81<\/li><li>find \u67e5\u8be2\u6570\u636e\u3002\u8fd4\u56de\u503c\u662fiterator\uff0c\u5982\u679c\u67e5\u627e\u5931\u8d25\uff0c\u5219\u8fd4\u56de\u503c\u7b49\u4e8eend()\uff1b\u6b64\u5916\u4e5f\u53ef\u4ee5\u7528count\u67e5\u8be2\u6570\u636e\uff0c\u67e5\u8be2\u6210\u529f\u4e3a1\uff0c\u5426\u5219\u4e3a0<\/li><li>\u4fee\u6539\u6570\u636e\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u7528iterator\u6216\u8005operator[]\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>unordered_map&lt;int, int&gt; uMap;\nuMap.insert(pair&lt;int, int&gt;(1, 1));\nuMap.insert(make_pair(2, 2));\n\nauto iter = uMap.find(2);\n++(*iter).second;\ncout &lt;&lt; uMap&#91;2] &lt;&lt; endl;\n++uMap&#91;2];\ncout &lt;&lt; uMap&#91;2] &lt;&lt; endl;\n<\/code><\/pre>\n\n\n\n<p>\u4f46\u662f\u9700\u8981\u6ce8\u610f\uff0coperator[]\u5728\u952e\u503c\u4e0d\u5b58\u5728\u7684\u65f6\u5019\u4f1a\u76f4\u63a5\u8fdb\u884c\u6570\u636e\u7684\u63d2\u5165\uff0c\u7136\u540e\u518d\u8fd4\u56de\u5176\u6620\u5c04\u503c\u7684\u5f15\u7528\u3002\u5f53\u4f60\u4e0d\u786e\u5b9a\u67d0\u952e\u503c\u662f\u5426\u5b58\u5728\u4e8e\u54c8\u5e0c\u8868\u4e2d\uff0c\u8bf7\u4f7f\u7528find\u800c\u975eoperator[]\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>erase\u5220\u9664\u6570\u636e\u3002\u53c2\u6570\u53ef\u4ee5\u662f\u952e\u503c\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u662f\u4e24\u4e2a\u8fed\u4ee3\u5668[left, right]\u3002<\/li><\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>uMap.erase(1);\nuMap.erase(iter);\nuMap.erase(uMap.begin(), uMap.end());\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"4-%E4%B8%80%E4%BA%9B%E6%AF%94%E8%BE%83%E6%9C%89%E5%90%AF%E5%8F%91%E7%9A%84%E7%94%A8%E6%B3%95\">4. \u4e00\u4e9b\u6bd4\u8f83\u6709\u542f\u53d1\u7684\u7528\u6cd5<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"leetcode-17-%E7%94%B5%E8%AF%9D%E5%8F%B7%E7%A0%81%E7%9A%84%E5%AD%97%E6%AF%8D%E7%BB%84%E5%90%88\">LeetCode-17 \u7535\u8bdd\u53f7\u7801\u7684\u5b57\u6bcd\u7ec4\u5408<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>unordered_map&lt;char, string&gt; table{\n        {'0', \" \"}, {'1',\"*\"}, {'2', \"abc\"},\n        {'3',\"def\"}, {'4',\"ghi\"}, {'5',\"jkl\"},\n        {'6',\"mno\"}, {'7',\"pqrs\"},{'8',\"tuv\"},\n        {'9',\"wxyz\"}};  \n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"5-%E5%8F%82%E8%80%83\">5. \u53c2\u8003<\/h2>\n\n\n\n<p><a href=\"https:\/\/www.cnblogs.com\/yimeixiaobai1314\/p\/14375195.html\">https:\/\/www.cnblogs.com\/yimeixiaobai1314\/p\/14375195.html<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ hash table and related stuff 1. \u5f15\u5165 \u4e3b\u8981\u6d89\u53ca\u7684\u6a21\u677f\u7c7b\u6709set\/uno [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,1],"tags":[],"class_list":["post-78","post","type-post","status-publish","format-standard","hentry","category-c","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/78","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}