lower_bounder()和upper_bound()的函数
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的位置。
lower_bound和upper_bound如下图所示:

不过除此之外,这两个函数还分别有一个重载函数,可以接受第四个参数。如果第四个参数传入greater<Type>(),其中Type改成对应类型,那么upper_bound则返回指向被查值<查找值的最小指针,lower_bound则返回指向被查值<=查找值的最小指针。
最后说一点使用的注意事项,先看这么一句话“ The elements in the range shall already be sorted according to this same criterion (operator< or comp), or at least partitioned with respect to val”(引用自http://www.cplusplus.com/reference/algorithm/upper_bound/)。简单来说,如果你用上述两个函数三个参数的那种形式,记得那个左闭右开的区间要为非递减的顺序,如果你给第四个参数传入greater<Type>(),则区间为非递增的顺序。
#include <iostream>
#include <algorithm> using namespace std; int seq1[] = {, , , , , }, seq2[] = {, , , , , };
//注释掉的是错误用法
int main()
{
//cout<<upper_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
//cout<<lower_bound(seq1, seq1+6, 3, greater<int>()) - seq1<<endl;
cout<<upper_bound(seq1, seq1+, ) - seq1<<endl;
cout<<lower_bound(seq1, seq1+, ) - seq1<<endl; cout<<endl; cout<<upper_bound(seq2, seq2+, , greater<int>()) - seq2<<endl;
cout<<lower_bound(seq2, seq2+, , greater<int>()) - seq2<<endl;
//cout<<upper_bound(seq2, seq2+6, 7) - seq2<<endl;
//cout<<lower_bound(seq2, seq2+6, 7) - seq2<<endl;
return ;
}
内容来源:【1】http://blog.csdn.net/u011008379/article/details/50725670
【2】http://blog.csdn.net/u013475704/article/details/46458723
感谢以上博主。
lower_bounder()和upper_bound()的函数的更多相关文章
- Java实现 lower_bound() 和 upper_bound()
Java实现 lower_bound() 和 upper_bound() lower_bound() 函数 lower_bound() 在 [begin, end) 进行二分查找,返回 大于或等于 t ...
- std:: lower_bound std:: upper_bound
std:: lower_bound 该函数返回范围内第一个不小于(大于或等于)指定val的值.如果序列中的值都小于val,则返回last.序列应该已经有序! eg: #include <iost ...
- 【模板】关于vector的lower_bound和upper_bound以及vector基本用法 STL
关于lower_bound和upper_bound 共同点 函数组成: 一个数组元素的地址(或者数组名来表示这个数组的首地址,用来表示这个数组的开头比较的元素的地址,不一定要是首地址,只是用于比较的& ...
- STL之map
参见http://www.cplusplus.com/reference/map/map/ template < class Key, ...
- STL之multiset
参见http://www.cplusplus.com/reference/set/multiset/ template < class T, ...
- STL之multimap
参见http://www.cplusplus.com/reference/map/multimap/ 多重映射multimap和map映射很相似,但是multimap允许重复的关键字,这使得multi ...
- 使用multimap创建重复键关联容器
在“使用 <map> 库创建关联容器”一文中,我们讨论了标准库中的 map 关联容器.但那只是 map 容器的一部分.标准库还定义了一个 multimap 容器,它与 map 类似,所不同 ...
- find-right-interval
https://leetcode.com/problems/find-right-interval/ Java里面TreeMap或者TreeSet有类似C++的lower_bound或者upper_b ...
- 标准C++中的STL容器类简单介绍
SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库. ...
随机推荐
- grep与find命令的区别
grep与find命令的区别:grep搜索的是文本,find搜索的是文件,换句话说就是grep是查找匹配条件的行,find是搜索匹配条件的文件. grep文本搜索/过滤 用法:grep[参数]搜索字符 ...
- 有趣的纯CSS实现动态晴阴雨雪
我们先来看看实现的效果吧 非常的美腻,对吧.这个是纯css,且单标签实现的哦~ 先贴完整代码,我们再来看看这个里面究竟有什么可以借鉴的知识点 <!DOCTYPE html> <htm ...
- nslookup获取域名对应的的ip地址
1.先用nslookup获得域名对应的主机ip:nslookup 域名 2.再根据主机ip获得对应的主机名称: host ip $ nslookup baidu.com Server: 127.0.1 ...
- git pull拉取远程指定分支
1.若git clone之后想拉取某个指定分支:先git pull ,然后git checkout 指定分支名称 2.若git clone之后想拉取某个指定分支:先git fetch origin 分 ...
- 原生js增加,移除类名
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- windows 2008 安装 apache + mysql + php
下载准备 php : http://windows.php.net/downloads/releases/archives/ apache : https://www.apachehaus.com/c ...
- 英特尔人工智能圆桌论坛举行 四位大咖论道AI
英特尔人工智能圆桌论坛举行 四位大咖论道AI 2017-04-01 17:57 北京2017年4月1日电 /美通社/ -- 毫无疑问,人工智能已是最具变革力的发展趋势之一,在3月31日举行的2 ...
- AntColony 磁力搜索引擎的核心
介绍 AntColony(Github)是findit磁力搜索引擎的核心.用来在DHT网络中,收集活跃资源的infohash,下载并解析资源的种子文件,存入数据库等.AntColony是若干功能的合集 ...
- springmvc 传参Required String parameter 'xxxx' is not present
报错 请求因该是已经被分配了,但是参数补全,无法被执行 加上这个参数就好了,表示请求参数,可以为空 这样的好处是,可以进入controller之后再去判断,比较好定位错误
- webpack4进阶配置
移动端CSS px自动转换成rem 需要两步来实现: px2rem-loader 在构建阶段将px转换成rem lib-flexible 页面渲染时动态计算根元素的font-size值(手机淘宝开源库 ...