STL二分查找函数的应用

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

其中二分查找函数有三个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";
}

原文地址:https://www.cnblogs.com/baccano-acmer/p/9960281.html

时间: 2024-08-13 20:31:27

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

【C++】【STL】二分查找函数

binary_search 这个函数的返回值是布尔型,也就是最简单的找到了就为真,没找到就是假. 传入参数有三个,数据集合的左端点,数据集合的右端点,查找的值. 注意这些左端点右端点是要求左开右闭原则的,就是和数学上的左开右闭区间[a, b)一样,右端点是个不会被查阅的值. 一般来说写法类似: bool flag = false; int data[n] = {...};///数据 sort(data, data + n); flag = binary_search(data, data + n

#STL函数学习:二分查找函数/去重函数

二分查找lower_bound() :https://blog.csdn.net/qq_40160605/article/details/80150252 https://www.jianshu.com/p/cb0d5488bb6a 去重函数: https://www.cnblogs.com/wangkundentisy/p/9033782.html https://www.cnblogs.com/hua-dong/p/7943983.html 原文地址:https://www.cnblogs.

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

一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找 的前提),下面记录一下这两个函数: (2)ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, last)中的第一个大于等于值val的位置: (3)ForwardIter up

利用STL二分查找范围

设要查询的数组为A,A有n个元素,且递增排列 查询>=x的第一个下标 int p=lower_bound(A,A+n,x)-A; 查询>x的第一个下标   int p=upper_bound(A,A+n,x)-A; 查询<=x的最后一个下标 int p=upper_bound(A,A+n,x)-A-1; 查询<x的最后一个下标 int p=lower_bound(A,A+n,x)-A-1; 查询等于x的数量int n=upper_bound(A,A+n)-lower_bound(A

STL二分查找

实现源码:https://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 1.在一个递增的数组(或vector)中查找元素属于[ s , e ) 的下标 int main() { const int n=10; //0 1 2 3 4 5 6 7 8 9 int arr[n]={1,2,3,4,5,5,5,5,9,10}; int s,e; I("%d%d",&s,&e); int s_pos=lower_

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

STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中,进行二分查找查找某一元素val,函数lower_bound()返回大于或等于val的第一个元素位置(即满足条件a[i]>=val(first<=i<last)的最小的i的值),当区间的所有元素都小于val时,函数返回的i值为last(注意:此时i的值是越界的!!!!!). 比如:已知数组元素是a[10]={0,2,2,2,6,8,10,16,60,100} 当val=0时,函

二分查找之新手篇——函数使用

首先介绍c++万能头文件 #include<bits/stdc++.h> using namespace std;(比较适合偷懒,但是不能确保不会出错)(用还是可以的,粗错了再回头来改嘛) 接下来介绍一下两个二分查找函数: upper_bound()与lower_bound(): ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法(函数)返回一个非递减序列[first, last)中

STL之二分查找:hdu 5178 ( BestCoder Round #31 1001 )

STL包含四种不同的二分查找算法,binary_search    lower_bound  upper_bound   equal_range.他们的作用域是已经排序好的的数组. ★binary_search试图在已排序的[first, last)中寻找元素value.如果找到它会返回true,否则返回false,它不返回查找位置. ★iterator lower_bound( const key_type &key ): 返回一个迭代器,指向键值>= key的第一个元素. ★iterat

关于二分查找及其上下界问题的一些思考

个人认为在编程的时候,我的代码能力应该是到位的,但是昨天参加的某公司笔试彻底把这个想法给终结了,才意识到自己是多么的弱.其中印象最深刻的是一道关于二分查找上下界的问题.当时洋洋得意,STL 分分钟搞定,结果到了面试的时候他要我自己重新实现一下.这个时候就拙计了,拿着笔的我是写了改改了写,最后勉强算是完成. 今天反思一下,决定自己再把二分查找重新实现一下.也作为给自己的一个警醒,不要总以为自己能力有多高,总有一天会被打脸的. 一.二分查找思想(参照<算法竞赛入门经典>,感谢刘老师). 在有序表中