C++ lower_bound 与 upper_bound 函数
头文件: #include <algorithm>
二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound
lower_bound(起始地址,结束地址,要查找的数值) 返回的是数值 第一个 出现的位置。
upper_bound(起始地址,结束地址,要查找的数值) 返回的是数值 最后一个 出现的位置。
binary_search(起始地址,结束地址,要查找的数值) 返回的是是否存在这么一个数,是一个bool值。
1 函数lower_bound() 参考:有关lower_bound()函数的使用
功能:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置.
注意:如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!
2 函数upper_bound()
功能:函数upper_bound()返回的在前闭后开区间查找的关键字的上界,返回大于val的第一个元素位置
注意:返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置。同样,如果val大于数组中全部元素,返回的是last。(注意:数组下标越界)
PS:
lower_bound(val):返回容器中第一个值【大于或等于】val的元素的iterator位置。
upper_bound(val): 返回容器中第一个值【大于】
例子:
void main()
{
vector<int> t;
t.push_back();
t.push_back();
t.push_back();
t.push_back();
t.push_back();
t.push_back();
t.push_back(); int low=lower_bound(t.begin(),t.end(),)-t.begin();
int upp=upper_bound(t.begin(),t.end(),)-t.begin();
cout<<low<<endl;
cout<<upp<<endl; system("pause");
}
屏幕输出:

参考:
STL之std::set、std::map的lower_bound和upper_bound函数使用说明
https://blog.csdn.net/jadeyansir/article/details/77015626 可以多看几遍
https://blog.csdn.net/tjpuacm/article/details/26389441
https://blog.csdn.net/lanzhihui_10086/article/details/43269511
C++ lower_bound 与 upper_bound 函数的更多相关文章
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- lower_bound和upper_bound函数
lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...
- Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)
传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- C++中lower_bound函数和upper_bound函数
STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两个函数. ...
- 二分检索函数lower_bound()和upper_bound()
二分检索函数lower_bound()和upper_bound() 一.说明 头文件:<algorithm> 二分检索函数lower_bound()和upper_bound() lower ...
- lower_bound()函数,upper_bound()函数
1.查找:STL中关于二分查找的函数有三个lower_bound .upper_bound .binary_search .这三个函数都运用于有序区间(当然这也是运用二分查找的前提),下面记录一下这两 ...
- 奇思妙想:利用野指针和lower_bound()/upper_bound()函数实现整数二分
众所周知,c++的STL中提供了三个二分查找函数,binary_search(),lower_bound(),upper_bound(),功能分别是找某值是否在数组中出现,找到数组中第一个大于等于某值 ...
- STL之lower_bound和upper_bound
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...
随机推荐
- jQ判断checked是否选中
(1).JQ1.6版本之前(不包括1.6版本)判断checkbox是否被选中用的是attr()方法,HTML代码与上面相同,只放JQ代码: console.log($("input[type ...
- dataTables使用的详细说明整理
本文共三个部分:官网|基本使用|遇到的问题 一.官方网站:http://www.datatables.club/ 二.基本使用: 1.dataTables的引入及初始化 <!--第一步:引入Ja ...
- 多线程04-ThreadPriority
; i < ; i++) { Thread.Sleep(); Console.WriteLine( ...
- [19/05/15-星期三] HTML_body标签(超链接标签和锚点)
一.超链接标签 <html> <head> <meta charset="UTF-8"> <title>04 body超链接标签学习 ...
- ajax与json总结
1.jquery中调用ajax方法 $.ajax({ async:true, type:"post", url:"xxxServlet", data:{&quo ...
- Billboard 题解 hdu2795
Billboard 题解 hdu2795 题意 有个广告牌,上面需要依次贴广告,广告的高度均为1,但是宽度不同,每次都想贴在最靠左上的位置,按照顺序进行广告的话,输出每个广告位于广告牌的高度. 解题思 ...
- yield-Python大牛必须掌握的高端语法
小白:大牛哥,刚才看到有一个函数不使用return返回结果,而是使用yield关键字返回结果,看不太明白,Python中yield关键字的用途是什么,它有什么作用呀? 大牛: 要想理解yield的作用 ...
- web框架Django一
一.django安装 # pip 安装 pip3 install Django # 克隆下载最新版本 git clone https://github.com/django/django.git # ...
- Python2/3 安装各类包的教程
1.pycryptodome(pyCrypto) pyCrypto包已经失效了,需要替换为pycryptodome 有SSR直接 pip install pycryptodome 国内用 pip in ...
- k3 cloud中获取自己开发的单据
设置基础资料(业务对象).系统自带用单据