std::lower_bound
STD:较低[医]定界
Defined in header | | |
---|---|---|
template< class ForwardIt, class T > ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value | (1) | |
template< class ForwardIt, class T, class Compare > ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value, Compare comp | (2) | |
返回指向范围中的第一个元素的迭代器。[first, last)
那就是不折不扣
大于%28i.e.。大于或等于%29value
...
范围[first, last)必须至少是部分有序的,即对表达式进行分区。element < value或comp(element, value)完全排序的范围符合此标准,调用std::partition...
第一个版本使用operator<为了比较元素,第二个版本使用给定的比较函数。comp...
参数
first, last | - | iterators defining the partially-ordered range to examine |
---|---|---|
value | - | value to compare the elements to |
comp | - | comparison function object (i.e. an object that satisfies the requirements of Compare) which returns true if the first argument is less than the second. The signature of the comparison function should be equivalent to the following: bool cmp(const Type1 &a, const Type2 &b The signature does not need to have const &, but the function object must not modify the objects passed to it. The type Type1 must be such that an object of type ForwardIt can be dereferenced and then implicitly converted to Type1. The type Type2 must be such that an object of type T can be implicitly converted to Type2. |
类型要求
---。
返回值
Iterator指向第一个元素,即不折不扣
比value
,或last
如果找不到这样的元素。
复杂性
执行的比较数为对数,其距离为first
和last
%28%,大部分原木
2%28最后-首%29+O%281%29比较%29。但是,对于非-RandomAccessIterator
斯,迭代器增量的数目是线性的。
可能的实施
第一版
*。
模板<类向前,类T>向前,更低[医]绑定%28 Forwardit First,Forwardit Lest,Const T&value%29{Forwardit;type Name std::iterator[医]性状<ForwardIt>*差异[医]输入COUNT,STEP;COUNT=std::距离%281,最后%29;而%28 Count>0%29{it=First;STEP=count/2;std:prep%28 it,步骤%29;if%28%2A它<值%29{First=++it;count-=Step+1;}or count=Step;}首先返回;}
第二版
模板<类向前,类T,类比较>向前低[医]绑定%28 ForwardIt First,Forwardit Lest,Const T&Value,比较comp%29{Forwardit;type Name std::iterator[医]性状<ForwardIt>*差异[医]类型计数,步骤;计数=std::距离%281,最后%29;而%28计数>0%29{it=先;步骤=计数/2;std:前进%28 it,步骤%29;if%28 comp%28%2A它的值为%29%29{First=++it;count-=Step+1;}or count=Step;}首先返回;}
例
二次
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
template<class ForwardIt, class T, class Compare=std::less<>>
ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp={})
{
// Note: BOTH type T and the type after ForwardIt is dereferenced
// must be implicitly convertible to BOTH Type1 and Type2, used in Compare.
// This is stricter than lower_bound requirement (see above)
first = std::lower_bound(first, last, value, comp
return first != last && !comp(value, *first) ? first : last;
}
int main()
{
std::vector<int> data = { 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };
auto lower = std::lower_bound(data.begin(), data.end(), 4
auto upper = std::upper_bound(data.begin(), data.end(), 4
std::copy(lower, upper, std::ostream_iterator<int>(std::cout, " ")
std::cout << '\n';
// classic binary search, returning a value only if it is present
data = { 1, 2, 4, 6, 9, 10 };
auto it = binary_find(data.cbegin(), data.cend(), 4 //< choosing '5' will return end()
if(it != data.cend())
std::cout << *it << " found at index "<< std::distance(data.cbegin(), it
return 0;
}
二次
产出:
二次
4 4 4
4 found at index 2
二次
另见
equal_range | returns range of elements matching a specific key (function template) |
---|---|
partition | divides a range of elements into two groups (function template) |
upper_bound | returns an iterator to the first element greater than a certain value (function template) |
© cppreference.com
在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。