C++ STL: lower_bound 和 upper_bound
接口声明
以下有两个不同的版本
lower_bound
template <class ForwardIterator, class T>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val);
template <class ForwardIterator, class T, class Compare>ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
upper_bound
template <class ForwardIterator, class T>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val);
template <class ForwardIterator, class T, class Compare>ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last,const T& val, Compare comp);
两个接口都是返回一个迭代器,且改迭代器指向一个非递减序列[first,last)内部 的一个元素地址,后续通过该迭代器继续访问剩余元素。
两个接口的主要区别是:
lower_bound 指向的是第一个大于等于val的位置
upper_bound 指向的是第一个大于val的位置
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-6B3E9GCI-1592484277761)(/Users/zhanghuigui/Library/Application Support/typora-user-images/image-20200618111744296.png)]
接口源码实现
Lower_bound
// function : lower_bound
/// @brief Returns an iterator pointing to the first element in the range
/// [first, last) that is not less than (i.e. greater or equal to) val.
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found
//-----------------------------------------------------------------------------
// 返回大于等于val的数据
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t lower_bound(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{assert((last - first) >= 0);if (last == first) return last;Iter_t itaux = internal_find_first(first, last, val, comp, flt);return (itaux == (last - 1) and comp(flt(*itaux), val)) ? last : itaux;
};//-----------------------------------------------------------------------------
// function : internal_find_first
/// @brief find if a value exist in the range [first, last).
/// Always return as valid iterator in the range [first, last-1]
/// If exist return the iterator to the first occurrence. If don't exist
/// return the first greater than val.
/// If val is greater than the *(last-1), return (last-1)
/// If val is lower than (*first), return first
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found,
//-----------------------------------------------------------------------------
// 使用internal function进行二分查找
template <class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t internal_find_first(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{Iter_t LI = first, LS = last - 1, it_out = first;while (LI != LS){it_out = LI + ((LS - LI) >> 1);if (comp(flt(*it_out), val))LI = it_out + 1;else LS = it_out;};return LS;
};
Upper bound
// function :upper_bound
/// @brief return the first element greather than val.If don't exist
/// return last
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found
/// @remarks
//-----------------------------------------------------------------------------
//返回大于val的数据
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t upper_bound(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt = Filter())
{assert((last - first) >= 0);if (last == first) return last;Iter_t itaux = internal_find_last(first, last, val, comp, flt);return (itaux == first and comp(val, flt(*itaux))) ? itaux : itaux + 1;
}// function : internal_find_last
/// @brief find if a value exist in the range [first, last).
/// Always return as valid iterator in the range [first, last-1]
/// If exist return the iterator to the last occurrence.
/// If don't exist return the first lower than val.
/// If val is greater than *(last-1) return (last-1).
/// If is lower than the first, return first
//
/// @param [in] first : iterator to the first element of the range
/// @param [in] last : iterator to the last element of the range
/// @param [in] val : value to find
/// @param [in] comp : object for to compare two value_t objects
/// @return iterator to the element found, if not found return last//-----------------------------------------------------------------------------
template<class Iter_t, class Filter = filter_pass<value_iter<Iter_t> >,class Compare = std::less<typename Filter::key> >
inline Iter_t internal_find_last(Iter_t first, Iter_t last,const typename Filter::key &val,const Compare & comp = Compare(), Filter flt =Filter())
{Iter_t LI = first, LS = last - 1, it_out = first;while (LI != LS){it_out = LI + ((LS - LI + 1) >> 1);if (comp(val, flt(*it_out))) LS = it_out - 1;else LI = it_out;};return LS;
};
应用
// lower_bound/upper_bound example
#include <iostream> // std::cout
#include <algorithm> // std::lower_bound, std::upper_bound, std::sort
#include <vector> // std::vectorint main () {int myints[] = {10,20,30,30,20,10,10,20};std::vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30std::vector<int>::iterator low,up;low=std::lower_bound (v.begin(), v.end(), 20); // ^up= std::upper_bound (v.begin(), v.end(), 20); // ^std::cout << "lower_bound at position " << (low- v.begin()) << '\n';std::cout << "upper_bound at position " << (up - v.begin()) << '\n';return 0;
}
输出如下
lower_bound at position 3 # 第一个大于等于20的位置
upper_bound at position 6 # 第一个大于20的位置
相关文章:

1199: 房间安排
1199: 房间安排 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 1 Solved: 1[Submit][Status][Web Board]Description 2010年上海世界博览会(Expo2010),是第41届世界博览会。于2010年5月1日至10月31日期间,在中国上海市举行。本次世博会也是由中国举办的首届世界…

oracle判断非空并拼接,oracle sql 判断字段非空,数据不重复,插入多跳数据
oracle sql 判断字段非空,数据不重复select distinct(mobile) from wx_user_mobile where active_time is not nullbegininsert into sms_submit(id,gateway_id,source_number,dest_number,message_content)values(sms_system.nextval,1,88…

量产工具介绍(2)
前面介绍了量产工具概念,U盘构造,量产工具获取途径,以及国内的芯片分类,今天,我们从注意事项及常见问题继续介绍量产相关知识注意事项厂家推出的量产工具也是在不断提高版本的,新版本添加有新主控型号驱动。…

C++ STL: 容器vector源码分析
文章目录前言vector的核心接口vector push_back实现vector 的 Allocatorvector 的 push_back总结前言 vector 是我们CSTL中经常使用的一个容器,提供容量可以动态增长,并且随机访问内部元素的数据存储功能。 这个容器我们最常使用,所以也是最…

STM32学习笔记9(SysTick滴答时钟)
我不得不说意法半导体确实有点风骚!甚至有点变态。我对ST文档 STM32F10XXX参考手册的编辑水平真是不敢恭维。手册中好多说明都是含糊不清,甚至将好多对初学者来说很重要的地方都一笔带过,让人着实摸不着头脑。比如前面我说过的关于NVIC嵌套向…

oracle存储空间管理,Oracle存储空间管理
Oracle存储空间管理1.查看每个数据文件的剩余表空间(一个表空间只对应N个数据文件,N一般等于1)主要是利用表dba_free_space(表空间剩余空间状况)和dba_data_files(数据文件空间占用情况)select b.file_id "文件ID",b.tablespace_name "表空间名",b.f…

漫谈Httpclient
引用地址: http://hc.apache.org/httpclient-3.x/ End of life The Commons HttpClient project is now end of life, and is no longer being developed. It has been replaced by the Apache HttpComponents project in its HttpClient andHttpCore modules, whic…

具体数学-扰动法
from scipy.special import combn int (input("n: "))k int (input("k: "))sum1 0for j in range(0, k):for i in range(0, n1):sum1 sum1 (i**j)*comb(k1,j)result ((n1)**(k1) - sum1)/comb(k1,k) print(result) python 中计算排列组合:…

C++ STL: 超详细 容器 deque 以及 适配器queue 和 stack 源码分析
文章目录前言deque 实现deque类_Deque_iterator 类deque 的元素插入 insert函数deque如何模拟空间连续queue 实现stack 的实现前言 C容器中 deque是一个非常有趣的容器结构,我们知道deque本身是一个支持双向扩容的结构,对外表现出连续的存储方式。 本节将…

php好的mvc中index方法,创建一个mvc应用目录架构并创建入口文件index.php
摘要:<?php require vendor/autoload.php;require pig/Base.php;define(ROOT_PATH,__DIR__./);$configrequire pig/config.php;$queryStr$_S<?php require vendor/autoload.php;require pig/Base.php;define(ROOT_PATH,__DIR__./);$configrequire pig/confi…

C语言对mysql数据库的操作
C语言对mysql数据库的操作 原文:C语言对mysql数据库的操作这已经是一相当老的话题。不过今天我才首次使用,把今天的一些体会写下来,也许能给一些新手带来一定的帮助,更重要的是供自己今后忘记的怎么使用而进行查阅的! 我们言归正传…

[转]MCC(移动国家码)和 MNC(移动网络码)
From : http://blog.chinaunix.net/uid-20484604-id-1941290.html 国际移动用户识别码(IMSI) international mobile subscriber identity 国际上为唯一识别一个移动用户所分配的号码。 从技术上讲,IMSI可以彻底解决国际漫游问题。但是由于…

虚拟机cenos 重置密码
许久不用虚拟机,临时想登录看下,结果想不起来密码了,尝试各种可能的密码都登录失败。然后百度查验各种方法,看到一篇文章然后根据上面说的成功解决了问题,贴出链接:https://blog.csdn.net/dannistang/artic…

g-git 相关命令 及其 基本原理探索 (一)
文章目录git 最小配置作用域git 创建本地仓库git log 查看版本演进.git 目录refs目录objectsgit 三种对象类型详解 (commit ,tree,blob)因为工作需求,接下来将从git的使用到其内部工作原理,来避免代码提交或者review或者版本管理上的一些尴尬,…

php表单退出怎么写,PHP提交表单失败后如何保留填写的信息
[导读]本文章来给各位同学介绍PHP提交表单失败后如何保留填写的信息一些方法总结,最常用的就是使用缓存方式了,这种方法如果网速慢是可能出问题的,最好的办法就是使用ajax了。1.使用header头设置缓存控制头Cache-c本文章来给各位同…

lists,tuples and sets of Python
(python2.7.x) Lists 列表 列表是一个有序序列(集合),可变的(可以修改),可以理解为其他语言中的数组类型,但是列表更灵活更强大。 列表由方括号[]来定义的,它的元素可以是任意类型或对象,一个列…

Javascript中使用WScript.Shell对象执行.bat文件和cmd命令
WScript.Shell(Windows Script Host Runtime Library)是一个对象,对应的文件是C:/WINDOWS/system32/wshom.ocx,Wscript.shell是服务器系统会用到的一种组件。shell 就是“壳”的意思,这个对象可以执行操作系统外壳常用…

控件绘制的方法
1处理WM_PAINT 最极端的选择是执行一个 WM_PAINT 处理程序,并且自己完成所有的绘制。这意味着,您的代码将需要进行一些与呈现控件相关的琐事 — 创建适当的设备上下文(一个或多个),决定控件的大小和位置,绘…

google gflags的参数解析,便捷实用
命令行参数解析,一直是我们后段开发人员需要经常使用的一个功能,用来从终端解析接口的输入 ,并做出对应的处理。这里为使用C/python的开发人员推荐一个便捷的命令行解析接口集 gflags。 我们之前使用C的getopt/getopt_long 函数需要自己使用…

linux进程下的线程数,Linux下查看进程线程数的方法
0x01:ps -ef只打印进程,而ps -eLf会打印所有的线程[rootcentos6 ~]# ps -ef | grep rsyslogdroot 1470 1 0 2011 ? 00:01:13 /sbin/rsyslogd -c 4root 29865 28596 0 22:45 pts/5 00:00:00 grep rsyslogd[rootcentos6 ~]# ps…

OpenCV学习(20) grabcut分割算法
在OpenCV中,实现了grabcut分割算法,该算法可以方便的分割出前景图像,操作简单,而且分割的效果很好。算法的原理参见papaer:“GrabCut” — Interactive Foreground Extraction using Iterated Graph Cuts 比如下面的一…

VMware workstation中rhel安装VMware tools失败
切换登录用户为root即可转载于:https://www.cnblogs.com/dazzleC/p/10555809.html

g-git 相关命令 及其 基本原理探索(二):git 在工作中的常用命令操作 ,超级实用!!!
上一篇git 基本原理对git的使用以及文件分布已经有了一个整体的了解。 本篇将对工作中常用的一些git 操作命令的操作进行总结归纳,方便今后查阅。 文章目录1. 分离头指针2. 通过HEAD 来进行不同提交的差异对比3. 删除不需要的分支4. 对当前分支最近一次提交的messag…

Linux中的文件寻址,Linux文件寻址算法:逻辑地址到物理地址的转换
题目描述:编写一个函数实现Linux文件寻址的算法,即读取文件当前位置到物理存储位置的转换函数,需要给出运行的测试数据,可以假设和模拟需要的数据和结构。即编写一个函数unsigned long ltop(unsigned long logblkNum). 计算逻辑块…

datatable和dataset的区别
DataSet 是离线的数据源 DataTable 是数据源中的表.当然也可以自己建一张虚表。插入数据库中 DataSet是DataTable的容器DataSet可以比作一个内存中的数据库,DataTable是一个内存中的数据表,DataSet里可以存储多个DataTabledatatable是dataset中的一个表另…

如何避免重构带来的危险
http://blog.jobbole.com/30049/ 重构代码很危险,它会给测试工作增加巨大的负担。除非你的程序需要重构,一定不要轻易重构代码。我这里所说的并不是把一个for循环改成while循环,或把一个StringBuffer改成StringBuilder,我说的是大…

ip通信(第二周)
计算机网络分为局域网(LAN)、城域网(MAN)和广域网(WAN)。拓扑结构分为星型网,树形网,分布式网络,总线型网络,环型网络和复合型网络。认识了几个常见的国际标准…

《DDIA》读书笔记
数据存储系统的经典书籍: 从数据系统的特性开始,先讲单机存储引擎 再到 分布式存储系统,最后到一些数据流的处理方式,作者深入浅出,译者更是精雕细琢,本书需要细品。 将持续阅读整理,先从理论走…

linux网卡设置adsl上网,Linux下设置ADSL自动拨号上网
前段时间下载了红帽的linux,版本为redhat 9.0,整整刻了三张CD。最初是为了体验一下linux下QQ聊天软件的功能,最后因内核太低(官方推荐内核在2.6以上,我下载的版本是2.4)而告终。最大的收获是了解了linux下文件系统及linux下软件与…

安卓天天酷跑脚本刷高分图文教程
http://news.gamedog.cn/a/20130923/241742.html