接口声明

以下有两个不同的版本

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的位置

C++ STL: lower_bound 和 upper_bound相关推荐

  1. C++ STL lower_bound,upper_bound的使用总结

    头文件:#include 时间复杂度:一次查询O(log n),n为数组长度. lower_bound: 功能:查找非递减序列[first,last) 内第一个大于或等于某个元素的位置. 返回值:如果 ...

  2. STL 二分查找 upper_bound和lower_bound用法

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

  3. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

  4. lower_bound和 upper_bound 用法(STL)

    lower_bound和 upper_bound的头文件是#include<algorithm> lower_bound 返回第一个大于等于 x 的数的地址/迭代器 upper_bound ...

  5. lower_bound和upper_bound算法

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  6. C++/C--二分查找之lower_bound( )和upper_bound( )【转载】

    文章目录 1 头文件及相关函数 2 函数lower_bound() 3 函数upper_bound() 4 示例 1 头文件及相关函数 头文件: #include <algorithm> ...

  7. C++ lower_bound 与 upper_bound 函数

    头文件: #include  <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...

  8. 关于lower_bound( )和upper_bound( )的常见用法

    ower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begin,end,num):从数组 ...

  9. upperbound找不到_关于lower_bound( )和upper_bound( )的常见用法

    头文件:#include lower_bound( )和upper_bound( )都是利用二分查找的方法在一个排好序的数组中进行查找的. 在从小到大的排序数组中, lower_bound( begi ...

最新文章

  1. DL之YoloV3:YoloV3论文《YOLOv3: An Incremental Improvement》的翻译与解读
  2. 2015 UESTC Training for Search Algorithm String - M - Palindromic String【Manacher回文串】
  3. Web API系列(三)统一异常处理
  4. 前端学习(1400):多人管理20代码优化
  5. 《力学》课时二质点运动学(二)
  6. java union pay 代码_Java标记了union / sum类型
  7. 速修复!VMware vCenter Server 所有版本受严重的 RCE 漏洞影响
  8. pbs 作业管理命令
  9. 托管银行数字化转型发展探讨|专家视角
  10. 2018蚂蚁金服Java开发实习生一面面经
  11. sam卡和sim卡区别_关于PSAM卡、SAM卡、SIM卡的特征和区别
  12. did you register the component correctly? For recursive components, make sure to provide the “name“
  13. 【海子诗全编】序及后记
  14. linux升级内核后vnc显示没有桌面,Intel NUC(NUC6i3SYH)在不接显示器的情况下VNC不显示桌面(Ubuntu 18.04)...
  15. Java实现TCP的长短链接
  16. Dr.com客户端提示“使用代理软件”并断网怎么解决?
  17. orecol与mysql_MDPI News | Entropy期刊被PubMed数据库收录
  18. 拉钩教育课程管理系统接口文档
  19. vc运行库或.net framework装不上的通用解决方法
  20. 重新理解CNN(精)(为什么多个滤波器?....)

热门文章

  1. 第20章 使用LNMP架构部署动态网站环境
  2. 菜鸟初涉SQL Server的总结
  3. ASP.NET常用函数
  4. usaco Riding the Fences(欧拉回路模板)
  5. c语言产生一m序列,其特征多相式:1+x^3+x^5,M序列伪随机码在测距回答概率控制中的 - FPGA/ASIC技术 - 电子发烧友网...
  6. python中如何判断两个字符串是否相等_python怎样判断两个字符串是否相同
  7. rancher 外置 mysql_rancher使用外部数据库无法正常使用
  8. linux 唯一行数量,linux – 确定bash中具有awk或类似内容的唯一行数
  9. 查看显卡显存_强力散热别浪费 显卡超频这样搞
  10. C语言结束输入(两种方法)