实现源码: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 二分查找的更多相关文章

  1. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  2. STL二分查找函数的应用

    应用二分查找的条件必须是数组有序! 其中二分查找函数有三个binary_serch,upper_bound,lower_bound 测试数组 int n1[]={1,2,2,3,3,4,5}; int ...

  3. C++ STL 二分查找

    转载自 https://www.cnblogs.com/Tang-tangt/p/9291018.html 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound ...

  4. SDUT2157——Greatest Number(STL二分查找)

    Greatest Number 题目描述Saya likes math, because she think math can make her cleverer.One day, Kudo invi ...

  5. 【转】STL之二分查找 (Binary search in STL)

    Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound,equal_range 本文是对Effective STL第4 ...

  6. STL之二分查找 (Binary search in STL)

    STL之二分查找 (Binary search in STL) Section I正确区分不同的查找算法count,find,binary_search,lower_bound,upper_bound ...

  7. pearl(二分查找,stl)

    最近大概把有关二分的题目都看了一遍... 嗯..这题是二分查找...二分查找的代码都类似,所以打起来会水很多 但是刚开始打二分还是很容易写挂..所以依旧需要注意 题2 天堂的珍珠 [题目描述] 我有很 ...

  8. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  9. STL中的二分查找

    本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...

随机推荐

  1. Redux + React-router 的入门和配置教程

    (转载)原文链接: https://juejin.im/post/5dcaaa276fb9a04a965e2c9b#heading-18 前言

  2. react+umi+netcore+signalR BS和客户端设备 简单通讯

    微信扫码登录工作用 仅作记录 扫码访问服务器地址 实现扫码服务器地址通讯中断设备解锁 采用signalR 双向异步通知中断 创建控制器 ChatController 注入集线器上下文 IHubCont ...

  3. 分享windows 10 下部署 elasticsearch 和 logstash

    最近和es杠上了.以前只听说过es一直没有机会体验一下. 最近有点时间,就着手体验一把.因为是第一次接触es,没有任何基础.入门的第一件是就是用 百度了, [不过建议改名为白度,基本上查不到想要的,一 ...

  4. opc 相关组件

    http://external.informer.com/opcsystems.com/download_opc_systems_net.htm

  5. tempermonkey文档及为google翻译添加清除输入框脚本

    想通过tempermonkey为google增加一个清除输入框的快捷键,这本来是很好做的事情,后来也证明确实是那么简单,不过中间遇到了几个奇怪的问题,有必要记录一下 tempermonkey 文档:h ...

  6. 将Python源程序打包成可独立执行的文件

    有时候需要将编写好的脚本发送给别人,但是在没有安装运行环境或依赖库的情况下,Python脚本程序无法执行.PyInstaller工具可以快速的将python脚本打包成一个二进制可执行的exe程序,并且 ...

  7. Redis之缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存降级

    目录 Redis之缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级 1.缓存雪崩 2.缓存穿透 3.缓存预热 4.缓存更新 5.缓存降级 Redis之缓存雪崩.缓存穿透.缓存预热.缓存更新.缓存降级 ...

  8. Winform中设置ZedGraph的曲线为折线、点折线、散点图

    场景 Winform中设置ZedGraph的曲线为散点图: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/102465399 在上 ...

  9. 将Redis设置为后台启动

    Linux 在执行redis-server  /etc/redis.conf 时默认开启的是一个前台的进程,也就是说启动的redis 就做不了其他的操作了,只有关闭redis 才能做其他的操作.非常的 ...

  10. File "tesserocr.pyx", line 2443, in tesserocr._tesserocr.image_to_text RuntimeError: Failed to in...

    将Tesseract-OCR安装目录下的tessdata文件夹复制到Python解释器目录下就可以了