{"id":163,"date":"2025-07-13T15:22:03","date_gmt":"2025-07-13T07:22:03","guid":{"rendered":"http:\/\/www.tgwttt.xyz\/?p=163"},"modified":"2025-11-24T16:41:34","modified_gmt":"2025-11-24T08:41:34","slug":"%e5%9f%ba%e4%ba%8e%e5%93%88%e5%b8%8c%e6%a1%b6%e6%a8%a1%e6%8b%9funordered_map-set","status":"publish","type":"post","link":"https:\/\/www.tgwttt.xyz\/?p=163","title":{"rendered":"\u57fa\u4e8e\u54c8\u5e0c\u6876\u6a21\u62dfunordered_map\/set"},"content":{"rendered":"\n<p>\u4ee5\u4e0b\u4ee3\u7801\u5df2\u5728gitee\u4e0a\u5f00\u6e90\uff0c\u4ed3\u5e93\u5730\u5740\uff1a<a href=\"https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/Hashbucket\">https:\/\/gitee.com\/tgwTTT\/c-lreant\/tree\/master\/Hashbucket<\/a><\/p>\n\n\n\n<p>\u54c8\u5e0c\u6876\u7684\u5b9e\u73b0\u8bf7\u53c2\u8003\u535a\u4e3b\u7684\u4e0a\u4e00\u7bc7\u6587\u7ae0\uff1a<a href=\"http:\/\/www.tgwttt.xyz\/?p=157\">http:\/\/www.tgwttt.xyz\/?p=157<\/a><\/p>\n\n\n\n<p>unordered\u2014\u2014map\/set\u662fc++11\u65b0\u63a8\u51fa\u7684stl\u5bb9\u5668\uff0c\u5e95\u5c42\u91c7\u7528hash\u8868\u5b9e\u73b0\uff0c\u8bbf\u95ee\u65f6\u65f6\u95f4\u590d\u6742\u5ea6\u4e3aO(1)\uff0c\u6700\u574f\u60c5\u51b5\u4e3aO(n)\uff0c\u8fd9\u662f\u53ca\u5176\u6781\u7aef\u60c5\u51b5\u548chash\u51b2\u7a81\u6ca1\u6709\u5904\u7406\u597d\u7684\u60c5\u51b5\u4e0b\uff0c\u4e0b\u9762\u662f\u5177\u4f53\u5b9e\u73b0\uff1a    <\/p>\n\n\n\n<p>\u8fed\u4ee3\u5668\u7684\u5b9a\u4e49\uff1a                             <br>template<class K, class T, class Ref, class Ptr, class KeyOfT, class Hash><br>\tstruct HTIterator<br>\t{<br>\t\ttypedef HashNode<T> Node;<br>\t\ttypedef HashTable<K, T, KeyOfT, Hash> HT;<br>\t\ttypedef HTIterator<K, T, Ref, Ptr, KeyOfT, Hash> Self;<br><br>\t\tNode* _node;<br>\t\tconst HT* _ht;<br>\t};<br>\u9996\u5148\u662f\u5176\u8fed\u4ee3\u5668\u7684\u5b9a\u4e49\uff0c\u8fed\u4ee3\u5668\u51fd\u6570\u6cdb\u578b\u4e00\u4e2a\u5b9a\u4e49\u4e866\u7684\u4e2a\u53c2\u6570<br>\u5206\u522b\u662f\uff1aK:\u5173\u952e\u5b57<br>        T:\u6570\u636e\u7c7b\u578b<br>        ref:\u6570\u636e\u5f15\u7528<br>        ptr:\u6570\u636e\u6307\u9488<br>        KeyOft:\u6bd4\u8f83\u5927\u5c0f\u4eff\u51fd\u6570<br>        Hash:\u8f6c\u6574\u5f62\u4eff\u51fd\u6570<br>\u4e0b\u9762\u662fNode\u8282\u70b9\u7c7b\u578b<br>      HT:\u54c8\u5e0c\u8868<br> \u4e0b\u9762\u5c31\u662f\u8fed\u4ee3\u5668operator++\u7684\u5b9e\u73b0<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\u5b9e\u73b0\u601d\u8def\uff1aiterator \u4e2d\u6709\u4e00\u4e2a\u6307\u5411\u7ed3\u70b9\u7684\u6307\u9488\uff0c\u5982\u679c\u5f53\u524d\u6876\u4e0b\u9762\u8fd8\u6709\u7ed3\u70b9\uff0c\u5219\u7ed3\u70b9\u7684\u6307\u9488\u6307\u5411\u4e0b\u4e00\u4e2a\u7ed3\u70b9\u5373\u53ef\u3002\n\u5982\u679c\u5f53\u524d\u6876\u8d70\u5b8c\u4e86\uff0c\u5219\u9700\u8981\u60f3\u529e\u6cd5\u8ba1\u7b97\u627e\u5230\u4e0b\u4e00\u4e2a\u6876\u3002\n\u8fd9\u91cc\u7684\u96be\u70b9\u53cd\u800c\u662f\u7ed3\u6784\u8bbe\u8ba1\u7684\u95ee\u9898\u3002\u53c2\u8003\u4e0a\u9762\u7684\u6e90\u7801\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230 iterator \u4e2d\u9664\u4e86\u6709\u7ed3\u70b9\u7684\u6307\u9488\uff0c\u8fd8\u6709\u54c8\u5e0c\u8868\u5bf9\u8c61\u7684\u6307\u9488\uff0c\u8fd9\u6837\u5f53\u524d\u6876\u8d70\u5b8c\u4e86\uff0c\u8981\u8ba1\u7b97\u4e0b\u4e00\u4e2a\u6876\u5c31\u76f8\u5bf9\u5bb9\u6613\u591a\u4e86\uff1a\n\u7528 key \u503c\u8ba1\u7b97\u51fa\u5f53\u524d\u6876\u4f4d\u7f6e\uff0c\u4f9d\u6b21\u5f80\u540e\u627e\u4e0b\u4e00\u4e2a\u4e0d\u4e3a\u7a7a\u7684\u6876\u5373\u53ef\u3002\u8fd9\u4e5f\u662f\u6211\u5b9a\u4e49_ht\u7684\u7528\u610f\n\tSelf& operator++()\n\t{\n\t\tif (_node->_next)\n\t\t{\n\t\t\t\/\/ \u5f53\u524d\u6876\u8fd8\u6709\u6570\u636e\uff0c\u8d70\u5230\u5f53\u524d\u6876\u4e0b\u4e00\u4e2a\u8282\u70b9\n\t\t\t_node = _node->_next;\n\t\t}\n\t\telse\n\t\t{\n\t\t\t\/\/ \u5f53\u524d\u6876\u8d70\u5b8c\u4e86\uff0c\u627e\u4e0b\u4e00\u4e2a\u4e0d\u4e3a\u7a7a\u7684\u6876\n\t\t\tKeyOfT kot;\n\t\t\tHash hash;\n\t\t\tsize_t hashi = hash(kot(_node->_data)) % _ht->_tables.size();\n\t\t\t++hashi;\n\t\t\twhile (hashi < _ht->_tables.size())\n\t\t\t{\n\t\t\t\t_node = _ht->_tables[hashi];\n\n\t\t\t\tif (_node)\n\t\t\t\t\tbreak;\n\t\t\t\telse\n\t\t\t\t\t++hashi;\n\t\t\t}\n\n\t\t\t\/\/ \u6240\u6709\u6876\u90fd\u8d70\u5b8c\u4e86\uff0cend()\u7ed9\u7684\u7a7a\u6807\u8bc6\u7684_node\n\t\t\tif (hashi == _ht->_tables.size())\n\t\t\t{\n\t\t\t\t_node = nullptr;\n\t\t\t}\n\t\t}\n\n\t\treturn *this;\n\t}\n\n};\n\u518d\u4e0b\u9762\u5c31\u662f map\u3001set\u7684\u5b9a\u4e49\uff0c\u56e0\u4e3a\u5b9a\u4e49\u6bd4\u8f83\u7b80\u5355\uff0c\u5c31\u4e0d\u4e00\u4e00\u89e3\u91ca\u4e86\nmap:\n#pragma once\n#include\"hashbucket.h\"\nnamespace bit\n{\n\ttemplate<class K,class V, class Hash = HashFunc<K>>\n\tclass unordered_map {\n\t\tstruct MapKeyOfT {\n\t\t\t\n\t\t\tconst K& operator()(const pair<K,V>&kv ) {\n\t\t\t\treturn kv.first;\n\t\t\t}\n\t\t};\n\tpublic:\n\t\tbool insert(const pair<K,V>&kv) {\n\t\t\treturn _ht.Insert(kv);\n\t\t}\n\tprivate:\n\t\thash_bucket::HashTable<K, pair<K,V>, MapKeyOfT,Hash>_ht;\n\t};\n\n}\nset\uff1a\n#pragma once\n#include\"hashbucket.h\"\nnamespace bit\n{\n\ttemplate<class K, class Hash = HashFunc<K>>\n\tclass unordered_set {\n\t\tstruct SetKeyOfT {\n\t\t\tconst K& operator()(const K& key) {\n\t\t\t\treturn key;\n\t\t\t}\n\t\t};\n\tpublic:\n\tprivate:\n\t\thash_bucket::HashTable<K, K, SetKeyOfT,Hash>_ht;\n\t};\n\n}<\/code><\/pre>\n\n\n\n<p>\u518d\u5c31\u662fhash\u6876\u91cc\u9762\u5bf9iterator\u7684\u5b9a\u4e49<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Iterator Begin()\n{\n\tif (_n == 0)\n\t\treturn End();\n\n\tfor (size_t i = 0; i < _tables.size(); i++)\n\t{\n\t\tNode* cur = _tables[i];\n\t\tif (cur)\n\t\t{\n\t\t\treturn Iterator(cur, this);\n\t\t}\n\t}\n\n\treturn End();\n}\n\nIterator End()\n{\n\treturn Iterator(nullptr, this);\n}\n\n\nConstIterator Begin() const\n{\n\tif (_n == 0)\n\t\treturn End();\n\n\tfor (size_t i = 0; i < _tables.size(); i++)\n\t{\n\t\tNode* cur = _tables[i];\n\t\tif (cur)\n\t\t{\n\t\t\treturn ConstIterator(cur, this);\n\t\t}\n\t}\n\n\treturn End();\n}\n\nConstIterator End() const\n{\n\treturn ConstIterator(nullptr, this);\n}\n\u5177\u4f53\u4ee3\u7801\u8bf7\u53c2\u8003\u4ee3\u7801\u4ed3\u5e93\u91cc\u9762\u7684\u4ee3\u7801\n\u4eca\u5929\u7684\u6587\u7ae0\u5c31\u66f4\u65b0\u5230\u8fd9\u91cc\u4e86\uff0c\u5982\u6709\u4e0d\u5bf9\uff0c\u6b22\u8fce\u8bc4\u8bba\u533a\u6307\u51fa\uff0c\u8c22\u8c22\uff01\uff01\uff01<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4ee5\u4e0b\u4ee3\u7801\u5df2\u5728gitee\u4e0a\u5f00\u6e90\uff0c\u4ed3\u5e93\u5730\u5740\uff1ahttps:\/\/gitee.com\/tgwTTT\/c-lreant\/t&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_kad_post_classname":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=163"}],"version-history":[{"count":4,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":875,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=\/wp\/v2\/posts\/163\/revisions\/875"}],"wp:attachment":[{"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tgwttt.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}