STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数
1、lower_bound函数
在一个非递减序列的前闭后开区间[first,last)中。进行二分查找查找某一元素val。函数lower_bound()返回大于或等于val的第一个元素位置(即满足条件a[i]>=val(first<=i<last)的最小的i的值),当区间的全部元素都小于val时,函数返回的i值为last(注意:此时i的值是越界的!!!!!
)。
比方:已知数组元素是a[10]={0,2,2,2,6,8,10,16,60,100}
当val=0时,函数lower_bound()返回值为0;
当val=1时。函数lower_bound()返回值为1。
当val=2时,函数lower_bound()返回值为1;
当val=3时。函数lower_bound()返回值为4;
当val=4时,函数lower_bound()返回值为4;
当val=5时,函数lower_bound()返回值为4;
当val=6时,函数lower_bound()返回值为4;
当val=7时。函数lower_bound()返回值为5。
当val=8时,函数lower_bound()返回值为5。
当val=9时,函数lower_bound()返回值为6;
当val=10时。函数lower_bound()返回值为6。
当val=11时,函数lower_bound()返回值为7;
当val=59时。函数lower_bound()返回值为8。
当val=60时。函数lower_bound()返回值为8;
当val=61时。函数lower_bound()返回值为9;
当val=100时。函数lower_bound()返回值为9;
当val=101时,函数lower_bound()返回值为10。
当val=150时。函数lower_bound()返回值为10。
当val=500时。函数lower_bound()返回值为10;
STL中函数lower_bound()的代码实现(first是终于要返回的位置)
int lower_bound(int *array, int size, int key)
{
int first = 0, middle, half, len;
len = size;
while(len > 0)
{
half = len >> 1;
middle = first + half;
if(array[middle] < key)
{
first = middle +1;
len = len - half -1;//在右边子序列中查找
}
else
len = half;//在左边子序列(包括middle)中查找
}
}
vector容器储存序列值时的执行程序:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
int i,x,n,m;
vector<int> a; printf("输入数组长度n(0<n<10000):");
scanf("%d",&n); printf("输入数组的n个元素:");
for(i=0;i<n;i++)
{
scanf("%d",&x);
a.push_back(x);
}
sort(a.begin(),a.end());//排序(非递减序列) printf("元素值a[i]/下标i:\n");
for(i=0;i<n;i++)
printf("%d/%d\n",a[i],i); printf("输入查找的元素m:");
vector<int>::iterator it;
while(scanf("%d",&m)!=EOF)
{
//vector容器储存序列值时的函数调用方式
it=lower_bound(a.begin(),a.end(),m);//返回值是迭代器的值
printf("返回位置对于的值:%d\n",*it); printf("输入查找的元素m:");
}
return 0;
}
数组储存序列值时的执行程序:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
int i,j,k,n,m,first,last;
int a[10000]; printf("输入数组长度n(0<n<10000):");
scanf("%d",&n); printf("输入数组的n个元素:");
for(i=0;i<n;i++)
scanf("%d",&a[i]); sort(a,a+n);//排序(非递减序列) printf("元素值a[i]/下标i:\n");
for(i=0;i<n;i++)
printf("%d/%d\n",a[i],i); printf("输入区间[first,last)(first<=last<=n):");
while(scanf("%d%d",&first,&last)!=EOF)
{
printf("输入查找的元素m:");
scanf("%d",&m);
//数组储存序列值时的函数调用方式
j=lower_bound(a+first,a+last,m)-a;
printf("返回位置:%d\n",j); printf("输入区间[first,last)(first<=last<=n):");
}
return 0;
}
2、upper_bound函数
在一个有序序列(升序或者降序)的区间中。进行二分查找某一元素val。函数upper_bound返回一个迭代器指向该区间中最后一个这个元素的下一个位置(简单的说就是返回可以将元素val插入区间的最后一个位置)。
升序排列的容器:
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值>key的第一个元素。
降序排列的容器:
iterator upper_bound( const key_type &key ):返回一个迭代器,指向键值<key的第一个元素。
STL中函数upper_bound()的代码实现(first是终于要返回的位置)
int upper_bound(int *array, int size, int key)
{
int first = 0, len = size-1, half, middle; while(len > 0)
{
half = len >> 1;
middle = first + half;
if(array[middle] > key)//中位数大于key,在包括last的左半边序列中查找。 len = half;
else
{
first = middle + 1;//中位数小于等于key,在右半边序列中查找。
len = len - half - 1;
}
}
return first;
}
数组储存序列值时的执行程序:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
int i,j,k,n,m;
int a[10000]; printf("输入数组长度n(0<n<10000):");
scanf("%d",&n); printf("输入数组的n个元素:");
for(i=0;i<n;i++)
scanf("%d",&a[i]); sort(a,a+n);//排序(非递减序列) printf("元素值a[i]/下标i:\n");
for(i=0;i<n;i++)
printf("%d/%d\n",a[i],i); printf("输入查找的元素m:");
while(scanf("%d",&m)!=EOF)
{
//数组储存序列值时的函数调用方式
j=upper_bound(a,a+n,m)-a;
printf("返回位置:%d\n",j); printf("输入查找的元素m:");
}
return 0;
}
3、binary_search函数
函数binary_search是在有序序列的[first,last)中寻找元素value,若存在就返回true,若不存在则返回false。
程序执行例如以下:
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-4)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1e9+7
using namespace std;
typedef long long ll;
typedef unsigned long long ULL;
int main()
{
int i,j,k,n,m;
int a[10000]; printf("输入数组长度n(0<n<10000):");
scanf("%d",&n); printf("输入数组的n个元素:");
for(i=0;i<n;i++)
scanf("%d",&a[i]); sort(a,a+n);//排序(非递减序列) printf("元素值a[i]/下标i:\n");
for(i=0;i<n;i++)
printf("%d/%d\n",a[i],i); printf("输入查找的元素m:");
while(scanf("%d",&m)!=EOF)
{
//数组储存序列值时的函数调用方式
j=binary_search(a,a+n,m);//j=upper_bound(a,a+n,m)-a;
printf("返回位置:%d\n",j); printf("输入查找的元素m:");
}
return 0;
}
STL中的二分查找——lower_bound 、upper_bound 、binary_search的更多相关文章
- STL中的二分查找———lower_bound,upper_bound,binary_search
关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...
- 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:拿中间索引的值和要查找的元 ...
随机推荐
- Centos下安装JDK、Maven和Git
原文地址:https://github.com/eacdy/spring-cloud-book/blob/master/3%20%E4%BD%BF%E7%94%A8Docker%E6%9E%84%E5 ...
- JanusGraph与Cassandra集成模式
//如果使用的是cassandra 2.2或更高版本,需要开启thift,以使janus连接到cassandra. ./bin/nodetool enablethrift. 15.1 Local Se ...
- 一个关于git push失败的解决方案
问题背景:在GitHub上创建了一个repositorie, 本地初始化并添加了远程仓库后,在GitHub上创建了一个README.md文件(注意不是从本地git push上去的),随后本地修改工程源 ...
- eclipse 断点类别
行断点 行断点位于一般方法内,双击代码行左边就可以设置行断点: 行断点可以设置条件,右键断点-properties: 1设置效果是第几次到达时暂停,图中所示是当第三次到达时暂停 2是开启条件断点 3是 ...
- int和Integer差别
种原始数据类型之中的一个. Java为每一个原始类型提供了封装类.Integer是java为int提供的封装类. 原始数据类型包含byte.int.char.long.float.double.boo ...
- Sublime 中 SFTP插件的使用
首先说明的是Sumblime Text 3,下载安装后,打开软件, 按下Ctrl+Shift+P调出命令面板, 按回车键后弹出下面的 然后 点击左上角的 文件 >SFTP/FTP > Se ...
- mysql 索引优化,索引建立原则和不走索引的原因
第一:选择唯一性索引 唯一性索引的值是唯一的,可以更快捷的通过该索引来确定某条记录. 2.索引的列为where 后面经常作为条件的字段建立索引 如果某个字段经常作为查询条件,而且又有较少的重复列或者是 ...
- Windows下使用python
Windows下使用python,一般安装python就有IDLE了,再这个里面使用就好了,很方便 安装完之后.py的文件右键会有Edit with IDLE,可是我脑残想要默认打开就是IDLE,结果 ...
- 【转】Monkey测试5-运行中停止monkey
停止monkey自动测试步骤: 1.ps命令 查找uiautomator的进程 打开cmd命令行窗口 输入: adb shell ; ps | grep monkey; 返回来的第一个数字,即是mo ...
- OpenCV中的SURF算法介绍
SURF:speed up robust feature,翻译为快速鲁棒特征.首先就其中涉及到的特征点和描述符做一些简单的介绍: 特征点和描述符 特征点分为两类:狭义特征点和广义特征点.狭义特征点的位 ...