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:拿中间索引的值和要查找的元 ...
随机推荐
- php 按汉字首字母查询[转载]
<?php function getfirstchar($s0){ //获取单个汉字拼音首字母.注意:此处不要纠结.汉字拼音是没有以U和V开头的 $fchar = ord($s0{0}); if ...
- Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾
Atitit.播放系统规划新版本 v4 q18 and 最近版本回顾 1 版本12 (ing)4 1.1 无映射nas系统..4 1.2 图片简介搜刮其4 1.3 12.8. 电影图片增加png, ...
- C#类库帮助类
前言 此篇专门记录一些常见DB帮助类及其他帮助类,以便使用时不用重复造轮子. DBHelper帮助类 ①首当其冲的就是Sql Server帮助类,创建名为DbHelperSQL 的类 ,全部代码如下: ...
- Html打印需要内容块(Js实现)
首先在head里面加入下面一段js代码: function preview(oper) { ) { bdhtml = window.document.body.innerHTML; //获取当前页的h ...
- OCR 即 光学字符识别
OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...
- Win7安装.Net framework 3.5时出错的某解决办法
情况: Win7卸载了.Net Framework 3.5后, 做了些盘符的Symlink操作, 将一些大文件从C盘移到D盘. 再次安装.Net 3.5时, 怎么都装不上, 直接下载离线安装包, 一装 ...
- spring security 3.1 实现权限控制
spring security 3.1 实现权限控制 简单介绍:spring security 实现的权限控制,能够分别保护后台方法的管理,url连接訪问的控制,以及页面元素的权限控制等, secur ...
- 一个奇怪的EL表达式错误
下图是在Struts2的action中写的一个方法 JSP页面代码如下: 在页面访问如下路径:http://localhost:8088/maven_ssh/cust_getCustList 目前推测 ...
- eclipse通过maven远程发布应用到Tomcat
好久没有写博客了,今天为大家分享一下如何在eclipse通过maven远程发布应用到Tomcat. 一般情况下,我们发布应用到服务器需要现将应用导出成war包,然后连接服务器部署更新,这样是很耗时的, ...
- 我的第五个程序 java的JDBC连接mysql数据库 实现输入查询
import java.sql.*; import java.util.Scanner; public class JDBCTest { public static void main(String[ ...