{"id":20609,"date":"2023-06-02T00:20:00","date_gmt":"2023-06-01T16:20:00","guid":{"rendered":"https:\/\/deepinout.com\/?p=20609"},"modified":"2023-06-03T07:03:36","modified_gmt":"2023-06-02T23:03:36","slug":"g_unordered_set-empty-function-in-c-stl","status":"publish","type":"post","link":"https:\/\/deepinout.com\/cpp\/cpp-tutorials\/g_unordered_set-empty-function-in-c-stl.html","title":{"rendered":"C++ STL\u4e2d\u7684unordered_set empty()\u51fd\u6570"},"content":{"rendered":"<h1>C++ STL\u4e2d\u7684unordered_set empty()\u51fd\u6570<\/h1>\n<p>unordered_set\u662f<a href=\"https:\/\/deepinout.com\/cpp\/cpp-top-tutorials\/1696211102_j_cpp-tutorial.html\" data-internallinksmanager029f6b8e52c=\"21\" title=\"C++ \u6559\u7a0b\" target=\"_blank\" rel=\"noopener\">C++<\/a> STL\u4e2d\u7684\u4e00\u4e2a\u5bb9\u5668\uff0c\u5b83\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5177\u6709\u5f88\u597d\u7684\u6548\u7387\u3002unordered_set\u652f\u6301\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u8fd8\u6709\u5f88\u591a\u5176\u4ed6\u7684\u6210\u5458\u51fd\u6570\u3002\u4eca\u5929\u6211\u4eec\u6765\u8bb2\u4e00\u4e0b\u5176\u4e2d\u7684\u4e00\u4e2a\u51fd\u6570\u2014\u2014empty()\u3002<\/p>\n<h2>empty()\u51fd\u6570\u7684\u8bf4\u660e<\/h2>\n<p>empty()\u51fd\u6570\u662f\u7528\u6765\u5224\u65ad\u4e00\u4e2aunordered_set\u5bb9\u5668\u662f\u5426\u4e3a\u7a7a\uff0c\u5982\u679c\u4e3a\u7a7a\u5219\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002\u51fd\u6570\u7684\u7528\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">bool empty() const noexcept;\n<\/code><\/pre>\n<h2>empty()\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801<\/h2>\n<p>\u4e0b\u9762\u6211\u4eec\u6765\u770b\u4e00\u4e0bempty()\u51fd\u6570\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-cpp line-numbers\">#include &lt;iostream&gt;\n#include &lt;unordered_set&gt;\n\nint main()\n{\n    std::unordered_set&lt;int&gt; mySet;\n\n    std::cout &lt;&lt; \"mySet is \" &lt;&lt; (mySet.empty() ? \"empty\" : \"not empty\") &lt;&lt; std::endl;\n\n    mySet.insert(1);\n\n    std::cout &lt;&lt; \"mySet is \" &lt;&lt; (mySet.empty() ? \"empty\" : \"not empty\") &lt;&lt; std::endl;\n\n    return 0;\n}\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u521b\u5efa\u4e86\u4e00\u4e2a\u7a7a\u7684unordered_set\uff0c\u7136\u540e\u901a\u8fc7\u8c03\u7528empty()\u51fd\u6570\u6765\u5224\u65ad\u5b83\u662f\u5426\u4e3a\u7a7a\uff0c\u7a0b\u5e8f\u7684\u8f93\u51fa\u7ed3\u679c\u4e3a\uff1a<\/p>\n<pre><code class=\"language-bash line-numbers\">mySet is empty\nmySet is not empty\n<\/code><\/pre>\n<p>\u53ef\u4ee5\u770b\u5230\uff0c\u5f53unordered_set\u4e3a\u7a7a\u65f6\uff0cempty()\u51fd\u6570\u8fd4\u56detrue\uff0c\u5426\u5219\u8fd4\u56defalse\u3002<\/p>\n<p>\u63a5\u7740\uff0c\u6211\u4eec\u5411unordered_set\u4e2d\u63d2\u5165\u4e00\u4e2a\u5143\u7d20\uff0c\u91cd\u65b0\u8c03\u7528empty()\u51fd\u6570\uff0c\u53ef\u4ee5\u53d1\u73b0\u6b64\u65f6\u8fd4\u56de\u7684\u662ffalse\u3002<\/p>\n<h2>empty()\u51fd\u6570\u7684\u6ce8\u610f\u4e8b\u9879<\/h2>\n<ul>\n<li>empty()\u51fd\u6570\u7684\u8fd4\u56de\u503c\u662f\u4e00\u4e2abool\u7c7b\u578b\uff0c\u56e0\u6b64\u53ef\u4ee5\u76f4\u63a5\u4f5c\u4e3a\u5224\u65ad\u6761\u4ef6\u6765\u4f7f\u7528\uff0c\u800c\u4e0d\u9700\u8981\u8fdb\u884c\u989d\u5916\u7684\u5904\u7406\u3002<\/p>\n<\/li>\n<li>\n<p>\u5f53unordered_set\u4e3a\u7a7a\u65f6\uff0c\u8c03\u7528empty()\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u5e38\u6570\u7ea7\u522b\uff0c\u5373O(1)\u3002<\/p>\n<\/li>\n<li>\n<p>\u5728\u4f7f\u7528empty()\u51fd\u6570\u4e4b\u524d\uff0c\u9700\u8981\u5148\u521b\u5efa\u597dunordered_set\u5bf9\u8c61\u3002<\/p>\n<\/li>\n<\/ul>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u5728<a href=\"https:\/\/deepinout.com\/cpp\/cpp-top-tutorials\/1696211102_j_cpp-tutorial.html\" data-internallinksmanager029f6b8e52c=\"21\" title=\"C++ \u6559\u7a0b\" target=\"_blank\" rel=\"noopener\">C++<\/a> STL\u4e2d\uff0cempty()\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u5b9e\u7528\u7684\u51fd\u6570\uff0c\u7279\u522b\u662f\u5728\u5224\u65ad\u5bb9\u5668\u662f\u5426\u4e3a\u7a7a\u65f6\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u4f7f\u7528empty()\u51fd\u6570\u6765\u4ee3\u66ff\u65e0\u804a\u7684size()>0\u5224\u65ad\u8bed\u53e5\uff0c\u8ba9\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\u6613\u61c2\u3002\u800c\u4e14\uff0c\u7531\u4e8eempty()\u51fd\u6570\u7684\u65f6\u95f4\u590d\u6742\u5ea6\u4e3a\u5e38\u6570\u7ea7\u522b\uff0c\u56e0\u6b64\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u4e5f\u5177\u5907\u66f4\u597d\u7684\u6027\u80fd\u8868\u73b0\u3002\u56e0\u6b64\uff0cempty()\u51fd\u6570\u662f\u6211\u4eec\u5728\u4f7f\u7528unordered_set\u5bb9\u5668\u65f6\u9700\u8981\u719f\u7ec3\u638c\u63e1\u5e76\u8fd0\u7528\u7684\u51fd\u6570\u4e4b\u4e00\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>C++ STL\u4e2d\u7684unordered_set empty()\u51fd\u6570 unordered_set\u662fC++ STL\u4e2d\u7684\u4e00\u4e2a\u5bb9\u5668\uff0c\u5b83\u662f\u54c8\u5e0c\u8868\u7684\u4e00\u79cd\u5b9e\u73b0\uff0c\u5728\u5904\u7406\u5927\u91cf\u6570\u636e\u65f6\u5177\u6709\u5f88\u597d\u7684\u6548\u7387\u3002unordered_set\u652f\u6301\u589e\u5220\u6539\u67e5\u7b49\u64cd\u4f5c\uff0c\u9664\u6b64\u4e4b\u5916\uff0c\u5b83\u8fd8\u6709\u5f88 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[467],"tags":[],"_links":{"self":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts\/20609"}],"collection":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/comments?post=20609"}],"version-history":[{"count":0,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/posts\/20609\/revisions"}],"wp:attachment":[{"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/media?parent=20609"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/categories?post=20609"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/deepinout.com\/wp-json\/wp\/v2\/tags?post=20609"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}