{"id":294,"date":"2026-03-15T17:25:42","date_gmt":"2026-03-15T09:25:42","guid":{"rendered":"https:\/\/jiangqvweihuan.cn\/?p=294"},"modified":"2026-03-16T16:45:05","modified_gmt":"2026-03-16T08:45:05","slug":"map%e5%ae%b9%e5%99%a8","status":"publish","type":"post","link":"https:\/\/jiangqvweihuan.cn\/index.php\/2026\/03\/15\/map%e5%ae%b9%e5%99%a8\/","title":{"rendered":"\u6784\u7b97\u6cd5\u5ea6\u00b7map\u5bb9\u5668"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">\u4e00\u3001map\u5bb9\u5668\u7684\u5b9a\u4e49\u548c\u6027\u8d28<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.map\u5bb9\u5668\u7684\u5b9a\u4e49<\/h3>\n\n\n\n<p>map\u662f\u4e00\u79cd\u57fa\u4e8e\u7ea2\u9ed1\u6811\u7684\u5173\u8054\u5f0f\u5bb9\u5668\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\uff08key-value pairs\uff09\uff0c\u5b58\u50a8\u7684\u90fd\u662fpair\u5bf9\u8c61\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u662f\u6309\u7167\u952e\u7684\u987a\u5e8f\u81ea\u52a8\u6392\u5e8f\u7684\uff0c\u8fd9\u4f7f\u5f97\u5b83\u975e\u5e38\u9002\u5408\u9700\u8981\u5feb\u901f\u67e5\u627e\u548c\u6709\u5e8f\u6570\u636e\u7684\u573a\u666f\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.map\u5bb9\u5668\u7684\u6027\u8d28<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u952e\u503c\u5bf9<\/strong>\uff1a<code>map<\/code>\u00a0\u5b58\u50a8\u7684\u662f\u952e\u503c\u5bf9\uff0c\u5176\u4e2d\u6bcf\u4e2a\u952e\u90fd\u662f\u552f\u4e00\u7684\u3002<\/li>\n\n\n\n<li><strong>\u6392\u5e8f<\/strong>\uff1a<code>map<\/code>\u00a0\u4e2d\u7684\u5143\u7d20\u6309\u7167\u952e\u7684\u987a\u5e8f\u81ea\u52a8\u6392\u5e8f\uff0c\u901a\u5e38\u662f\u5347\u5e8f\u3002<\/li>\n\n\n\n<li><strong>\u552f\u4e00\u6027<\/strong>\uff1a\u6bcf\u4e2a\u952e\u5728\u00a0<code>map<\/code>\u00a0\u4e2d\u53ea\u80fd\u51fa\u73b0\u4e00\u6b21\u3002<\/li>\n\n\n\n<li><strong>\u53cc\u5411\u8fed\u4ee3\u5668<\/strong>\uff1a<code>map<\/code>\u00a0\u63d0\u4f9b\u4e86\u53cc\u5411\u8fed\u4ee3\u5668\uff0c\u53ef\u4ee5\u5411\u524d\u548c\u5411\u540e\u904d\u5386\u5143\u7d20\u3002<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001map\u5bb9\u5668\u7684\u4f7f\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.map\u5bb9\u5668\u7684\u58f0\u660e<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;bits\/stdc++.h>\nusing namespace std;\n\nint main()\n{\n    map&lt;string, int> mp;\n\n    mp&#91;\"Alice\"] = 30;\n    mp&#91;\"Bob\"] = 25;\n    mp&#91;\"Charlie\"] = 35;\n\n    for (auto it = mp.begin(); it != mp.end(); ++it)\n    {\n        cout &lt;&lt; it->first &lt;&lt; \" is \" &lt;&lt; it->second &lt;&lt; \" years old.\" &lt;&lt; std::endl;\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n\n\n<p>Alice is 30 years old.<br \/>\nBob is 25 years old.<br \/>\nCharlie is 35 years old.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2.map\u5bb9\u5668\u7684\u57fa\u7840\u64cd\u4f5c<\/h3>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>\u5e38\u89c1\u6210\u5458\u51fd\u6570<\/th><th><strong>\u4f5c\u7528<\/strong><\/th><th><strong>\u65f6\u95f4\u590d\u6742\u5ea6<\/strong><\/th><\/tr><\/thead><tbody><tr><td>insert({key, value})<\/td><td>\u5411 map \u4e2d\u63d2\u5165\u4e00\u4e2a\u952e-\u503c\u5bf9 &lt;key,value><\/td><td>O(log n)<\/td><\/tr><tr><td>emplace({key, value})<\/td><td>\u76f4\u63a5\u5411 map \u4e2d\u63d2\u5165\u4e00\u4e2a\u952e-\u503c\u5bf9 &lt;key,value>\uff0c\u6548\u7387\u66f4\u9ad8<\/td><td>O(log n)<\/td><\/tr><tr><td>erase(key)<\/td><td>\u5220\u9664 map \u4e2d\u6307\u5b9a\u7684\u952e-\u503c\u5bf9<\/td><td>O(log n)<\/td><\/tr><tr><td>find(key)<\/td><td>\u67e5\u627e map \u4e2d\u6307\u5b9a\u952e\u5bf9\u5e94\u7684\u952e-\u503c\u5bf9\u7684\u8fed\u4ee3\u5668<\/td><td>O(log n)<\/td><\/tr><tr><td>operator[key]<\/td><td>\u67e5\u627e map \u4e2d\u6307\u5b9a\u952e\u5bf9\u5e94\u7684\u503c<\/td><td>O(log n)<\/td><\/tr><tr><td>count(key)<\/td><td>\u67e5\u627e map \u4e2d\u952e\u7684\u6570\u91cf\uff0c\u7531\u4e8e\u952e\u662f\u552f\u4e00\u7684\uff0c\u6545\u53ea\u8fd4\u56de 0 \u6216 1<\/td><td>O(log n)<\/td><\/tr><tr><td>size()<\/td><td>\u8fd4\u56de map \u4e2d\u952e-\u503c\u5bf9\u7684\u6570\u91cf<\/td><td>O(1)<\/td><\/tr><tr><td>clear()<\/td><td>\u6e05\u7a7a map \u4e2d\u7684\u6240\u6709\u952e-\u503c\u5bf9<\/td><td>O(n)<\/td><\/tr><tr><td>empty()<\/td><td>\u5224\u65ad map \u662f\u5426\u4e3a\u7a7a<\/td><td>O(1)<\/td><\/tr><tr><td>begin()<\/td><td>\u8fd4\u56de map \u4e2d\u7b2c\u4e00\u4e2a\u952e-\u503c\u5bf9\u7684\u8fed\u4ee3\u5668<\/td><td>O(1)<\/td><\/tr><tr><td>end()<\/td><td>\u8fd4\u56de map \u4e2d\u6700\u540e\u4e00\u4e2a\u952e-\u503c\u5bf9\u7684\u4e0b\u4e00\u4e2a\u8fed\u4ee3\u5668<\/td><td>O(1)<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>\u5728\u6267\u884c\u53d6\u503c\u8fd0\u7b97\uff08[] \u8fd0\u7b97\u7b26\uff09\u65f6\uff0c\u5fc5\u987b\u786e\u4fdd\u952e\u5b58\u5728\uff0c\u5426\u5219\u53ef\u80fd\u4ea7\u751f\u9519\u8bef\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u4fdd\u8bc1\u5b89\u5168\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>map&lt;int, int> mp;          \/\/ map \u7684\u58f0\u660e\n\/\/ \u65b9\u6cd5\u4e00\uff1a\u901a\u8fc7 find() \u51fd\u6570\u5224\u65ad\uff0c\u8fd4\u56de\u952e-\u503c\u5bf9\u7684\u8fed\u4ee3\u5668\uff1b\u5982\u679c\u672a\u627e\u5230\uff0c\u8fd4\u56de end()\nif (mp.find(key) != mp.end()) {\n    cout &lt;&lt; mp&#91;key] &lt;&lt; '\\n';\n}\n\/\/ \u65b9\u6cd5\u4e8c\uff1a\u901a\u8fc7 count \u5224\u65ad\u952e\u662f\u5426\u5b58\u5728\uff0c\u63a8\u8350\nif (mp.count(key)) {\n    cout &lt;&lt; mp&#91;key] &lt;&lt; '\\n';\n}<\/code><\/pre>\n\n\n\n<p>\u904d\u5386map\u5bb9\u5668\u4e2d\u6240\u6709\u7684\u952e-\u503c\u5bf9\u6709\u4ee5\u4e0b\u4e24\u79cd\u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>map&lt;int, int> mp;\n\/\/ \u65b9\u6cd5\u4e00\uff1a\u4f7f\u7528 auto \u5173\u952e\u5b57\u8fdb\u884c\u904d\u5386\uff08\u63a8\u8350\uff09\nfor (auto &amp;i : mp) {\n    cout &lt;&lt; i.first &lt;&lt; \" \" &lt;&lt; i.second &lt;&lt; '\\n';\n}\n\/\/ \u65b9\u6cd5\u4e8c\uff1a\u4f7f\u7528\u8fed\u4ee3\u5668\u8fdb\u884c\u904d\u5386\uff08\u4e0d\u63a8\u8350\uff09\nfor (map&lt;int, int>::iterator it = mp.begin(); it != mp.end(); ++it) {\n    cout &lt;&lt; it->first &lt;&lt; \" \" &lt;&lt; it->second &lt;&lt; '\\n';\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u7ecf\u5178\u4f8b\u9898<\/h2>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001map\u5bb9\u5668\u7684\u5b9a\u4e49\u548c\u6027\u8d28 1.map\u5bb9\u5668\u7684\u5b9a\u4e49 map\u662f\u4e00\u79cd\u57fa\u4e8e\u7ea2\u9ed1\u6811\u7684\u5173\u8054\u5f0f\u5bb9\u5668\uff0c\u7528\u4e8e\u5b58\u50a8\u952e\u503c\u5bf9\uff08key- [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":151,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,1,12],"tags":[],"class_list":["post-294","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-stl-container","category-article","category-programming-algorithm-road"],"_links":{"self":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/294","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/comments?post=294"}],"version-history":[{"count":2,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions"}],"predecessor-version":[{"id":296,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/posts\/294\/revisions\/296"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/media\/151"}],"wp:attachment":[{"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/media?parent=294"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/categories?post=294"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jiangqvweihuan.cn\/index.php\/wp-json\/wp\/v2\/tags?post=294"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}