转自http://blog.csdn.net/niushuai666/article/details/6734403

函数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的位置是越界的!!~

返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置

转自:http://blog.csdn.net/niushuai666/article/details/6734650

函数upper_bound()返回的在前闭后开区间查找的关键字的上界,如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)

返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置

lower_bound() upper_bound()函数的更多相关文章

  1. 奇思妙想:利用野指针和lower_bound()/upper_bound()函数实现整数二分

    众所周知,c++的STL中提供了三个二分查找函数,binary_search(),lower_bound(),upper_bound(),功能分别是找某值是否在数组中出现,找到数组中第一个大于等于某值 ...

  2. C++中lower_bound函数和upper_bound函数

    STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...

  3. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  4. lower_bound和upper_bound函数

    lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...

  5. Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)

    传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...

  6. C++ lower_bound 与 upper_bound 函数

    头文件: #include  <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...

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

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

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

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

  9. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

  10. 鬼知道是啥系列之——STL(lower_bound(),upper_bound() )

    引子,不明觉厉:   百度,渐入佳境: 头铁,入门到放弃: lower_bound(): 头文件:  #include<algorithm>函数功能:  函数lower_bound()在f ...

随机推荐

  1. 深入探讨API调用性能优化与错误处理

    ​ 随着互联网技术的不断发展,API(应用程序接口)已经成为软件系统中重要的组成部分.而优化API调用的性能以及处理错误和异常情况则是保障系统稳定性和可靠性的关键.本文将从以下几个方面来探讨如何进行性 ...

  2. DHorse v1.4.0 发布,基于 k8s 的发布平台

    版本说明 新增特性 提供Fabric8客户端操作k8s(预览)的功能,可以通过指定-Dkubernetes-client=fabric8参数开启: Vue.React应用增加Pnpm.Yarn的构建方 ...

  3. 小札 Combinatorics & Inclusion-Exclusion Principle 1

    「codeforces - 340E」Iahub and Permutations link. 把 \(1,\dots,n\) 中剩下没被固定的数的数量记作 \(s\),再把这其中不担心有会填到自己身 ...

  4. 使用 OpenTelemetry 构建 .NET 应用可观测性(3):.NET SDK 概览

    目录 前言 概览 opentelemetry-dotnet opentelemetry-dotnet-contrib opentelemetry-dotnet-instrumentation SDK ...

  5. 手动实现Transformer

      Transformer和BERT可谓是LLM的基础模型,彻底搞懂极其必要.Transformer最初设想是作为文本翻译模型使用的,而BERT模型构建使用了Transformer的部分组件,如果理解 ...

  6. c语言代码练习6

    //输入三个数字,依次按照从大到小输出#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a = 0 ...

  7. Go语言常用标准库——fmt

    文章目录 fmt 向外输出 Print Fprint Sprint Errorf 格式化占位符 通用占位符 布尔型 整型 浮点数与复数 字符串和[]byte 指针 宽度标识符 其他falg 获取输入 ...

  8. Python基础——二分法、面向过程编程思想、有名函数、lambda、max、_min的应用、sorted排序、map的应用、filter的应用、reduce的应用

    文章目录 内容回顾 二分法 伪代码模板 面向过程编程思想 函数式 def用于定义有名函数 lambda用于定义匿名函数 调用匿名函数 匿名函数作用 匿名函数的示范 max的应用 min的应用 sort ...

  9. ApiPost发送请求报错UT000036: Connection terminated parsing multipart data

    发送请求报错Caused by: java.io.IOException: UT000036: Connection terminated parsing multipart data 这个报错是因为 ...

  10. springboot整合shiro框架学习

    springboot整合shiro框架做用户认证与授权 springboot整合shiro框架的流程主要分为: 导入shiro依赖 <dependency> <groupId> ...