stl lower_bound upper_bound binary_search equal_range
自己按照stl实现了一个:
http://www.cplusplus.com/reference/algorithm/binary_search/ 这里有个注释,如何判断两个元素相同:
Two elements, a and bare considered equivalent if (!(a<b) && !(b<a))
lower_bound返回!(*it<val) ,所以binary_search 只要再判断 !(val<*it) 即可。
template <class ForwardIterator, class T>
bool binary_search (ForwardIterator first, ForwardIterator last, const T& val)
{
first = std::lower_bound(first,last,val);
return (first!=last && !(val<*first));
}
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
#include<sstream>//istringstream
#include<cstring> using namespace std; //return first element >= int * lowerbound(int *first, int* last, int value)
{
int* it;
int count=last-first;
int step;
while(count>0)
{
it=first;
step=count/2;
it+=step;
//it指向的<, ++后排除<
if(*it<value)
{
first=++it;
count-=step+1;
}
else//左边
count=step; }
return first;
} //return first element >
int* upperbound(int *first, int *last, int value)
{
int count=last-first;
int step;
int *it;
while(count>0)
{
step=count/2;
it=first+step;
//it指向 <= ,++后排除<=
if(!(value<*it))
{
first=++it;
count-=step+1;
}
else
{
count=step;
}
}
return first;
} int main()
{
int a[]={ 1, 1, 2, 3, 3, 3, 3, 4, 4, 4, 5, 5, 6 };
int *lb=lowerbound(a, a+sizeof(a)/sizeof(int), 4);
cout<<*lb<<endl; int *ub=upperbound(a, a+sizeof(a)/sizeof(int), 4);
cout<<*ub<<endl;
return 0;
}
stl lower_bound upper_bound binary_search equal_range的更多相关文章
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- [STL]lower_bound&upper_bound
源码 lower_bound template <class ForwardIterator, class T> ForwardIterator lower_bound (ForwardI ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL入门--sort,lower_bound,upper_bound,binary_search及常见错误
首先,先定义数组 int a[10]; 这是今天的主角. 这四个函数都是在数组上操作的 注意要包含头文件 #include<algorithm> sort: sort(a,a+10) 对十 ...
- STL lower_bound upper_bound binary-search
STL中的二分查找——lower_bound .upper_bound .binary_search 二分查找很简单,原理就不说了.STL中关于二分查找的函数有三个lower_bound .upper ...
- 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )
引子,不明觉厉: 百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件: #include<algorithm>函数功能: 函数lower_bound()在f ...
- STL lower_bound upper_bound 用法
1.lower_bound(begin,end,x) 返回第一个>=x的位置,找不到return .end() 2.upper_bound (begin,end,x) 返回第一个>x的位置 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的unique()和lower_bound ,upper_bound
unique(): 作用:unique()的作用是去掉容器中相邻元素的重复元素(数组可以是无序的,比如数组可以不是按从小到大或者从大到小的排列方式) 使用方法:unique(初始地址,末地址): 这里 ...
随机推荐
- substring函数——sql
--substring( expression, start, length ) expression待截取的文字 start 截取位置的起始下标 length 要截取的长度 --左边第一个字符的下标 ...
- <摘录>TS,PS,PES包格式
PES是打包过的ES,已经插入PTS和DTS,一般是一个pes包为一帧图像 PES包格式: PES再打包成TS流或PS流,往往一个PES会分存到多个ts包中, start_code: 0x00 00 ...
- JavaScript--事件模型(转)
在各种浏览器中存在三种事件模型:原始事件模型( original event model),DOM2事件模型,IE事件模型.其中原始的事件模型被所有浏览器所支持,而DOM2中所定义的事件模型目前被除了 ...
- HDU 5818 Joint Stacks
Joint Stacks Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tota ...
- hdu 5335 Walk Out(bfs+斜行递推) 2015 Multi-University Training Contest 4
题意—— 一个n*m的地图,从左上角走到右下角. 这个地图是一个01串,要求我们行走的路径形成的01串最小. 注意,串中最左端的0全部可以忽略,除非是一个0串,此时输出0. 例: 3 3 001 11 ...
- Android访问权限大全
android.permission.ACCESS_CHECKIN_PROPERTIES 允许读写访问”properties”表在checkin数据库中,改值可以修改上传( Allows read/w ...
- IOS NSNotificationCenter 通知的使用
1.注册通知 [NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notify) name:@" ...
- DataGird导出EXCEL的几个方法
DataGird导出EXCEL的几个方法(WebControl) using System;using System.Data;using System.Text;using System.Web;u ...
- 网页抓取:PHP实现网页爬虫方式小结
来源:http://www.ido321.com/1158.html 抓取某一个网页中的内容,需要对DOM树进行解析,找到指定节点后,再抓取我们需要的内容,过程有点繁琐.LZ总结了几种常用的.易于实现 ...
- dwr消息推送
闲来无事,把自己关于对dwr消息推送的实现过程描述一番. 首先第一步,当然在工程中是加入dwr.jar了,接着在web.xml中配置以下代码 <servlet> <servlet-n ...