{"id":1660,"date":"2026-05-30T01:09:14","date_gmt":"2026-05-29T17:09:14","guid":{"rendered":"http:\/\/wordpress.fangt.online\/?p=1660"},"modified":"2026-05-30T01:09:14","modified_gmt":"2026-05-29T17:09:14","slug":"%e7%ae%97%e6%b3%95%e5%ba%93%ef%bc%9a%e4%ba%8c%e5%88%86%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"http:\/\/wordpress.fangt.online\/index.php\/2026\/05\/30\/%e7%ae%97%e6%b3%95%e5%ba%93%ef%bc%9a%e4%ba%8c%e5%88%86%e5%87%bd%e6%95%b0\/","title":{"rendered":"\u7b97\u6cd5\u5e93\uff1a\u4e8c\u5206\u51fd\u6570"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u6240\u6709\u51fd\u6570\u8981\u6c42\u5e8f\u5217<strong>\u5df2\u6309\u5347\u5e8f\u6392\u5e8f<\/strong>\uff08\u6216\u6309\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\u6392\u5e8f\uff09\u3002<br>\u5934\u6587\u4ef6\uff1a<code>#include &lt;algorithm&gt;<\/code><br>\u65f6\u95f4\u590d\u6742\u5ea6\uff1a<strong>O(log n)<\/strong>\uff08\u968f\u673a\u8bbf\u95ee\u8fed\u4ee3\u5668\uff0c\u5982 <code>vector<\/code>\/<code>array<\/code>\/<code>deque<\/code>\uff1b<code>list<\/code> \u4e0d\u652f\u6301\uff09<\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u56db\u79cd\u6838\u5fc3\u51fd\u6570\u5bf9\u6bd4<\/h2>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>\u51fd\u6570<\/th><th>\u8fd4\u56de\u503c<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>lower_bound(beg, end, val)<\/code><\/td><td>\u7b2c\u4e00\u4e2a <strong>\u2265 val<\/strong> \u7684\u4f4d\u7f6e<\/td><td>\u627e\u4e0b\u754c<\/td><\/tr><tr><td><code>upper_bound(beg, end, val)<\/code><\/td><td>\u7b2c\u4e00\u4e2a <strong>&gt; val<\/strong> \u7684\u4f4d\u7f6e<\/td><td>\u627e\u4e0a\u754c<\/td><\/tr><tr><td><code>binary_search(beg, end, val)<\/code><\/td><td><code>bool<\/code> \u662f\u5426\u5b58\u5728<\/td><td>\u5355\u7eaf\u5224\u65ad<\/td><\/tr><tr><td><code>equal_range(beg, end, val)<\/code><\/td><td><code>pair&lt;\u8fed\u4ee3\u5668, \u8fed\u4ee3\u5668&gt;<\/code><\/td><td>\u7b49\u4e8e val \u7684\u8303\u56f4 <code>[lower, upper)<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001<code>lower_bound<\/code> \u2014 \u7b2c\u4e00\u4e2a\u4e0d\u5c0f\u4e8e val \u7684\u4f4d\u7f6e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>auto it = lower_bound(v.begin(), v.end(), val);\n\/\/ \u7528\u6cd5\u793a\u4f8b\nvector&lt;int&gt; v = {1,3,5,7,9};\nauto it = lower_bound(v.begin(), v.end(), 5);  \/\/ \u6307\u5411 5\uff08\u7d22\u5f152\uff09\nauto it2 = lower_bound(v.begin(), v.end(), 6); \/\/ \u6307\u5411 7\uff08\u7d22\u5f153\uff09<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001<code>upper_bound<\/code> \u2014 \u7b2c\u4e00\u4e2a\u5927\u4e8e val \u7684\u4f4d\u7f6e<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>auto it = upper_bound(v.begin(), v.end(), val);\n\/\/ \u793a\u4f8b\nauto it = upper_bound(v.begin(), v.end(), 5);  \/\/ \u6307\u5411 7\uff08\u7d22\u5f153\uff09\nauto it2 = upper_bound(v.begin(), v.end(), 7); \/\/ \u6307\u5411 9\uff08\u7d22\u5f154\uff09<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001<code>binary_search<\/code> \u2014 \u5224\u65ad\u662f\u5426\u5b58\u5728<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>bool found = binary_search(v.begin(), v.end(), val);\n\/\/ \u793a\u4f8b\nif (binary_search(v.begin(), v.end(), 5)) \n    cout &lt;&lt; \"\u5b58\u5728\";<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001<code>equal_range<\/code> \u2014 \u83b7\u53d6\u76f8\u7b49\u5143\u7d20\u7684\u533a\u95f4<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>auto &#91;l, r] = equal_range(v.begin(), v.end(), val);\n\/\/ \u6216 pair&lt;iterator, iterator&gt; p = equal_range(...);\n\/\/ l = lower_bound, r = upper_bound<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u81ea\u5b9a\u4e49\u6bd4\u8f83\uff08\u964d\u5e8f\u6216\u7ed3\u6784\u4f53\uff09<\/h2>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p class=\"wp-block-paragraph\">\u9700\u8981\u4fdd\u6301\u6392\u5e8f\u89c4\u5219\u4e0e\u4e8c\u5206\u89c4\u5219<strong>\u4e00\u81f4<\/strong>\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u6bd4\u8f83\u51fd\u6570\u5bf9\u8c61\u3002<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ \u964d\u5e8f\u6392\u5e8f\u7684 vector\nvector&lt;int&gt; v = {9,7,5,3,1};\n\/\/ \u5728\u964d\u5e8f\u5e8f\u5217\u4e2d\u627e\u7b2c\u4e00\u4e2a \u2264 val \u7684\u4f4d\u7f6e\uff08\u81ea\u5b9a\u4e49\uff09\nauto it = lower_bound(v.begin(), v.end(), 5, greater&lt;int&gt;());\n\n\/\/ \u7ed3\u6784\u4f53\u793a\u4f8b\nstruct Person { string name; int age; };\nvector&lt;Person&gt; ps = {{\"A\",20},{\"B\",25},{\"C\",30}};\n\/\/ \u6309 age \u5347\u5e8f\u6392\u5e8f\u540e\uff0c\u4e8c\u5206\u67e5\u627e age &gt;= 25\nauto cmp = &#91;](const Person&amp; p, int age) { return p.age &lt; age; };\nauto it = lower_bound(ps.begin(), ps.end(), 25, cmp);<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><\/h2>\n","protected":false},"excerpt":{"rendered":"<p>\u6240\u6709\u51fd\u6570\u8981\u6c42\u5e8f\u5217\u5df2\u6309\u5347\u5e8f\u6392\u5e8f\uff08\u6216\u6309\u81ea\u5b9a\u4e49\u6bd4\u8f83\u89c4\u5219\u6392\u5e8f\uff09\u3002\u5934\u6587\u4ef6\uff1a#include &lt;algorithm&#038; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61],"tags":[],"class_list":["post-1660","post","type-post","status-publish","format-standard","hentry","category-zl"],"_links":{"self":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1660","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/comments?post=1660"}],"version-history":[{"count":1,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions"}],"predecessor-version":[{"id":1661,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/posts\/1660\/revisions\/1661"}],"wp:attachment":[{"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/media?parent=1660"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/categories?post=1660"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wordpress.fangt.online\/index.php\/wp-json\/wp\/v2\/tags?post=1660"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}