STL 二分查找
实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html
1.在一个递增的数组(或vector)中查找元素属于[ s , e ) 的下标
int main()
{
const int n=;
//0 1 2 3 4 5 6 7 8 9
int arr[n]={,,,,,,,,,};
int s,e;
I("%d%d",&s,&e);
int s_pos=lower_bound(arr,arr+n,s)-arr;
int e_pos=upper_bound(arr,arr+n,e)-arr;
O("%d,%d\n",s_pos,e_pos);
return ;
}

2.查找递增数组中元素是否存在
使用binary_search
注:
对于结构体,要么重载小于符号:
①bool operator<(const struct b) const
②要么定义有小于符号含义的cmp函数。
3.应用在递减序列中
#include<cstdio>
#include<stdlib.h>
#include<algorithm> using namespace std; bool cmp(int x,int y){
return x>y;
} int main(){
int asc[]={,,,,,,,,,};
int desc[]={,,,,,,,,,};
int obj=;
int p1=upper_bound(asc,asc+,obj)-asc;
int p2=upper_bound(desc,desc+,obj,cmp)-desc;
printf("%d,%d\n",p1,p2) ;
return ;
}

STL 二分查找的更多相关文章
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- STL二分查找函数的应用
应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...
- C++ STL 二分查找
转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...
- SDUT2157——Greatest Number(STL二分查找)
Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...
- 【转】STL之二分查找 (Binary search in STL)
Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...
- STL之二分查找 (Binary search in STL)
STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...
- pearl(二分查找,stl)
最近大概把有关二分的题目都看了一遍... 嗯..这题是二分查找...二分查找的代码都类似,所以打起来会水很多 但是刚开始打二分还是很容易写挂..所以依旧需要注意 题2 天堂的珍珠 [题目描述] 我有很 ...
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- STL中的二分查找
本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
随机推荐
- 使用numpy处理数组
def fun_ndarray(): a = [[1,2,7], [-6,-2,-3], [-4,-8,-55] ] b = np.array(a) b = np.abs(b)#取数组的绝对值 pri ...
- Destoon手机搜索点击提示 http 403 forbidden解决方法
以下是网上搜到的答案: 最近发现用destoon开发的手机版网站,在手机版百度搜素网站的时候,点击之后出现 http 403 forbidden的弹出窗.必须再次的刷新网页才可以打开网站.出现这个问题 ...
- windows 下验证文件MD5
CertUtil -hashfile C:\Users\admin\Downloads\aaa.txt MD5 CertUtil -hashfile C:\Users\admin\Downloads\ ...
- springboot+RabbitMQ 问题 RabbitListener 动态队列名称:Attribute value must be constant
因为多机环境fanout广播模式,每台机器需要使用自己的队列接受消息 所以尝试使用以下的方案 private static final String QUEUE_NAME="foo.&quo ...
- WPF 精修篇 Winform 嵌入WPF控件
原文:WPF 精修篇 Winform 嵌入WPF控件 首先 创建WPF控件库 这样就有了一个WPF界面 在wpf中增加界面等 在winform中增加WPFDLL 重新生成解决方案 在左侧工具栏 出现W ...
- 需要“jquery”ScriptResourceMapping。请添加一个名为 jquery (区分大小写)的 ScriptResourceMapping。
问题: 该错误是因为应用程序需要jQuery,但是当前项目中并没有jQuery,或者存在jQuery但是程序不知道jQuery的存放路径. 解决方案: 一.下载jQuery,引入必要的jquery-X ...
- jquery点击放大图片
参考地址:https://blog.csdn.net/qq_42249896/article/details/86569636 一.应用场景:点击图片可以对图片进行放大显示. 二.实现代码: 说明:我 ...
- mvc视图双下拉框联动
html部分的代码 <tr class="trs"> <td class="item1"><div class="ite ...
- python 变量作用域、闭包
先看一个问题: 下面代码输出的结果是0,换句话说,这个fucn2虽然已经用global声明了variable1,但还是没有改变变量的值 def func1(): variable1=0 def fun ...
- Python——XPath提取某个标签下所有文本
/text()获取指定标签下的文本内容,//text()获取指定标签下的文本内容,包括子标签下的文本内容,比较简单的是利用字符串相加: room_infos = li.xpath('.//a[@cla ...