这是二分查找(binary search)的一种版本,试图在已排序的[first,last)中寻找元素value:
如果[first,last)具有与value相等的元素(s),便返回一个迭代器,指向其中第一个元素;
如果没有这样的元素存在,便返回“假设这样的元素存在时应该出现的位置”,也就是说,它返回一个迭代器,指向第一个“不小于value”的元素;
如果value大于{first,last)内的任何一个元素,则返回last。STL源码:
template
inline ForwardIterator lower_bound(ForwardIterator first,ForwardIterator last,const T& value)
{
return _lower_bound(first,last,value,distance_type(first),iterator_category(first));
}
//forward_iterator版本
template
ForwardIterator _lower_bound(ForwardIteartor first,ForwardIterator last,const T& value,Distance*,forward_iterator_tag)
{
Distance len=0;
distacne(first,last,len);
Diatacne half;
ForwardIterator middle;
while(len>0)
{
half=len>>1; //除以2
middle=first; //迭代器必须有初始值,才能应用advance
advance(middle,half); //middle+=half; 支持所有容器迭代器的操作
if(*middle
//random_access_iterator版本
template
RandomAccessIterator _lower_bound(RandomAccessIterator first,RandomAccessIterator last,const T& value,Distance*,random_access_iterator_tag)
{
Distance len=last-first;
Diatacne half;
RandomAccessIterator middle;
while(len>0)
{
half=len>>1; //除以2
middle=first+half;
if(*middle
代码示例:
//测试lower_bound
#include
#include
#include
#include
#include
using namespace std;
int main()
{
//vector 测试---序列式容器
vector coll;
vector::iterator pos;
coll.push_back(1);
coll.push_back(1);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(5);
coll.push_back(7);
coll.push_back(9);
copy(coll.begin(),coll.end(),ostream_iterator(cout," "));//1 1 3 3 3 3 5 7 9
cout<(cout," "));
cout<(cout," "));
cout< coll1;
multiset::iterator pos1;
coll1.insert(1);
coll1.insert(3);
coll1.insert(5);
coll1.insert(7);
coll1.insert(7);
coll1.insert(7);
coll1.insert(9);
coll1.insert(9);
copy(coll1.begin(),coll1.end(),ostream_iterator(cout," "));
cout<(cout," "));
cout<(cout," "));
cout<
STL源码:
template
inline ForwardIterator upper_bound(ForwardIterator first,ForwardIterator last,const T& value)
{
return _upper_bound(first,last,value,distance_type(first),iterator_category(first));
}
//forward_iterator版本
template
ForwardIterator _upper_bound(ForwardIteartor first,ForwardIterator last,const T& value,Distance*,forward_iterator_tag)
{
Distance len=0;
distacne(first,last,len);
Diatacne half;
ForwardIterator middle;
while(len>0)
{
half=len>>1; //除以2
middle=first; //迭代器必须有初始值,才能应用advance
advance(middle,half); //middle+=half; 支持所有容器迭代器的操作
if(value<*middle)
{
len=half;
}
else
{
first=middle;
++first;
len=len-half-1;
}
}
return first;
}
//random_access_iterator版本
template
RandomAccessIterator _upper_bound(RandomAccessIterator first,RandomAccessIterator last,const T& value,Distance*,random_access_iterator_tag)
{
Distance len=last-first;
Diatacne half;
RandomAccessIterator middle;
while(len>0)
{
half=len>>1; //除以2
middle=first+half;
if(value<*middle)
{
len=half;
}
else
{
first=middle+1;
len=len-half-1;
}
}
return first;
}
//测试upper_bound
#include
#include
#include
#include
#include
using namespace std;
int main()
{
//vector 测试---序列式容器
vector coll;
vector::iterator pos;
coll.push_back(1);
coll.push_back(1);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(5);
coll.push_back(7);
coll.push_back(9);
copy(coll.begin(),coll.end(),ostream_iterator(cout," "));//1 1 3 3 3 3 5 7 9
cout<(cout," "));
cout<(cout," "));
cout< coll1;
multiset::iterator pos1;
coll1.insert(1);
coll1.insert(3);
coll1.insert(5);
coll1.insert(7);
coll1.insert(7);
coll1.insert(7);
coll1.insert(9);
coll1.insert(9);
copy(coll1.begin(),coll1.end(),ostream_iterator(cout," "));
cout<(cout," "));
cout<(cout," "));
cout<
STL源码:
template
bool binary_search(ForwardIterator first,ForwardIterator last,const T& value)
{
ForwardIterator i=lower_bound(first,last,value);
return i!=last && !(value<*i);
}
代码示例:
#include
#include
#include
using namespace std;
int main()
{
vector coll;
vector::iterator pos;
coll.push_back(1);
coll.push_back(1);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(3);
coll.push_back(5);
coll.push_back(7);
coll.push_back(9);
if(binary_search(coll.begin(),coll.end(),2))
{
cout<<"value Found!"<