lower_bound和upper_bound函数用法

  • 一,在数组中使用lower_bound()upper_bound()
  • 二、STL中的lower_bound()和upper_bound()

lower_bound()函数与upper_bound()函数在头文件中;
使用时添加头文件:

#include <algorithm>

解释:lower_bound()和upper_bound()为二分法查找元素,其时间复杂度为O(log n)。
使用条件:有序数组。

一,在数组中使用lower_bound()upper_bound()

1. int i_lower = lower_bound(arr, arr + n, val) - arr;
作用:lower_bound()函数返回数组 arr 中大于等于 val 的第一个元素的地址,若arr中的元素均小于 val 则返回尾后插入val位置地址。

2. int i_upper = upper_bound(arr, arr + n, 8) - arr
作用:upper_bound()函数返回数组 arr 中大于 val 的第一个元素的地址,若 arr 中的元素均小于等于 val 则返回尾后插入val位置地址。
示例:

#include <iostream>
#include <algorithm>
using namespace std;int main() {const int n = 6;int arr[n]{ 0,1,5,8,10,11};int i_lower = lower_bound(arr, arr + n, 8) - arr;//大于等于8int i_upper = upper_bound(arr, arr + n, 8) - arr;//大于8cout << "i_lower:" << i_lower << endl;//i_lower:3cout << "i_upper:" << i_upper << endl;//i_upper:4int j_lower = lower_bound(arr, arr + n, 10) - arr;int j_upper = upper_bound(arr, arr + n, 10) - arr;cout << "j_lower:" << j_lower << endl;//j_lower:4cout << "j_upper:" << j_upper << endl;//j_upper:5return 0;
}

二、STL中的lower_bound()和upper_bound()

用法与数组中基本一样,但在STL中,返回值为迭代器。但在vector中,可以是迭代器也可以是位置。
(1)vector动态数组

int main()
{vector<int> vec{ 0,1,5,8,10,11};//有序数组vector<int>::iterator it1 = lower_bound(vec.begin(), vec.end(), 11);bool flag1 = it1 == vec.end() - 1;cout << flag1 << endl;//it1指向最后一个元素的迭代器unsigned i_lower = lower_bound(vec.begin(), vec.end(), 11) - vec.begin();cout << "i_lower:" << i_lower << endl;//位置5vector<int>::iterator it2 = upper_bound(vec.begin(), vec.end(), 11);bool flag2 = it2 == vec.end();cout << flag2 << endl;//it2为尾后迭代器unsigned j_upper = upper_bound(vec.begin(), vec.end(), 11) - vec.begin();cout << "j_upper:" << j_upper << endl;//位置6return 0;
}

运行结果

1
i_lower:5
1
j_upper:6

3) set集合(去重)

#include<algorithm>
#include<set>
using namespace std;int main()
{set<int> s{ 0,2,5,8,11};//原本就有序set<int>::iterator it1 = s.lower_bound(2);cout << *it1 << endl;set<int>::iterator it2 = s.upper_bound(2);cout << *it2 << endl;return 0;
}

运行结果:

2
5

(3)map字典

#include<algorithm>
#include<map>
using namespace std;int main()
{map<int, int> m{ {1,2},{2,2},{9,2},{7,2} };//有序map<int, int>::iterator it1 = m.lower_bound(2);cout << it1->first << endl;//it1->first=2map<int, int>::iterator it2 = m.upper_bound(2);cout << it2->first << endl;//it2->first=7;return 0;
}

运行结果:

2
7

参考:https://blog.csdn.net/weixin_42051815/article/details/115873582

lower_bound()与upper_bound()相关推荐

  1. C++ STL: lower_bound 和 upper_bound

    接口声明 以下有两个不同的版本 lower_bound template <class ForwardIterator, class T>ForwardIterator lower_bou ...

  2. 【ACM】Uva 1152 (4 Values whose Sum is 0) 二分查找lower_bound() 和upper_bound()的使用

    [问题描述] The SUM problem can be formulated as follows: given four lists A, B, C, D of integer values, ...

  3. vector的插入、lower_bound、upper_bound、equal_range实例

    对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...

  4. lower_bound和 upper_bound 用法(STL)

    lower_bound和 upper_bound的头文件是#include<algorithm> lower_bound 返回第一个大于等于 x 的数的地址/迭代器 upper_bound ...

  5. lower_bound和upper_bound算法

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  6. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

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

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

  8. lower_bound()和upper_bound()

    二分检索函数lower_bound()和upper_bound() lower_bound():找到大于等于某值的第一次出现 upper_bound():找到大于某值的第一次出现 必须从小到大排序后才 ...

  9. C++/C--二分查找之lower_bound( )和upper_bound( )【转载】

    文章目录 1 头文件及相关函数 2 函数lower_bound() 3 函数upper_bound() 4 示例 1 头文件及相关函数 头文件: #include <algorithm> ...

  10. C++ lower_bound 与 upper_bound 函数

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

最新文章

  1. AWS 开源 SageMaker,帮助开发人员优化机器学习模型
  2. 操作系统原理第六章:进程同步
  3. 智能风控平台核心之风控决策引擎(二)
  4. Redis进阶-Redis的惰性删除
  5. QT的QStackedWidget类的使用
  6. Python学习笔记——glob模块【文件、路径操作】
  7. 一台计算机连入计算机网络后通过该计算机,一台计算机连入计算机网络后,该计算机( )。...
  8. 老师“鬼话”全曝光!哈哈哈哈哈哈全国的老师都这样吗?
  9. html怎么添加5px高的线,css给div添加0.5px的边框
  10. springboot入门书籍推荐,电商实战之优惠卷实现(1)
  11. python如何爬取网站所有目录_用python爬虫爬取网站的章节目录及其网址
  12. list 分页_mybatis一对多分页查询
  13. 怎么获取当前页面的URL
  14. Struts2与Ajax数据交互
  15. 怎么把excel表格内的数据导入数据库?
  16. Linux时间子系统
  17. 使用Bigemap下载地图生成GST(Mapinfo格式)地图包
  18. 中国网络词“no zuo no die”被收入美国俚语词典
  19. 华硕主板怎么进入bios
  20. WIN10网络打印机-打印失败解决方案

热门文章

  1. SPI思想应用之拔插式插件
  2. 计算机程序由算法,涉及计算机程序算法的发明专利申请问答
  3. revit中在三维视图下显示房间文字和“房间集成”
  4. python合成心形_python如何绘制心形
  5. 在计算机网络互联设备中,网络互联设备
  6. 最大流 最小费用最大流模板
  7. HTML是一种标识语音,HTML实现网页端语音输入(语音识别,语义理解,olami)
  8. 64位Ubuntu安装PPStream
  9. 海航集团怎么样温暖世界的呢?
  10. 我们对Google的排名算法到底了解多少?