lower_bound() 与 upper_bound()
1. lower_bound()
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数组的下标为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 ;
}
2. 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 ;
}
参考:http://blog.csdn.net/niushuai666/article/details/6734403
http://blog.csdn.net/niushuai666/article/details/6734650
lower_bound() 与 upper_bound()的更多相关文章
- STL源码学习----lower_bound和upper_bound算法
转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...
- [STL] lower_bound和upper_bound
STL中的每个算法都非常精妙, ForwardIter lower_bound(ForwardIter first, ForwardIter last,const _Tp& val)算法返回一 ...
- vector的插入、lower_bound、upper_bound、equal_range实例
对于这几个函数的一些实例以便于理解: #include <cstdlib> #include <cstdio> #include <cstring> #includ ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- lower_bound和upper_bound算法
参考:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html ForwardIter lower_bound(ForwardIte ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- STL源码学习----lower_bound和upper_bound算法[转]
STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...
- [转] STL源码学习----lower_bound和upper_bound算法
http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...
- STL algorithm算法lower_bound和upper_bound(31)
lower_bound原型: function template <algorithm> std::lower_bound default (1) template <class F ...
随机推荐
- MSSQL横列转纵列
上篇我们说到了纵列转横列,这篇讲下横列转纵列,具体代码: 1.建表 CREATE TABLE [dbo].[EndLongChangeAcross]( ,) NOT NULL, ) NOT NULL, ...
- Netty源码学习(五)ChannelInitializer
0. ChannelInitializer简介 直接用ChannelInitializer的注释吧:A special ChannelInboundHandler which offers an ea ...
- OpenCV和Boost C++库的安装
关于一般的安装步骤,此博客给出了详细的OpenCV的安装.一个步骤也不要落下,应该是不会出问题的. 主要的坑在Boost. 不知什么原因,我的电脑装boost_1_62_0-msvc-14.0-64, ...
- 51nod 1004 n^n的末位数字【快速幂】
1004 n^n的末位数字 题目来源: Author Ignatius.L (Hdu 1061) 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 给出 ...
- [Python Cookbook] Pandas: Indexing of DataFrame
Selecting a Row df.loc[index] # if index is a string, add ' '; if index is a number, no ' ' or df.il ...
- #420 Div2 C
#420 Div2 C 题意 不断把数加入到一个栈里,取数的时候要求按照 1~n 的顺序取数,每次取数保证数一定在栈里,如果要取的数不在栈头,可以选择对栈排序一次.问最少排序几次. 分析 只要栈头的数 ...
- Linux/Unix面试题
shell中如何改变文件中的某个关键字 unix命令 unix shell中在特定文件夹内查找包含指定字符串的文件用哪个命令 如何用要shell找到指定目录下的最近一天更新的文件,要包含子目录 Lin ...
- (转)Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
http://www.xuanyusong.com/archives/1427 异步任务相信大家应该不会陌生,那么本章内容MOMO将带领大家学习Unity中的一些异步任务.在同步加载游戏场景的时候通 ...
- 小程序 wx:for 循环嵌套
json数据: [//library-6F [//library-6F-601 [//id:1-1 ,8(Y/N),9(Y/N)……21(Y/N) 'Y','Y','Y' ...
- 【Linux】Linux下 环境变量/etc/profile、/etc/bashrc、~/.bashrc的区别【转】
转自:http://blog.csdn.net/qiao1245/article/details/44650929 ------------------------------------------ ...