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

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

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

一、 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
*/



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

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

分享给朋友:

相关文章

unsigned

在一些代码中,经常能看到unsigned这种数据类型,比如下面这样的。#include<iostream> using namespace std; int&nbs...

树的存储与遍历—顺序存储

顺序存储使用数组来存储二叉树节点,通过数组下标表示节点间的父子关系,一般适用于完全二叉树。1.存储规则根节点存储在索引 0 位置对于索引为 i 的节点:左子节点索引:2*i + 1右子节点索引:2*i...

CSP-J2021年普及组复赛T2——插入排序

CSP-J2021年普及组复赛T2——插入排序

【题目描述】插入排序是一种非常常见且简单的排序算法。小 Z 是一名大一的新生,今天 H 老 师刚刚在上课的时候讲了插入排序算法。 假设比较两个元素的时间为 O(1),则插入排序可以以 O(n 2...

【题解】组合数学

【题解】组合数学

一、排列与组合口诀:有序排列,无序组合,分类相加,分步相乘。1.排列数公式:表示的含义是从n个数中选出m个进行排队,有多少种不同的排法。从n个不同的元素中任取m(m≤n)个元素的所有排列的个数,叫做从...

【数据结构】并查集2

【数据结构】并查集2

上一篇文章,简单介绍了并查集。这篇文章,介绍一下并查集的改进以及优化。find函数的优化(路径压缩)因为并查集的merge操作:void merge(int a, int...

【数据结构】队列—基本操作

【数据结构】队列—基本操作

一、C++实例分析       C++队列queue模板类的定义在<queue>头文件中,queue 模板类需要两个模板参数,一个是元素类型,一个容...