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

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

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

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



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

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

分享给朋友:

相关文章

【数据结构】栈—括号匹配检验

【数据结构】栈—括号匹配检验

【题目描述】假设表达式中允许包含两种括号:圆括号和方括号,其嵌套的顺序随意,如([ ]())或[  (  [  ] [  ] ) ] 等为正确的匹配,[&nbs...

【STL】二分查找函数(算法)—binary_search

【说明】binary_search() 实现了一个二分查找算法。它会在前两个参数指定范围内搜索等同于第三个参数的元素。指定范围的迭代器必须是正向迭代器而且元素必须可以使用 < 运算符来比较。这个...

C++中的宏

一、预处理和编译器    首先,预编译器就是在编译器之前运行,换句话说,预编译器根据程序员的指示,决定实际要编译的内容。预编译器编译指令都以 # 开头。例如:1...

CSP-J2021年普及组复赛T3——网络连接

【题目描述】TCP/IP 协议是网络通信领域的一项重要协议。今天你的任务,就是尝试利用这个 协议,还原一个简化后的网络连接场景。在本问题中,计算机分为两大类:服务机(Server)和客户机(Clie...

NOIP2013年普及组初赛题目及答案分析

NOIP2013年普及组初赛题目及答案分析

一、单项选择题1. 一个 32 位整型变量占用( A )个字节。 A. 4    B. 8      C. 32     &nbs...

unsigned

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