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(初始地址,末地址): 这里 ...
随机推荐
- poj 2184 Cow Exhibition
// 给定n头牛,每头有属性智商和幽默感,这两个属性值有正有负,现在要从这n头牛中选出若干头使得他们的智商和与幽默感和不为负数,// 并且两者两家和最大,如果无解输出0,n<=100,-1000 ...
- curl命令访问域名
1.前言 curl是利用URL语法在命令行方式下工作的开源文件传输工具(来自百度百科).cURL 是一种简单有效的工具,可以使用cURL工具进行WEB相关的调试开发工具,相对于Yeelink这样的云平 ...
- context.Response.End()的用法和本质
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace Web_C ...
- Android性能调优
本文主要分享自己在appstore项目中的性能调优点,包括同步改异步.缓存.Layout优化.数据库优化.算法优化.延迟执行等.一.性能瓶颈点整个页面主要由6个Page的ViewPager,每个Pag ...
- bjfu1100 圆环
这题也是2011百度之星的一道题.知道做法后代码极简单. 不过我做完后随便上网搜了一下,发现竟然还有很多不同的做法.别的做法我就不管了,我只把我的做法的原理说清楚.我做题时是按如下顺序逐步找到规律的: ...
- 微软控制台带来的PHP控制台输出问题
/** * 测试文件包含方式对跨平台的影响 * 控制台下测试. * 默认的文件编码为 UTF-8 */ function testChinese() { $file = __DIR__ . '/con ...
- JavaScript中的*top、*left、*width、*Height详解
来源:http://www.ido321.com/911.html html代码 1: <body> 2: <div class="father" id=&quo ...
- 多校5 1004 HDU5784 统计锐角三角形数目
http://acm.hdu.edu.cn/showproblem.php?pid=5784 题意:n个点,找多少个锐角三角形数目 思路:极角排序+two pointers 当前选择的点集要倍增一倍, ...
- 《GettingThingsDone》--GTD学习笔记(二)-GTD实践指导
一.准备阶段的建议: 高级的工作管理方法就是学会一套系统并付诸实施,直到将这套系统和方法融入你的工作和生活中. 通过行动使自己感觉良好,要比通过使自己感觉良好而进入一种较佳的行动状态容易的多. 当你在 ...
- C++多态分析(polymorphisn)
前言 借着这次学校的生产实习来回顾下C++的多态,这里讨论下C++的多态以及实现原理.我都是在QT下使用C++的,就直接在QT下进行演示了 多态介绍 面向对象语言中最核心的几个理念就是:封装.继承.多 ...