STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search 。
STL中关于二分查找的函数有三个lower_bound 、upper_bound 、binary_search 。这三个函数都运用于有序区间(当然这也是运用二分查找的前提)。
Tips:1.在检索前,应该用sort函数对数组进行从小到大排序。
2.使用以上函数时必须包含头文件:#include < algorithm >
一、lower_bound() 函数
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
举例如下:
一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标
则:
pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个素)。
所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
测试代码如下:
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector> using namespace std; int main()
{
const int VECTOR_SIZE = ; // Define a template class vector of int
typedef vector<int > IntVector ; //Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ; IntVector Numbers(VECTOR_SIZE) ; IntVectorIt start, end, it, location ; // Initialize vector Numbers
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ; start = Numbers.begin() ; // location of first
// element of Numbers end = Numbers.end() ; // one past the location
// last element of Numbers // print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ; // return the first location at which 10 can be inserted
// in Numbers
location = lower_bound(start, end, ) ; cout << "First location element 10 can be inserted in Numbers is: "
<< location - start<< endl ;
}
二、upper_bound()函数
函数upper_bound()返回的在前闭后开区间查找的关键字的上界,
如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)
返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置
测试代码如下:
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
using namespace std; void main()
{
const int VECTOR_SIZE = ; // Define a template class vector of int
typedef vector<int, allocator<int> > IntVector ; //Define an iterator for template class vector of strings
typedef IntVector::iterator IntVectorIt ; IntVector Numbers(VECTOR_SIZE) ; IntVectorIt start, end, it, location, location1; // Initialize vector Numbers
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ;
Numbers[] = ; start = Numbers.begin() ; // location of first
// element of Numbers end = Numbers.end() ; // one past the location
// last element of Numbers // print content of Numbers
cout << "Numbers { " ;
for(it = start; it != end; it++)
cout << *it << " " ;
cout << " }\n" << endl ; //return the last location at which 10 can be inserted
// in Numbers
location = lower_bound(start, end, ) ;
location1 = upper_bound(start, end, ) ; cout << "Element 10 can be inserted at index "
<< location - start<< endl ;
cout << "Element 10 can be inserted at index "
<< location1 - start<< endl ;
}
三、binary_search
函数模版:
template<typename T>
int binary_search (T arr[], int size, T target) ;
参数说明:
T: 模版参数
arr : 数组首地址
size: 数组元素个数
T target : 需要查找的值
返回值: 如果数组中找到target, 返回其下标,否则返回 -1
要求数组元素顺序非递减
binary_search试图在已排序的[first,last)中寻找元素value,若存在就返回true,若不存在则返回false。返回单纯的布尔值也许不能满足需求,而lower_bound、upper_bound能提供额外的信息。事实上由源码可知binary_search便是利用lower_bound求出元素应该出现的位置,然后再比较该位置的值与value的值。
STL中的二分查找———lower_bound,upper_bound,binary_search的更多相关文章
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
- STL中的二分查找
本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
Search Insert Position Given a sorted array and a target value, return the index if the target is fo ...
- STL入门--sort,lower_bound,upper_bound,binary_search及常见错误
首先,先定义数组 int a[10]; 这是今天的主角. 这四个函数都是在数组上操作的 注意要包含头文件 #include<algorithm> sort: sort(a,a+10) 对十 ...
- STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())
一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...
- cuda中的二分查找
使用背景 通常,在做高性能计算时,我们需要随机的连接某些点.这些点都具有自己的度量值,显然,度量值越大的值随机到的概率就会越大.因此,采用加权值得方法: void getdegreeSum(DG *g ...
- 二分查找、upper_bound、lower_bound
整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...
- Long Jumps(二分查找lower_bound()函数的运用)
Valery is a PE teacher at a school in Berland. Soon the students are going to take a test in long ju ...
- Java中的二分查找
二分查找:(折半查找) 前提:数组必须是有序的. 思想:每次都猜中间的那个元素,比较大或者小,就能减少一半的元素.思路:A:定义最小索引,最大索引. B:比较出中间索引 C:拿中间索引的值和要查找的元 ...
随机推荐
- nodejs 搭建 RESTful API 服务器的常用包及其简介
常用包 框架: yarn add express 数据库链接: yarn add sequelize yarn add mysql2 处理 favicon: yarn add serve-favico ...
- pyqt5 在qt designer后以弹窗的方式连接多个UI图形界面
当我们通过pyqt开发时,eric6为我们提供了一个方便的工具:图形化的绘制UI工具--qt designer. 我们可以通过它开发多个UI,然后利用信号-槽工具,将功能代码附着在上面.也可以将多个界 ...
- SpringMVC源码情操陶冶-View视图渲染
本节简单分析View视图对象的render方法 View接口 最重要的就是render()方法,具体源码如下 /** * Render the view given the specified mod ...
- (转)每天一个linux命令(28):tar命令
场景:Linux环境中压缩和解压的命令经常会使用到,所以学好这个命令很有必要! 原文出处:http://www.cnblogs.com/peida/archive/2012/11/30/2795656 ...
- Python蜕变-2017-4-23
我的第一篇博客,这是试水练习.这次上的菜是Haporxy配置文件操作. <1> 上需求: 具体配置文件如下: global log 127.0.0.1 local2 daemon maxc ...
- Azure 认知服务 (5) 计算机视觉API - 使用C#代码实现读取图片中的文字(OCR)功能
<Windows Azure Platform 系列文章目录> 在笔者之前的文章:Azure 认知服务 (4) 计算机视觉API - 读取图片中的文字 (OCR) 介绍了使用用户界面,在海 ...
- Java中使用POI读取大的Excel文件或者输入流时发生out of memory异常参考解决方案
注意:此参考解决方案只是针对xlsx格式的excel文件! 背景 前一段时间遇到一种情况,服务器经常宕机,而且没有规律性,查看GC日志发生了out of memory,是堆溢出导致的,分析了一下堆的d ...
- gitlab和github一起使用
还是在转我笔记上的内容, 也算备份 参考(https://segmentfault.com/a/1190000002994742) 可以对比着看, 我记得参考里面有个点没有说详细, 我把自己的流程记下 ...
- selenium + ChromeDriver
Selenium是一个用于Web应用程序测试的工具.Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.而对于爬虫来说,使用Selenium操控浏览器来爬取网上的数据那么肯定是爬虫中的杀 ...
- NYOJ-63 小猴子下落(二叉树及优化算法详解)
小猴子下落 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 有一颗二叉树,最大深度为D,且所有叶子的深度都相同.所有结点从左到右从上到下的编号为1,2,3,··· ...