STL中lower_bound()、 upper_bound()的返回值
来源:互联网 发布:apa酒店 知乎 编辑:程序博客网 时间:2024/05/08 11:26
lower_bound()返回值
函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于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,所以返回最后一个元素的下一个元素)。
所以,要记住:函数lower_bound()在first和last中的前闭后开区间进行二分查找,返回大于或等于val的第一个元素位置。如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!~
返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
测试代码如下:
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- using namespace std;
- int main()
- {
- const int VECTOR_SIZE = 8 ;
- // Define a template class vector of int
- typedef vector<int > IntVector ;
- //Define an iterator for template class vector of strings
- typedef IntVector::iterator IntVectorIt ;
- IntVector Numbers(VECTOR_SIZE) ;
- IntVectorIt start, end, it, location ;
- // Initialize vector Numbers
- Numbers[0] = 4 ;
- Numbers[1] = 10;
- Numbers[2] = 11 ;
- Numbers[3] = 30 ;
- Numbers[4] = 69 ;
- Numbers[5] = 70 ;
- Numbers[6] = 96 ;
- Numbers[7] = 100;
- start = Numbers.begin() ; // location of first
- // element of Numbers
- end = Numbers.end() ; // one past the location
- // last element of Numbers
- // print content of Numbers
- cout << "Numbers { " ;
- for(it = start; it != end; it++)
- cout << *it << " " ;
- cout << " }\n" << endl ;
- // return the first location at which 10 can be inserted
- // in Numbers
- location = lower_bound(start, end, 1) ;
- cout << "First location element 10 can be inserted in Numbers is: "
- << location - start<< endl ;
- }
upper_bound()返回值
函数upper_bound()返回的在前闭后开区间查找的关键字的上界,如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置,同样,如果插入元素大于数组中全部元素,返回的是last。(注意:此时数组下标越界!!)
返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置
测试代码如下:
- #include <iostream>
- #include <algorithm>
- #include <functional>
- #include <vector>
- using namespace std;
- void main()
- {
- const int VECTOR_SIZE = 8 ;
- // Define a template class vector of int
- typedef vector<int, allocator<int> > IntVector ;
- //Define an iterator for template class vector of strings
- typedef IntVector::iterator IntVectorIt ;
- IntVector Numbers(VECTOR_SIZE) ;
- IntVectorIt start, end, it, location, location1;
- // Initialize vector Numbers
- Numbers[0] = 4 ;
- Numbers[1] = 10;
- Numbers[2] = 10 ;
- Numbers[3] = 30 ;
- Numbers[4] = 69 ;
- Numbers[5] = 70 ;
- Numbers[6] = 96 ;
- Numbers[7] = 100;
- start = Numbers.begin() ; // location of first
- // element of Numbers
- end = Numbers.end() ; // one past the location
- // last element of Numbers
- // print content of Numbers
- cout << "Numbers { " ;
- for(it = start; it != end; it++)
- cout << *it << " " ;
- cout << " }\n" << endl ;
- //return the last location at which 10 can be inserted
- // in Numbers
- location = lower_bound(start, end, 9) ;
- location1 = upper_bound(start, end, 10) ;
- cout << "Element 10 can be inserted at index "
- << location - start<< endl ;
- cout << "Element 10 can be inserted at index "
- << location1 - start<< endl ;
- }
- STL中lower_bound()、 upper_bound()的返回值
- upper_bound 和lower_bound 的返回值问题
- stl lower_bound,upper_bound的实现;
- lower_bound()和upper_bound()返回值
- C/C++-STL中lower_bound与upper_bound的用法
- C++ STL 中lower_bound() 和 upper_bound()讲解
- upper_bound()返回值 lower_bound()返回值
- lower_bound()返回值 & upper_bound()返回值
- lower_bound upper_bound的简单实现(STL)
- STL的lower_bound和upper_bound源码
- stl lower_bound 和 upper_bound
- C++ STL lower_bound upper_bound
- C++ STL lower_bound & upper_bound
- STL lower_bound和upper_bound
- STL lower_bound与upper_bound
- [STL] lower_bound和upper_bound
- lower_bound/upper_bound(STL)
- STL函数 lower_bound / upper_bound
- cocos2d-x 3.0屏幕 抖动特效 重写CCShake
- 巧妙利用before和after伪类实现文字的展开和收起
- hdu 2680 Choose the best route
- Redis与Memcached的区别
- 树形DP+贪心(乱搞)(HDU4714)
- STL中lower_bound()、 upper_bound()的返回值
- Climbing Stairs
- 搜索Maven仓库 获取 groupid artifactId
- HTML+CSS小结
- uva-111 G - History Grading
- 八旬安徽老人在漳州辛苦养鱼两年 一夜间遭哄抢
- Xamarin.Android之下拉刷新
- 8种不同类型的数据库对比!
- linux系统总线SPI总线四之SPI设备驱动程序