STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用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值调用的情况下。)
从msdn及c++标准来看,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函数使用说明的更多相关文章
- C++ lower_bound 与 upper_bound 函数
头文件: #include <algorithm> 二分查找的函数有 3 个: 参考:C++ lower_bound 和upper_bound lower_bound(起始地址,结束地址 ...
- STL 源码分析《5》---- lower_bound and upper_bound 详解
在 STL 库中,关于二分搜索实现了4个函数. bool binary_search (ForwardIterator beg, ForwardIterator end, const T& v ...
- lower_bound和upper_bound函数
lower_bound(ForwardIter first,ForwardIter last,const_TP & val) upper_bound(ForwardIter first,For ...
- Maximum Value(unique函数,lower_bound()函数,upper_bound()函数的使用)
传送门 在看大佬的代码时候遇到了unique函数以及二分查找的lower_bound和upper_bound函数,所以写这篇文章来记录以备复习. unique函数 在STL中unique函数是一个去重 ...
- C++二分查找:lower_bound( )和upper_bound( )
#include<algorithm>//头文件 //标准形式 lower_bound(int* first,int* last,val); upper_bound(int* first, ...
- stl map中的lower_bound和 upper_bound
map中的lower_bound和upper_bound的意思其实很简单,就两句话: map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针 map::upper_ ...
- std::binary_serach, std::upper_bound以及std::lower_bound
c++二分查找的用法 主要是 std::binary_serach, std::upper_bound以及std::lower_bound 的用法,示例如下: std::vector<int& ...
- STL:map中的lower_bound和upper_bound
今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了<STL源码剖析>这本书,发现map里面有lower_bound和 ...
- STL map详细用法和make_pair函数
今天练习华为上机测试题,遇到了map的用法,看来博客http://blog.csdn.net/sprintfwater/article/details/8765034:感觉很详细,博主的其他内容也值得 ...
随机推荐
- 使用 Chrome 开发者工具进行 JavaScript 问题定位与调试
转自:https://www.ibm.com/developerworks/cn/web/1410_wangcy_chromejs/ 引言 Google Chrome 是由 Goole 公司开发的一款 ...
- 【RS】Local Collaborative Ranking - LCR: 局部协同排序
[论文标题]Local Collaborative Ranking (WWW '14 23rd WWW ) [论文作者]Joonseok [论文链接]Paper(11-pages // Doubl ...
- memcached缓存基本概念
Memcached是一套分布式内存对象缓存系统. 用于在动态应用系统中缓存数据库的数据,减少数据库的访问压力,达到提升网站系统性能的目的:memcached在企业应用场景中一般是用来作为数据库的cac ...
- VI 基本可视模式
可视模式让你可以选择文件的一部分内容,以便作比如删除,复制等工作. 进入可视模式 v 用v命令进入可视模式.当光标移动时,就能看到有一些文本被高亮显示了,它们就是被选中的内容. 三种可视模式 v 一个 ...
- Linux下面安装RabbitMQ Cluster
安装rabbitmq cluster: 设置 Erlang Cookie安装完RabbitMQ之后,在第一台机器上面启动RabbitMQ,然后在停止.复制node1上的/var/lib/rabbitm ...
- NET MVC全局异常处理(一) 【转载】网站遭遇DDoS攻击怎么办 使用 HttpRequester 更方便的发起 HTTP 请求 C#文件流。 Url的Base64编码以及解码 C#计算字符串长度,汉字算两个字符 2019周笔记(2.18-2.23) Mysql语句中当前时间不能直接使用C#中的Date.Now传输 Mysql中Count函数的正确使用
NET MVC全局异常处理(一) 目录 .NET MVC全局异常处理 IIS配置 静态错误页配置 .NET错误页配置 程序设置 全局异常配置 .NET MVC全局异常处理 一直知道有.NET有相关 ...
- mvc 使用预置队列类型存储异常对象
using PaiXie.Utils; using System; using System.Collections.Generic; using System.Linq; using System. ...
- 跟我学SharePoint 2013视频培训课程——怎样创建文档库并上传文档(8)
课程简介 第8天,怎样在SharePoint 2013怎样创建文档库并上传文档. 视频 SharePoint 2013 交流群 41032413
- FreeSWITCH收到重复的DTMF信号
一.背景 用户是运营商手机,拨打的是运营商的固话号码进入的FreeSWITCH的IVR,进入IVR语音播报后,按指定的分机号呼相关人员. 二.现象 用户反映拨打124870找不到指定人员,以前是正常的 ...
- 为什么有时候PHP没有闭合标签结束符 ?>
找了一些资料,大家对PHP闭合标签的总结如下: 好处:如果这个是一个被别人包含的程序,没有这个结束符,可以减少很多很多问题,比如说:header, setcookie, session_start这些 ...