应用二分查找的条件必须是数组有序!

其中二分查找函数有三个binary_serch,upper_bound,lower_bound

测试数组

int n1[]={1,2,2,3,3,4,5};
int n2[]={5,4,3,3,2,2,1};

binary_serch

没有什么好说的,这个很简单,接受三个参数first,last,key三个值。如果在数组中查询到的话,那么就返回1否则返回0

代码

if(binary_search(n1,n1+7,3))
cout<<1<<"\n";
if(binary_search(n1,n1+7,8))
cout<<2<<"\n";

输出的结果为1

upper_bound和lower_bound

upper_bound返回数组中第一个大于指定数的指针,lower_bound返回数组第一个小于等于指定数的指针,他们的基本用法都是first,last,key

解析图片

测试程序

cout<<n1[upper_bound(n1,n1+7,3)-n1]<<"\n";
cout<<n1[lower_bound(n1,n1+7,3)-n1]<<"\n";

输出结果为4,3

解锁cmp参数

upper_bound和lower_bound都可以自定义排序用cmp函数

①默认a<b

bool cmp(int a,int b)
{
return a<b;
}

这种情况及是最初的情况,并没有什么其他的变化

②降序数组a>b

bool cmp(int a,int b)
{
return a>b;
}

这种情况是对于降序数列的的自定义,upper是小于的第一个位置的指针,lower是大于等于的第一个位置的指针

测试程序

cout<<n2[upper_bound(n2,n2+7,3,cmp)-n2]<<"\n";
cout<<n2[lower_bound(n2,n2+7,3,cmp)-n2]<<"\n";

测试结果为2 3

如果加等号例如<-><=那么就是upper和lower的效果颠倒

完整测试程序

#include <bits/stdc++.h>
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n1[]={1,2,2,3,3,4,5};
int n2[]={5,4,3,3,2,2,1};
if(binary_search(n1,n1+7,3))
cout<<1<<"\n";
if(binary_search(n1,n1+7,8))
cout<<2<<"\n";
cout<<n1[upper_bound(n1,n1+7,3)-n1]<<"\n";
cout<<n1[lower_bound(n1,n1+7,3)-n1]<<"\n";
cout<<n2[upper_bound(n2,n2+7,3,cmp)-n2]<<"\n";
cout<<n2[lower_bound(n2,n2+7,3,cmp)-n2]<<"\n";
}

STL二分查找函数的应用的更多相关文章

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

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

  2. C++ STL 二分查找

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

  3. STL 二分查找

    实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , ...

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

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

  5. 分治算法(二分查找)、STL函数库的应用第五弹——二分函数

    分治算法:二分查找!昨天刚说不写算法了,但是突然想起来没写过分治算法的博客,所以强迫症的我…… STL函数库第五弹——二分函数lower_bound().upper_bound().binary_se ...

  6. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

  7. Golang实现二分查找法

    二分查找法就是实现在一组有序的数字数组集合中最快找到指定元素的下标 思路 ①先找到中间的下标middle = (leftIndex + RightIndex) /2 ,然后让中间的下标值和FindVa ...

  8. cuda中的二分查找

    使用背景 通常,在做高性能计算时,我们需要随机的连接某些点.这些点都具有自己的度量值,显然,度量值越大的值随机到的概率就会越大.因此,采用加权值得方法: void getdegreeSum(DG *g ...

  9. Go语言 二分查找算法的实现

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,二分查找算法的前提是传入的序列是有序的(降序或升序),并且有一个目标值. 二分查找的核心思想是将 n 个元素分成大 ...

随机推荐

  1. HDU 1023 Catalan数+高精度

    链接:HDU 1023 /**************************************** * author : Grant Yuan * time : 2014/10/19 15:5 ...

  2. RK3288 6.0 双屏异显,横屏+竖屏【转】

    本文转载自:http://blog.csdn.net/clx44551/article/details/78215730?locationNum=8&fps=1 RK3288 6.0 双屏异显 ...

  3. 嵌入式Linux系统---ppp拨号,4G模块上网【转】

    本文转载自:http://blog.csdn.net/qq562029186/article/details/65438553 4G模块PPP拨号上网 方法1 所需文件: xxx-chat-conne ...

  4. 【OI新闻】2016.10.26 - NOIP初赛泄题再次引爆

    有点劲,开车注意点,小心翻啦~ http://www.noi.cn/noi-news/noi/743-ccfnoip2016 CCF在NOIP2016初赛结束后接到举报:初赛前(22日12时左右),网 ...

  5. [翻译]NUnit---Action Attributes(八)

    Attributes NUnit 1.0使用传统的基于继承和命名约定来识别测试.从2.0开始NUnit使用自定义特性来实现. 因为NUnit的test fixtures不是从框架类库继承,所以开发人员 ...

  6. xargs 主要用于不支持管道的shell命令*****

    变量置换,主要用于不支持管道的shell命令,如:rm.sed等,但有些命令需要占位符“{}”需要注意.比如:删除文件- ls|xargs -i rm -rf {} 文件改名-   ls|xargs ...

  7. Create schema error (unknown database schema '')

    Andrey Devyatka 4 years ago Permalink Raw Message Hi,Please tell me, can I use the static library in ...

  8. Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible

    Could not find modernizr-2.6.2 in any of the sources GitLab: API is not accessible bundle exec rake ...

  9. 错误: 实例 "ruiy" 执行所请求操作失败,实例处于错误状态。: 请稍后再试 [错误: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)].

    错误: 实例 "ruiy" 执行所请求操作失败,实例处于错误状态.: 请稍后再试 [错误: 'ascii' codec can't decode byte 0xe6 in posi ...

  10. bzoj 1571: [Usaco2009 Open]滑雪课Ski【dp】

    参考:https://blog.csdn.net/cgh_andy/article/details/52506738 没有get到什么重点的dp--做的莫名其妙 注意滑雪一个坡可以滑很多次 设f[i] ...