徒手实现lower_bound和upper_bound
STL中lower_bound和upper_bound的使用方法:STL 二分查找
lower_bound:
int obj=;
int l=; //初始化 l ,为第一个合法地址
int r=; //初始化 r , 地址的结束地址
int mid;
while(l<r) {
mid=(l+r)/;
if(arr[mid]<obj){
l=mid+;
}else{
r=mid;
}
}
upper_bound:
int obj=;
int l=; //初始化 l ,为第一个合法地址
int r=; //初始化 r , 地址的结束地址
int mid;
while(l<r) {
mid=(l+r)/;
if(arr[mid]<=obj){
l=mid+;
}else{
r=mid;
}
}
(将上文的lower_bound的 < 替换为 <= 即可)
为便于记忆可以修改判断条件。
lower_bound:
int l=; //初始化 l ,为第一个合法地址
int r=; //初始化 r , 地址的结束地址
int mid;
while(l<r) {
mid=(l+r)/;
if(arr[mid]>=obj){
r=mid;
}else{
l=mid+;
}
}
upper_bound:
int l=; //初始化 l ,为第一个合法地址
int r=; //初始化 r , 地址的结束地址
int mid;
while(l<r) {
mid=(l+r)/;
if(arr[mid]>obj){
r=mid;
}else{
l=mid+;
}
}
徒手实现lower_bound和upper_bound的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- 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 ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- 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 就是最后一个 < ...
随机推荐
- ThreadPoolExecutor 线程池 简单解析
jdk1.8 ThreadPoolExecutor ThreadPoolExecutor实际就是java的线程池,开发常用的Executors.newxxxxx()来创建不同类型和作用的线程池,其底部 ...
- 无聊系列 - C#中一些常用类型与java的类型对应关系
昨天在那个.NET转java群里,看到一位朋友在问C#的int 对应java的哪个对象,就心血来潮,打算写一下C#中一些基础性的东西,在java中怎么找. 1. 基础值类型 如:int,long,do ...
- python3 消耗CPU的性能,使CPU满载(可以设置进程名称)
需要安装库:setproctitle 1.1.10,设置进程名称,测试操作系统centos 7.0 # -*- coding: utf-8 -*- from multiprocessing im ...
- 大话设计模式Python实现-备忘录模式
备忘录模式(Memento Pattern):不破坏封装性的前提下捕获一个对象的内部状态,并在该对象之外保存这个状态,这样已经后就可将该对象恢复到原先保存的状态 下面是一个备忘录模式的demo: #! ...
- Leetcode练习题Remove Element
Leetcode练习题Remove Element Question: Given an array nums and a value val, remove all instances of tha ...
- ShellScript值传递参数
Shell传递参数 ######################################摘自菜鸟教程:http://www.runoob.com/linux/linux-shell-passi ...
- Layui新手教程----帮助小白少走弯路
Layui的学习 Layui官方文档:https://www.layui.com/ 先说说为啥我接触到了layui,因为需要去参与做一个项目,被学长推荐去学习layui,用来处理一些前端的问题. La ...
- Mongodb--内存管理MMAP
MongoDB使用的是内存映射存储引擎,即Memory Mapped Storage Engine,简称MMAP. MMAP可以把磁盘文件的一部分或全部内容直接映射到内存,这样文件中的信息位置就会在内 ...
- 也作一下装配脑袋的Expression习题【转】
一.习题 http://www.cnblogs.com/Ninputer/archive/2009/08/28/expression_tree1.html 二.参考 http://msdn.micro ...
- docker命令之link
1.新建两台容器,第二台(busybox_2)link到第一台(busybox_1) [root@localhost ~]# docker run -d -it --name busybox_1 bu ...