lower_bound函数的使用
来源:互联网 发布:sql的链接服务器 编辑:程序博客网 时间:2024/05/16 10:54
函数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 ;}
转载自:http://blog.csdn.net/niushuai666/article/details/6734403
0 0
- lower_bound函数的使用
- 函数lower_bound()的使用
- 有关lower_bound()函数的使用
- lower_bound()与upper_bound()函数的使用
- lower_bound upper_bound 【 二分函数的使用】
- lower_bound() 的使用
- lower_bound的使用
- 有关lower_bound的比较函数
- STL--lower_bound()函数的用法
- map::lower_bound/upper_bound的使用
- map::lower_bound/upper_bound的使用
- upper_bound()和lower_bound()的使用
- lower_bound函数
- lower_bound函数
- lower_bound函数
- lower_bound函数
- 论c++的二分函数lower_bound
- lower_bound函数的用法(STL库函数)
- Java基础笔记-第十五记
- Android.mk高级写法
- 高手速成android开源项目【View篇】
- HHVM安装使用教程
- 第十一周项目6-回文、素数(输出10000以内的可逆素数)
- lower_bound函数的使用
- 多线程之线程通信条件Condition二
- Makefile变量---简单的文本替换
- Linux学习路线
- 我的第一个web开发环境:基于eclipse java EE 的java web系统搭建
- 数据结构之归并排序(迭代实现)
- 归并排序
- 第十一周项目六 (2+)输出1000以内所有回文数
- MySQL数据库导入到SQL Server