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

其中二分查找函数有三个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. Bing必应地图中国API - 添加实时交通信息

    Bing必应地图中国API - 添加实时交通信息 2011-05-24 14:44:58|  分类: Bing&Google|字号 订阅     2009年4月23日,微软必应地图中国API新 ...

  2. BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay

    BZOJ_1862_[Zjoi2006]GameZ游戏排名系统&&BZOJ_1056_[HAOI2008]排名系统_Splay Description 排名系统通常要应付三种请求:上传 ...

  3. BZOJ_1115_[POI2009]石子游戏Kam_博弈论

    BZOJ_1115_[POI2009]石子游戏Kam_博弈论 Description 有N堆石子,除了第一堆外,每堆石子个数都不少于前一堆的石子个数.两人轮流操作每次操作可以从一堆石子中移走任意多石子 ...

  4. 杂项-Java:Ehcache

    ylbtech-杂项-Java:Ehcache EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider. 1.返回顶部 1. 基 ...

  5. 提交图片base64格式问题

    提交图片base64格式给后台,一定把data:image/png;base64,去掉,不然提交后后台返回的图片URL打开是一个破坏的图片.使用split("base64,")[1 ...

  6. bzoj 1689: [Usaco2005 Open] Muddy roads 泥泞的路【贪心】

    按左端点排序,贪心的选即可 #include<iostream> #include<cstdio> #include<algorithm> using namesp ...

  7. git merge合并时遇上refusing to merge unrelated histories的解决方案

    如果git merge合并的时候出现refusing to merge unrelated histories的错误,原因是两个仓库不同而导致的,需要在后面加上--allow-unrelated-hi ...

  8. 基于.Net Core的API框架的搭建(4)

    6.加入日志功能 日志我们选用log4net,首先引入程序包: 选择2.0.8版本安装.然后在项目根目录新增log4net的配置文件log4net.config: <?xml version=& ...

  9. Linux重启和关机命令

    Linux重启命令: 方式1:shutdown –r now 方式2:reboot Linux关机命令: shutdown –h now

  10. C# 代码笔记_文件

           string Route = @"D:\ksy\ksy\WebSite1\";//文件地址         string File_name = "user ...