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

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

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

  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. ubuntu上安装nginx+mysql+php5-fpm(PHP5 - FastCGI Process Manager)

    题外话:由于近段时间测试环境ssh链路质量不大好,经常短线.故我把整个安装过程放到screen里去执行,以防止断线中断了安装过程.执行screen -S install,这样断线后,只要再执行scre ...

  2. 一个最最简易的RPC框架雏形---转载自梁飞的博客

    查阅RPC与HTTP区别的时候, 无意间发现一篇博客,内容是一个简易的RPC服务框架, 仔细一看, 不得了,博主竟然就是阿里dubbo的作者. 原文链接在此: http://javatar.iteye ...

  3. SharePoint 2016 配置工作流环境

    前言 SharePoint 2016 默认创建工作流的时候,工作流平台只包含2010版本,如果想要使用状态机工作流,需要单独安装workflow manager 1.0才可以,下面,我们为大家介绍一下 ...

  4. 那些年,用C#调用过的外部Dll

    经常有人找到我咨询以前在csdn资源里分享的dll调用.算算也写过N多接口程序.翻一翻试试写篇随笔. 明华IC读写器DLL 爱迪尔门锁接口DLL 通用OPOS指令打印之北洋pos打印机dll 明泰非接 ...

  5. C++ STL 优先队列详解

    一.解释: 优先队列是队列的一种,不过它可以按照自定义的一种方式(数据的优先级)来对队列中的数据进行动态的排序,每次的push和pop操作,队列都会动态的调整,以达到我们预期的方式来存储. 例如,将元 ...

  6. swipe和swiper的区别

    swipe.js--移动WEB页面内容触摸滑动类库 参考http://www.jiawin.com/swipe-mobile-touch-slider 1.swipe只提供简单轮播切换,底部的圆点颜色 ...

  7. spring学习之spring 插件 for eclipse

    1) 在公司一直使用固定的eclipse IDE版本3.3 确实太out了. eclipse官方网址:http://download.eclipse.org  奇怪的是eclipse 发布的版本顺序是 ...

  8. shell编程之文件包含

    shell脚本也可以包含文件 格式: . filename 或者source filename 举例: a.sh #!/bin/sh calla() { echo "a.sh is call ...

  9. Qt支持中文显示

    Qt支持中文显示<1>.在代码中添加以下代码 QTextCodec* codec = QTextCodec::codecForLocale(); if (codec != NULL) { ...

  10. php面试题汇总四(基础篇附答案)

    1. 什么事面向对象?主要特征是什么? 面向对象是程序的一种设计方式,它利于提高程序的重用性,使程序结构更加清晰.主要特征:封装.继承.多态. 2. SESSION 与 COOKIE的区别是什么,请从 ...