upper_bound 源码

template <class ForwardIterator, class T>
ForwardIterator upper_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = std::distance(first,last);
while (count>0)
{
it = first; step=count/2; std::advance (it,step);
if (!(val<*it)) // or: if (!comp(val,*it)), for version (2)
{ first=++it; count-=step+1; }
else count=step;
}
return first;
}

lower_bound 源码

template <class ForwardIterator, class T>
ForwardIterator lower_bound (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator it;
iterator_traits<ForwardIterator>::difference_type count, step;
count = distance(first,last);
while (count>0)
{
it = first; step=count/2; advance (it,step);
if (*it<val) { // or: if (comp(*it,val)), for version (2)
first=++it;
count-=step+1;
}
else count=step;
}
return first;
}

若val在原数组中存在,则upper_bound返回最后一个val的位置,lower_bound返回第一个val的位置

若val在原数组中不存在,则upper_bound和lower_bound返回的都是val因放在哪个位置而不影响原序列

[C++] upper_bound和lower_bound的更多相关文章

  1. 【刷题记录】 && 【算法杂谈】折半枚举与upper_bound 和 lower_bound

    [什么是upper_bound 和 lower_bound] 简单来说lower_bound就是你给他一个非递减数列[first,last)和x,它给你返回非递减序列[first, last)中的第一 ...

  2. upper_bound()与lower_bound()的使用

    upper_bound()与lower_bound()的使用 c++中的许多库函数可以使我们的代码量大大减少,也可使问题简单化.很早之前就接触了upper_bound()与lower_bound(), ...

  3. upper_bound和lower_bound的用法

    首先介绍这两种函数是什么意思 upper_bound是找到大于t的最小地址,如果没有就指向末尾 lower_bound是找到大于等于t的最小地址 题目链接:https://vjudge.net/con ...

  4. 二分查找、upper_bound、lower_bound

    整理及总结二分查找的判断和边界细节 修改版 package com.leej.binarysearch; import java.util.Arrays; /** * @author jerry * ...

  5. upper_bound()和lower_bound()

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

  6. C++中二分法之upper_bound()、lower_bound、binary_search()函数

    前言 数组.容器vector都适用,在头文件"algorithm"中 下面的例子是针对容器的,注意返回的是距离元素3最近的指针it,输出的是*it结果为元素4,假如我想得到位置而非 ...

  7. lower_bound && upper_bound

     用lower_bound进行二分查找 ●在从小到大排好序的基本类型数组上进行二分查找. 这是二分查找的一种版本,试图在已排序的[first,last)中寻找元素value.如果[first,last ...

  8. hdu 5178(二分-lower_bound,upper_bound)

    pairs Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submi ...

  9. lower_bound和upper_bound函数

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

随机推荐

  1. MongoDB数据模型和索引学习总结

    MongoDB数据模型和索引学习总结 1. MongoDB数据模型: MongoDB数据存储结构: MongoDB针对文档(大文件採用GridFS协议)採用BSON(binary json,採用二进制 ...

  2. hdu-3401-Trade-单调队列优化的DP

    单调队列入门题... dp[i][j]:第i天.手中拥有j个股票时,获得的最大利润. 若第i天不买不卖:dp[i][j]=max(dp[i][j],dp[i-1][j]); 若第i天买         ...

  3. MVVM 中 ViewModelBase和 CommandBase

    public class ViewModelBase : INotifyPropertyChanged , IDisposable { public virtual string DisplayNam ...

  4. BZOJ 1898 构造矩阵+矩阵快速幂

    思路: T的最小公倍数是12 那么12以内暴力 整除12 的部分用矩阵快速幂 //By SiriusRen #include <cstdio> #include <cstring&g ...

  5. 电信流氓注入JS

    (function () { var cs_url = _pushshowjs_.url, cs_delay = window.cs_delay; var cs_styles = window.sty ...

  6. (转载)Android支付宝支付封装代码

    Android支付宝支付封装代码 投稿:lijiao 字体:[增加 减小] 类型:转载 时间:2015-12-22我要评论 这篇文章主要介绍了Android支付宝支付封装代码,Android支付的时候 ...

  7. 51nod 1272 最大距离 O(nlog(n)) , 快排 , 最大连续子串

    题目: 解法:排序,把值小的和索引小的放在前面,记录一下之前索引最小的就可以了. 没什么可以讲的,上代码吧: #include <bits\stdc++.h> using namespac ...

  8. SLAM概念学习之特征图Feature Maps

    特征图(或者叫地标图,landmark maps)利用参数化特征(如点和线)的全局位置来表示环境.如图1所示,机器人的外部环境被一些列参数化的特征,即二维坐标点表示.这些静态的地标点被观测器(装有传感 ...

  9. 【转载】spring boot 链接 虚拟机(Linux) redis

    原文:https://www.imooc.com/article/43279?block_id=tuijian_wz 前提是你已经安装redis且支持远程连接,redis的安装这里不再赘述,有需要的可 ...

  10. Centos6.6 系统优化

    1:最小化安装 2:修改网卡 vim /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=52:54:00:0e:c2:c3TYPE ...