{"id":107334,"date":"2026-09-19T13:02:21","date_gmt":"2026-09-19T05:02:21","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/107334.html"},"modified":"2026-09-19T13:02:21","modified_gmt":"2026-09-19T05:02:21","slug":"de%e9%a3%8e-%e3%80%90%e4%bb%8e%e9%9b%b6%e5%bc%80%e5%a7%8b%e5%ad%a6c%e3%80%91%ef%bc%88%e4%ba%8c%e5%8d%81%ef%bc%89%e7%ba%a2%e9%bb%91%e6%a0%91%e5%b0%81%e8%a3%85map%e5%92%8cset","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/107334.html","title":{"rendered":"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset"},"content":{"rendered":"<h2 style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919050216-6aae1758859dc.jpg\" \/><\/h2>\n<p>\u3010\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60C&#043;&#043;\u3011\u7b2c 20 \u7bc7 \u7cfb\u5217\u5b9a\u4f4d&#xff1a;\u5199\u7ed9\u65b0\u624b\u5c0f\u767d\u7684 C&#043;&#043; \u8fdb\u9636\u4e4b\u8def\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u624b\u6495\u5b8c\u4e86 map \/ set \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u2014\u2014\u7ea2\u9ed1\u6811&#xff0c;\u8fd9\u4e00\u7bc7\u6211\u4eec\u5c31\u628a\u5b83\u5305\u4e00\u5c42\u76ae&#xff0c;\u771f\u6b63\u505a\u51fa\u5c5e\u4e8e\u6211\u4eec\u81ea\u5df1\u7684 mymap \u548c myset\u3002<\/p>\n<hr \/>\n<h3>&#x1f4cc; \u5168\u6587\u601d\u7ef4\u5bfc\u56fe<\/h3>\n<p style=\"text-align:center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919050216-6aae1758d0f6d.png\" \/><\/p>\n<p>\u5efa\u8bae\u5148\u628a\u4e0a\u9762\u8fd9\u5f20\u56fe\u4fdd\u5b58\u4e0b\u6765&#xff0c;\u8bfb\u5230\u54ea\u4e00\u6b65\u5fd8\u4e86\u5c31\u56de\u5934\u770b\u54ea\u4e00\u652f\u3002<\/p>\n<hr \/>\n<h3>\u4e00\u3001\u5148\u56de\u7b54\u4e00\u4e2a\u7075\u9b42\u95ee\u9898&#xff1a;\u4e3a\u4ec0\u4e48 map \u548c set \u80fd\u5171\u7528\u4e00\u68f5\u6811&#xff1f;<\/h3>\n<h4>1.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>set \u662f key \u641c\u7d22\u573a\u666f&#xff0c;\u5b58\u8fdb\u53bb\u7684\u5c31\u662f\u4e00\u4e2a key&#xff0c;\u53ea\u5173\u5fc3&#034;\u5728\u4e0d\u5728&#034;\u3002<\/p>\n<p>map \u662f key\/value \u641c\u7d22\u573a\u666f&#xff0c;\u5b58\u8fdb\u53bb\u7684\u662f\u4e00\u4e2a pair&lt;const K, V&gt;&#xff0c;\u5173\u5fc3&#034;key \u5bf9\u5e94\u7684 value \u662f\u591a\u5c11&#034;\u3002<\/p>\n<p>\u6309\u7406\u8bf4\u8fd9\u662f\u4e24\u4e2a\u5b8c\u5168\u4e0d\u540c\u7684\u4e1c\u897f&#xff0c;\u4f46\u4f60\u770b STL \u6e90\u7801\u4f1a\u53d1\u73b0\u4e00\u4e2a\u60ca\u4eba\u7684\u4e8b\u5b9e&#xff1a;\u5b83\u4eec\u5e95\u5c42\u7528\u7684\u662f\u540c\u4e00\u68f5\u7ea2\u9ed1\u6811\u3002<\/p>\n<p>\u90a3\u5b83\u662f\u600e\u4e48\u505a\u5230&#034;\u4e00\u68f5\u6811\u5e72\u4e24\u4efd\u6d3b&#034;\u7684&#xff1f;\u7b54\u6848\u5c31\u4fe9\u5b57&#xff1a;\u6cdb\u578b\u3002<\/p>\n<h4>1.2 \u4f8b\u5b50\u4e00&#xff1a;\u4ece STL \u6e90\u7801\u770b\u5b83\u600e\u4e48\u590d\u7528\u7684<\/h4>\n<p>&#xff08;1&#xff09;\u6e90\u7801\u91cc set \u548c map \u5404\u81ea\u7684\u5b9a\u4e49<\/p>\n<p>\/\/ stl_set.h<br \/>\ntemplate &lt;class Key, class Compare &#061; less&lt;Key&gt;, class Alloc &#061; alloc&gt;<br \/>\nclass set {<br \/>\npublic:<br \/>\ntypedef Key key_type;<br \/>\ntypedef Key value_type;          \/\/ set \u7684 value \u5c31\u662f key \u672c\u8eab<br \/>\nprivate:<br \/>\ntypedef rb_tree&lt;key_type, value_type,<br \/>\n                identity&lt;value_type&gt;, key_compare, Alloc&gt; rep_type;<br \/>\nrep_type t;                      \/\/ \u4e00\u68f5\u7ea2\u9ed1\u6811<br \/>\n};<\/p>\n<p>\/\/ stl_map.h<br \/>\ntemplate &lt;class Key, class T, class Compare &#061; less&lt;Key&gt;, class Alloc &#061; alloc&gt;<br \/>\nclass map {<br \/>\npublic:<br \/>\ntypedef Key key_type;<br \/>\ntypedef T mapped_type;<br \/>\ntypedef pair&lt;const Key, T&gt; value_type;   \/\/ map \u7684 value \u662f pair<br \/>\nprivate:<br \/>\ntypedef rb_tree&lt;key_type, value_type,<br \/>\n                select1st&lt;value_type&gt;, key_compare, Alloc&gt; rep_type;<br \/>\nrep_type t;                      \/\/ \u8fd8\u662f\u4e00\u68f5\u7ea2\u9ed1\u6811<br \/>\n};<\/p>\n<p>&#x1f4a1; \u4e00\u53e5\u8bdd\u770b\u61c2&#xff1a;set \u548c map \u91cc\u9762\u90fd\u53ea\u6709\u4e00\u4e2a\u6210\u5458 \u2014\u2014 \u4e00\u68f5\u7ea2\u9ed1\u6811 t\u3002\u5b83\u4eec\u81ea\u5df1\u51e0\u4e4e\u4e0d\u5e72\u6d3b&#xff0c;\u5168\u662f&#034;\u8f6c\u53d1&#034;\u7ed9\u8fd9\u68f5\u6811\u3002<\/p>\n<p>&#xff08;2&#xff09;\u5173\u952e\u5dee\u5f02\u53ea\u5728\u7b2c\u4e8c\u4e2a\u6a21\u677f\u53c2\u6570<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u5bb9\u5668<\/p>\n<\/td>\n<td>\n<p>\u4f20\u7ed9 rb_tree \u7684\u7b2c 1 \u4e2a\u53c2\u6570<\/p>\n<\/td>\n<td>\n<p>\u4f20\u7ed9 rb_tree \u7684\u7b2c 2 \u4e2a\u53c2\u6570<\/p>\n<\/td>\n<td>\n<p>\u7ed3\u679c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>set<\/p>\n<\/td>\n<td>\n<p>Key<\/p>\n<\/td>\n<td>\n<p>Key<\/p>\n<\/td>\n<td>\n<p>\u7ed3\u70b9\u91cc\u5b58 key<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>map<\/p>\n<\/td>\n<td>\n<p>Key<\/p>\n<\/td>\n<td>\n<p>pair&lt;const Key, T&gt;<\/p>\n<\/td>\n<td>\n<p>\u7ed3\u70b9\u91cc\u5b58\u952e\u503c\u5bf9<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7b2c\u4e8c\u4e2a\u6a21\u677f\u53c2\u6570\u51b3\u5b9a\u4e86\u7ed3\u70b9\u91cc\u5230\u5e95\u5b58\u4ec0\u4e48&#xff0c;\u6240\u4ee5\u540c\u4e00\u4efd\u7ea2\u9ed1\u6811\u4ee3\u7801&#xff0c;\u5582 K \u8fdb\u53bb\u5c31\u53d8\u6210 set&#xff0c;\u5582 pair&lt;const K, V&gt; \u8fdb\u53bb\u5c31\u53d8\u6210 map\u3002<\/p>\n<p>&#xff08;3&#xff09;\u90a3\u4e2a&#034;\u591a\u4f59\u7684&#034;\u7b2c\u4e00\u4e2a\u6a21\u677f\u53c2\u6570 K \u662f\u5e72\u561b\u7684&#xff1f;<\/p>\n<p>\u5f88\u591a\u540c\u5b66\u5230\u8fd9\u513f\u90fd\u61f5&#xff1a;\u65e2\u7136\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5df2\u7ecf\u51b3\u5b9a\u5b58\u4ec0\u4e48\u4e86&#xff0c;set \u91cc\u4fe9\u53c2\u6570\u8fd8\u5199\u6210\u4e00\u6837\u7684&#xff0c;\u90a3\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f\u4e0d\u662f\u5e9f\u8bdd&#xff1f;<\/p>\n<p>\u4e0d\u662f\u5e9f\u8bdd\u3002\u56e0\u4e3a find \/ erase \u7684\u5f62\u53c2\u7c7b\u578b\u662f key&#xff0c;\u4e0d\u662f\u7ed3\u70b9\u91cc\u5b58\u7684\u90a3\u4e2a\u6574\u4f53&#xff1a;<\/p>\n<p>\/\/ stl_tree.h<br \/>\ntemplate &lt;class Key, class Value, class KeyOfValue, class Compare, class Alloc &#061; alloc&gt;<br \/>\nclass rb_tree {<br \/>\npublic:<br \/>\n\/\/ insert \u7528\u7b2c\u4e8c\u4e2a\u6a21\u677f\u53c2\u6570 Value \u505a\u5f62\u53c2<br \/>\npair&lt;iterator, bool&gt; insert_unique(const value_type&amp; x);<\/p>\n<p>\/\/ \u800c erase \/ find \u7528\u7684\u662f\u7b2c\u4e00\u4e2a\u6a21\u677f\u53c2\u6570 Key<br \/>\nsize_type erase(const key_type&amp; x);<br \/>\niterator  find(const key_type&amp; x);<br \/>\n};<\/p>\n<p>\u5bf9 set \u6765\u8bf4 key \u548c value \u662f\u540c\u4e00\u4e2a\u4e1c\u897f&#xff0c;\u770b\u8d77\u6765&#034;\u91cd\u590d&#034;&#xff1b;\u4f46\u5bf9 map \u6765\u8bf4\u5c31\u5b8c\u5168\u4e0d\u4e00\u6837\u4e86&#xff1a;\u63d2\u5165\u65f6\u7ed9\u7684\u662f pair&#xff0c;\u67e5\u627e\u65f6\u7ed9\u7684\u662f key\u3002\u6240\u4ee5\u7b2c\u4e00\u4e2a\u53c2\u6570\u5fc5\u987b\u7559\u7740\u3002<\/p>\n<p>&#xff08;4&#xff09;\u987a\u624b\u5410\u69fd\u4e00\u4e0b<\/p>\n<p>\u6e90\u7801\u91cc\u547d\u540d\u98ce\u683c\u5176\u5b9e\u5f88\u4e71&#xff1a;set \u7528 Key&#xff0c;map \u7528 Key \u548c T&#xff0c;\u5230\u4e86 rb_tree \u53c8\u53d8\u6210 Key \u548c Value\u3002\u800c\u4e14\u6e90\u7801\u91cc\u7684 value_type \u6307\u7684\u662f\u7ed3\u70b9\u91cc\u771f\u5b9e\u5b58\u50a8\u7684\u7c7b\u578b&#xff0c;\u8ddf\u6211\u4eec\u5e73\u65f6\u8bf4\u7684&#034;key\/value \u91cc\u7684 value&#034;\u5b8c\u5168\u4e0d\u662f\u4e00\u56de\u4e8b\u3002\u5927\u4f6c\u5199\u4ee3\u7801\u4e5f\u4e0d\u4e00\u5b9a\u89c4\u6574&#xff0c;\u6240\u4ee5\u770b\u6e90\u7801\u65f6\u4e00\u5b9a\u8981\u4ee5\u5b9e\u9645\u5b9a\u4e49\u4e3a\u51c6&#xff0c;\u522b\u88ab\u540d\u5b57\u9a97\u4e86\u3002<\/p>\n<hr \/>\n<h3>\u4e8c\u3001KeyOfT \u4eff\u51fd\u6570&#xff1a;\u6574\u4e2a\u5c01\u88c5\u6700\u6838\u5fc3\u7684\u4e00\u6b65<\/h3>\n<h4>2.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>\u6211\u4eec\u81ea\u5df1\u7684\u7ea2\u9ed1\u6811\u662f\u6cdb\u578b\u7684&#xff0c;\u6a21\u677f\u53c2\u6570 T \u5230\u5e95\u662f K \u8fd8\u662f pair&lt;K, V&gt;&#xff0c;\u7ea2\u9ed1\u6811\u81ea\u5df1\u4e0d\u77e5\u9053\u3002<\/p>\n<p>\u90a3\u4e48\u95ee\u9898\u6765\u4e86&#xff1a;\u63d2\u5165\u7684\u65f6\u5019\u8981\u6bd4\u8f83\u5927\u5c0f&#xff0c;\u5982\u679c T \u662f pair&lt;K, V&gt;&#xff0c;pair \u9ed8\u8ba4\u7684 &lt; \u662f\u628a first \u548c second \u4e00\u8d77\u6bd4\u7684&#xff1a;<\/p>\n<p>template &lt;class T1, class T2&gt;<br \/>\nbool operator&lt;(const pair&lt;T1, T2&gt;&amp; lhs, const pair&lt;T1, T2&gt;&amp; rhs)<br \/>\n{<br \/>\nreturn lhs.first &lt; rhs.first ||<br \/>\n       (!(rhs.first &lt; lhs.first) &amp;&amp; lhs.second &lt; rhs.second);<br \/>\n}<\/p>\n<p>\u800c\u6211\u4eec\u5e0c\u671b\u7684\u662f&#xff1a;\u4efb\u4f55\u65f6\u5019\u90fd\u53ea\u6bd4\u8f83 key\u3002<\/p>\n<p>\u89e3\u51b3\u529e\u6cd5\u5c31\u662f\u5927\u540d\u9f0e\u9f0e\u7684 KeyOfT \u4eff\u51fd\u6570&#xff1a;\u8ba9 map \u548c set \u5404\u81ea\u63d0\u4f9b\u4e00\u4e2a&#034;\u4ece T \u91cc\u62a0\u51fa key&#034;\u7684\u5c0f\u5de5\u5177&#xff0c;\u4f20\u7ed9\u7ea2\u9ed1\u6811\u7528\u3002<\/p>\n<h4>2.2 \u4f8b\u5b50\u4e8c&#xff1a;\u7ed9 set \u548c map \u5404\u5199\u4e00\u4e2a KeyOfT<\/h4>\n<p>&#xff08;1&#xff09;SetKeyOfT&#xff1a;\u8981\u7684\u5c31\u662f\u5b83\u81ea\u5df1<\/p>\n<p>struct SetKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const K&amp; key)<br \/>\n{<br \/>\nreturn key;          \/\/ set \u91cc K \u5c31\u662f key&#xff0c;\u76f4\u63a5\u8fd4\u56de<br \/>\n}<br \/>\n};<\/p>\n<p>&#xff08;2&#xff09;MapKeyOfT&#xff1a;\u628a pair \u7684 first \u62a0\u51fa\u6765<\/p>\n<p>struct MapKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nreturn kv.first;     \/\/ map \u91cc\u53d6 pair \u7684\u7b2c\u4e00\u4e2a<br \/>\n}<br \/>\n};<\/p>\n<p>&#xff08;3&#xff09;\u7ea2\u9ed1\u6811\u62ff\u5230\u5b83\u4e4b\u540e\u600e\u4e48\u7528<\/p>\n<p>template&lt;class K, class T, class KeyOfT&gt;   \/\/ T \u662f\u7ed3\u70b9\u5b58\u7684\u6570\u636e\u7c7b\u578b<br \/>\nclass RBTree<br \/>\n{<br \/>\npublic:<br \/>\nbool Insert(const T&amp; data)<br \/>\n{<br \/>\n\/\/ &#8230;<br \/>\nKeyOfT kot;                       \/\/ \u9020\u4e00\u4e2a\u4eff\u51fd\u6570\u5bf9\u8c61<br \/>\nNode* parent &#061; nullptr;<br \/>\nNode* cur &#061; _root;<\/p>\n<p>while (cur)<br \/>\n{<br \/>\nif (kot(cur-&gt;_data) &lt; kot(data))      \/\/ \u53ea\u6bd4 key<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (kot(cur-&gt;_data) &gt; kot(data)) \/\/ \u53ea\u6bd4 key<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn false;                     \/\/ key \u76f8\u540c&#xff0c;\u4e0d\u5141\u8bb8\u91cd\u590d\u63d2\u5165<br \/>\n}<br \/>\n}<br \/>\n\/\/ &#8230;<br \/>\n}<br \/>\n};<\/p>\n<p>\u770b\u5230\u6ca1&#xff1f;\u7ea2\u9ed1\u6811\u91cc\u6240\u6709\u6bd4\u8f83\u90fd\u53d8\u6210\u4e86 kot(&#8230;) &lt; kot(&#8230;)&#xff0c;\u5b83\u5f7b\u5e95\u4e0d\u5173\u5fc3 T \u5230\u5e95\u662f\u4ec0\u4e48\u4e86\u3002\u8fd9\u5c31\u662f\u89e3\u8026\u3002<\/p>\n<h4>2.3 \u7ecf\u5178 Bug&#xff1a;\u76f4\u63a5\u6bd4\u8f83 pair \u4f1a\u600e\u6837&#xff1f;<\/h4>\n<p>\u5982\u679c\u4f60\u5077\u61d2\u4e0d\u5199 KeyOfT&#xff0c;\u63d2\u5165\u65f6\u76f4\u63a5 cur-&gt;_data &lt; data&#xff0c;\u4f1a\u53d1\u751f\u4e24\u4ef6\u4e8b&#xff1a;<\/p>\n<li>set \u80fd\u7f16\u8bd1\u901a\u8fc7\u4f46\u8bed\u4e49\u9519&#xff08;\u56e0\u4e3a K \u672c\u8eab\u5c31\u662f key&#xff0c;\u78b0\u5de7\u5bf9&#xff09;<\/li>\n<li>map \u76f4\u63a5\u7ffb\u8f66&#xff1a;pair \u7684\u6bd4\u8f83\u89c4\u5219\u53d8\u6210\u4e86&#034;\u5148\u6bd4 key&#xff0c;key \u76f8\u7b49\u518d\u6bd4 value&#034;\u3002\u8fd9\u4e0d\u5149\u8fdd\u53cd\u4e86\u6211\u4eec&#034;\u53ea\u6bd4 key&#034;\u7684\u8bed\u4e49&#xff0c;\u66f4\u4e25\u91cd\u7684\u662f\u2014\u2014V \u8fd9\u4e2a\u7c7b\u578b\u672a\u5fc5\u652f\u6301 &lt; \u8fd0\u7b97&#xff01;<\/li>\n<p>struct Date { int y, m, d; };       \/\/ \u6ca1\u6709\u91cd\u8f7d &lt;<br \/>\nmap&lt;string, Date&gt; m;<br \/>\nm.insert({ &#034;\u751f\u65e5&#034;, {2025, 9, 14} });<br \/>\n\/\/ \u7f16\u8bd1\u62a5\u9519&#xff1a;no match for &#039;operator&lt;&#039; (operand types are &#039;Date&#039; and &#039;Date&#039;)<\/p>\n<p>&#x1f41b; \u907f\u5751\u7ed3\u8bba&#xff1a;KeyOfT \u4e0d\u662f\u4e00\u4e2a\u53ef\u9009\u9879&#xff0c;\u5b83\u662f map \u80fd\u7528\u7684\u524d\u63d0\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001\u642d\u6846\u67b6&#xff1a;\u8ba9 mymap \/ myset \u7b2c\u4e00\u6b21\u8dd1\u8d77\u6765<\/h3>\n<h4>3.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>\u6709\u4e86 KeyOfT&#xff0c;\u6211\u4eec\u5c31\u53ef\u4ee5\u628a set \u548c map \u7684&#034;\u5916\u58f3&#034;\u642d\u8d77\u6765\u4e86\u3002\u8fd9\u4e00\u6b65\u76ee\u6807\u5f88\u7b80\u5355&#xff1a;\u80fd\u63d2\u5165\u3001\u80fd\u6309 key \u67e5\u3002<\/p>\n<p>\u5148\u89c4\u5b9a\u597d\u6211\u4eec\u81ea\u5df1\u7684\u547d\u540d&#xff08;\u6bd4\u6e90\u7801\u5e72\u51c0\u4e00\u70b9&#xff09;&#xff1a;<\/p>\n<ul>\n<li>\u7ea2\u9ed1\u6811\u6a21\u677f\u53c2\u6570&#xff1a;K&#xff08;key \u7c7b\u578b&#xff09;\u3001T&#xff08;\u7ed3\u70b9\u771f\u5b9e\u5b58\u50a8\u7c7b\u578b&#xff09;\u3001KeyOfT&#xff08;\u53d6 key \u7684\u4eff\u51fd\u6570&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>\u7ed3\u70b9\u7c7b&#xff1a;RBTreeNode&lt;T&gt;&#xff1b;\u5bb9\u5668\u5728 bit \u547d\u540d\u7a7a\u95f4\u91cc\u3002<\/li>\n<\/ul>\n<h4>3.2 \u4f8b\u5b50\u4e09&#xff1a;Myset.h \u7684\u7b2c\u4e00\u7248<\/h4>\n<p>\/\/ Myset.h<br \/>\n#pragma once<br \/>\n#include &#034;RBTree.h&#034;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\ntemplate&lt;class K&gt;<br \/>\nclass set<br \/>\n{<br \/>\nstruct SetKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const K&amp; key)<br \/>\n{<br \/>\nreturn key;<br \/>\n}<br \/>\n};<\/p>\n<p>public:<br \/>\nbool insert(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Insert(key);<br \/>\n}<\/p>\n<p>private:<br \/>\nRBTree&lt;K, K, SetKeyOfT&gt; _t;   \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u7ed9 K<br \/>\n};<br \/>\n}<\/p>\n<h4>3.3 \u4f8b\u5b50\u56db&#xff1a;Mymap.h \u7684\u7b2c\u4e00\u7248<\/h4>\n<p>\/\/ Mymap.h<br \/>\n#pragma once<br \/>\n#include &#034;RBTree.h&#034;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\ntemplate&lt;class K, class V&gt;<br \/>\nclass map<br \/>\n{<br \/>\nstruct MapKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nreturn kv.first;<br \/>\n}<br \/>\n};<\/p>\n<p>public:<br \/>\nbool insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nreturn _t.Insert(kv);<br \/>\n}<\/p>\n<p>private:<br \/>\nRBTree&lt;K, pair&lt;K, V&gt;, MapKeyOfT&gt; _t;   \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u7ed9 pair<br \/>\n};<br \/>\n}<\/p>\n<h4>3.4 \u4f8b\u5b50\u4e94&#xff1a;\u914d\u5957\u7684\u7ea2\u9ed1\u6811\u7ed3\u70b9<\/h4>\n<p>\/\/ RBTree.h<br \/>\nenum Colour<br \/>\n{<br \/>\nRED,<br \/>\nBLACK<br \/>\n};<\/p>\n<p>template&lt;class T&gt;<br \/>\nstruct RBTreeNode<br \/>\n{<br \/>\nT _data;<br \/>\nRBTreeNode&lt;T&gt;* _left;<br \/>\nRBTreeNode&lt;T&gt;* _right;<br \/>\nRBTreeNode&lt;T&gt;* _parent;<br \/>\nColour _col;<\/p>\n<p>RBTreeNode(const T&amp; data)<br \/>\n: _data(data)<br \/>\n, _left(nullptr)<br \/>\n, _right(nullptr)<br \/>\n, _parent(nullptr)<br \/>\n, _col(RED)      \/\/ \u65b0\u7ed3\u70b9\u9ed8\u8ba4\u7ed9\u7ea2\u8272&#xff0c;\u63d2\u5165\u903b\u8f91\u624d\u597d\u5904\u7406<br \/>\n{}<br \/>\n};<\/p>\n<h4>3.5 \u6574\u4ef6\u4e8b\u7684\u5b9e\u73b0\u6b65\u9aa4\u6e05\u5355<\/h4>\n<p>\u2460 \u5b9e\u73b0\u7ea2\u9ed1\u6811&#xff08;\u524d\u9762\u5df2\u7ecf\u641e\u5b9a&#xff09;<br \/>\n\u2461 \u5c01\u88c5 map \/ set \u6846\u67b6&#xff0c;\u89e3\u51b3 KeyOfT<br \/>\n\u2462 \u7ed9\u7ea2\u9ed1\u6811\u52a0 iterator<br \/>\n\u2463 \u52a0\u4e0a const_iterator<br \/>\n\u2464 \u89e3\u51b3 key \u4e0d\u652f\u6301\u4fee\u6539\u7684\u95ee\u9898<br \/>\n\u2465 \u7ed9 map \u52a0 operator[]<\/p>\n<hr \/>\n<h3>\u56db\u3001\u8fed\u4ee3\u5668 iterator&#xff1a;\u8ba9\u5bb9\u5668\u80fd\u88ab for \u5faa\u73af<\/h3>\n<h4>4.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>\u8981\u80fd\u7528\u8303\u56f4 for&#xff0c;\u5bb9\u5668\u5c31\u5f97\u63d0\u4f9b begin() \u548c end()\u3002\u7ea2\u9ed1\u6811\u8fed\u4ee3\u5668\u7684\u601d\u8def\u548c list \u5b8c\u5168\u4e00\u81f4&#xff1a;\u7528\u4e00\u4e2a\u7c7b\u628a\u7ed3\u70b9\u6307\u9488\u5305\u8d77\u6765&#xff0c;\u518d\u91cd\u8f7d\u8fd0\u7b97\u7b26&#xff0c;\u8ba9\u5b83\u7528\u8d77\u6765\u50cf\u6307\u9488\u3002<\/p>\n<p>\u96be\u70b9\u53ea\u6709\u4e00\u4e2a&#xff1a;&#043;&#043; \u548c &#8211;\u3002\u56e0\u4e3a map \/ set \u7684\u904d\u5386\u987a\u5e8f\u662f\u4e2d\u5e8f&#xff08;\u5de6\u5b50\u6811 \u2192 \u6839 \u2192 \u53f3\u5b50\u6811&#xff09;&#xff0c;\u6240\u4ee5&#xff1a;<\/p>\n<ul>\n<li>begin() \u8fd4\u56de\u7684\u662f\u6574\u68f5\u6811\u7684\u6700\u5de6\u7ed3\u70b9&#xff08;\u4e2d\u5e8f\u7b2c\u4e00\u4e2a&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>end() \u6211\u4eec\u7528\u4e00\u4e2a\u7279\u6b8a\u503c\u8868\u793a&#034;\u8d70\u5b8c\u4e86&#034;\u3002<\/li>\n<\/ul>\n<h4>4.2 \u4f8b\u5b50\u516d&#xff1a;\u8fed\u4ee3\u5668\u7c7b\u7684\u9aa8\u67b6&#xff08;Ref \/ Ptr \u5206\u79bb&#xff09;<\/h4>\n<p>template&lt;class T, class Ref, class Ptr&gt;<br \/>\nstruct RBTreeIterator<br \/>\n{<br \/>\ntypedef RBTreeNode&lt;T&gt; Node;<br \/>\ntypedef RBTreeIterator&lt;T, Ref, Ptr&gt; Self;<\/p>\n<p>Node* _node;<br \/>\nNode* _root;    \/\/ \u989d\u5916\u5b58\u4e00\u4efd\u6839&#xff0c;\u4e3a\u4e86\u652f\u6301 &#8211;end()<\/p>\n<p>RBTreeIterator(Node* node, Node* root)<br \/>\n: _node(node)<br \/>\n, _root(root)<br \/>\n{}<\/p>\n<p>Ref operator*()<br \/>\n{<br \/>\nreturn _node-&gt;_data;<br \/>\n}<\/p>\n<p>Ptr operator-&gt;()<br \/>\n{<br \/>\nreturn &amp;_node-&gt;_data;<br \/>\n}<\/p>\n<p>bool operator!&#061;(const Self&amp; s) const { return _node !&#061; s._node; }<br \/>\nbool operator&#061;&#061;(const Self&amp; s) const { return _node &#061;&#061; s._node; }<br \/>\n};<\/p>\n<p>&#x1f4a1; \u4e3a\u4ec0\u4e48\u8981 Ref \/ Ptr \u4e24\u4e2a\u989d\u5916\u53c2\u6570&#xff1f; iterator \u4f20 T&amp; \u548c T*&#xff0c;const_iterator \u4f20 const T&amp; \u548c const T*\u2014\u2014\u540c\u4e00\u4efd\u4ee3\u7801&#xff0c;\u81ea\u52a8\u751f\u6210\u4e24\u4e2a\u7248\u672c\u3002\u8fd9\u5c31\u662f\u6cdb\u578b\u7684\u751c\u5934\u3002<\/p>\n<h4>4.3 \u4f8b\u5b50\u4e03&#xff1a;operator&#043;&#043; \u7684\u4e24\u79cd\u8d70\u6cd5&#xff08;\u672c\u6587\u6700\u70e7\u8111\u7684\u4e00\u6bb5&#xff09;<\/h4>\n<p>\u6838\u5fc3\u5fc3\u6cd5\u53ea\u6709\u4e00\u53e5&#xff1a;\u4e0d\u770b\u5168\u5c40&#xff0c;\u53ea\u770b\u5c40\u90e8\u2014\u2014\u53ea\u5173\u5fc3&#034;\u5f53\u524d\u4e2d\u5e8f\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9\u662f\u8c01&#034;\u3002<\/p>\n<p>\u60c5\u51b5\u4e00&#xff1a;\u53f3\u5b50\u6811\u4e0d\u4e3a\u7a7a<\/p>\n<p>\u8bf4\u660e\u5f53\u524d\u7ed3\u70b9\u5df2\u7ecf\u8bbf\u95ee\u5b8c\u4e86&#xff0c;\u4e0b\u4e00\u4e2a\u662f\u53f3\u5b50\u6811\u7684\u4e2d\u5e8f\u7b2c\u4e00\u4e2a&#xff0c;\u4e5f\u5c31\u662f\u53f3\u5b50\u6811\u7684\u6700\u5de6\u7ed3\u70b9\u3002<\/p>\n<p>\u60c5\u51b5\u4e8c&#xff1a;\u53f3\u5b50\u6811\u4e3a\u7a7a<\/p>\n<p>\u8bf4\u660e\u5f53\u524d\u7ed3\u70b9\u548c\u5b83\u6240\u5728\u7684\u5b50\u6811\u90fd\u8bbf\u95ee\u5b8c\u4e86&#xff0c;\u5f97\u5f80\u4e0a\u627e\u7956\u5148&#xff1a;<\/p>\n<ul>\n<li>\u5982\u679c\u5f53\u524d\u7ed3\u70b9\u662f\u7236\u4eb2\u7684\u5de6&#xff08;\u6bd4\u5982 25 \u662f 30 \u7684\u5de6&#xff09;&#xff0c;\u90a3\u4e0b\u4e00\u4e2a\u5c31\u662f\u7236\u4eb2&#xff08;30&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u5f53\u524d\u7ed3\u70b9\u662f\u7236\u4eb2\u7684\u53f3&#xff08;\u6bd4\u5982 15 \u662f 10 \u7684\u53f3&#xff09;&#xff0c;\u8bf4\u660e\u8fde\u7236\u4eb2\u90a3\u68f5\u5b50\u6811\u4e5f\u7ed3\u675f\u4e86&#xff0c;\u7ee7\u7eed\u5f80\u4e0a\u722c&#xff0c;\u76f4\u5230\u627e\u5230&#034;\u5b69\u5b50\u662f\u7236\u4eb2\u5de6&#034;\u7684\u90a3\u4e2a\u7956\u5148&#xff08;15 \u2192 10 \u2192 18&#xff0c;18 \u5c31\u662f 10 \u7684\u7236\u4eb2\u4e14 10 \u662f\u5de6\u5b69\u5b50&#xff0c;\u6240\u4ee5\u4e0b\u4e00\u4e2a\u662f 18&#xff09;&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>\u5982\u679c\u4e00\u76f4\u722c\u5230\u6839\u90fd\u6ca1\u627e\u5230&#xff0c;\u8bf4\u660e\u6574\u68f5\u6811\u8d70\u5b8c\u4e86&#xff0c;\u628a\u7ed3\u70b9\u6307\u9488\u7f6e\u4e3a nullptr&#xff0c;\u6211\u4eec\u7528 nullptr \u5145\u5f53 end()\u3002<\/li>\n<\/ul>\n<p>\/\/ &#043;&#043;it \u2014\u2014 \u4e2d\u5e8f\u7684\u4e0b\u4e00\u4e2a\u7ed3\u70b9<br \/>\nSelf&amp; operator&#043;&#043;()<br \/>\n{<br \/>\nif (_node-&gt;_right)<br \/>\n{<br \/>\n\/\/ \u60c5\u51b5\u4e00&#xff1a;\u53f3\u5b50\u6811\u4e0d\u4e3a\u7a7a -&gt; \u53f3\u5b50\u6811\u7684\u6700\u5de6\u7ed3\u70b9<br \/>\nNode* leftMost &#061; _node-&gt;_right;<br \/>\nwhile (leftMost-&gt;_left)<br \/>\n{<br \/>\nleftMost &#061; leftMost-&gt;_left;<br \/>\n}<br \/>\n_node &#061; leftMost;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ \u60c5\u51b5\u4e8c&#xff1a;\u53f3\u5b50\u6811\u4e3a\u7a7a -&gt; \u5f80\u4e0a\u627e&#034;\u5b69\u5b50\u662f\u7236\u4eb2\u5de6&#034;\u7684\u90a3\u4e2a\u7956\u5148<br \/>\nNode* cur &#061; _node;<br \/>\nNode* parent &#061; cur-&gt;_parent;<br \/>\nwhile (parent &amp;&amp; cur &#061;&#061; parent-&gt;_right)<br \/>\n{<br \/>\ncur &#061; parent;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\n_node &#061; parent;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<\/p>\n<p>&#x1f4a1; \u987a\u5e26\u4e00\u63d0&#xff1a;STL \u6e90\u7801\u91cc\u5e76\u6ca1\u6709\u7528 nullptr \u505a end()&#xff0c;\u800c\u662f\u5728\u7ea2\u9ed1\u6811\u9876\u4e0a\u6302\u4e86\u4e00\u4e2a\u54e8\u5175\u4f4d\u5934\u7ed3\u70b9&#xff08;\u5b83\u548c\u6839\u4e92\u4e3a\u7236\u4eb2&#xff0c;\u5de6\u6307\u5411\u6700\u5de6\u3001\u53f3\u6307\u5411\u6700\u53f3&#xff09;\u3002\u4e0d\u8fc7\u5b9e\u529b\u544a\u8bc9\u6211\u4eec&#xff1a;\u5b83\u80fd\u7684\u6211\u4eec\u4e5f\u80fd&#xff0c;\u7528 nullptr \u53ea\u662f &#8211;end() \u8981\u7279\u6b8a\u5904\u7406\u4e00\u4e0b\u800c\u5df2\u3002<\/p>\n<h4>4.4 \u4f8b\u5b50\u516b&#xff1a;operator&#8211; \u4e0e &#8211;end() \u7684\u7279\u6b8a\u5904\u7406<\/h4>\n<p>&#8212; \u7684\u903b\u8f91\u548c &#043;&#043; \u5b8c\u5168\u5bf9\u79f0&#xff0c;\u53cd\u8fc7\u6765\u60f3\u5c31\u884c&#xff08;\u987a\u5e8f\u53d8\u6210 \u53f3\u5b50\u6811 \u2192 \u6839 \u2192 \u5de6\u5b50\u6811&#xff09;&#xff1a;<\/p>\n<p>\/\/ &#8211;it \u2014\u2014 \u4e2d\u5e8f\u7684\u4e0a\u4e00\u4e2a\u7ed3\u70b9<br \/>\nSelf&amp; operator&#8211;()<br \/>\n{<br \/>\nif (_node &#061;&#061; nullptr)          \/\/ \u5904\u7406 &#8211;end()<br \/>\n{<br \/>\n\/\/ end() \u662f\u7a7a&#xff0c;&#8211;end() \u5e94\u8be5\u8d70\u5230\u6574\u68f5\u6811\u7684\u6700\u53f3\u7ed3\u70b9<br \/>\nNode* rightMost &#061; _root;<br \/>\nwhile (rightMost &amp;&amp; rightMost-&gt;_right)<br \/>\n{<br \/>\nrightMost &#061; rightMost-&gt;_right;<br \/>\n}<br \/>\n_node &#061; rightMost;<br \/>\n}<br \/>\nelse if (_node-&gt;_left)<br \/>\n{<br \/>\n\/\/ \u5de6\u5b50\u6811\u4e0d\u4e3a\u7a7a -&gt; \u5de6\u5b50\u6811\u7684\u6700\u53f3\u7ed3\u70b9<br \/>\nNode* rightMost &#061; _node-&gt;_left;<br \/>\nwhile (rightMost-&gt;_right)<br \/>\n{<br \/>\nrightMost &#061; rightMost-&gt;_right;<br \/>\n}<br \/>\n_node &#061; rightMost;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ \u5f80\u4e0a\u627e&#034;\u5b69\u5b50\u662f\u7236\u4eb2\u53f3&#034;\u7684\u90a3\u4e2a\u7956\u5148<br \/>\nNode* cur &#061; _node;<br \/>\nNode* parent &#061; cur-&gt;_parent;<br \/>\nwhile (parent &amp;&amp; cur &#061;&#061; parent-&gt;_left)<br \/>\n{<br \/>\ncur &#061; parent;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\n_node &#061; parent;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<\/p>\n<h4>4.5 \u4f8b\u5b50\u4e5d&#xff1a;\u7ea2\u9ed1\u6811\u91cc\u7684 Begin() \/ End()<\/h4>\n<p>typedef RBTreeIterator&lt;T, T&amp;, T*&gt;             Iterator;<br \/>\ntypedef RBTreeIterator&lt;T, const T&amp;, const T*&gt; ConstIterator;<\/p>\n<p>Iterator Begin()<br \/>\n{<br \/>\nNode* leftMost &#061; _root;<br \/>\nwhile (leftMost &amp;&amp; leftMost-&gt;_left)    \/\/ \u4e00\u8def\u5411\u5de6&#xff0c;\u5c31\u662f\u4e2d\u5e8f\u7b2c\u4e00\u4e2a<br \/>\n{<br \/>\nleftMost &#061; leftMost-&gt;_left;<br \/>\n}<br \/>\nreturn Iterator(leftMost, _root);       \/\/ \u8bb0\u5f97\u628a _root \u4f20\u8fdb\u53bb<br \/>\n}<\/p>\n<p>Iterator End()<br \/>\n{<br \/>\nreturn Iterator(nullptr, _root);        \/\/ \u7528 nullptr \u8868\u793a end<br \/>\n}<\/p>\n<p>const \u7248\u672c\u540c\u7406&#xff0c;\u53ea\u662f\u6362\u6210 ConstIterator\u3002<\/p>\n<h4>4.6 \u7ecf\u5178 Bug&#xff1a;\u8fed\u4ee3\u5668\u91cc\u7684\u5751<\/h4>\n<p>Bug 1&#xff1a;&#8211;end() \u76f4\u63a5\u5d29\u6e83<\/p>\n<p>end() \u91cc\u7ed3\u70b9\u7684\u6307\u9488\u662f nullptr&#xff0c;\u5982\u679c\u4f60\u5728 operator&#8211; \u91cc\u4e0d\u52a0 _node &#061;&#061; nullptr \u7684\u5224\u65ad&#xff0c;\u7b2c\u4e00\u4ef6\u4e8b\u5c31\u662f\u89e3\u5f15\u7528\u7a7a\u6307\u9488 \u2014\u2014 \u7a0b\u5e8f\u5f53\u573a\u53bb\u4e16\u3002<\/p>\n<p>Bug 2&#xff1a;\u5fd8\u8bb0\u628a _root \u4f20\u7ed9\u8fed\u4ee3\u5668<\/p>\n<p>&#8211;end() \u9700\u8981\u9760 _root \u53bb\u627e\u6700\u53f3\u7ed3\u70b9\u3002\u5982\u679c\u4f60\u7684\u8fed\u4ee3\u5668\u6784\u9020\u51fd\u6570\u53ea\u4f20\u4e86\u7ed3\u70b9\u6307\u9488&#xff0c;_root \u5c31\u662f\u91ce\u6307\u9488&#xff0c;\u8fd0\u884c\u8d77\u6765\u65f6\u597d\u65f6\u574f&#xff08;\u5178\u578b\u7684&#034;\u7384\u5b66 Bug&#034;&#xff09;\u3002\u8bb0\u4f4f Iterator(node, _root) \u4e24\u4e2a\u53c2\u6570\u90fd\u8981\u4f20\u3002<\/p>\n<p>Bug 3&#xff1a;Begin() \u4e0d\u5224\u7a7a\u6811<\/p>\n<p>\u7a7a\u6811\u65f6 _root \u5c31\u662f nullptr&#xff0c;\u4e0a\u9762\u7684 while (leftMost &amp;&amp; leftMost-&gt;_left) \u91cc\u7684\u5224\u7a7a\u4e0d\u80fd\u5c11&#xff0c;\u5426\u5219\u7a7a\u6811 begin() \u76f4\u63a5\u5d29\u3002<\/p>\n<hr \/>\n<h3>\u4e94\u3001\u4e0d\u8ba9\u6539 key&#xff1a;const K \u7684\u5999\u7528<\/h3>\n<h4>5.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>\u7ea2\u9ed1\u6811\u662f\u6392\u5e8f\u7ed3\u6784&#xff0c;\u5b83\u9760 key \u7684\u5927\u5c0f\u5173\u7cfb\u7ef4\u6301\u5e73\u8861\u3002\u5982\u679c\u5141\u8bb8\u7528\u6237\u968f\u624b\u6539 key&#xff0c;\u6574\u68f5\u6811\u7684\u6709\u5e8f\u6027\u77ac\u95f4\u5c31\u4e71\u4e86 \u2014\u2014 \u4e4b\u540e\u518d\u67e5\u627e\u5c31\u4f1a\u627e\u4e0d\u5230\u672c\u6765\u5b58\u5728\u7684\u5143\u7d20\u3002<\/p>\n<p>\u6240\u4ee5\u5728\u8bbe\u8ba1\u4e0a\u5fc5\u987b\u4ece\u7c7b\u578b\u5c42\u9762\u76f4\u63a5\u9501\u6b7b&#xff1a;<\/p>\n<ul>\n<li>set \u7684 key \u5c31\u662f value&#xff0c;\u5168\u90fd\u4e0d\u8bb8\u6539&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>map \u7684 key \u662f pair \u7684 first&#xff0c;\u53ea\u9501\u6b7b first&#xff0c;second \u968f\u4fbf\u6539\u3002<\/li>\n<\/ul>\n<h4>5.2 \u4f8b\u5b50\u5341&#xff1a;\u6539\u4e00\u884c\u6a21\u677f\u53c2\u6570\u5c31\u641e\u5b9a\u4e86<\/h4>\n<p>\/\/ Myset.h \u2014\u2014 \u7b2c\u4e8c\u4e2a\u53c2\u6570\u52a0 const<br \/>\nRBTree&lt;K, const K, SetKeyOfT&gt; _t;<\/p>\n<p>\/\/ Mymap.h \u2014\u2014 \u628a first \u53d8\u6210 const<br \/>\nRBTree&lt;K, pair&lt;const K, V&gt;, MapKeyOfT&gt; _t;<\/p>\n<p>\u5c31\u8fd9\u4e48\u7b80\u5355&#xff01; \u52a0\u4e0a const \u4e4b\u540e&#xff1a;<\/p>\n<ul>\n<li>iterator \u7684 Ref \u53d8\u6210 const T&amp;&#xff0c;\u901a\u8fc7 *it \/ it-&gt; \u62ff\u5230\u7684\u4e1c\u897f\u5929\u7136\u5e26 const&#xff1b;<\/li>\n<\/ul>\n<ul>\n<li>\u60f3\u6539 key&#xff1f;\u7f16\u8bd1\u5668\u7b2c\u4e00\u4e2a\u4e0d\u7b54\u5e94\u3002<\/li>\n<\/ul>\n<h4>5.3 \u4f8b\u5b50\u5341\u4e00&#xff1a;\u540c\u65f6\u63d0\u4f9b iterator \u548c const_iterator<\/h4>\n<p>\/\/ Myset.h<br \/>\ntypedef typename RBTree&lt;K, const K, SetKeyOfT&gt;::Iterator      iterator;<br \/>\ntypedef typename RBTree&lt;K, const K, SetKeyOfT&gt;::ConstIterator const_iterator;<\/p>\n<p>iterator begin()             { return _t.Begin(); }<br \/>\niterator end()               { return _t.End();   }<br \/>\nconst_iterator begin() const { return _t.Begin(); }<br \/>\nconst_iterator end()   const { return _t.End();   }<\/p>\n<p>pair&lt;iterator, bool&gt; insert(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Insert(key);<br \/>\n}<\/p>\n<p>iterator find(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Find(key);<br \/>\n}<\/p>\n<h4>5.4 \u4f8b\u5b50\u5341\u4e8c&#xff1a;\u5012\u7740\u6253\u5370\u4e00\u4e2a set<\/h4>\n<p>void Print(const set&lt;int&gt;&amp; s)<br \/>\n{<br \/>\nset&lt;int&gt;::const_iterator it &#061; s.end();<br \/>\nwhile (it !&#061; s.begin())<br \/>\n{<br \/>\n&#8211;it;                  \/\/ \u4ece end \u5f80\u524d\u9000&#xff0c;\u6b63\u597d\u662f\u964d\u5e8f<br \/>\n\/\/ *it &#043;&#061; 2;           \/\/ \u274c \u7f16\u8bd1\u62a5\u9519&#xff1a;read-only&#xff0c;\u8bf4\u660e const \u751f\u6548\u4e86<br \/>\ncout &lt;&lt; *it &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_set()<br \/>\n{<br \/>\nset&lt;int&gt; s;<br \/>\nint a[] &#061; { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };<br \/>\nfor (auto e : a)<br \/>\n{<br \/>\ns.insert(e);<br \/>\n}<\/p>\n<p>for (auto e : s)          \/\/ \u8303\u56f4 for \u8d70\u4e2d\u5e8f&#xff0c;\u5929\u7136\u5347\u5e8f<br \/>\n{<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<\/p>\n<p>Print(s);                 \/\/ \u964d\u5e8f\u8f93\u51fa<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff1a;<\/p>\n<p>1 2 3 4 5 6 7 14 15 16<br \/>\n16 15 14 7 6 5 4 3 2 1<\/p>\n<h4>5.5 \u7ecf\u5178 Bug&#xff1a;it-&gt;first &#043;&#061; &#039;x&#039; \u7f16\u8bd1\u4e0d\u8fc7<\/h4>\n<p>map&lt;string, string&gt;::iterator it &#061; dict.begin();<br \/>\n\/\/ it-&gt;first &#043;&#061; &#039;x&#039;;      \/\/ \u274c \u62a5\u9519&#xff1a;assignment of read-only member<br \/>\nit-&gt;second &#043;&#061; &#039;x&#039;;        \/\/ \u2705 \u6ca1\u95ee\u9898&#xff0c;value \u53ef\u4ee5\u968f\u4fbf\u6539<\/p>\n<p>&#x1f41b; \u63d0\u793a&#xff1a;\u5982\u679c\u4f60\u53d1\u73b0 it-&gt;first \u5c45\u7136\u80fd\u6539&#xff0c;\u8bf4\u660e\u4f60\u5728 Mymap.h \u91cc\u7b2c\u4e8c\u53c2\u6570\u5199\u7684\u662f pair&lt;K, V&gt;&#xff0c;\u6f0f\u4e86 const&#xff0c;\u8d76\u7d27\u8865\u4e0a\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001map \u7684 operator[]&#xff1a;\u4e00\u884c\u4ee3\u7801\u5e72\u4e24\u4ef6\u4e8b<\/h3>\n<h4>6.1 \u7b80\u4ecb\u4f5c\u7528<\/h4>\n<p>map \u6700\u723d\u7684\u529f\u80fd\u5c31\u662f operator[]&#xff0c;\u5b83\u662f&#034;\u6709\u5219\u8fd4\u56de&#xff0c;\u65e0\u5219\u63d2\u5165&#034;&#xff1a;<\/p>\n<p>dict[&#034;left&#034;] &#061; &#034;\u5de6\u8fb9&#xff0c;\u5269\u4f59&#034;;   \/\/ left \u5df2\u5b58\u5728 -&gt; \u6539\u5b83\u7684 value<br \/>\ndict[&#034;insert&#034;] &#061; &#034;\u63d2\u5165&#034;;       \/\/ insert \u4e0d\u5b58\u5728 -&gt; \u63d2\u5165\u540e\u518d\u8d4b\u503c<br \/>\ndict[&#034;string&#034;];                \/\/ \u4e0d\u5b58\u5728 -&gt; \u63d2\u5165\u9ed8\u8ba4\u503c &#034;&#034;<\/p>\n<p>\u8981\u652f\u6301\u5b83&#xff0c;\u524d\u63d0\u662f Insert \u5f97\u628a&#034;\u63d2\u5165\u7ed3\u679c&#034;\u544a\u8bc9\u5916\u9762&#xff1a;\u65e2\u60f3\u77e5\u9053\u6709\u6ca1\u6709\u63d2\u5165\u6210\u529f&#xff0c;\u53c8\u60f3\u62ff\u5230\u90a3\u4e2a\u7ed3\u70b9\u7684\u8fed\u4ee3\u5668\u3002\u6240\u4ee5\u8fd4\u56de\u503c\u8981\u4ece bool \u5347\u7ea7\u6210 pair&lt;iterator, bool&gt;\u3002<\/p>\n<h4>6.2 \u4f8b\u5b50\u5341\u4e09&#xff1a;\u628a Insert \u7684\u8fd4\u56de\u503c\u5347\u7ea7<\/h4>\n<p>pair&lt;Iterator, bool&gt; Insert(const T&amp; data)<br \/>\n{<br \/>\nif (_root &#061;&#061; nullptr)<br \/>\n{<br \/>\n_root &#061; new Node(data);<br \/>\n_root-&gt;_col &#061; BLACK;<br \/>\nreturn make_pair(Iterator(_root, _root), true);      \/\/ \u65b0\u6839&#xff0c;\u63d2\u6210\u529f<br \/>\n}<\/p>\n<p>KeyOfT kot;<br \/>\nNode* parent &#061; nullptr;<br \/>\nNode* cur &#061; _root;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (kot(cur-&gt;_data) &lt; kot(data))<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (kot(cur-&gt;_data) &gt; kot(data))<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ key \u5df2\u5b58\u5728&#xff1a;\u628a&#034;\u5df2\u6709\u7ed3\u70b9&#034;\u7684\u8fed\u4ee3\u5668\u8fd4\u56de\u53bb&#xff0c;bool \u7ed9 false<br \/>\nreturn make_pair(Iterator(cur, _root), false);<br \/>\n}<br \/>\n}<\/p>\n<p>cur &#061; new Node(data);          \/\/ \u5230\u8fd9\u513f cur \u624d\u771f\u6b63\u88ab\u63a5\u4e0a<br \/>\nNode* newnode &#061; cur;<br \/>\ncur-&gt;_col &#061; RED;<\/p>\n<p>if (kot(parent-&gt;_data) &lt; kot(data))<br \/>\nparent-&gt;_right &#061; cur;<br \/>\nelse<br \/>\nparent-&gt;_left &#061; cur;<\/p>\n<p>cur-&gt;_parent &#061; parent;<\/p>\n<p>\/\/ \u2026\u2026\u4e2d\u95f4\u662f\u7ea2\u9ed1\u6811\u7684\u65cb\u8f6c &#043; \u53d8\u8272\u8c03\u6574&#xff08;\u524d\u9762\u6587\u7ae0\u5df2\u8be6\u89e3&#xff0c;\u8fd9\u91cc\u7565&#xff09;<\/p>\n<p>_root-&gt;_col &#061; BLACK;                                  \/\/ \u6839\u6c38\u8fdc\u662f\u9ed1\u7684<br \/>\nreturn make_pair(Iterator(newnode, _root), true);<br \/>\n}<\/p>\n<p>&#x1f4a1; \u6ce8\u610f\u6700\u540e\u4e00\u884c\u7528\u7684\u662f newnode\u3002\u56e0\u4e3a\u5728\u8c03\u6574\u8fc7\u7a0b\u4e2d cur \u53ef\u80fd\u5df2\u7ecf\u88ab\u6539\u6210\u4e86\u7956\u5148\u7ed3\u70b9&#xff0c;\u800c\u6211\u4eec\u8981\u8fd4\u56de\u7684\u662f\u65b0\u63d2\u5165\u7684\u90a3\u4e2a\u7ed3\u70b9\u7684\u8fed\u4ee3\u5668\u3002<\/p>\n<h4>6.3 \u4f8b\u5b50\u5341\u56db&#xff1a;operator[] \u7684\u5b9e\u73b0<\/h4>\n<p>\u6709\u4e86\u4e0a\u9762\u8fd9\u4e2a\u8fd4\u56de\u503c&#xff0c;operator[] \u53ea\u6709\u4e09\u884c&#xff1a;<\/p>\n<p>V&amp; operator[](const K&amp; key)<br \/>\n{<br \/>\npair&lt;iterator, bool&gt; ret &#061; insert(make_pair(key, V()));<br \/>\nreturn ret.first-&gt;second;      \/\/ \u76f4\u63a5\u8fd4\u56de value \u7684\u5f15\u7528<br \/>\n}<\/p>\n<p>\u62c6\u5f00\u770b\u5c31\u662f&#xff1a;<\/p>\n<li>\u62ff (key, V()) \u53bb\u63d2\u5165 \u2014\u2014 V() \u662f value \u7c7b\u578b\u7684\u9ed8\u8ba4\u503c&#xff08;string \u5c31\u662f &#034;&#034;&#xff0c;int \u5c31\u662f 0&#xff09;&#xff1b;<\/li>\n<li>\u5982\u679c key \u5df2\u5b58\u5728&#xff0c;insert \u4f1a\u8fd4\u56de false \u548c\u5df2\u6709\u7ed3\u70b9\u7684\u8fed\u4ee3\u5668&#xff0c;\u63d2\u5165\u52a8\u4f5c\u88ab\u5ffd\u7565&#xff1b;<\/li>\n<li>\u5982\u679c key \u4e0d\u5b58\u5728&#xff0c;\u771f\u7684\u63d2\u5165\u6210\u529f&#xff0c;\u8fd4\u56de true \u548c\u65b0\u7ed3\u70b9\u7684\u8fed\u4ee3\u5668&#xff1b;<\/li>\n<li>\u65e0\u8bba\u54ea\u79cd\u60c5\u51b5&#xff0c;ret.first \u90fd\u6307\u5411\u90a3\u4e2a key \u5bf9\u5e94\u7684\u7ed3\u70b9&#xff0c;-&gt;second \u5c31\u662f\u5b83\u7684 value\u3002<\/li>\n<p>\u6240\u4ee5 operator[] \u8fd4\u56de\u7684\u662f\u5f15\u7528&#xff0c;\u65e2\u80fd\u8bfb\u4e5f\u80fd\u5199\u3002<\/p>\n<h4>6.4 \u4f8b\u5b50\u5341\u4e94&#xff1a;\u8dd1\u4e00\u904d map<\/h4>\n<p>void test_map()<br \/>\n{<br \/>\nmap&lt;string, string&gt; dict;<br \/>\ndict.insert({ &#034;sort&#034;, &#034;\u6392\u5e8f&#034; });<br \/>\ndict.insert({ &#034;left&#034;, &#034;\u5de6\u8fb9&#034; });<br \/>\ndict.insert({ &#034;right&#034;, &#034;\u53f3\u8fb9&#034; });<\/p>\n<p>dict[&#034;left&#034;] &#061; &#034;\u5de6\u8fb9&#xff0c;\u5269\u4f59&#034;;     \/\/ \u4fee\u6539\u5df2\u6709<br \/>\ndict[&#034;insert&#034;] &#061; &#034;\u63d2\u5165&#034;;         \/\/ \u63d2\u5165\u65b0\u7684<br \/>\ndict[&#034;string&#034;];                  \/\/ \u63d2\u5165\u9ed8\u8ba4\u503c &#034;&#034;<\/p>\n<p>map&lt;string, string&gt;::iterator it &#061; dict.begin();<br \/>\nwhile (it !&#061; dict.end())<br \/>\n{<br \/>\n\/\/ it-&gt;first &#043;&#061; &#039;x&#039;;         \/\/ \u274c key \u4e0d\u80fd\u6539<br \/>\nit-&gt;second &#043;&#061; &#039;x&#039;;           \/\/ \u2705 value \u53ef\u4ee5\u6539<br \/>\ncout &lt;&lt; it-&gt;first &lt;&lt; &#034;:&#034; &lt;&lt; it-&gt;second &lt;&lt; endl;<br \/>\n&#043;&#043;it;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n}<\/p>\n<p>\u8fd0\u884c\u7ed3\u679c&#xff08;key \u81ea\u52a8\u5347\u5e8f&#xff0c;\u56e0\u4e3a\u4e2d\u5e8f\u904d\u5386&#xff09;&#xff1a;<\/p>\n<p>insert:\u63d2\u5165x<br \/>\nleft:\u5de6\u8fb9&#xff0c;\u5269\u4f59x<br \/>\nright:\u53f3\u8fb9x<br \/>\nsort:\u6392\u5e8fx<br \/>\nstring:x<\/p>\n<h4>6.5 \u7ecf\u5178 Bug&#xff1a;Insert \u53ea\u8fd4\u56de bool<\/h4>\n<p>\u5982\u679c\u4f60\u524d\u9762\u7684 Insert \u5199\u7684\u662f&#xff1a;<\/p>\n<p>bool Insert(const T&amp; data);      \/\/ \u274c \u6ca1\u6709\u8fed\u4ee3\u5668<\/p>\n<p>\u90a3 operator[] \u5c31\u5b8c\u5168\u6ca1\u6cd5\u5b9e\u73b0\u4e86 \u2014\u2014 \u4f60\u65e2\u4e0d\u77e5\u9053\u65b0\u7ed3\u70b9\u5728\u54ea&#xff0c;\u4e5f\u4e0d\u77e5\u9053\u5230\u5e95\u63d2\u5165\u6ca1\u6709&#xff1a;<\/p>\n<p>V&amp; operator[](const K&amp; key)<br \/>\n{<br \/>\nbool ret &#061; insert(make_pair(key, V()));<br \/>\n\/\/ ret.first -&gt; \u274c \u7f16\u8bd1\u62a5\u9519&#xff0c;bool \u6ca1\u6709 first<br \/>\nreturn ???;                  \/\/ \u5361\u6b7b\u5728\u8fd9\u91cc<br \/>\n}<\/p>\n<p>&#x1f41b; \u907f\u5751\u7ed3\u8bba&#xff1a;map \u60f3\u8981 operator[]&#xff0c;Insert \u5fc5\u987b\u8fd4\u56de pair&lt;iterator, bool&gt;\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 STL \u7684 insert \u8981\u8fd4\u56de pair&lt;iterator, bool&gt; \u7684\u771f\u6b63\u539f\u56e0\u3002<\/p>\n<hr \/>\n<h3>\u4e03\u3001\u5b8c\u6574\u4ee3\u7801\u6c47\u603b<\/h3>\n<h4>7.1 RBTree.h<\/h4>\n<p>#pragma once<br \/>\n#include &lt;iostream&gt;<br \/>\n#include &lt;utility&gt;<br \/>\nusing namespace std;<\/p>\n<p>enum Colour<br \/>\n{<br \/>\nRED,<br \/>\nBLACK<br \/>\n};<\/p>\n<p>template&lt;class T&gt;<br \/>\nstruct RBTreeNode<br \/>\n{<br \/>\nT _data;<br \/>\nRBTreeNode&lt;T&gt;* _left;<br \/>\nRBTreeNode&lt;T&gt;* _right;<br \/>\nRBTreeNode&lt;T&gt;* _parent;<br \/>\nColour _col;<\/p>\n<p>RBTreeNode(const T&amp; data)<br \/>\n: _data(data)<br \/>\n, _left(nullptr)<br \/>\n, _right(nullptr)<br \/>\n, _parent(nullptr)<br \/>\n, _col(RED)<br \/>\n{}<br \/>\n};<\/p>\n<p>\/\/ \u8fed\u4ee3\u5668<br \/>\ntemplate&lt;class T, class Ref, class Ptr&gt;<br \/>\nstruct RBTreeIterator<br \/>\n{<br \/>\ntypedef RBTreeNode&lt;T&gt; Node;<br \/>\ntypedef RBTreeIterator&lt;T, Ref, Ptr&gt; Self;<\/p>\n<p>Node* _node;<br \/>\nNode* _root;<\/p>\n<p>RBTreeIterator(Node* node, Node* root)<br \/>\n: _node(node)<br \/>\n, _root(root)<br \/>\n{}<\/p>\n<p>Ref operator*()  { return _node-&gt;_data; }<br \/>\nPtr operator-&gt;() { return &amp;_node-&gt;_data; }<\/p>\n<p>bool operator!&#061;(const Self&amp; s) const { return _node !&#061; s._node; }<br \/>\nbool operator&#061;&#061;(const Self&amp; s) const { return _node &#061;&#061; s._node; }<\/p>\n<p>Self&amp; operator&#043;&#043;()<br \/>\n{<br \/>\nif (_node-&gt;_right)<br \/>\n{<br \/>\nNode* leftMost &#061; _node-&gt;_right;<br \/>\nwhile (leftMost-&gt;_left)<br \/>\n{<br \/>\nleftMost &#061; leftMost-&gt;_left;<br \/>\n}<br \/>\n_node &#061; leftMost;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nNode* cur &#061; _node;<br \/>\nNode* parent &#061; cur-&gt;_parent;<br \/>\nwhile (parent &amp;&amp; cur &#061;&#061; parent-&gt;_right)<br \/>\n{<br \/>\ncur &#061; parent;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\n_node &#061; parent;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<\/p>\n<p>Self&amp; operator&#8211;()<br \/>\n{<br \/>\nif (_node &#061;&#061; nullptr)               \/\/ &#8211;end()<br \/>\n{<br \/>\nNode* rightMost &#061; _root;<br \/>\nwhile (rightMost &amp;&amp; rightMost-&gt;_right)<br \/>\n{<br \/>\nrightMost &#061; rightMost-&gt;_right;<br \/>\n}<br \/>\n_node &#061; rightMost;<br \/>\n}<br \/>\nelse if (_node-&gt;_left)<br \/>\n{<br \/>\nNode* rightMost &#061; _node-&gt;_left;<br \/>\nwhile (rightMost-&gt;_right)<br \/>\n{<br \/>\nrightMost &#061; rightMost-&gt;_right;<br \/>\n}<br \/>\n_node &#061; rightMost;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nNode* cur &#061; _node;<br \/>\nNode* parent &#061; cur-&gt;_parent;<br \/>\nwhile (parent &amp;&amp; cur &#061;&#061; parent-&gt;_left)<br \/>\n{<br \/>\ncur &#061; parent;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\n_node &#061; parent;<br \/>\n}<br \/>\nreturn *this;<br \/>\n}<br \/>\n};<\/p>\n<p>template&lt;class K, class T, class KeyOfT&gt;<br \/>\nclass RBTree<br \/>\n{<br \/>\ntypedef RBTreeNode&lt;T&gt; Node;<\/p>\n<p>public:<br \/>\ntypedef RBTreeIterator&lt;T, T&amp;, T*&gt;             Iterator;<br \/>\ntypedef RBTreeIterator&lt;T, const T&amp;, const T*&gt; ConstIterator;<\/p>\n<p>RBTree() &#061; default;<\/p>\n<p>~RBTree()<br \/>\n{<br \/>\nDestroy(_root);<br \/>\n_root &#061; nullptr;<br \/>\n}<\/p>\n<p>Iterator Begin()<br \/>\n{<br \/>\nNode* leftMost &#061; _root;<br \/>\nwhile (leftMost &amp;&amp; leftMost-&gt;_left)<br \/>\n{<br \/>\nleftMost &#061; leftMost-&gt;_left;<br \/>\n}<br \/>\nreturn Iterator(leftMost, _root);<br \/>\n}<\/p>\n<p>Iterator End()<br \/>\n{<br \/>\nreturn Iterator(nullptr, _root);<br \/>\n}<\/p>\n<p>ConstIterator Begin() const<br \/>\n{<br \/>\nNode* leftMost &#061; _root;<br \/>\nwhile (leftMost &amp;&amp; leftMost-&gt;_left)<br \/>\n{<br \/>\nleftMost &#061; leftMost-&gt;_left;<br \/>\n}<br \/>\nreturn ConstIterator(leftMost, _root);<br \/>\n}<\/p>\n<p>ConstIterator End() const<br \/>\n{<br \/>\nreturn ConstIterator(nullptr, _root);<br \/>\n}<\/p>\n<p>pair&lt;Iterator, bool&gt; Insert(const T&amp; data)<br \/>\n{<br \/>\nif (_root &#061;&#061; nullptr)<br \/>\n{<br \/>\n_root &#061; new Node(data);<br \/>\n_root-&gt;_col &#061; BLACK;<br \/>\nreturn make_pair(Iterator(_root, _root), true);<br \/>\n}<\/p>\n<p>KeyOfT kot;<br \/>\nNode* parent &#061; nullptr;<br \/>\nNode* cur &#061; _root;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (kot(cur-&gt;_data) &lt; kot(data))<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (kot(cur-&gt;_data) &gt; kot(data))<br \/>\n{<br \/>\nparent &#061; cur;<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn make_pair(Iterator(cur, _root), false);<br \/>\n}<br \/>\n}<\/p>\n<p>cur &#061; new Node(data);<br \/>\nNode* newnode &#061; cur;<br \/>\ncur-&gt;_col &#061; RED;<\/p>\n<p>if (kot(parent-&gt;_data) &lt; kot(data))<br \/>\nparent-&gt;_right &#061; cur;<br \/>\nelse<br \/>\nparent-&gt;_left &#061; cur;<\/p>\n<p>cur-&gt;_parent &#061; parent;<\/p>\n<p>\/\/ \u5e73\u8861\u8c03\u6574<br \/>\nwhile (parent &amp;&amp; parent-&gt;_col &#061;&#061; RED)<br \/>\n{<br \/>\nNode* grandfather &#061; parent-&gt;_parent;<br \/>\nif (parent &#061;&#061; grandfather-&gt;_left)<br \/>\n{<br \/>\nNode* uncle &#061; grandfather-&gt;_right;<br \/>\nif (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n{<br \/>\n\/\/ \u53d4\u53d4\u5b58\u5728\u4e14\u4e3a\u7ea2 -&gt; \u53d8\u8272\u7ee7\u7eed\u5f80\u4e0a\u5904\u7406<br \/>\nparent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\ncur &#061; grandfather;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (cur &#061;&#061; parent-&gt;_left)<br \/>\n{<br \/>\n\/\/ \u5355\u65cb&#xff1a;g \u5de6&#xff0c;p \u5de6&#xff0c;c \u5de6<br \/>\nRotateR(grandfather);<br \/>\nparent-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ \u53cc\u65cb&#xff1a;\u5de6\u53f3<br \/>\nRotateL(parent);<br \/>\nRotateR(grandfather);<br \/>\ncur-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\n}<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nNode* uncle &#061; grandfather-&gt;_left;<br \/>\nif (uncle &amp;&amp; uncle-&gt;_col &#061;&#061; RED)<br \/>\n{<br \/>\nparent-&gt;_col &#061; uncle-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\ncur &#061; grandfather;<br \/>\nparent &#061; cur-&gt;_parent;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (cur &#061;&#061; parent-&gt;_right)<br \/>\n{<br \/>\n\/\/ \u5355\u65cb&#xff1a;\u53f3\u53f3<br \/>\nRotateL(grandfather);<br \/>\nparent-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\n\/\/ \u53cc\u65cb&#xff1a;\u53f3\u5de6<br \/>\nRotateR(parent);<br \/>\nRotateL(grandfather);<br \/>\ncur-&gt;_col &#061; BLACK;<br \/>\ngrandfather-&gt;_col &#061; RED;<br \/>\n}<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>_root-&gt;_col &#061; BLACK;<br \/>\nreturn make_pair(Iterator(newnode, _root), true);<br \/>\n}<\/p>\n<p>Iterator Find(const K&amp; key)<br \/>\n{<br \/>\nKeyOfT kot;                    \/\/ \u7528 KeyOfT \u53d6 key&#xff0c;\u4e0d\u8981\u76f4\u63a5\u5199 _kv.first&#xff01;<br \/>\nNode* cur &#061; _root;<br \/>\nwhile (cur)<br \/>\n{<br \/>\nif (kot(cur-&gt;_data) &lt; key)<br \/>\n{<br \/>\ncur &#061; cur-&gt;_right;<br \/>\n}<br \/>\nelse if (kot(cur-&gt;_data) &gt; key)<br \/>\n{<br \/>\ncur &#061; cur-&gt;_left;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nreturn Iterator(cur, _root);<br \/>\n}<br \/>\n}<br \/>\nreturn End();<br \/>\n}<\/p>\n<p>private:<br \/>\nvoid RotateL(Node* parent)<br \/>\n{<br \/>\nNode* subR &#061; parent-&gt;_right;<br \/>\nNode* subRL &#061; subR-&gt;_left;<\/p>\n<p>parent-&gt;_right &#061; subRL;<br \/>\nif (subRL)<br \/>\nsubRL-&gt;_parent &#061; parent;<\/p>\n<p>Node* parentParent &#061; parent-&gt;_parent;<br \/>\nsubR-&gt;_left &#061; parent;<br \/>\nparent-&gt;_parent &#061; subR;<\/p>\n<p>if (parentParent &#061;&#061; nullptr)<br \/>\n{<br \/>\n_root &#061; subR;<br \/>\nsubR-&gt;_parent &#061; nullptr;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (parent &#061;&#061; parentParent-&gt;_left)<br \/>\nparentParent-&gt;_left &#061; subR;<br \/>\nelse<br \/>\nparentParent-&gt;_right &#061; subR;<\/p>\n<p>subR-&gt;_parent &#061; parentParent;<br \/>\n}<br \/>\n}<\/p>\n<p>void RotateR(Node* parent)<br \/>\n{<br \/>\nNode* subL &#061; parent-&gt;_left;<br \/>\nNode* subLR &#061; subL-&gt;_right;<\/p>\n<p>parent-&gt;_left &#061; subLR;<br \/>\nif (subLR)<br \/>\nsubLR-&gt;_parent &#061; parent;<\/p>\n<p>Node* parentParent &#061; parent-&gt;_parent;<br \/>\nsubL-&gt;_right &#061; parent;<br \/>\nparent-&gt;_parent &#061; subL;<\/p>\n<p>if (parentParent &#061;&#061; nullptr)<br \/>\n{<br \/>\n_root &#061; subL;<br \/>\nsubL-&gt;_parent &#061; nullptr;<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nif (parent &#061;&#061; parentParent-&gt;_left)<br \/>\nparentParent-&gt;_left &#061; subL;<br \/>\nelse<br \/>\nparentParent-&gt;_right &#061; subL;<\/p>\n<p>subL-&gt;_parent &#061; parentParent;<br \/>\n}<br \/>\n}<\/p>\n<p>void Destroy(Node* root)<br \/>\n{<br \/>\nif (root &#061;&#061; nullptr)<br \/>\nreturn;<br \/>\nDestroy(root-&gt;_left);<br \/>\nDestroy(root-&gt;_right);<br \/>\ndelete root;<br \/>\n}<\/p>\n<p>private:<br \/>\nNode* _root &#061; nullptr;<br \/>\n};<\/p>\n<h4>7.2 Myset.h<\/h4>\n<p>#pragma once<br \/>\n#include &#034;RBTree.h&#034;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\ntemplate&lt;class K&gt;<br \/>\nclass set<br \/>\n{<br \/>\nstruct SetKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const K&amp; key)<br \/>\n{<br \/>\nreturn key;<br \/>\n}<br \/>\n};<\/p>\n<p>public:<br \/>\ntypedef typename RBTree&lt;K, const K, SetKeyOfT&gt;::Iterator      iterator;<br \/>\ntypedef typename RBTree&lt;K, const K, SetKeyOfT&gt;::ConstIterator const_iterator;<\/p>\n<p>iterator begin()             { return _t.Begin(); }<br \/>\niterator end()               { return _t.End();   }<br \/>\nconst_iterator begin() const { return _t.Begin(); }<br \/>\nconst_iterator end()   const { return _t.End();   }<\/p>\n<p>pair&lt;iterator, bool&gt; insert(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Insert(key);<br \/>\n}<\/p>\n<p>iterator find(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Find(key);<br \/>\n}<\/p>\n<p>private:<br \/>\nRBTree&lt;K, const K, SetKeyOfT&gt; _t;    \/\/ \u7b2c\u4e8c\u4e2a\u53c2\u6570\u52a0 const&#xff0c;key \u4e0d\u53ef\u6539<br \/>\n};<br \/>\n}<\/p>\n<h4>7.3 Mymap.h<\/h4>\n<p>#pragma once<br \/>\n#include &#034;RBTree.h&#034;<\/p>\n<p>namespace bit<br \/>\n{<br \/>\ntemplate&lt;class K, class V&gt;<br \/>\nclass map<br \/>\n{<br \/>\nstruct MapKeyOfT<br \/>\n{<br \/>\nconst K&amp; operator()(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nreturn kv.first;<br \/>\n}<br \/>\n};<\/p>\n<p>public:<br \/>\ntypedef typename RBTree&lt;K, pair&lt;const K, V&gt;, MapKeyOfT&gt;::Iterator      iterator;<br \/>\ntypedef typename RBTree&lt;K, pair&lt;const K, V&gt;, MapKeyOfT&gt;::ConstIterator const_iterator;<\/p>\n<p>iterator begin()             { return _t.Begin(); }<br \/>\niterator end()               { return _t.End();   }<br \/>\nconst_iterator begin() const { return _t.Begin(); }<br \/>\nconst_iterator end()   const { return _t.End();   }<\/p>\n<p>pair&lt;iterator, bool&gt; insert(const pair&lt;K, V&gt;&amp; kv)<br \/>\n{<br \/>\nreturn _t.Insert(kv);<br \/>\n}<\/p>\n<p>iterator find(const K&amp; key)<br \/>\n{<br \/>\nreturn _t.Find(key);<br \/>\n}<\/p>\n<p>V&amp; operator[](const K&amp; key)<br \/>\n{<br \/>\npair&lt;iterator, bool&gt; ret &#061; insert(make_pair(key, V()));<br \/>\nreturn ret.first-&gt;second;<br \/>\n}<\/p>\n<p>private:<br \/>\nRBTree&lt;K, pair&lt;const K, V&gt;, MapKeyOfT&gt; _t;    \/\/ \u53ea\u9501\u6b7b first<br \/>\n};<br \/>\n}<\/p>\n<h4>7.4 test.cpp<\/h4>\n<p>#include &#034;RBTree.h&#034;<br \/>\n#include &#034;Myset.h&#034;<br \/>\n#include &#034;Mymap.h&#034;<\/p>\n<p>void test_set()<br \/>\n{<br \/>\nbit::set&lt;int&gt; s;<br \/>\nint a[] &#061; { 4, 2, 6, 1, 3, 5, 15, 7, 16, 14 };<br \/>\nfor (auto e : a)<br \/>\n{<br \/>\ns.insert(e);<br \/>\n}<\/p>\n<p>for (auto e : s)<br \/>\ncout &lt;&lt; e &lt;&lt; &#034; &#034;;<br \/>\ncout &lt;&lt; endl;<br \/>\n}<\/p>\n<p>void test_map()<br \/>\n{<br \/>\nbit::map&lt;string, string&gt; dict;<br \/>\ndict.insert({ &#034;sort&#034;, &#034;\u6392\u5e8f&#034; });<br \/>\ndict.insert({ &#034;left&#034;, &#034;\u5de6\u8fb9&#034; });<br \/>\ndict.insert({ &#034;right&#034;, &#034;\u53f3\u8fb9&#034; });<\/p>\n<p>dict[&#034;left&#034;] &#061; &#034;\u5de6\u8fb9&#xff0c;\u5269\u4f59&#034;;<br \/>\ndict[&#034;insert&#034;] &#061; &#034;\u63d2\u5165&#034;;<br \/>\ndict[&#034;string&#034;];<\/p>\n<p>bit::map&lt;string, string&gt;::iterator it &#061; dict.begin();<br \/>\nwhile (it !&#061; dict.end())<br \/>\n{<br \/>\nit-&gt;second &#043;&#061; &#039;x&#039;;<br \/>\ncout &lt;&lt; it-&gt;first &lt;&lt; &#034;:&#034; &lt;&lt; it-&gt;second &lt;&lt; endl;<br \/>\n&#043;&#043;it;<br \/>\n}<br \/>\ncout &lt;&lt; endl;<br \/>\n}<\/p>\n<p>int main()<br \/>\n{<br \/>\ntest_set();<br \/>\ntest_map();<br \/>\nreturn 0;<br \/>\n}<\/p>\n<hr \/>\n<h3>\u516b\u3001\u672c\u7bc7\u5c0f\u7ed3<\/h3>\n<p>\u628a\u6574\u4ef6\u4e8b\u634b\u4e00\u904d&#xff0c;\u5176\u5b9e\u5c31\u53ea\u6709 6 \u6b65&#xff1a;<\/p>\n<table>\n<tbody>\n<tr>\n<td>\n<p>\u6b65\u9aa4<\/p>\n<\/td>\n<td>\n<p>\u8981\u505a\u7684\u4e8b<\/p>\n<\/td>\n<td>\n<p>\u5173\u952e\u70b9<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2460<\/p>\n<\/td>\n<td>\n<p>\u5b9e\u73b0\u7ea2\u9ed1\u6811<\/p>\n<\/td>\n<td>\n<p>\u6cdb\u578b\u6a21\u677f RBTree&lt;K, T, KeyOfT&gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2461<\/p>\n<\/td>\n<td>\n<p>\u5c01\u88c5 map \/ set \u6846\u67b6<\/p>\n<\/td>\n<td>\n<p>\u7b2c\u4e8c\u4e2a\u6a21\u677f\u53c2\u6570\u51b3\u5b9a\u5b58 K \u8fd8\u662f pair&#xff1b;\u5199 SetKeyOfT \/ MapKeyOfT<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2462<\/p>\n<\/td>\n<td>\n<p>\u52a0 iterator<\/p>\n<\/td>\n<td>\n<p>operator&#043;&#043; \/ operator&#8211; \u8d70\u4e2d\u5e8f&#xff1b;end() \u7528 nullptr<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2463<\/p>\n<\/td>\n<td>\n<p>\u52a0 const_iterator<\/p>\n<\/td>\n<td>\n<p>Ref \/ Ptr \u5206\u79bb&#xff0c;\u4e00\u5957\u4ee3\u7801\u4e24\u4e2a\u7248\u672c<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2464<\/p>\n<\/td>\n<td>\n<p>\u9501\u6b7b key<\/p>\n<\/td>\n<td>\n<p>const K \/ pair&lt;const K, V&gt;<\/p>\n<\/td>\n<\/tr>\n<tr>\n<td>\n<p>\u2465<\/p>\n<\/td>\n<td>\n<p>map \u7684 operator[]<\/p>\n<\/td>\n<td>\n<p>Insert \u8fd4\u56de pair&lt;iterator, bool&gt;<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6700\u5bb9\u6613\u8e29\u7684\u4e09\u4e2a\u5751&#xff0c;\u518d\u5f3a\u8c03\u4e00\u6b21&#xff1a;<\/p>\n<li>\u5fd8\u4e86 KeyOfT \u2014\u2014 map \u76f4\u63a5\u7f16\u8bd1\u4e0d\u8fc7&#xff08;\u56e0\u4e3a V \u4e0d\u652f\u6301 &lt;&#xff09;&#xff1b;<\/li>\n<li>Insert \u53ea\u8fd4\u56de bool \u2014\u2014 operator[] \u65e0\u4ece\u4e0b\u624b&#xff1b;<\/li>\n<li>\u8fed\u4ee3\u5668\u4e0d\u4f20 _root \u6216 &#8211;end() \u4e0d\u5224\u7a7a \u2014\u2014 \u8fd0\u884c\u671f\u5d29\u6e83&#xff0c;\u800c\u4e14\u5f88\u96be\u67e5\u3002<\/li>\n<p>\u5230\u8fd9\u91cc&#xff0c;map \u548c set \u7684\u5e95\u5c42\u5bf9\u6211\u4eec\u6765\u8bf4\u5c31\u4e0d\u518d\u662f\u9ed1\u76d2\u4e86 \u2014\u2014 \u5b83\u4eec\u4e0d\u8fc7\u662f\u4e00\u68f5\u7ea2\u9ed1\u6811 &#043; \u4e00\u5c42\u8584\u8584\u7684\u5c01\u88c5\u800c\u5df2\u3002<\/p>\n<p>\u524d\u9762\u6211\u4eec\u624b\u6495\u4e86\u7ea2\u9ed1\u6811\u7684\u65cb\u8f6c\u4e0e\u53d8\u8272&#xff0c;\u8fd9\u4e00\u7bc7\u628a&#034;\u5916\u58f3&#034;\u8865\u9f50\u3002\u4e0b\u4e00\u7bc7\u6211\u4eec\u7ee7\u7eed\u5f80\u524d\u63a8\u8fdb&#xff0c;\u522b\u5fd8\u4e86\u628a\u601d\u7ef4\u5bfc\u56fe\u5b58\u4e0b\u6765\u968f\u65f6\u590d\u4e60&#xff5e;<\/p>\n<p>\u5982\u679c\u8fd9\u7bc7\u5e2e\u4f60\u7406\u6e05\u4e86\u601d\u8def&#xff0c;\u6b22\u8fce\u70b9\u8d5e &#043; \u6536\u85cf &#043; \u5173\u6ce8&#xff0c;\u4f60\u7684\u652f\u6301\u662f\u6211\u6301\u7eed\u66f4\u65b0\u7684\u6700\u5927\u52a8\u529b&#xff01; &#x1f525;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3010\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60C\u3011\u7b2c 20 \u7bc7 \u7cfb\u5217\u5b9a\u4f4d&#xff1a;\u5199\u7ed9\u65b0\u624b\u5c0f\u767d\u7684 C \u8fdb\u9636\u4e4b\u8def\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u624b\u6495\u5b8c\u4e86 map \/ set \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u2014\u2014\u7ea2\u9ed1\u6811&#xff0c;\u8fd9\u4e00\u7bc7\u6211\u4eec\u5c31\u628a\u5b83\u5305\u4e00\u5c42\u76ae&#xff0c;\u771f\u6b63\u505a\u51fa\u5c5e\u4e8e\u6211\u4eec\u81ea\u5df1\u7684 mymap \u548c myset\u3002&#x1f4cc; \u5168\u6587\u601d\u7ef4\u5bfc\u56fe\u5efa\u8bae\u5148\u628a\u4e0a\u9762\u8fd9\u5f20\u56fe\u4fdd\u5b58\u4e0b\u6765&#xff0c;\u8bfb\u5230\u54ea\u4e00\u6b65\u5fd8\u4e86\u5c31\u56de\u5934\u770b\u54ea\u4e00\u652f\u3002\u4e00\u3001\u5148\u56de\u7b54\u4e00\u4e2a\u7075\u9b42\u95ee\u9898&#xff1a;\u4e3a\u4ec0\u4e48 map \u548c set \u80fd\u5171\u7528\u4e00\u68f5\u6811&#xff1f;1.1 \u7b80\u4ecb\u4f5c\u7528set<\/p>\n","protected":false},"author":2,"featured_media":107332,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[11653,55,371],"topic":[],"class_list":["post-107334","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-11653","tag-c","tag-371"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \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\/107334.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u3010\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60C\u3011\u7b2c 20 \u7bc7 \u7cfb\u5217\u5b9a\u4f4d&#xff1a;\u5199\u7ed9\u65b0\u624b\u5c0f\u767d\u7684 C \u8fdb\u9636\u4e4b\u8def\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u624b\u6495\u5b8c\u4e86 map \/ set \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u2014\u2014\u7ea2\u9ed1\u6811&#xff0c;\u8fd9\u4e00\u7bc7\u6211\u4eec\u5c31\u628a\u5b83\u5305\u4e00\u5c42\u76ae&#xff0c;\u771f\u6b63\u505a\u51fa\u5c5e\u4e8e\u6211\u4eec\u81ea\u5df1\u7684 mymap \u548c myset\u3002&#x1f4cc; \u5168\u6587\u601d\u7ef4\u5bfc\u56fe\u5efa\u8bae\u5148\u628a\u4e0a\u9762\u8fd9\u5f20\u56fe\u4fdd\u5b58\u4e0b\u6765&#xff0c;\u8bfb\u5230\u54ea\u4e00\u6b65\u5fd8\u4e86\u5c31\u56de\u5934\u770b\u54ea\u4e00\u652f\u3002\u4e00\u3001\u5148\u56de\u7b54\u4e00\u4e2a\u7075\u9b42\u95ee\u9898&#xff1a;\u4e3a\u4ec0\u4e48 map \u548c set \u80fd\u5171\u7528\u4e00\u68f5\u6811&#xff1f;1.1 \u7b80\u4ecb\u4f5c\u7528set\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/107334.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-19T05:02:21+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919050216-6aae1758859dc.jpg\" \/>\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=\"17 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107334.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/107334.html\",\"name\":\"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-19T05:02:21+00:00\",\"dateModified\":\"2026-09-19T05:02:21+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/107334.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/107334.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/107334.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset\"}]},{\"@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":"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \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\/107334.html","og_locale":"zh_CN","og_type":"article","og_title":"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u3010\u4ece\u96f6\u5f00\u59cb\u5b66\u4e60C\u3011\u7b2c 20 \u7bc7 \u7cfb\u5217\u5b9a\u4f4d&#xff1a;\u5199\u7ed9\u65b0\u624b\u5c0f\u767d\u7684 C \u8fdb\u9636\u4e4b\u8def\u3002\u524d\u9762\u6211\u4eec\u5df2\u7ecf\u624b\u6495\u5b8c\u4e86 map \/ set \u7684\u5e95\u5c42\u6570\u636e\u7ed3\u6784\u2014\u2014\u7ea2\u9ed1\u6811&#xff0c;\u8fd9\u4e00\u7bc7\u6211\u4eec\u5c31\u628a\u5b83\u5305\u4e00\u5c42\u76ae&#xff0c;\u771f\u6b63\u505a\u51fa\u5c5e\u4e8e\u6211\u4eec\u81ea\u5df1\u7684 mymap \u548c myset\u3002&#x1f4cc; \u5168\u6587\u601d\u7ef4\u5bfc\u56fe\u5efa\u8bae\u5148\u628a\u4e0a\u9762\u8fd9\u5f20\u56fe\u4fdd\u5b58\u4e0b\u6765&#xff0c;\u8bfb\u5230\u54ea\u4e00\u6b65\u5fd8\u4e86\u5c31\u56de\u5934\u770b\u54ea\u4e00\u652f\u3002\u4e00\u3001\u5148\u56de\u7b54\u4e00\u4e2a\u7075\u9b42\u95ee\u9898&#xff1a;\u4e3a\u4ec0\u4e48 map \u548c set \u80fd\u5171\u7528\u4e00\u68f5\u6811&#xff1f;1.1 \u7b80\u4ecb\u4f5c\u7528set","og_url":"https:\/\/www.wsisp.com\/helps\/107334.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-19T05:02:21+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/09\/20260919050216-6aae1758859dc.jpg"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"17 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/107334.html","url":"https:\/\/www.wsisp.com\/helps\/107334.html","name":"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-19T05:02:21+00:00","dateModified":"2026-09-19T05:02:21+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/107334.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/107334.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/107334.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"de\u98ce\u2014\u2014\u3010\u4ece\u96f6\u5f00\u59cb\u5b66C++\u3011\uff08\u4e8c\u5341\uff09\u7ea2\u9ed1\u6811\u5c01\u88c5map\u548cset"}]},{"@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\/107334","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=107334"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/107334\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/107332"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=107334"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=107334"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=107334"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=107334"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}