lower_bound

  (ForwardIterator first, ForwardIterator last,const T& val)

  (ForwardIterator first, ForwardIterator last,const T& val, Compare cmp)

  返回一个 指向 [first,last) 中第一个大于等于 数val的 iterator。

upper_bound

  (ForwardIterator first, ForwardIterator last,const T& val)

  (ForwardIterator first, ForwardIterator last,const T& val, Compare cmp)

  返回一个 指向 [first,last) 中第一个大于 数val的 iterator。

用法示例:
#include <iostream>
#include <algorithm>
using namespace std;
int a[100];
int d[100];
const int INF = 0x3f3f3f3f;
int main(){int n;int num[] = {1,5,2,6,8,1,2,5};sort(num,num+sizeof(num)/sizeof(int));// 1 1 2 2 5 5 6 8int pos1 = lower_bound(num,num+8,8) - num;//返回数组中第一个大于或等于被查数的下标位置 int pos2 = upper_bound(num,num+8,8) - num;//返回数组中第一个大于被查数的下标位置 cout << pos1 << " " << pos2 << endl;sort(num,num+sizeof(num)/sizeof(int),greater<int>());//8 6 5 5 2 2 1 1//如果从小到大排序,除非查找不到返回8,否则都返回0,没有意义。 pos1 = lower_bound(num,num+8,-1,greater<int>()) - num;//返回数组中第一个小于或等于被查数的下标位置 pos2 = upper_bound(num,num+8,8,greater<int>()) - num;//返回数组中第一个小于被查数的下标位置 cout << pos1 << " " << pos2 << endl;return 0;
}

转载于:https://www.cnblogs.com/--zz/p/10571596.html

C++ 二分查找函数 lower_bound upper_bound相关推荐

  1. c++ 二分查找的函数 lower_bound upper_bound binary_search

    简介 C++ STL 中二分查找函数主要有这三种: lower_bound() upper_bound() binary_search() 这三个函数都运用于有序区间. 用法 1. lower_bou ...

  2. 二分检索函数lower_bound()和upper_bound()

    二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...

  3. 【C++ 二分函数——lower_bound upper_bound用法】

    大家好,我是Amy~ 好久不见啊(主要是我太懒了,这几天比较忙,不想更新--再加上快开学了,我的作业还没有做完QAQ)你们的暑假作业都做完了吗(哦对,大佬不用做作业QAQ) 那今天我们就接着上次的bi ...

  4. STL之lower_bound,upper_bound二分查找函数 结构体

    codeforces上的代码是开放的,常常就能看到本渣与大神们的差距 比如二分查找... 1.在数组中,找出第一个4所在位置 输入: 14 4 1 2 2 3 4 4 4 4 5 6 7 9 9 10 ...

  5. C++的二分查找函数

    一.背景 lower_bound()和upper_bound(),binary_search()都是利用二分查找的方法在一个排好序的数组中进行查找的. lower_bound():返回大于或等于目标值 ...

  6. C语言库自带的二分查找函数bsearch函数的使用示例

    bsearch 使用二分查找,查找一个被排序过的数组 依赖头文件 #include <stdlib.h> void *bsearch(const void *key, const void ...

  7. python 二分查找函数_Python基础14_递归函数,二分查找

    一. 递归 在函数中调用函数本身, 就是递归 prthon中递归的最大深度是998 def func(n): print(n) n += 1 func(n) func(1) 递归的应用:我们可以使用递 ...

  8. 软件测试二分查找函数,二分查找

    二分查找也称折半查找(Binary Search),它是一种效率较高的查找方法.但是,折半查找要求线性表必须采用顺序存储结构,而且表中元素按关键字有序排列.[1] 中文名 二分查找 外文名 Binar ...

  9. 给不会调用C++STL库中二分函数lower_bound,upper_bound,binary_search同学的一些话!

    lower_bound算法返回第一个大于等于给定值所在的位置.设置两个指针start和last,其中start指向数组的起始位置,last指向数组末尾位置之后的位置.当start和last指向相同位置 ...

最新文章

  1. mysql 索引合并
  2. 理解JavaScript继承(二)
  3. flowable 启动流程到完成所有任务之间的数据库变化
  4. OJ1048: 阶乘表(C语言)(易错,关于int和double范围问题)
  5. 总奖金高达180万元 蚂蚁金服启动区块链创新大赛
  6. 修改telnet提示并非_热血传奇:老玩家揭秘!这4个地方都被盛大修改过,第3个80%都想不到...
  7. 剑指offer最新版_剑指offer第二版速查表
  8. arm平台下的反汇编pdf_stm32逆向与安全科普,bin文件逆向反汇编
  9. MTF直播整合导航网站源码
  10. 51单片机程序执行流程详细分析
  11. 软件测试人员必看的十本经典书籍
  12. stm32F103 模拟I2C mpu6050收到数据全为0,或者地址为209,104,0x68,0xD0的一些解决办法总结
  13. 数据库学习整理之常见运算符
  14. 电视剧《一代枭雄》观后感
  15. 稳压管和TVS管的工作原理
  16. Unity shader 空气扭曲效果
  17. Java Socket详解+基础模板+各种常见问题+端口映射
  18. react,tsx中使用微信jssdk分享总结
  19. Monkey Business 骗人的把戏
  20. 11,基于JESD204 IP核的设计实现方法

热门文章

  1. FAST:基于FPGA的SDN交换机开源项目
  2. js判断输入是否为正整数、浮点数等数字的函数代码
  3. spark mllib 预测之LinearRegression(线性回归)
  4. 操作系统选择成固定模式 HTML5是潜在方向
  5. 安装phpredis扩展
  6. 喜欢的Groove Coverage She
  7. docker 安装jenkins
  8. 玩转X-CTR100 l STM32F4 l DAC数字模拟转换
  9. 【TensorFlow篇】--DNN初始和应用
  10. C# 如何以参数的形式调用.exe程序