stl map中的lower_bound和 upper_bound
map中的lower_bound和upper_bound的意思其实很简单,就两句话:
map::lower_bound(key):返回map中第一个大于或等于key的迭代器指针
map::upper_bound(key):返回map中第一个大于key的迭代器指针
所以,理解这两个函数请不要按照字面意义思考太复杂,因为仅仅是不小于(lower_bound)和大于(upper_bound)这么简单。
看两个msdn里的例子
// map_upper_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream> int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
typedef pair <int, int> Int_Pair; m1.insert ( Int_Pair ( , ) );
m1.insert ( Int_Pair ( , ) );
m1.insert ( Int_Pair ( , ) );
// 返回m1中第一个key值大于2的元素的迭代器,当然是<3,30>
m1_RcIter = m1.upper_bound( );
cout << "The first element of map m1 with a key "
<< "greater than 2 is: "
<< m1_RcIter -> second << "." << endl; // If no match is found for the key, end is returned
m1_RcIter = m1. upper_bound ( ); // m1中key值并没有大于或等于4的
if ( m1_RcIter == m1.end( ) )
cout << "The map m1 doesn't have an element "
<< "with a key greater than 4." << endl;
else
cout << "The element of map m1 with a key > 4 is: "
<< m1_RcIter -> second << "." << endl; // The element at a specific location in the map can be found
// using a dereferenced iterator addressing the location
m1_AcIter = m1.begin( );
m1_RcIter = m1. upper_bound ( m1_AcIter -> first );
cout << "The 1st element of m1 with a key greater than\n"
<< "that of the initial element of m1 is: "
<< m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key greater than 2 is: 30.
The map m1 doesn't have an element with a key greater than 4.
The 1st element of m1 with a key greater than
that of the initial element of m1 is: 20.
// map_lower_bound.cpp
// compile with: /EHsc
#include <map>
#include <iostream> int main( )
{
using namespace std;
map <int, int> m1;
map <int, int> :: const_iterator m1_AcIter, m1_RcIter;
typedef pair <int, int> Int_Pair; m1.insert ( Int_Pair ( , ) );
m1.insert ( Int_Pair ( , ) );
m1.insert ( Int_Pair ( , ) );
//key值大于等于2的是<2,20>
m1_RcIter = m1.lower_bound( );
cout << "The first element of map m1 with a key of 2 is: "
<< m1_RcIter -> second << "." << endl; // If no match is found for this key, end( ) is returned
m1_RcIter = m1. lower_bound ( ); if ( m1_RcIter == m1.end( ) )
cout << "The map m1 doesn't have an element "
<< "with a key of 4." << endl;
else
cout << "The element of map m1 with a key of 4 is: "
<< m1_RcIter -> second << "." << endl; // The element at a specific location in the map can be found
// using a dereferenced iterator addressing the location
m1_AcIter = m1.end( );
m1_AcIter--;
m1_RcIter = m1. lower_bound ( m1_AcIter -> first );
cout << "The element of m1 with a key matching "
<< "that of the last element is: "
<< m1_RcIter -> second << "." << endl;
}
The first element of map m1 with a key of 2 is: 20.
The map m1 doesn't have an element with a key of 4.
The element of m1 with a key matching that of the last element is: 30.
stl map中的lower_bound和 upper_bound的更多相关文章
- STL:map中的lower_bound和upper_bound
今天在做leetcode的Longest Increasing Subsequence题目时,需要用到二分查找,于是翻看了<STL源码剖析>这本书,发现map里面有lower_bound和 ...
- STL中的lower_bound和upper_bound的理解
STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...
- 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中的每个算法都非常精妙,接下来的几天我想集中学习一下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 map中的一些基本函数
来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博客 Map是c++的一个标准容器 ...
- STL之std::set、std::map的lower_bound和upper_bound函数使用说明
由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...
- python实现lower_bound和upper_bound
由于对于二分法一直都不是很熟悉,这里就用C++中的lower_bound和upper_bound练练手.这里用python实现 lower_bound和upper_bound本质上用的就是二分法,lo ...
- STL中的二分查找——lower_bound 、upper_bound 、binary_search
STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...
随机推荐
- Windows搭建python开发环境[一]
首先需要去python的官网下载环境.鼠标移动到Downloads的tab上,在这里可以下载. python的环境还是很人性化的,没有那么多罗里吧嗦的配置什么的,下载好以后直接无脑next就行了,直到 ...
- 16.Spark Streaming源码解读之数据清理机制解析
原创文章,转载请注明:转载自 听风居士博客(http://www.cnblogs.com/zhouyf/) 本期内容: 一.Spark Streaming 数据清理总览 二.Spark Streami ...
- 【C#】Unicode的流言终结者和编码大揭秘
如果你是一个生活在2003年的程序员,却不了解字符.字符集.编码和Unicode这些基础知识.那你可要小心了,要是被我抓到你,我会让你在潜水艇里剥六个月洋葱来惩罚你. 这个邪恶的恐吓是Joel Spo ...
- 提起Ajax请求的方式(POST)
前言 => 是ES6中的arrow function x=>x+6 就相当于 function(x){ return x+6; } 正文 XMLHttpRequest a=new XMLH ...
- eclipse JavaEE的配置
Eclipse IDE for Java EE Developers(win32) 下载地址:http://mirror.bjtu.edu.cn/eclipse/technology/epp/down ...
- TCP/IP——基础概念简记
TCP/IP协议族的分层: 应用层 运输层 网络层 链路层 互联网地址(IP地址):互联网上的每个接口必须有一个唯一的Internet地址,它一定的结构,分为ABCDE五类.A类保留给政府机构,B类分 ...
- [TJOI2017]DNA --- 后缀数组
[TJOI2017]DNA 题目描述 加里敦大学的生物研究所,发现了决定人喜不喜欢吃藕的基因序列S, 有这个序列的碱基序列就会表现出喜欢吃藕的性状,但是研究人员发现对碱基序列S,任意修改其中不超过3个 ...
- [bzoj1025][SCOI2009]游戏 (分组背包)
Description windy学会了一种游戏.对于1到N这N个数字,都有唯一 且不同的1到N的数字与之对应.最开始windy把数字按顺序1,2,3,……,N写一排在纸上.然后再在这一排下面写上它们 ...
- iScroll4插件的使用实例
iScroll是Matteo Spinelli开发的一个滚动插件,使用原生js编写,其不依赖与任何js框架.iScroll 4 完全重写了iScroll这个框架的原始代码.旨在解决移动webkit系浏 ...
- ADC for programmable logic uses one capacitor
Many electronic devices require user input for setting the application properties. Typical input dev ...