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

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

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

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



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

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

分享给朋友:

相关文章

2023 CSP 山东地区分数线汇总

地区CSP-XCSP-JCSP-S烟台556648.5临沂516416青岛476753淄博446547.5...

如何计算一个程序的运行时间(防止超时)

再一些OJ系统中,做题的时候常常会超时,但是很多人不知道自己的程序是否会超时,不知道如何检查自己的程序。这篇文章主要介绍几种监测自己程序运行时间的程序。头文件<time.h> ...

【数据结构】栈的基本操作

0.前言上一篇中简单介绍了栈的定义,这一篇中介绍栈的基本用法,包含压栈,出栈,判断栈空,判断栈中元素个数等。下面进行详细介绍1.基本用法本文介绍的栈的主要操作,使用栈之前加入<stack>...

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

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

组合数的写法

前面我们写过 全排列和排列数 等。这篇文章。我们写一下组合数。例题:从n个数中,选出m个,一共有多少种不同的选法?这是一道典型的组合数公式。我们直接用dfs公式肯定会出现重复的。#include<...

【高级篇】C++中的sort函数详解

【高级篇】C++中的sort函数详解

0.简介sort函数用于C++中,对给定区间所有元素进行排序,默认为升序,也可进行降序排序。sort函数进行排序的时间复杂度为n*log2n,比冒泡之类的排序算法效率要高,sort函数包含在头文件为#...