头文件: #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 函数的更多相关文章

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

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

  2. lower_bound和upper_bound函数

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

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

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

  4. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

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

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

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

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

  7. lower_bound()函数,upper_bound()函数

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

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

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

  9. STL之lower_bound和upper_bound

    ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一个非递减序列[first, la ...

随机推荐

  1. 结合element-ui封装的一个分页函数

    第一次写博客,专门写给菜鸟看的,如果你是老鸟,你可以直接无视. 首先我们从豆瓣api获取到电影的数据列表 然后我们把他们切成一块一块的小数组 最后的数组将会是这样  原理就是以上的内容,接下来直接附上 ...

  2. 配置OSPF认证

      按照上图拓扑配置路由器的IP 配置完后测试直连网段连通性 搭建OSPF网络 注意是多区域的配置,R2是ABR 连着area0和area1 并且每个路由器的环回接口IP也要加进去  此时密码以明文方 ...

  3. 转 jvisualvm 工具使用 https://www.cnblogs.com/kongzhongqijing/articles/3625340.html

    VisualVM 是Netbeans的profile子项目,已在JDK6.0 update 7 中自带(java启动时不需要特定参数,监控工具在bin/jvisualvm.exe). https:// ...

  4. [Git] 004 初识 Git 与 GitHub 之查看历史记录

    在 GitHub 的 UI 界面使用 Git 查看历史纪录 1. 右侧有个 history 2. 点击后跳转页面 3. 点击相应标题(commit 时写的),进入相应版本(历史) 4. 我选择了 I ...

  5. [2019杭电多校第二场][hdu6598]Harmonious Army(最小割)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6598 题意是说一个军队有n人,你可以给他们每个人安排战士或者法师的职业,有m对人有组合技,组合技的信息 ...

  6. 小白学Python(13)——pyecharts 绘制 柱状图/条形图 Bar

    Bar-基本示例 from example.commons import Faker from pyecharts import options as opts from pyecharts.char ...

  7. P3379 【模板】最近公共祖先(LCA)(欧拉序+rmq)

    P3379 [模板]最近公共祖先(LCA) 用欧拉序$+rmq$维护的$lca$可以做到$O(nlogn)$预处理,$O(1)$查询 从这里剻个图 #include<iostream> # ...

  8. springboot多数据源&动态数据源(主从)

    多数据源 使用Spring Boot时,默认情况下,配置DataSource非常容易.Spring Boot会自动为我们配置好一个DataSource. 如果在application.yml中指定了s ...

  9. C#取模的理解:为什么当a<b,a%b=a?

    一,取模a%b 1,如果a>b,例如10%7=3,这是什么原因呢?可以根据下面的理解 10 =7*1+3,则模就是3 2,如果a<b,例如7%10 = 7,这时怎么得到的呢?根据下面来理解 ...

  10. linux加大服务器文件描述符

    最简单的说,在 unix/liux 里面,你的服务只要开启一个进程,就要占用文件描述符的.liunx 默认 是 1024,如果描述符少了,你的访问量多了,你的服务器支撑不了,所以要把描述符加大. #e ...