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的更多相关文章

  1. STL:map中的lower_bound和upper_bound

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

  2. STL中的lower_bound和upper_bound的理解

    STL迭代器表述范围的时候,习惯用[a, b),所以lower_bound表示的是第一个不小于给定元素的位置 upper_bound表示的是第一个大于给定元素的位置. 譬如,值val在容器内的时候,从 ...

  3. STL源码学习----lower_bound和upper_bound算法

    转自:http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html 先贴一下自己的二分代码: #include <cstdio&g ...

  4. STL源码学习----lower_bound和upper_bound算法[转]

    STL中的每个算法都非常精妙,接下来的几天我想集中学习一下STL中的算法. ForwardIter lower_bound(ForwardIter first, ForwardIter last,co ...

  5. [转] STL源码学习----lower_bound和upper_bound算法

    http://www.cnblogs.com/cobbliu/archive/2012/05/21/2512249.html PS: lower_bound of value 就是最后一个 < ...

  6. [转载]STL map中的一些基本函数

    来源:(http://blog.sina.com.cn/s/blog_61533c9b0100fa7w.html) - C++ map的基本操作和使用_Live_新浪博客 Map是c++的一个标准容器 ...

  7. STL之std::set、std::map的lower_bound和upper_bound函数使用说明

    由于在使用std::map时感觉lower_bound和upper_bound函数了解不多,这里整理并记录下相关用法及功能. STL的map.multimap.set.multiset都有三个比较特殊 ...

  8. python实现lower_bound和upper_bound

    由于对于二分法一直都不是很熟悉,这里就用C++中的lower_bound和upper_bound练练手.这里用python实现 lower_bound和upper_bound本质上用的就是二分法,lo ...

  9. STL中的二分查找——lower_bound 、upper_bound 、binary_search

    STL中的二分查找函数 1.lower_bound函数 在一个非递减序列的前闭后开区间[first,last)中.进行二分查找查找某一元素val.函数lower_bound()返回大于或等于val的第 ...

随机推荐

  1. 解决 .net HttpClient 调用时出现的 "A task was cancelled" 错误

    近日在系统中集成ElasticClient客户端,自动创建索引.删除索引,发现通过 ElasticClient 的 LowerLevelClient 无法正确返回结果,但是索引已成功创建或删除. 并会 ...

  2. 最新JAVA编程题全集(50题及答案)

    [程序1]题目:古典问题:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子总数为多少?    //这是一个菲波拉契数列问题 pu ...

  3. (11)go 数组和切片

    一.数组 1.定义数组 定义时付给该类型默认值 2.初始化 箭头指向的数组代表数组的下标 3.数组遍历 方法1: 方法2: 二.切片 数组的数量不固定 1. 2. 3. string可以进行切片处理

  4. 洛谷P3857 [TJOI2008]彩灯 [线性基]

    题目传送门 彩灯 题目描述 Peter女朋友的生日快到了,他亲自设计了一组彩灯,想给女朋友一个惊喜.已知一组彩灯是由一排N个独立的灯泡构成的,并且有M个开关控制它们.从数学的角度看,这一排彩灯的任何一 ...

  5. IntelliJ 、Pycharm、webstorm 2017 注册码及注册服务器

    jetbrains 家的东西都非常好看,但是价格贵的令人发指,所以我搭建了一个 Pycharm激活服务器,可以用来激活 Pycharm,IntelliJ IDEA,WebStorm.避免频繁更换激活码 ...

  6. 深入理解javascript作用域系列第五篇

    前面的话 对于执行环境(execution context)和作用域(scope)并不容易区分,甚至很多人认为它们就是一回事,只是高程和犀牛书关于作用域的两种不同翻译而已.但实际上,它们并不相同,却相 ...

  7. 解决关于stack溢出的问题

    开发中经常遇到: 前端遇到Uncaught RangeError: Maximum call stack size exceeded错误 后台遇到java.lang.OutOfMemoryError: ...

  8. BZOJ1086 SCOI2005王室联邦

    想学树上莫队结果做了个树分块. 看完题后想到了菊花图的形状认为无解,结果仔细一瞧省会可以在外省尴尬 对于每一颗子树进行深搜,一旦遇到加在一起大小达到B则将它们并为一省,因为他子树搜完以后没有分出块的大 ...

  9. 【四边形不等式】noi95- 合并石子

    [题目大意] 在一个园形操场的四周摆放N堆石子,现要将石子有次序地合并成一堆.规定每次只能选相邻的2堆合并成新的一堆,并将新的一堆的石子数,记为该次合并的得分.试设计出1个算法,计算出将N堆石子合并成 ...

  10. [HDU5492]Find a path

    题目大意: 一个n*m的格子,每个格子上都有一个数. 你可以向下或者向右走,从(1,1)走到(n,m),问方差*(n+m-1)最小的路径是哪个? 思路: 方差*(n+m-1)就相当于给格子里每个数乘上 ...