{"id":580,"date":"2020-09-20T21:40:58","date_gmt":"2020-09-20T13:40:58","guid":{"rendered":"https:\/\/blog.sukasuka.cn\/?p=580"},"modified":"2023-11-06T01:24:35","modified_gmt":"2023-11-05T17:24:35","slug":"%e4%bd%bf%e7%94%a8stdset%e5%8e%bb%e9%87%8d","status":"publish","type":"post","link":"https:\/\/blog.sukasuka.cn\/?p=580","title":{"rendered":"\u4f7f\u7528std::set\u53bb\u91cd"},"content":{"rendered":"<blockquote><p>\u9996\u5148\u8c08\u8c08set\uff0c\u4ee5\u4e0b\u662f\u6765\u81eacppreference\u7684\u5185\u5bb9\uff1a<\/p>\n<p>std::set \u662f\u5173\u8054\u5bb9\u5668\uff0c\u542b\u6709 Key \u7c7b\u578b\u5bf9\u8c61\u7684\u5df2\u6392\u5e8f\u96c6\u3002\u7528\u6bd4\u8f83\u51fd\u6570 \u6bd4\u8f83 (Compare) \u8fdb\u884c\u6392\u5e8f\u3002\u641c\u7d22\u3001\u79fb\u9664\u548c\u63d2\u5165\u62e5\u6709\u5bf9\u6570\u590d\u6742\u5ea6\u3002 set \u901a\u5e38\u4ee5\u7ea2\u9ed1\u6811\u5b9e\u73b0\u3002<\/p>\n<p>\u5728\u6bcf\u4e2a\u6807\u51c6\u5e93\u4f7f\u7528\u6bd4\u8f83 (Compare) \u6982\u5ff5\u7684\u573a\u6240\uff0c\u7528\u7b49\u4ef7\u5173\u7cfb\u786e\u5b9a\u552f\u4e00\u6027\u3002\u4e0d\u7cbe\u786e\u5730\u8bf4\uff0c\u82e5\u4e8c\u4e2a\u5bf9\u8c61 a \u4e0e b \u76f8\u4e92\u95f4\u65e2\u4e0d\u6bd4\u8f83\u5927\u4e8e\u4ea6\u4e0d\u6bd4\u8f83\u5c0f\u4e8e\uff1a !comp(a, b) &amp;&amp; !comp(b, a) \uff0c\u5219\u8ba4\u4e3a\u5b83\u4eec\u7b49\u4ef7\u3002<\/p>\n<p>\u590d\u6742\u5ea6<br \/>\ninsert( value_type&amp;&amp; value )\u4ee5\u53ca\u8fed\u4ee3\u5668\u8bbf\u95ee \u4e0e\u5bb9\u5668\u5927\u5c0f\u6210\u5bf9\u6570 O(log(size()))<\/p><\/blockquote>\n<p><!--more--><\/p>\n<p>\u51fa\u4eba\u610f\u6599\uff08\u60c5\u7406\u4e4b\u4e2d\uff09\u7684\u662f\uff0c\u5982\u679c\u6211\u4eec\u53ea\u91cd\u8f7d\u5e03\u5c14\u8fd0\u7b97\u7b26==\uff0c\u662f\u65e0\u6cd5\u901a\u8fc7\u7f16\u8bd1\u7684\u3002<code>stl_function.h\t[Error] no match for 'operator&lt;' (operand types are 'const edge' and 'const edge')<\/code><\/p>\n<p>\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u9700\u8981\u91cd\u8f7d&lt;\u8fd0\u7b97\u7b26\u3002<\/p>\n<blockquote><p>C++ \u5177\u540d\u8981\u6c42\uff1a\u6bd4\u8f83 (Compare)<br \/>\n\u8981\u6c42:<\/p>\n<ol>\n<li>comp(a, b) \u53ef\u9690\u5f0f\u8f6c\u6362\u4e3a bool<\/li>\n<li>\u5efa\u7acb\u5177\u6709\u4e0b\u5217\u6027\u8d28\u7684\u4e25\u683c\u5f31\u5e8f\u5173\u7cfb:<br \/>\n\u5bf9\u4e8e\u6240\u6709 a\uff0ccomp(a,a)==false<br \/>\n\u82e5 comp(a,b)==true \u5219 comp(b,a)==false<br \/>\n\u82e5 comp(a,b)==true \u4e14 comp(b,c)==true \u5219 comp(a,c)==true<\/li>\n<\/ol>\n<\/blockquote>\n<p>\u6839\u636e\u7f51\u4e0a\u7684\u4e00\u6bb5\u5f15\u7528<\/p>\n<blockquote><p>\u6709\u4e86a&lt;b\u7684\u5b9a\u4e49\uff0c\u6211\u4eec\u5c31\u53ef\u4ee5\u7528b&lt;a\u6765\u5224a&gt;b\uff0c\u7136\u540e!(a&lt;b)\u5c31\u662fa&gt;=b\uff0c!(a&gt;b)\u5c31\u662fa&lt;=b\uff0c!(b&lt;a)&amp;&amp;!(a&lt;b)\u5c31\u662fa==b\uff0c!(a==b)\u5c31\u662fa!=b<\/p><\/blockquote>\n<p>\u4ee5\u4e0a\u524d\u56db\u79cd\u4e0d\u7b49\u5173\u7cfb\uff0c\u77e5\u4e00\u63a8\u5269\u4e0b\u7684\uff0c\u81f3\u4e8e\u4e3a\u4ec0\u4e48\u8981\u91cd\u8f7d\u5c0f\u4e8e<\/p>\n<blockquote><p>hyv 2020\/9\/20 21:37:11<br \/>\n\u4e3a\u4ec0\u4e48\u8981\u7528\u5c0f\u4e8e\uff0c\u5927\u6982\u662f\u4e2a\u89c4\u5b9a\u5427<\/p><\/blockquote>\n<p><del>\u6216\u8bb8rbtree\u9700\u8981\u4e00\u4e2a&lt;<\/del><\/p>\n<div class=\"hcb_wrap\">\n<pre><code class=\"language-cpp\">struct edge{\r\n    int u,v,w;\r\n    bool operator &lt;(const edge&amp; b)const{\r\n        if(this-&gt;u==b.u&amp;&amp;this-&gt;v==b.v&amp;&amp;this-&gt;w==b.w)return 0;\r\n        return 1;\r\n    }\r\n};\r\nset&lt;edge&gt;g;\r\nset&lt;edge&gt;::iterator pt;\r\nint n,m,x,y,z;\r\nint main(){\r\n    cin&gt;&gt;n&gt;&gt;m;\r\n    for(int i=1;i&lt;=m;i++){\r\n        cin&gt;&gt;x&gt;&gt;y&gt;&gt;z;\r\n        g.insert(edge{x,y,z});\r\n    }\r\n    for(pt=g.begin();pt!=g.end();pt++)\r\n        cout&lt;&lt;pt-&gt;u&lt;&lt;\" \"&lt;&lt;pt-&gt;v&lt;&lt;\" \"&lt;&lt;pt-&gt;w&lt;&lt;endl;\r\n    return 0;\r\n}<\/code><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u9996\u5148\u8c08\u8c08set\uff0c\u4ee5\u4e0b\u662f\u6765\u81eacppreference\u7684\u5185\u5bb9\uff1a std::set \u662f\u5173\u8054\u5bb9\u5668\uff0c\u542b\u6709 Key \u7c7b\u578b\u5bf9 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1085,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-580","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=580"}],"version-history":[{"count":5,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/580\/revisions"}],"predecessor-version":[{"id":1871,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/posts\/580\/revisions\/1871"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=\/wp\/v2\/media\/1085"}],"wp:attachment":[{"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.sukasuka.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}