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的更多相关文章

  1. STL中的二分查找———lower_bound,upper_bound,binary_search

    关于STL中的排序和检索,排序一般用sort函数即可,今天来整理一下检索中常用的函数——lower_bound , upper_bound 和 binary_search . STL中关于二分查找的函 ...

  2. STL中的二分查找

    本文转载于https://blog.csdn.net/riba2534/article/details/69240450 使用的时候注意:必须用在非递减的区间中 二分查找的原理非常简单,但写出的代码中 ...

  3. 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 ...

  4. STL入门--sort,lower_bound,upper_bound,binary_search及常见错误

    首先,先定义数组 int a[10]; 这是今天的主角. 这四个函数都是在数组上操作的 注意要包含头文件 #include<algorithm> sort: sort(a,a+10) 对十 ...

  5. STL 二分查找三兄弟(lower_bound(),upper_bound(),binary_search())

    一:起因 (1)STL中关于二分查找的函数有三个:lower_bound .upper_bound .binary_search  -- 这三个函数都运用于有序区间(当然这也是运用二分查找的前提),以 ...

  6. cuda中的二分查找

    使用背景 通常,在做高性能计算时,我们需要随机的连接某些点.这些点都具有自己的度量值,显然,度量值越大的值随机到的概率就会越大.因此,采用加权值得方法: void getdegreeSum(DG *g ...

  7. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...

  8. 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 ...

  9. Java中的二分查找

    二分查找:(折半查找) 前提:数组必须是有序的. 思想:每次都猜中间的那个元素,比较大或者小,就能减少一半的元素.思路:A:定义最小索引,最大索引. B:比较出中间索引 C:拿中间索引的值和要查找的元 ...

随机推荐

  1. MySQL性能监控工具-Spotlight on MySQL

    原文地址:http://blog.csdn.net/k_scott/article/details/8167018 这是我的第二款用于监控MySQL数据库性能的工具:Spotlight on MySQ ...

  2. EditPlus设置html和js文件的注释快捷键

    EditPlus默认是可以通过快捷键注释html的,格式是 <!-- </label> <label>类型</label> <label> --& ...

  3. Linux Tools

    WinSCP  http://winscp.net/eng/download.php Xshell 5

  4. 203. Remove Linked List Elements【easy】

    203. Remove Linked List Elements[easy] Remove all elements from a linked list of integers that have ...

  5. 使用webapi实现windows本地用户管理

    1. 引言 最近一段时间设计和实现公司内部的基于OAuth2.0的统一身份认证中心,经梳理,公司部分自研系统可以使用OAuth2.0的方式进行身份认证,还有一部分系统无源代码,未开放接口,使用wind ...

  6. vue 过渡的-css-类名

    会有6个css类名在leave/enter过渡中切换 1,v-enter:定义进入过渡的开始状态,在元素被插入时生效,在下一帧中移除 2,v-enter-active:定义过渡的状态.在元素整个过渡过 ...

  7. deepin linux下eclipse c/c++ 调试开源代码

    1.deepin linux 下使用eclipse c/c++ 调试2. 编译选项,-g3 -O0,-g3表示输出调试信息,-O0不优化代码(第一个字母o的大写,第二个是数字0) 3.必备环境: gd ...

  8. 获取UUID

    UDID 设备的唯一标识符,也就是设备的序列号,在iOS2.0版本中UIDevice提供了一个获取设备唯一标识符的方法uniqueldentifier,这个方法也是为一个可以确认获取此标识符的方法.但 ...

  9. Android Studio 使用笔记:给包重命名~~有点水

    很简单,选择需要重命名的包,按下 Shift + F6 这时候出现提示,选择 Rename package 输入新的包名,Refactor按钮会变亮,点击就可以了. 注意:这个是重命名一个包名,想做 ...

  10. tcp/iP协议族——IP工作原理及实例具体解释(下)

     IP协议具体解释 上一篇文章文章主要介绍了IP服务的特点,IPv4头部结构IP分片.并用tcpdump抓取数据包,来观察IP数据报传送过程中IP的格式,以及分片的过程.本文主要介绍IP路由,IP ...