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 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
随机推荐
- OpenShift下的JVM监控
去年写过一篇基于jmx监控的文章,这次在Openshift上实现,发现确实不少变化.主要重点问题在 1. prometheus jmx exporter的改进,不再需要运行一个独立的进程,不需要把数据 ...
- Hash冲突的解决--暴雪的Hash算法
Hash冲突的解决--暴雪的Hash算法https://usench.iteye.com/blog/2199399https://www.bbsmax.com/A/kPzOO7a8zx/
- 《 .NET并发编程实战》实战习题集 - 4 - 如何重用一次性资源
如何重用以下一次性资源代码呢? string text; using (var stream = new StreamReader(path)) { text = stream.ReadToEnd() ...
- MVC下通过jquery的ajax调用webapi
如题 jquery的应用,不会的自己去补. 创建一个mvc项目,新建控制器.视图如下: 其中data控制器负责向前台提供数据,home控制器是一个简单的访问页控制器. data控制器代码如下: pub ...
- asp.net core 在centeros 7.x下创建服务
Netcore服务生成说明 如有个项目/opt/wwwroot/dpms.1633.com 启动为/usr/bin/dotnet /opt/wwwroot/dpms.1633.com/DPMS.Web ...
- Django的视图系统:View
一.CBV和FBV FBV:functoin based view,基于函数的view 我们之前写过的都是基于函数的view CBV:class based view,基于类的view 定义CBV: ...
- vue中路由拦截无限循环的情况
router.beforeEach(async (to, from, next) => { if (token) { if (whiteList.indexOf(to.path) != -1) ...
- vue展示md文件,前端读取展示markdown文件
方案1:每次都需要重新打包,每次修改都需要build 直接使用require + v-html: 核心代码如下: 1. 首先需要添加MD文件的loader就是 markdown-loader npm ...
- Clang交叉编译初识
最近工作中要编译一个第三方的C库用于iOS端使用,我直接在Mac OS的终端中./configure & make & make install常规走下来,却无法在真机iOS上使用,提 ...
- Date工具类
总结了下项目中常用的时间转化方法,目前就这么点啦,以后再慢慢添加,先储备起来,免得丢啦. package com.example.keranbin.testdemo; import android.u ...