C++ STL 之 lower_bound and upper_bound
2017-10-25 18:14
471 查看
头文件:#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,所以返回最后一个元素的下一个元素)。
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; }
相关文章推荐
- C++ STL 之 lower_bound and upper_bound
- C++ STL 之 lower_bound and upper_bound
- /*****************c++ STL 里的upper_bound 和 lower_bound 的用法~~*********************/
- C/C++-STL中lower_bound与upper_bound的用法
- C++的STL进一步总结之lower_bound和upper_bound
- STL 源码分析《5》---- lower_bound and upper_bound 详解
- C++ STL 二分查找函数(lower_bound 和 upper_bound)
- STL 源码分析《5》---- lower_bound and upper_bound 详解
- C++ STL 算法:查找算法(7) lower_bound、upper_bound、equal_range
- C++ STL中的 lower_bound() 和 upper_bound()
- C++ STL lower_bound & upper_bound
- c++之STL(13) STL 算法 - 查找算法(7)lower_bound() upper_bound() equal_range(0
- STL: lower_bound(·) and upper_bound(·)
- C++ STL lower_bound 和 upper_bound
- STL: lower_bound, upper_bound用法(C++)
- C/C++-STL中lower_bound与upper_bound的用法以及cmp函数
- C++ STL:lower_bound与upper_bound实现
- C++之 lower_bound And upper_bound And insert
- C++ STL lower_bound & upper_bound
- STL之sort(),binary_search(),lower_bound(),upper_bound()函数