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. 自己动手制作更好用的markdown编辑器-01

    这里文章都是从个人的github博客直接复制过来的,排版可能有点乱. 原始地址  http://benq.im   文章目录 1. 简介 2. 项目结构 3. 程序主界面 4. 拖动窗口 5. app ...

  2. Java(Android)解析KML文件

    參考自:http://blog.csdn.net/yyywyr/article/details/38359049 http://blog.csdn.net/warrenwyf/article/deta ...

  3. ModelSim6.2 설치에 관한(About the Installation problem of ModelSim 6.2)

    ModelSim 설치는 PC OS 따라서 호환성 문제가 발생한다. !!!!!! Vista OS에서는 ModelSim 설치가 안됨(호환성 문제) XP, Win7에서는 호환성 문제 없 ...

  4. Effective C++:条款22:将成员变量声明为private

    (一)为什么不採用public成员变量 (1)首先,从语法一致性考虑,客户唯一能訪问对象的方法就是通过成员函数,客户不必考虑是否该记住使用小括号(). (2)其次,使用函数能够让我们对成员变量的处理有 ...

  5. 初识Quartz(二)

    简单作业: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 package quartz_pr ...

  6. C 调用 lua 函数

    C 调用 lua 函数 需要考虑的问题: 1. 使用 lua_pcall 可以调用 lua 函数,首先把 lua 函数入栈,然后把参数入栈, lua_pcall(luaState, 参数个数, 返回值 ...

  7. java - day06 - arraycopy

    package day05; import java.util.Arrays; import java.util.Random; /* * 附:如果需要使用引用类,如Random类的方法, * 需要新 ...

  8. Python 中,matplotlib绘图无法显示中文的问题

    在python中,默认情况下是无法显示中文的,如下代码: import matplotlib.pyplot as plt # 定义文本框和箭头格式 decisionNode = dict(boxsty ...

  9. mysql 数据库复制表 create table city1 like city;

    -- 只复制表结构 create table city1 like city; INSERT INTO test2 SELECT * FROM test; -- 上面的表必须存在 -- 复制整张表的数 ...

  10. JPA(Java Persistence API)Java持久化API-介绍

    JPA全称: Java Persistence API JPA的宗旨是为POJO提供持久化标准规范,能够脱离容器独立运行,很方便开发和测试.JPA通过JDK 5.0注解或XML描述对象-关系表的映射关 ...