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 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
随机推荐
- 19条常用的MySQL优化方法(转)
本文我们来谈谈项目中常用的MySQL优化方法,共19条,具体如下:1.EXPLAIN命令做MySQL优化,我们要善用EXPLAIN查看SQL执行计划.下面来个简单的示例,标注(1.2.3.4.5)我们 ...
- 前端学习:学习笔记(CSS部分)
前端学习:学习笔记(CSS部分) CSS的学习总结(图解) CSS的引入方式和书写规范 CSS的插入方式_内嵌样式 <!DOCTYPE html> <html> <hea ...
- kafka 解密:破除单机topic数多性能下降魔咒
https://bbs.huaweicloud.com/blogs/112956 版权归PUMA项目组所有,转载请声明,多谢. kakfa大规模集群能力在前面已给大家分享过,kafka作为消息总线,在 ...
- govendor用法
为什么使用govendor go语言的依赖管理最主要的是版本控制问题. govendor是Golang的依赖包管理工具,它的出现可以避免不同用户在clone同一个项目后从外部获取不同依赖库版本的问题. ...
- 使用Hybris的customer coupon进行促销活动(promotion)
登录Backoffice,在Coupon菜单里创建一个新的类型为Customer Coupon的优惠券: 在菜单Marketing->Promotion Rules里,创建一条新的促销规则Pro ...
- 7.redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗?
作者:中华石杉 面试题 redis 集群模式的工作原理能说一下么?在集群模式下,redis 的 key 是如何寻址的?分布式寻址都有哪些算法?了解一致性 hash 算法吗? 面试官心理分析 在前几年, ...
- 设置linux代理完成apt-get
最近ubuntu的服务器被公司关闭了外网使用权限,但是安装软件又需要连接外网,那么就只能通过代理来连接了. 先按照下面的这篇帖子来设置windows端的代理. https://blog.csdn.ne ...
- iview admin动态路由实现
参考 https://blog.csdn.net/weixin_41538490/article/details/93749942
- 【GPU加速系列】PyCUDA(一):上手简单操作
PyCUDA 可以通过 Python 访问 NVIDIA 的 CUDA 并行计算 API. 具体介绍和安装可以参考 PyCUDA 官网文档和 pycuda PyPI. 本文涵盖的内容有: 通过 PyC ...
- JavaScript三种方法获取地址栏参数的方法
今天碰到要在一个页面获取另外一个页面url传过来的参数,一开始很本能的想到了用 split(“?”)这样一步步的分解出需要的参数. 后来想了一下,肯定会有更加简单的方法的!所以在网上找到了两个很又简单 ...