{"id":551,"date":"2024-09-27T10:19:26","date_gmt":"2024-09-27T02:19:26","guid":{"rendered":"http:\/\/114.55.108.251\/?p=551"},"modified":"2024-12-26T15:36:44","modified_gmt":"2024-12-26T07:36:44","slug":"c%e8%af%ad%e6%b3%95%e9%80%9f%e6%9f%a5","status":"publish","type":"post","link":"https:\/\/guapicoding.com\/?p=551","title":{"rendered":"C++\u8bed\u6cd5\u901f\u67e5"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">map<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b0\u65e7\u4e24\u79cd\u65b9\u5f0f\u904d\u5386map<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>unordered_map&lt;char, int&gt; mp;\nfor(const auto &amp;mypair : mp) {     \n    cout &lt;&lt; mypair.first &lt;&lt; \" \" &lt;&lt; mypair.second &lt;&lt; endl;\n}\n\u6216\u8005\nfor(const auto &amp;&#91;name, age] : mp) {\n    cout &lt;&lt; name &lt;&lt; \": \" &lt;&lt; age &lt;&lt; endl;  \n}<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>unordered_map&lt;char, int&gt; mp;\nfor(unordered_map&lt;char, int&gt;::iterator it = mp.begin(); it != mp.end(); ++it) {\n    cout &lt;&lt; \"Key: \" &lt;&lt; it-&gt;first &lt;&lt; \", Value: \" &lt;&lt; it-&gt;second &lt;&lt; endl;\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">count\u4e0efind\u51fd\u6570<\/h4>\n\n\n\n<p><code>count()<\/code>\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6307\u5b9akey\u7684\u6570\u91cf\uff08\u6ce8\u610f\u4e0d\u662f\u8fd4\u56devalue\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728unordered_map\u91cc\u53ea\u80fd\u8fd4\u56de0\u62161\uff0c\u800c\u5728multi_map\u91cc\u53ef\u4ee5\u8fd4\u56de\u5176\u5b83\u6570\u503c<\/p>\n\n\n\n<p><code>find()<\/code>\u51fd\u6570\u7528\u4e8e\u5224\u65ad\u6307\u5b9akey\u662f\u5426\u5b58\u5728\uff0c\u8fd4\u56de\u4e00\u4e2a\u6307\u5411\u8be5\u5143\u7d20\u7684\u8fed\u4ee3\u5668\uff0c\u5982\u679c\u672a\u627e\u5230\uff0c\u5219\u8fd4\u56de\u6307\u5411 <code>map::end()<\/code> \u7684\u8fed\u4ee3\u5668 <\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">set<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f7f\u7528vector\u6784\u9020set<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>vector&lt;int&gt; nums = {1, 2, 2};\nset&lt;int&gt; st(nums.begin(), nums.end()); \/\/\u591a\u7528\u4e8e\u53bb\u91cd,\u4e14\u4f1a\u81ea\u52a8\u5347\u5e8f\u6392\u5e8f\nvector&lt;int&gt; v(st.begin(), st.end()); \/\/\u53bb\u5b8c\u91cd\u518d\u8fd4\u56devector\uff0c\u4fbf\u4e8e\u7528\u4f20\u7edffor\u5faa\u73af<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">priority_queue<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u521b\u5efa\u4f18\u5148\u961f\u5217<\/h4>\n\n\n\n<p><code>priority_queue<\/code> \u9ed8\u8ba4\u662f\u6700\u5927\u5806\uff08\u5373\u4f18\u5148\u7ea7\u6700\u9ad8\u7684\u5143\u7d20\u5728\u9876\u90e8\uff09\uff1a<\/p>\n\n\n\n<p><code>priority_queue&lt;int&gt; pq; \/\/ \u521b\u5efa\u4e00\u4e2a\u9ed8\u8ba4\u7684\u6700\u5927\u5806<\/code><\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528 <code>std::greater<\/code> \u4f5c\u4e3a\u6bd4\u8f83\u5668\u6765\u521b\u5efa\u6700\u5c0f\u5806\uff1a<\/p>\n\n\n\n<p><code>priority_queue&lt;int, vector&lt;int&gt;, greater&lt;int&gt;&gt; minHeap;<\/code><\/p>\n\n\n\n<p>ps\uff1a<code>vector&lt;int&gt;<\/code> \u662f <code>priority_queue<\/code> \u7684\u7b2c\u4e8c\u4e2a\u6a21\u677f\u53c2\u6570\uff0c\u6307\u5b9a\u4e86\u5e95\u5c42\u5bb9\u5668\u7684\u7c7b\u578b\u3002 <code>priority_queue<\/code> \u9700\u8981\u4e00\u4e2a\u5e95\u5c42\u5bb9\u5668\u6765\u5b58\u50a8\u5143\u7d20\uff0c\u5176\u4ed6\u53ef\u80fd\u7684\u9009\u9879\u8fd8\u5305\u62ec <code>deque<\/code> \u6216 <code>list<\/code>\uff0c\u4f46 <code>vector<\/code> \u901a\u5e38\u662f\u6700\u5e38\u7528\u7684\u9009\u62e9\uff0c\u56e0\u4e3a\u5b83\u5728\u5185\u5b58\u4f7f\u7528\u548c\u968f\u673a\u8bbf\u95ee\u6027\u80fd\u4e0a\u8868\u73b0\u8f83\u597d\u3002<\/p>\n\n\n\n<p><code><br><\/code>\u5982\u679c\u8981\u521b\u5efa\u7684\u4f18\u5148\u961f\u5217\u5143\u7d20\u6bd4\u8f83\u7279\u6b8a\uff0c\u6bd4\u5982\u94fe\u8868\u8282\u70b9\u6307\u9488\uff0c\u5c31\u8981\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668\u4e86\ud83d\udc47<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>struct Comparator {  \n    bool operator()(ListNode *a,ListNode *b) {\n        return a-&gt;val &gt; b-&gt;val; \/\/\u4ee3\u8868\u5c0f\u6839\u5806\uff0c\u56e0\u4e3a\u5982\u679c\u8fd4\u56detrue\uff0c\u5c31\u8868\u793a\u5de6\u4fa7\u5143\u7d20\u7684\u4f18\u5148\u7ea7\u4f4e\u4e8e\u53f3\u4fa7\u5143\u7d20(\u6709\u70b9\u53cd\u76f4\u89c9)\n    }\n};\n\npriority_queue&lt;ListNode*, vector&lt;ListNode* &gt;, Comparator&gt; queue;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u51fd\u6570\u539f\u578b<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>pq.push(10); \/\/\u63d2\u5165\u5143\u7d20\npq.top(); \/\/\u8bbf\u95ee\u9876\u90e8\u5143\u7d20\npq.pop(); \/\/\u5220\u9664\u9876\u90e8\u5143\u7d20\npq.empty(); \/\/\u68c0\u67e5\u4f18\u5148\u961f\u5217\u662f\u5426\u4e3a\u7a7a\npq.size(); \/\/\u83b7\u53d6\u4f18\u5148\u961f\u5217\u4e2d\u5143\u7d20\u7684\u6570\u91cf<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">functions<\/h2>\n\n\n\n<h4 class=\"wp-block-heading\">\u65b0\u65e7\u4e24\u79cdsort\u51fd\u6570<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>string str = \"bac\";\nranges::sort(str); \/\/\u6309\u5b57\u5178\u987a\u5e8f\u6392\u5e8f\uff0cstr = \"abc\"\nranges::sort(str, greater());  \/\/\u964d\u5e8f <\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>string str = \"bac\";\nsort(str.begin(), str.end()); \/\/\u540c\u4e0a\uff0c\u5982\u679c\u662fint\uff0c\u5219\u6309\u5347\u5e8f\u6392\u5217 <\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">ranges<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>ranges::max(v); \/\/v\u662fvector\u6570\u7ec4\nranges::sort(str);<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>map \u65b0\u65e7\u4e24\u79cd\u65b9\u5f0f\u904d\u5386map count\u4e0efind\u51fd\u6570 count()\u51fd\u6570\u8fd4\u56de\u7684\u662f\u6307\u5b9akey\u7684\u6570\u91cf\uff08\u6ce8\u610f\u4e0d\u662f [&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],"tags":[],"class_list":["post-551","post","type-post","status-publish","format-standard","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/551","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=551"}],"version-history":[{"count":30,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions"}],"predecessor-version":[{"id":2196,"href":"https:\/\/guapicoding.com\/index.php?rest_route=\/wp\/v2\/posts\/551\/revisions\/2196"}],"wp:attachment":[{"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=551"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=551"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guapicoding.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=551"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}