C++ STL:lower_bound与upper_bound实现
lower_bound
lower_bound(begin, end, target)用来查找一个已排序的序列中[begin, end)第一个大于等于target的元素index。数组A如下:
value: 1, 2, 2, 3, 4, 5, 5, 6, 7
index: 0, 1, 2, 3, 4, 5, 6, 7, 8
这样的一个序列,如果查找5的lower_bound,返回的应该是第一个5即A[5]。下面是摘自cplusplus.com上的lower_bound代码
template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>)
{
it = first; step=count/; advance (it,step);
if (*it < val) { // or: if (comp(*it,val)), for version (2)
first = ++it;
count -= step+;
}
else count = step;
}
return first;
}
如果搜索对象只是数组的话还可以再简化一点:
count = last - start;
while (count > ) {
step = count/;
int* it = first + step;
if (*it < target) {
count = count - (step + );
first = it + ;
} else {
count=step;
}
}
return first;

基本情况: 当输入只有一个元素时,而该元素不是要查找的元素时返回last,即该元素的后一个位置
case: target=4
当在上图中的数组中找4的lower_bound时,第一次*it取到的值是4,因为这不是简单的二分搜索,而是要返回第一大于等于查找元素的位置,所以搜索不能在此时结束。但是可以确定5~7这一部分可以不用搜索了,因为当前至少有一个元素即*it是大于等于4了,因而缩小查找范围(count=step)。这个查找范围并不包括已找到的4,为什么是这样?分情况讨论:
1. 当前面的这个范围没有符合条件的数时,就会将范围最后的位置的后一位置返回,而此位置正好是4所在的位置(*it>= target时it所在的位置,它是符合查找条件的),其正好是lower_bound。
2. 当前面的这个方位含有符合条件的数时,此时当前的这个4就不是lower_bound,真正的lower_bound会在该区间内产生
case: target=5
当求5的lower_bound时,第一次找到中间元素时4,4<5,所以4和4前面的所有都不会含有5的lower_bound,因而下一次搜索只会在5~7这个区间进行,这个就和一个全新的问题一样了。
upper_bound
upper_bound用来在[begin, end)中找到第一个大于target的index
template <class ForwardIterator, class T>
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = std::distance(first,last);
while (count>)
{
it = first; step=count/; std::advance (it,step);
if (!(val<*it)) // or: if (!comp(val,*it)), for version (2)
{ first=++it; count-=step+; }
else count=step;
}
return first;
}
简化版本:
lower_bound:
int lo = , hi = n;
// lower_bound
while (lo < hi) {
int mid = (lo + hi) / ;
if (A[mid] < target) {
lo = mid + ;
} else {
hi = mid;
}
} return lo;
upper_bound:
lo = , hi = n;
// upper_bound
while (lo < hi) {
int mid = (lo + hi) / ;
if (A[mid] <= target) {
lo = mid + ;
} else {
hi = mid;
}
}
return lo;
就在判断条件上多了个等号
C++ STL:lower_bound与upper_bound实现的更多相关文章
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- C++ STL lower_bound()和upper_bound()
lower_bound()和upper_bound()用法 1.在数组上的用法 假设a是一个递增数组,n是数组长度,则 lower_bound(a, a+n, x):返回数组a[0]~a[n-1]中, ...
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
随机推荐
- [JS] 理解jquery的$.extend()、$.fn和$.fn.extend()
jQuery为开发插件提拱了两个方法,分别是: jQuery.fn.extend(); jQuery.extend(); jQuery.fn jQuery.fn = jQuery.prototype ...
- 考试题 T3
题意分析 首先\(\%\%\%\%olinr\)以及花_Q\(julao\)当场切题 然后就是怎么求 \[max(|a-A|,|b-B|)=max(a-A,A-a,B-b,b-B)\] 我们令\(x_ ...
- mac安装gdb调试(转载)
转载自:http://blog.plotcup.com/a/129 最近一直用go写一个项目,本想在mac上用gdb调试一下,但xcode4.6带的gdb版 本还是太低了,不支持go,只好自己安装一个 ...
- C#-WebForm-GridView表格展示数据
GrideView 控件,功能是将数据库的数据用表格的形式展示在页面上 一.<源>代码中放入 GridView 控件 打开<设计>界面 二.绑定数据源 (一)创建 LinQ 类 ...
- java连接SqlServer2012
要用java连接数据库 首先是要通过JDBC驱动 要先去下载一个sqljdbc4.jar,我这里放百度云盘了, 下载地址:链接:http://pan.baidu.com/s/1slJl89B 密码: ...
- mysql中权限的小知识
参考:https://www.cnblogs.com/apollo1616/articles/10294490.html mysql中user表中host列的值的意义 % 匹配所有主机 localho ...
- 洛谷 P2014 选课(树形背包)
洛谷 P2014 选课(树形背包) 思路 题面:洛谷 P2014 如题这种有依赖性的任务可以用一棵树表示,因为一个儿子要访问到就必须先访问到父亲.然后,本来本题所有树是森林(没有共同祖先),但是题中的 ...
- Bomb(要49)--数位dp
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total Submi ...
- VS2015 release模式下进行debug调试
有时候软件发布,又不得不调试其中的某个dll模块, 这时候就需要在发布的release版本的软件中来调试其中的dll模块了. vs2015设置: 1.Release模式下右键工作属性,选择C/C++, ...
- easyui的datagrid对应的java对象
Easyui中datagrid控件要求的数据格式为: {total:”2”,rows:[{“id”:”1”,”name”,”张三”},{“id”:”2”,”name”,”李四”}]} 所以可以建一个对 ...