C++ STL 之 lower_bound and upper_bound
来源:互联网 发布:linux运维工程师工资 编辑:程序博客网 时间:2024/06/05 15:46
头文件:#include< algorithm >
1.lower_bound (first, last, const_val)
返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
2.upper_bound(first, last, const_val)
返回大于val的第一个元素位置。如果所有元素都小于val,则返回last的位置
举例如下:
一个数组number序列为:4,10,11,30,69,70,96,100.设要插入数字3,9,111.pos为要插入的位置的下标
则
pos = lower_bound( number, number + 8, 3) - number,pos = 0.即number数组的下标为0的位置。
pos = lower_bound( number, number + 8, 9) - number, pos = 1,即number数组的下标为1的位置(即10所在的位置)。
pos = lower_bound( number, number + 8, 111) - number, pos = 8,即number数组的下标为8的位置(但下标上限为7,所以返回最后一个元素的下一个元素)。
//uva10474#include<cstdio>#include<algorithm>using namespace std;const int maxn=10000;int main(){ int arr[maxn],n,q,x,i,p,count=0; while(scanf("%d %d",&n,&q)==2&& n) { printf("CASE# %d:\n",++count); for(i=0;i<n;i++) scanf("%d",&arr[i]); sort(arr,arr+n); while(q--) { scanf("%d",&x); p=lower_bound(arr,arr+n,x)-arr; if(arr[p]==x) printf("%d found at %d\n",x,p+1); else printf("%d not found\n",x); } } return 0;}
阅读全文
0 0
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- STL之lower_bound()、upper_bound()、binary_search
- STL函数之lower_bound upper_bound
- C++ STL 之 查找函数 lower_bound and upper_bound用法讲解
- 【STL】STL之lower_bound与upper_bound
- STL: lower_bound(·) and upper_bound(·)
- STL: lower_bound, upper_bound用法(C++)
- C++STL算法lower_bound、upper_bound、equal_range
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- stl函数之lower_bound 与 upper_bound
- STL之sort(),binary_search(),lower_bound(),upper_bound()函数
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- STL之二分查找(binary_search(),lower_bound(),upper_bound() )
- C++ STL之lower_bound和upper_bound
- c++设计模式之单例模式
- Nginx的安装
- delphi监控windows远程控制消息
- 为什么读了很多书,却学不到什么东西?
- #Thinking in Java阅读笔记# 第五章 初始化与清理
- C++ STL 之 lower_bound and upper_bound
- MVp
- MySQL 性能优化技巧
- 重温基数排序
- MySQL的or/in/union与索引优化
- 文章标题
- 什么是闭包
- 28个java常用的工具类.zip
- 长文干货:如何轻松应对工作中最棘手的13种场景?