STL——容器(Map & multimap)的查找
- map.find(key); //查找键key是否存在,若存在,返回该键的元素的迭代器;若不存在,返回map.end();
- map.count(key); //返回容器中键值为key的对组个数。对map来说,要么是0,要么是1;对multimap来说,值>=0。
- map.lower_bound(keyElem); //返回第一个key>=keyElem元素的迭代器。
- map.upper_bound(keyElem); // 返回第一个key>keyElem元素的迭代器。
- map.equal_range(keyElem); //返回容器中key与keyEl相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。
1. map.find(key);
find(); 的所有返回值均为 key 的 iterator 类型迭代器




代码示例:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 map<int, string> mapStu1;
9
10 mapStu1.insert(pair<int, string>(1, "内容A"));
11 mapStu1.insert(pair<int, string>(2, "内容B"));
12 mapStu1.insert(pair<int, string>(3, "内容C"));
13 mapStu1.insert(pair<int, string>(4, "内容D"));
14 mapStu1.insert(pair<int, string>(5, "内容E"));
15
16 map<int, string>::iterator it_1 = mapStu1.find(3); //将返回值赋值给 iterator 类型的 it
17
18 if (it_1 != mapStu1.end()) //由于 find 为全部遍历,如果没有找到元素, 会返回 end()
19 {
20 cout << "key值: " << it_1->first << " 对应的velue为:" << it_1->second << endl;
21 }
22 else
23 {
24 cout << "没有找到对应的" << it_1->first << endl;
25 }
26
27 map<int, string>::iterator it_2 = mapStu1.find(6);
28
29 if (it_2 != mapStu1.end())
30 {
31 cout << "key值: " << it_2->first << " 对应的velue为:" << it_2->second << endl;
32 }
33 else
34 {
35 cout << "没有找到对应的key" << endl;
36 }
37
38 return 0;
39 }
打印结果:

由于 map 的 key具有唯一性, multimap 这种具有多个相同的 key 如何解决, 示例代码如下:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(2, "内容B"));
12 multimapStu1.insert(pair<int, string>(3, "内容C"));
13 multimapStu1.insert(pair<int, string>(3, "内容D"));
14 multimapStu1.insert(pair<int, string>(3, "内容E"));
15 multimapStu1.insert(pair<int, string>(4, "内容F"));
16
17 map<int, string>::iterator it_1 = multimapStu1.find(3); //将返回值赋值给 iterator 类型的 it
18
19 if (it_1 != multimapStu1.end())
20 {
21 for (; it_1 != multimapStu1.end(); it_1++)
22 {
23 if (it_1->first == 3)
24 {
25 cout << "key值: " << it_1->first << " 对应的velue为:" << it_1->second << endl;
26 }
27 }
28 }
29
30 return 0;
31 }
打印结果:

2. map.count(key);
统计拥有 key 的数量, 示例代码如下:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(2, "内容B"));
12 multimapStu1.insert(pair<int, string>(3, "内容C"));
13 multimapStu1.insert(pair<int, string>(3, "内容D"));
14 multimapStu1.insert(pair<int, string>(3, "内容E"));
15
16 map<int, string>::iterator it_1 = multimapStu1.find(3);
17
18 int count = multimapStu1.count(3);
19 cout << "找到" << count << "个相关元素" << endl;
20
21 if (it_1 != multimapStu1.end())
22 {
23 for (int i = 0; i < count; i++)
24 {
25 cout << "key值: " << it_1->first << " 对应的velue为:" << it_1->second << endl;
26 }
27 }
28 else
29 {
30 cout << "没找到" << endl;
31 }
32
33 return 0;
34 }
打印结果:

3. map.lower_bound(keyElem);
返回第一个key>=keyElem元素的迭代器,示例代码如下:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(2, "内容B"));
12 multimapStu1.insert(pair<int, string>(3, "内容C"));
13 multimapStu1.insert(pair<int, string>(4, "内容D"));
14 multimapStu1.insert(pair<int, string>(4, "内容DD"));
15 multimapStu1.insert(pair<int, string>(4, "内容DDD"));
16 multimapStu1.insert(pair<int, string>(5, "内容E"));
17
18 //map<int, string>::iterator it_1 = multimapStu1.lower_bound(4);
19
20 cout << "输出 key 值大等于4以后的内容" << endl;
21
22 for (map<int, string>::iterator it = multimapStu1.lower_bound(3); it != multimapStu1.end(); it++)
23 {
24 if (it->first == 4)
25 {
26 cout << "第一个 key 为:" << it->first << " 其内容为:" << it->second << endl;
27 }
28 }
29
30 return 0;
31 }
打印结果:

4. map.upper_bound(keyElem);
返回第一个key>keyElem元素的迭代器,代码示例:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(2, "内容B"));
12 multimapStu1.insert(pair<int, string>(3, "内容C"));
13 multimapStu1.insert(pair<int, string>(4, "内容D"));
14 multimapStu1.insert(pair<int, string>(4, "内容DD"));
15 multimapStu1.insert(pair<int, string>(4, "内容DDD"));
16 multimapStu1.insert(pair<int, string>(5, "内容E"));
17
18 cout << "输出 key 值大等于4以后的内容" << endl;
19
20 for (map<int, string>::iterator it = multimapStu1.upper_bound(3); it != multimapStu1.end(); it++)
21 {
22 if (it->first == 4)
23 {
24 cout << "第一个 key 为:" << it->first << " 其内容为:" << it->second << endl;
25 }
26 }
27
28 return 0;
29 }
打印结果:

5. map.equal_range(keyElem);
返回容器中key与keyElem相等的上下限的两个迭代器。上限是闭区间,下限是开区间,如[beg,end)。代码如下:
1 #include <iostream>
2 #include <map>
3
4 using namespace std;
5
6 int main()
7 {
8 multimap<int, string> multimapStu1;
9
10 multimapStu1.insert(pair<int, string>(1, "内容A"));
11 multimapStu1.insert(pair<int, string>(2, "内容B"));
12 multimapStu1.insert(pair<int, string>(3, "内容C"));
13 multimapStu1.insert(pair<int, string>(3, "内容CC"));
14 multimapStu1.insert(pair<int, string>(3, "内容CCC"));
15 multimapStu1.insert(pair<int, string>(4, "内容D"));
16
17 cout << "输出 key 与 keyElem 相等的上下限的两个迭代器" << endl;
18
19 pair<multimap<int, string>::iterator, multimap<int, string>::iterator> mmit = multimapStu1.equal_range(3);
20
21 if (mmit.first != multimapStu1.end())
22 {
23 cout << "第一个迭代器的 key 为:" << (*mmit.first).first << " 其内容为:" << (*mmit.first).second << endl;
24 }
25
26 if (mmit.second != multimapStu1.end())
27 {
28 cout << "第二个迭代器的 key 为:" << (*mmit.second).first << " 其内容为:" << (*mmit.second).second << endl;
29 }
30
31 return 0;
32 }
打印结果:

==========================================================================================================================
STL——容器(Map & multimap)的查找的更多相关文章
- STL:map/multimap用法详解
map/multimap 使用map/multimap之前要加入头文件#include<map>,map和multimap将key/value当作元素,进行管理.它们可根据key的排序准则 ...
- STL之map&multimap使用简介
map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...
- STL容器 -- Map
核心描述: map 就是从键(key) 到 值(value) 的一个映射.且键值不可重复,内部按照键值排序. 头文件: #include <map> 拓展: multimap 是一个多重映 ...
- STL容器Map
Map的常见函数 Map的实现机制 STL中的Map底层实现机制是RB树(红-黑树)
- 【STL】-Map/Multimap的用法
初始化: map<string,double> salaries; 算法: 1. 赋值.salaries[ "Pat" ] = 75000.00; 2. 无效的索引将自 ...
- STL - 容器 - Map(二)
把Map用作关联式数组 MapAdvanceTest.cpp #include <map> #include <string> #include <iostream> ...
- STL - 容器 - Map(一)
MapTest.cpp #include <map> #include <string> #include <iostream> #include <algo ...
- iBinary C++STL模板库关联容器之map/multimap
目录 一丶关联容器map/multimap 容器 二丶代码例子 1.map的三种插入数据的方法 3.map集合的遍历 4.验证map集合数据是否插入成功 5.map数据的查找 6.Map集合删除元素以 ...
- C++STL容器(lower_bound,upper_bound)
C++STL容器中有三种二分查找函数,这里分享其中的两个 这两个函数其实都可以理解为不破坏数组次序的前期下能将目标元素插入到数组的第几个位置,不过在细节上两个函数有所差异 int d[6]={0,2, ...
随机推荐
- Ceph如何实现文件系统的横向扩展
前言 在跟一个朋友聊天的时候,聊到一个技术问题,他们的一个环境上面小文件巨多,是我目前知道的集群里面规模算非常大的了,但是目前有个问题,一方面会进行一倍的硬件的扩容,而文件的数量也在剧烈的增长着,所以 ...
- Java中常量池详解
在Java的内存分配中,总共3种常量池: 转发链接:https://blog.csdn.net/zm13007310400/article/details/77534349 1.字符串常量池(Stri ...
- 在CorelDRAW中为对象添加块阴影效果
我们可以使用CorelDRAW来绘制矢量图形,在勾画出简单的图形后,往往还需要对它们进行一些或简单或复杂的处理,以增加一定的艺术效果.CDR中可供选择的效果有很多,作用的对象可以是文字,也可以是图案. ...
- 如何用FL Studio将乐器组合与分层
有过音乐制作经历的小伙伴应该知道,我们在用以FL Studio20为代表的音乐编曲软件制作音乐时,往往需要在同一节奏点添加多种音效,这样可以使音乐听起来更具层次感.正因如此,我们就需要不断添加音符,就 ...
- Boom 3D带你聆听《我们的乐队》音乐盛宴
说到前段时间大热的音乐类综艺节目,<我们的乐队>肯定值得一提.<我们的乐队>是由谢霆锋.萧敬腾.王俊凯担任导师,以团队的形式组成各种风格的乐队,并通过同场比拼,最终选出获胜的乐 ...
- Postman实用小技巧
Postman使用小技巧 软件测试工程师 张江涛 废话就不多说了,直奔主题,这里的技巧就以对话方式来阐述吧. 问:公司的环境也太多了吧,本地.开发.测试以及生产环境,这么多环境,每次使用的时候都要来回 ...
- 【爬虫】基于PUPPETEER页面爬虫
一.简介 本文简单介绍一下如何用puppeteer抓取页面数据. 二.下载 npm install puppeteer --save-dev npm install typescrip --save- ...
- Java IDEA根据database以及脚本代码自动生成DO,DAO,SqlMapper文件(一)
根据数据库代码自动生成的插件挺多的,这里主要分享两种: 1.根据database以及脚本代码自动生成 2.根据mybatis-generator-core自动生成(下一章节进行分享,包含sqlserv ...
- Spring 对Apache Kafka的支持与集成
1. 引言 Apache Kafka 是一个分布式的.容错的流处理系统.在本文中,我们将介绍Spring对Apache Kafka的支持,以及原生Kafka Java客户端Api 所提供的抽象级别. ...
- 精尽MyBatis源码分析 - MyBatis-Spring 源码分析
该系列文档是本人在学习 Mybatis 的源码过程中总结下来的,可能对读者不太友好,请结合我的源码注释(Mybatis源码分析 GitHub 地址.Mybatis-Spring 源码分析 GitHub ...