std::upper_bound
来源:互联网 发布:蒙古族 知乎 编辑:程序博客网 时间:2024/06/09 17:10
Returns an iterator pointing to the first element in the range [first,last) which compares greater than val.
定义如下:
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;}
示例:
#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 20 std::sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 std::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;}
output:
lower_bound at position 3
upper_bound at position 6
0 0
- std::upper_bound
- std::upper_bound & lower_bound & equal_range
- std::lower_bound()和std::upper_bound()总结
- std::upper_bound 的通用class Predicate
- std::map lower_bound,upper_bound的用法举例
- upper_bound
- STL插入排序std::upper_bound和std::lower_bound的使用,以及通用排序类
- 使用std::lower_bound和std::upper_bound解决常见的二分查找问题
- std
- std
- std
- lower_bound()、upper_bound()
- lower_bound upper_bound
- lower_bound() + upper_bound()
- lower_bound,upper_bound
- lower_bound & upper_bound
- lower_bound & upper_bound
- lower_bound & upper_bound
- Qt中QPicture对报表的实现
- 面向接口编程详解(一)——思想基础
- mysql多表合并分页
- iOS拨打电话(三种方法)
- ListView 请求(加载)可视区域Item的图片
- std::upper_bound
- 面向接口编程详解(二)——编程实例
- JSON,ListView AsyncHttpClient 等第三方包的综合实应用
- 高斯消元算法实现(Java)
- 当用eclipse远程调试tomcat上的应用
- 面向接口编程详解(三)——模式研究
- 穷人的语义处理工具箱之一:语义版Jaccard
- Python 图形处理库PIL批量修改图片大小和格式
- 【后会有期】