由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能。

STL的map、multimap、set、multiset都有三个比较特殊的函数,lower_bound、upper_bound、equal_range。

原型如下:

iterator lower_bound (const value_type& val) const;

iterator upper_bound (const value_type& val) const;

pair<iterator,iterator> equal_range (const value_type& val) const;

上面三个函数是相关联的,equal_range返回两个迭代器,第一个迭代器是lower_bound的返回值,第二个迭代器是upper_bound的返回值。(注意是使用相同val值调用的情况下。)

msdnc++标准来看,lower_bound、upper_bound两个函数用于记录允许元素重复出现的数据集中给定关键字val在当前集合中区间范围。

对诸如set、map这种关键字唯一的集合而言,lower_bound、upper_bound返回迭代器是相同,关键字val在集合中不存在,二者返回结果一样,都是按照集合实例化时给定的Compare比较,不在val之前的第一个元素(亦即之后或者等于,如果按照默认的比较类型less,函数返回的是≥val的最小的元素);如果关键在val在集合中存在,lower_bound返回val关键字本身的迭代器,upper_bound返回关键字val下一个元素迭代器。

对multiset、multimap这类关键字不唯一的集合而言。按照关键字后面一个关键字在集合中出现的次数可分为:关键字val出现在集合中,但是是唯一的,这种情况和set、map情况类似;关键字val出现在集合中,出现多次,这种情况下lower_bound返回第一个出现关键字val对应的迭代器,upper_bound返回位于关键字val对应位置后第一个不是val的位置的迭代器;关键字val不在集合中,这种情况下与set、map一致。

综合一下(按照默认集合升序排序的情况下),lower_bound、upper_bound函数不管在什么情况下,以下条件均成立。

Iterator(val) ≤ Iterator(lower_bound)≤Iterator(upper_bound)

也就是lower_bound、upper_bound构成的上下限的区间总是表示一个有效的迭代器区间(equal_range返回值),该迭代区间的长度表示关键字val在集合中出现的次数

如果二者返回值相等,表示关键字val在集合中未出现。(例外情况,集合中的所有元素均≥关键字val,返回集合的iterator::end)

如果迭代器区间长度是1,表示关键字val在集合中仅出现1次。

迭代器区间长度大于1,则表示关键字val出现多次,并且一定不是set和map这种关键字唯一的集合。

示例代码:

// g++ -o setLowerUpperBoundTest setLowerUpperBoundTest.cpp

#include <set>
#include <iostream> using namespace std ; typedef set<int> SET_INT; int main()
{
SET_INT s1;
SET_INT::iterator i;
cout << "s1.insert(5)" << endl;
s1.insert();
cout << "s1.insert(10)" << endl;
s1.insert();
cout << "s1.insert(15)" << endl;
s1.insert();
cout << "s1.insert(20)" << endl;
s1.insert();
cout << "s1.insert(25)" << endl;
s1.insert(); cout << "s1 -- starting at s1.lower_bound(12)" << endl;
// prints: 15,20,25
for (i=s1.lower_bound();i!=s1.end();i++)
cout << "s1 has " << *i << " in its set." << endl; cout << "s1 -- starting at s1.lower_bound(15)" << endl;
// prints: 15,20,25
for (i=s1.lower_bound();i!=s1.end();i++)
cout << "s1 has " << *i << " in its set." << endl; cout << "s1 -- starting at s1.upper_bound(12)" << endl;
// prints: 15,20,25
for (i=s1.upper_bound();i!=s1.end();i++)
cout << "s1 has " << *i << " in its set." << endl; cout << "s1 -- starting at s1.upper_bound(15)" << endl;
// prints: 20,25
for (i=s1.upper_bound();i!=s1.end();i++)
cout << "s1 has " << *i << " in its set." << endl; cout << "s1 -- s1.equal_range(12)" << endl;
// does not print anything
for (i=s1.equal_range().first;i!=s1.equal_range().second;i++)
cout << "s1 has " << *i << " in its set." << endl; cout << "s1 -- s1.equal_range(15)" << endl;
// prints: 15
for (i=s1.equal_range().first;i!=s1.equal_range().second;i++)
cout << "s1 has " << *i << " in its set." << endl;
}
/* Output in windows
s1.insert(5)
s1.insert(10)
s1.insert(15)
s1.insert(20)
s1.insert(25)
s1 -- starting at s1.lower_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.lower_bound(15)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(12)
s1 has 15 in its set.
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- starting at s1.upper_bound(15)
s1 has 20 in its set.
s1 has 25 in its set.
s1 -- s1.equal_range(12)
s1 -- s1.equal_range(15)
s1 has 15 in its set.
*/

上述代码正好验证并说明了lower_bound、upper_bound、equal_range三个函数的功能的功能,以及本文中的解释。

STL之std::set、std::map的lower_bound和upper_bound函数使用说明的更多相关文章

  1. C++ lower_bound 与 upper_bound 函数

    头文件: #include  <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...

  2. STL 源码分析《5》---- lower_bound and upper_bound 详解

    在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...

  3. lower_bound和upper_bound函数

    lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...

  4. Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)

    传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...

  5. C++二分查找:lower_bound( )和upper_bound( )

    #include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...

  6. stl map中的lower_bound和 upper_bound

    map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...

  7. std::binary_serach, std::upper_bound以及std::lower_bound

    c++二分查找的用法 主要是 std::binary_serach,  std::upper_bound以及std::lower_bound 的用法,示例如下: std::vector<int& ...

  8. STL:map中的lower_bound和upper_bound

    今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了<STL源码剖析>这本书,发现map里面有lower_bound和 ...

  9. STL map详细用法和make_pair函数

    今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...

随机推荐

  1. hibernate的hql语句不支持 count(case...when ....else..)?

    查看帖子:http://www.iteye.com/problems/4499 第一次这么写, 不行, 关键就是: 同是聚集函数,sum 就OK, 而 count 就不行...........后来还是 ...

  2. suricata 的安装编译

    最近打算研究suricata源码,下载并安装了稳定版3.2.3版本,操作系统是Ubuntu 16.04.2 LTS,下来描述我的操作过程: 1,安装suricata运行可能用到的库: sudo apt ...

  3. HDU 4602 Partition (矩阵乘法)

    Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. System.Web.HttpValueCollection.ThrowIfMaxHttpCollectionKeysExceeded

    昨天客户跟我说,突然一个页面频繁地报ThrowIfMaxHttpCollectionKeysExceeded这个异常.而且是数据量大的时候报错,数据量小的时候OK. 根据异常的名称也能看得差不多超过了 ...

  5. jmeter 的java请求代码在main方法里面执行

    1.新建一个java请求执行加法类 public class TestDemo { public int Tdemo(int a,int b){ int sum = 0; sum = a+b; ret ...

  6. influxdb 配置文件注释

    ### Welcome to the InfluxDB configuration file. # The values in this file override the default value ...

  7. 复习下C 链表操作(双向链表)

    双向链表 创建.删除.反转.插入 //struct #include <stdio.h> #include <stdlib.h> #include <string.h&g ...

  8. VS2017中建立ASP.NET MVC 4.0项目

    新的项目需要运行在WIN2003上,又不想用ASPX了,只好用回ASP.NET MVC4.0了,可是在VS2017中已经没有MVC4的模板了,网上下载的安装了也没有,只好把以前的MVC4的项目拿 出来 ...

  9. 深入理解Linux内核-Ext2和Ext3文件系统

    Ext2的一般特征: 1.创建Ext2文件系统时,系统管理员可以根据预期的文件平均长度来选择最佳块大小(从1024B-4096B).来减少文件碎片2.创建Ext2文件系统时,系统管理员可以根据在给定大 ...

  10. HTML5学习笔记(三):语义化和新增结构元素

    在HTML5之前,使用机器来阅读一个网页是非常困难的,我们使用不同样式的div来标记不同的内容,所以实际上机器无法得知页面的哪个部分是正文,哪个部分是标题,那么在HTML5里,针对这个问题就引入了语义 ...