C++ Essentials 之 lower_bound 和 upper_bound 的比较函数格式不同
第一次注意到这个问题。
cppreference 上的条目:
lower_bound
upper_bound
C++17 草案 N4659
lower_bound
template<class ForwardIterator, class T>
ForwardIterator
lower_bound(ForwardIterator first, ForwardIterator last, const T& value);
template<class ForwardIterator, class T, class Compare>
ForwardIterator
lower_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
Requires: The elements e of [first, last) shall be partitioned with respect to the expression e < value or comp(e, value).
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the
range [first, i) the following corresponding conditions hold: *j < value or comp(*j, value) != false.
upper_bound
template<class ForwardIterator, class T>
ForwardIterator
upper_bound(ForwardIterator first, ForwardIterator last, const T& value);
template<class ForwardIterator, class T, class Compare>
ForwardIterator
upper_bound(ForwardIterator first, ForwardIterator last, const T& value, Compare comp);
Requires: The elements e of [first, last) shall be partitioned with respect to the expression !(value < e) or !comp(value, e).
Returns: The furthermost iterator i in the range [first, last] such that for every iterator j in the
range [first, i) the following corresponding conditions hold: !(value < *j) or comp(value, *j) == false.
分析
为何如此设计?
这是有原因的。
lower_bound 和 upper_bound 的比较函数都只能判定严格偏序。也就是说 comp(value, j) 只能判定 value < *j,而不能判定 value == *j 和 *j < value 。
如果 upper_bound 的比较函数只能判定 *j < value,那么对于一个有序的全序序列,它就无法二分找出大于 value 的第一个元素所在的位置(iterator)。原因在于对于任意 iterator j,判定 *j > value 根本不可能。
C++ Essentials 之 lower_bound 和 upper_bound 的比较函数格式不同的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
随机推荐
- 解决centos7 nslookup:command not found
解析域名www.google.com时,报错,命令找不到,是因为新安装的centos7,没有安装bind-utils,安装后就可以运行nslookup了
- ScriptMaker
0x00 前言 pwn脚本千篇一律,之前也是保存了一份模板,每次都用它,但还是觉得每次都复制一次各种名字还是有的累,于是就写了一份脚本生成器 0x01 ScriptMaker #!/usr/bin/e ...
- CURLOPT_PROGRESSFUNCTION
Author:李强(李祥祥) Date :2012-12-15 17:26 LIBCURL官方网站都没有说清楚道明白进度函数的参数的作用,这点我感觉文档很片面,经总结如下: size_t CUpl ...
- 随机数生成器java实现
/** 设计一个随机数生成器,可以产生给定平均概率的随机证书序列. 即输入一个概率比如:0.9 然后输入要求的概率样本个数比如:1000 输出一个接近所输入的0.9的概率数(要求样本数越大越接近输入的 ...
- linux网络编程之断点传输文件
以下载链接"http://www.boa.org/boa-0.94.13.tar.gz"为例: 断点续传实验大概步骤: ===================== 1,使用geth ...
- mysql基础,事物
- 绘制圆形 和 椭圆形:边圆形 imageellipse() 、 填充圆形imagefilledellipse()
<?php //1. 绘制图像资源(创建一个画布) $image = imagecreatetruecolor(500, 300); //2. 先分配一个绿色 $green = imagecol ...
- 第1-5章 慕课网微信小程序开发学习笔记
第1章 前言:不同的时代,不同的Web --微信小程序商城构建全栈应用 http://note.youdao.com/noteshare?id=a0e9b058853dbccf886c1a890594 ...
- 如何用eclipse运行导入的maven项目
1.配置jdk系统环境变量.找到安装的jdk的安装目录,新建系统环境变量,变量名为JAVA_HOME(作为一个引用),变量值为该路径. 找到Path,将%JAVA_HOME%/bin; 添加到变量值的 ...
- 初学python来进行odoo12版本开发
这是我的第一篇博客.请多多指教! 首先要下载odoo-12的源代码 官方下载路径: https://github.com/odoo/odoo/archive/12.0.zip 随便新 ...