std::lower_bound
定義於標頭檔案 <algorithm> |
||
(1) | ||
template< class ForwardIt, class T > ForwardIt lower_bound( ForwardIt first, ForwardIt last, |
(C++20 起為 constexpr) (直到 C++26) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type > |
(C++26 起) | |
(2) | ||
template< class ForwardIt, class T, class Compare > ForwardIt lower_bound( ForwardIt first, ForwardIt last, |
(C++20 起為 constexpr) (直到 C++26) |
|
template< class ForwardIt, class T = typename std::iterator_traits <ForwardIt>::value_type, |
(C++26 起) | |
在分割槽範圍 [
first,
last)
中搜索第一個不排在 value 之前的元素。
返回 如果 |
(C++20 前) |
等價於 std::lower_bound(first, last, value, std::less{})。 |
(C++20 起) |
[
first,
last)
中第一個迭代器 iter,使得 bool(comp(*iter, value)) 為 false,如果不存在這樣的 iter,則返回 last。目錄 |
[編輯] 引數
first, last | - | 定義要檢查的元素已分割槽範圍的迭代器對。 |
value | - | 用於比較元素的數值 |
comp | - | 二元謂詞,如果第一個引數排在第二個引數之前,則返回true。 謂詞函式的簽名應等效於以下內容: bool pred(const Type1 &a, const Type2 &b); 雖然簽名不需要有 const &,但函式不得修改傳遞給它的物件,並且必須能夠接受 |
型別要求 | ||
-ForwardIt 必須滿足 LegacyForwardIterator 的要求。 | ||
-Compare 必須滿足 二元謂詞的要求。它不需要滿足 比較的要求。 |
[編輯] 返回值
指向範圍 [
first,
last)
中第一個不排在 value 之前的元素的迭代器,如果未找到此類元素,則為 last。
[編輯] 複雜度
給定 N 作為 std::distance(first, last)
然而,如果 ForwardIt
不是 舊式隨機訪問迭代器,則迭代器增量的數量與 N 成線性關係。值得注意的是,std::map、std::multimap、std::set 和 std::multiset 迭代器不是隨機訪問的,因此應優先使用它們的成員 lower_bound
函式。
[編輯] 可能的實現
lower_bound (1) |
---|
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type> ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value) { return std::lower_bound(first, last, value, std::less{}); } |
lower_bound (2) |
template<class ForwardIt, class T = typename std::iterator_traits<ForwardIt>::value_type, class Compare> ForwardIt lower_bound(ForwardIt first, ForwardIt last, const T& value, Compare comp) { ForwardIt it; typename std::iterator_traits<ForwardIt>::difference_type count, step; count = std::distance(first, last); while (count > 0) { it = first; step = count / 2; std::advance(it, step); if (comp(*it, value)) { first = ++it; count -= step + 1; } else count = step; } return first; } |
[編輯] 注意
儘管 std::lower_bound
只要求 [
first,
last)
是分割槽的,但此演算法通常用於 [
first,
last)
已排序的情況,以便二分搜尋對任何 value 都有效。
與 std::binary_search 不同,std::lower_bound
不要求 operator< 或 comp 是非對稱的(即 a < b 和 b < a 總是產生不同的結果)。實際上,它甚至不要求 value < *iter 或 comp(value, *iter) 對於 [
first,
last)
中的任何迭代器 iter 都是良好形式的。
特性測試宏 | 值 | 標準 | 特性 |
---|---|---|---|
__cpp_lib_algorithm_default_value_type |
202403 |
(C++26) | 演算法的列表初始化 (1,2) |
[編輯] 示例
#include <algorithm> #include <cassert> #include <complex> #include <iostream> #include <vector> struct PriceInfo { double price; }; int main() { const std::vector<int> data{1, 2, 4, 5, 5, 6}; for (int i = 0; i < 8; ++i) { // Search for first element x such that i ≤ x auto lower = std::lower_bound(data.begin(), data.end(), i); std::cout << i << " ≤ "; lower != data.end() ? std::cout << *lower << " at index " << std::distance(data.begin(), lower) : std::cout << "not found"; std::cout << '\n'; } std::vector<PriceInfo> prices{{100.0}, {101.5}, {102.5}, {102.5}, {107.3}}; for (const double to_find : {102.5, 110.2}) { auto prc_info = std::lower_bound(prices.begin(), prices.end(), to_find, [](const PriceInfo& info, double value) { return info.price < value; }); prc_info != prices.end() ? std::cout << prc_info->price << " at index " << prc_info - prices.begin() : std::cout << to_find << " not found"; std::cout << '\n'; } using CD = std::complex<double>; std::vector<CD> nums{{1, 0}, {2, 2}, {2, 1}, {3, 0}}; auto cmpz = [](CD x, CD y) { return x.real() < y.real(); }; #ifdef __cpp_lib_algorithm_default_value_type auto it = std::lower_bound(nums.cbegin(), nums.cend(), {2, 0}, cmpz); #else auto it = std::lower_bound(nums.cbegin(), nums.cend(), CD{2, 0}, cmpz); #endif assert((*it == CD{2, 2})); }
輸出
0 ≤ 1 at index 0 1 ≤ 1 at index 0 2 ≤ 2 at index 1 3 ≤ 4 at index 2 4 ≤ 4 at index 2 5 ≤ 5 at index 3 6 ≤ 6 at index 5 7 ≤ not found 102.5 at index 2 110.2 not found
[編輯] 缺陷報告
下列更改行為的缺陷報告追溯地應用於以前出版的 C++ 標準。
缺陷報告 | 應用於 | 釋出時的行為 | 正確的行為 |
---|---|---|---|
LWG 270 | C++98 | Compare 需要滿足 比較,且 T 需要是 可小於比較的(需要嚴格弱序) |
只需要分割槽; 允許異構比較 |
LWG 384 | C++98 | 最多允許 log(N)+1 次比較 | 更正為 log2(N)+1 |
LWG 2150 | C++98 | 如果 [ first, last) 中存在任何迭代器 iter,使得bool(comp(*iter, value)) 為 false,則 std::lower_bound 可以返回 [ iter, last) 中的任何迭代器 |
不能返回 iter 之後的迭代器 |
[編輯] 另請參閱
返回與特定鍵匹配的元素範圍 (函式模板) | |
將一個範圍的元素分成兩組 (函式模板) | |
(C++11) |
定位一個已劃分範圍的劃分點 (函式模板) |
返回一個指向第一個大於某個值的元素的迭代器 (函式模板) | |
返回指向第一個不小於給定鍵的元素的迭代器 ( std::set<Key,Compare,Allocator> 的公共成員函式) | |
返回指向第一個不小於給定鍵的元素的迭代器 ( std::multiset<Key,Compare,Allocator> 的公共成員函式) | |
(C++20) |
返回一個指向第一個不小於給定值的元素的迭代器 (演算法函式物件) |