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, ...
随机推荐
- SpringAop切面实现日志记录
SpringAop切面实现日志记录代码实现:https://www.cnblogs.com/wenjunwei/p/9639909.html 问题记录 1.signature.getMethod(). ...
- mysql_用命令行备份数据库
MySQL数据库使用命令行备份|MySQL数据库备份命令 例如: 数据库地址:127.0.0.1 数据库用户名:root 数据库密码:pass 数据库名称:myweb 备份数据库到D盘跟目录 mysq ...
- MySQL_where和having的区别
1. where和having都可以使用的场景 select goods_price,goods_name from sw_goods where goods_price > 100 selec ...
- mysql之用户
1.通过Navicat For Mysql可以查看目前的用户情况 2.创建用户 create user 'Fqq'@'127.0.0.1' IDENTIFIED by '123'; -- 创建一个用户 ...
- Python:利用Entrez库筛选下载PubMed文献摘要
一个不是学生物的孩子来搞生物,当真是变成了一块废铁啊,但也是让我体会到了一把生物信息的力量. 废话不多说,开整! 任务:快速高效从PubMed上下载满足条件的文献PMID.标题(TI).摘要(AB). ...
- 二叉堆python实现
二叉堆是一种完全二叉树,我们可以使用列表来方便存储,也就是说,用列表将树的所有节点存储起来. 如下图,是小根堆方式的二叉堆,假设父节点的下标为p,则他的左孩子下标为2P+1,右孩子下标为2P+2 cl ...
- JUC并发工具包之Semaphore
目录 Semaphore (JDK) Timed Semaphore (Apache Commons) Semaphore vs. Mutex CodeRepo Semaphore (JDK) 我们使 ...
- Contest 1435
A \(x\times-y+y\times x=0\),因为 \(n\) 是偶数,所以两两这样构造即可. 时间复杂度 \(O\left(Tn\right)\). B 有点绕的题,要理清思路. 发现行和 ...
- JavaSE 学习笔记07丨IO流
Chapter 13. IO流 13.1 File类 java.io.File类是文件(file)和目录(文件夹)(directory)路径名(path)的抽象表示,主要用于文件和目录的创建.查找和删 ...
- 【mq读书笔记】mq索引文件刷盘
索引文件的刷盘并不是采取定时刷盘机制,而是每更新一次索引文件就会将上一次的改动刷写到磁盘. 同步刷盘: GroupCommitRequest将被提交到GroupCommitService线程,Grou ...