当前位置:首页 > C++知识 > 正文内容

【STL】二分查找函数 lower_bound 和 upper_bound

亿万年的星光3年前 (2022-03-12)C++知识18602

一、 lower_bound


【功能】

在数组a中从a[begin]开始到a[end - 1]按照cmp函数来比较进行二分查找第一个大于等于k的数的地址,如果有第一个大于等于k的数则返回该数的地址,否则返回a[end]的地址。

【头文件】

algorithm

【模板】

lower_bound(a + begin, a + end, k, cmp);

首地址(a + begin) 必要
末地址(a + end) 必要
需要比较的值 必要
比较函数表示序列如何有序 (多数情况下适用于对结构体的搜索) 选要

【必要条件】

必须是有序数组

【例子】

# include <cstdio>
# include <iostream>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;
const int NR = 100;
int n = 6;
int a[50] = {0, 1, 5, 7, 9, 23, 60};
int main() {
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于等于10的数的地址为"<<lower_bound(a+1,a+n+1,10)<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于等于9的数的位置为"<<lower_bound(a+1,a+n+1,9)-a<<endl;
	cout<<"a数组从a[0]到a[n-1]这n个数中第一大于于等于10的数的位置为"<<lower_bound(a,a+n,10)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于等于13的数的位置为"<<lower_bound(a+1,a+n+1,13)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于等于7的数的位置为"<<lower_bound(a+1,a+n+1,7)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于等于10000的数的位置为"<<lower_bound(a+1,a+n+1,10000)-a<<endl;
	return 0;
}

/*
a数组从a[1]到a[n]这n个数中第一个大于等于10的数的地址为0x46a054
a数组从a[1]到a[n]这n个数中第一个大于等于9的数的位置为4
a数组从a[0]到a[n-1]这n个数中第一大于于等于10的数的位置为5
a数组从a[1]到a[n]这n个数中第一个大于等于13的数的位置为5
a数组从a[1]到a[n]这n个数中第一个大于等于7的数的位置为3
a数组从a[1]到a[n]这n个数中第一个大于等于10000的数的位置为7
*/



二、upper_bound


【功能】

在数组a中从a[begin]开始到a[end - 1]按照cmp函数来比较进行二分查找第一个大于k的数的地址,如果有第一个大于k的数则返回该数的地址,否则返回a[end]的地址。

【头文件】

algorithm

【必要条件】

从a[begin]开始到a[end - 1]的序列是有序序列。

【模板】

upper_bound(a + begin, a + end, k, cmp);
首地址(a + begin) 必要
末地址(a + end) 必要
需要比较的值 必要
比较函数表示序列如何有序(多数情况下适用于对结构体的搜索) 选要

【例子

# include <cstdio>
# include <iostream>
# include <cmath>
# include <cstring>
# include <algorithm>
using namespace std;
int n = 6;
int a[50] = {0, 1, 5, 7, 9, 23, 60};
int main() {
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于9的数的地址为"<<upper_bound(a+1,a+n+1,9)<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于9的数的位置为"<<upper_bound(a+1,a+n+1,9)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于10的数的位置为"<<upper_bound(a+1,a+n+1,10)-a<<endl;
	cout<<"a数组从a[0]到a[n-1]这n个数中第一个大于9的数的位置为"<<upper_bound(a,a+n,9)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于13的数的位置为"<<upper_bound(a+1,a+n+1,13)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于7的数的位置为"<<upper_bound(a+1,a+n+1,7)-a<<endl;
	cout<<"a数组从a[1]到a[n]这n个数中第一个大于10000的数的位置为"<<upper_bound(a+1,a+n+1,10000)-a<<endl;
	return 0;
}

/*
a数组从a[1]到a[n]这n个数中第一个大于9的数的地址为0x46a054
a数组从a[1]到a[n]这n个数中第一个大于9的数的位置为5
a数组从a[1]到a[n]这n个数中第一个大于10的数的位置为5
a数组从a[0]到a[n-1]这n个数中第一个大于9的数的位置为5
a数组从a[1]到a[n]这n个数中第一个大于13的数的位置为5
a数组从a[1]到a[n]这n个数中第一个大于7的数的位置为4
a数组从a[1]到a[n]这n个数中第一个大于10000的数的位置为7
*/



扫描二维码推送至手机访问。

版权声明:本文由青少年编程知识记录发布,如需转载请注明出处。

分享给朋友:

相关文章

C++读取磁盘文件

0.前言简单介绍一下C++读取文件的基本操作。关键技术:freopen() 文件的打开函数 FILE *fp fp=fopen(文件名,使用文件方式) 例如: fp...

取模运算总结——数论

编程竞赛有相当一部分题目的结果过于庞大,整数类型无法存储,往往只要求输出取模的结果。例如(a+b)%p,若a+b的结果我们存储不了,再去取模,结果显然不对,我们为了防止溢出,可以先分别对a取模,b取模...

【题解】围圈报数(约瑟夫问题)

【题解】围圈报数(约瑟夫问题)

【题目描述】有n个人依次围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个热呢又出列,... ,如此反复到所有的人全部出列为止。设n个人的编号分别为1,2,......

【数据结构】栈(Stack)的介绍

栈是只能在某一端插入和删除的特殊线性表。栈就是一种类似桶堆积物品的数据结构,进行删除和插入的一端称栈顶,另一端称栈底。插入一般称为进栈(PUSH),删除则称为退栈(POP)。栈也称为后进先出表(LIF...

C++ 如何隐藏光标

在C++控制台做小游戏的时候,光标一直在闪,影响体验效果,我们可以通过下面的函数隐藏光标位置。void HideCursor(){ CONSOLE_CURSOR_INFO cu...

【算法】单链表的一些操作(存取、查找、取出、插入、删除)

一、单链表结构的建立与输出#include<iostream> using namespace std; struct Node{ int ...