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

其中二分查找函数有三个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. Codeforces Round #362 (Div. 2) D. Puzzles

    D. Puzzles time limit per test 1 second memory limit per test 256 megabytes input standard input out ...

  2. ZOJ 1871:Steps

    Steps Time Limit: 2 Seconds      Memory Limit: 65536 KB One steps through integer points of the stra ...

  3. [C++ STL] 常用算法总结

    1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...

  4. [转]C语言常见错误总结1

    指针与数组的对比c程序中,指针和数组在不少地方可以相互替换着用,让人产生一种错觉,以为两者是等价的 数组要么在静态存储区被创建(如全局数组),要么在栈上被创建.数组名对应着(而不是指向)一块内存,其地 ...

  5. 三分 HDOJ 3714 Error Curves

    题目传送门 /* 三分:凹(凸)函数求极值 */ #include <cstdio> #include <algorithm> #include <cstring> ...

  6. BFS HDOJ 1728 逃离迷宫

    题目传送门 /* BFS:三维BFS,加上方向.用dp[x][y][d]记录当前需要的最少转向数 */ #include <cstdio> #include <algorithm&g ...

  7. Android 性能优化(4)Optimizing Layout Hierarchies:用Hierarchy Viewer和Layoutopt优化布局

    Optimizing Layout Hierarchies This lesson teaches you to Inspect Your Layout Revise Your Layout Use ...

  8. 纵横填字js

    新数据结构设计: 定义一个map: key是横纵坐标字符串,比如“0,4” value是一个json,包含以下属性:字,横向的词(若 有的话,无的话,空串),纵向的词(若有的话,无的话,空串). 另有 ...

  9. opencv边缘滤波

    2018-03-0422:16:11 import cv2 as cv import numpy as np def bi_demo (image): print ("ceshi" ...

  10. dbcp数据源配置

    <bean id="dbcpDataSource"  class="org.apache.commons.dbcp.BasicDataSource" de ...