STL--lower_bound()upper_bound();
又是两个黑科技一般的存在。
首先我们来介绍一下这两个函数:
ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)
返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
ForwardIter upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)
返回一个非递减序列[first, last)中第一个大于val的位置。
如图:
两种函数均采用了二分的方法。
注意:
函数调用序列必须有序。
upper_bound()和lower_bound()的返回值都是迭代器的位置,不能直接与int等类型进行赋值。
std::lower_bound:
default (1) |
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val); |
---|
custom (2) |
template <class ForwardIterator, class T, class Compare>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp); |
---|
作用:
返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
源代码为:
1 template <class ForwardIterator, class T> 2 ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val) 3 { 4 ForwardIterator it; 5 iterator_traits<ForwardIterator>::difference_type count, step; 6 count = distance(first,last); 7 while (count>0) 8 { 9 it = first; step=count/2; advance (it,step); 10 if (*it<val) { // or: if (comp(*it,val)), for version (2) 11 first=++it; 12 count-=step+1; 13 } 14 else count=step; 15 } 16 return first; 17 }
Example:
1 // lower_bound/upper_bound example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::lower_bound, std::upper_bound, std::sort 4 #include <vector> // std::vector 5 6 int main () { 7 int myints[] = {10,20,30,30,20,10,10,20}; 8 std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 9 10 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 11 12 std::vector<int>::iterator low,up; 13 low=std::lower_bound (v.begin(), v.end(), 20); // ^ 14 up= std::upper_bound (v.begin(), v.end(), 20); // ^ 15 16 std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; 17 std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; 18 19 return 0; 20 }
Output:
lower_bound at position 3 upper_bound at position 6
资料参考:
http://www.cplusplus.com/reference/algorithm/lower_bound/
std::upper_bound:
default (1) |
template <class ForwardIterator, class T>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val); |
---|---|
custom (2) |
template <class ForwardIterator, class T, class Compare>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp); |
作用:
返回一个非递减序列[first, last)中第一个大于val的位置。
源代码为:
1 template <class ForwardIterator, class T> 2 ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val) 3 { 4 ForwardIterator it; 5 iterator_traits<ForwardIterator>::difference_type count, step; 6 count = std::distance(first,last); 7 while (count>0) 8 { 9 it = first; step=count/2; std::advance (it,step); 10 if (!(val<*it)) // or: if (!comp(val,*it)), for version (2) 11 { first=++it; count-=step+1; } 12 else count=step; 13 } 14 return first; 15 }
Example:
1 // lower_bound/upper_bound example 2 #include <iostream> // std::cout 3 #include <algorithm> // std::lower_bound, std::upper_bound, std::sort 4 #include <vector> // std::vector 5 6 int main () { 7 int myints[] = {10,20,30,30,20,10,10,20}; 8 std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 9 10 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 11 12 std::vector<int>::iterator low,up; 13 low=std::lower_bound (v.begin(), v.end(), 20); // ^ 14 up= std::upper_bound (v.begin(), v.end(), 20); // ^ 15 16 std::cout << "lower_bound at position " << (low- v.begin()) << '\n'; 17 std::cout << "upper_bound at position " << (up - v.begin()) << '\n'; 18 19 return 0; 20 }
Output:
lower_bound at position 3 upper_bound at position 6
资料参考:
http://www.cplusplus.com/reference/algorithm/upper_bound/
附一道简单例题:
Interesting drink
转载于:https://www.cnblogs.com/Kiven5197/p/5767583.html
STL--lower_bound()upper_bound();相关推荐
- STL源码剖析 lower_bound | upper_bound | binary_search
lower_bound 二分查找的一种版本,试图在已经排序的区间内查找元素value,如果区间内存在和value数值相等的元素,便返回一个迭代器,指向其中的第一个元素. 如果没有数值相等的元素,会返回 ...
- C++ STL: lower_bound 和 upper_bound
接口声明 以下有两个不同的版本 lower_bound template <class ForwardIterator, class T>ForwardIterator lower_bou ...
- c++ 二分查找的函数 lower_bound upper_bound binary_search
简介 C++ STL 中二分查找函数主要有这三种: lower_bound() upper_bound() binary_search() 这三个函数都运用于有序区间. 用法 1. lower_bou ...
- pair用法 lower_bound upper_bound
int a,b;pair<int,int>p;cin>>a>>b;p=make_pair(a,b);cout<<p.first<<" ...
- lower_bound()/upper_bound()函数(C++)
文章目录 1.lower_bound()/upper_bound()函数简单介绍 2.lower_bound()/upper_bound()函数分析 3.lower_bound()/upper_bou ...
- lower_bound, upper_bound, greater, less 用法
一般lower_bound(), upper_bound() 基本用法 定义: algorithm头文件的函数 功能: 二分查找,查询一个数组(vector也可以使用)中大于给定数的位置 条件: 因为 ...
- C++ STL lower_bound,upper_bound的使用总结
头文件:#include 时间复杂度:一次查询O(log n),n为数组长度. lower_bound: 功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置. 返回值:如果 ...
- 给不会调用C++STL库中二分函数lower_bound,upper_bound,binary_search同学的一些话!
lower_bound算法返回第一个大于等于给定值所在的位置.设置两个指针start和last,其中start指向数组的起始位置,last指向数组末尾位置之后的位置.当start和last指向相同位置 ...
- 标准模板库(STL)之算法篇 —— lower_bound/upper_bound
无论是 lower_bound 还是 upper_bound 都作用于有序区间.也即事先需要对无需序列进行排序. 1. lower_bound 其返回值是:在不破坏排序状态的原则下,可插入 value ...
- STL之lower_bound,upper_bound二分查找函数 结构体
codeforces上的代码是开放的,常常就能看到本渣与大神们的差距 比如二分查找... 1.在数组中,找出第一个4所在位置 输入: 14 4 1 2 2 3 4 4 4 4 5 6 7 9 9 10 ...
最新文章
- vuetify table_vuex 封装设计全局可用的vuetify中的snackbar
- [OS复习]存储管理1
- INamingContainer接口解决多个自定义控件ID冲突
- C++之yaml-cpp开源库的使用笔记
- 统治世界的十大算法(转)
- 蓝牙协议栈中的 OSAL
- 一、SCVMM2008R2安装部署
- windows安装软件最好使用独立的文件夹
- matlab单元刚度矩阵,求助:关于有限元三角形单元合成总刚度矩阵怎么处理
- ZigBee--CC2530单片机--按键控制LED灯
- 实验二.常用网络命令
- 流利阅读12.16 ‘Aquaman’ is already a box office titan
- 《数值分析》李庆扬 03 曲线拟合的最小二乘法
- java perm 查看_JVM 分析工具和查看命令,超详细
- 什么是威胁建模及其最重要的优势?
- 达梦数据库:备份 冷备与热备
- 司铭宇老师谈大客户销售技巧之如何做好大客户营销
- 双十一一大波建站优惠来袭,这不薅点来建站?
- shell入门学习笔记-15-命令详解: 三剑客之一awk-分支语句、数组
- 数据缺失,数据异常,数据标准化的处理方法
热门文章
- Android7.0 Doze模式分析(一)Doze介绍 amp; DeviceIdleController
- 加快推进智慧城市建设 促进城乡共发展共繁荣
- 《漫画线性代数》读书笔记 用矩阵解方程组
- 绝对定位元素设置水平居中
- Spark资源分配异常闪Bug
- Gradle sync failed: Minimum supported Gradle version is 3.3.Current version is 3.2
- Widget创建过程(将RemoteViews发给WidgetHost)
- GNU (内部)make函数
- GC垃圾回收的三色标记算法
- 【Redis】11.Redis事务、事务锁