[C++ STL] map使用详解
一、概述
map 由红黑树实现,其元素都是 “键值/实值” 所形成的一个对组(key/value pairs)。每个元素有一个键,是排序准则的基础。每一个键只能出现一次,不允许重复。
map主要用于资料一对一映射的情况,map 内部自建一颗红黑树,这颗树具有对数据自动排序的功能,所以在 map 内部所有的数据都是有序的。比如一个班级中,每个学生的学号跟他的姓名就存在着一对一映射的关系。
二、定义及初始化
使用之前必须加相应容器的头文件:
#include <map> // map属于std命名域的,因此需要通过命名限定,例如using std::map;
定义的代码如下:
map<int, string> a; // 定义一个int类型的映射a
// map<int, string> a(10); // error,未定义这种构造函数
// map<int, string> a(10, 1); // error,未定义这种构造函数
map<int, string> b(a); // 定义并用映射a初始化映射b
// map<int, string> b(a.begin(), a.end()); // error,未定义这种构造函数
三、基本操作
3.1 容量函数
- 容器大小:
mp.size();
- 容器最大容量:
mp.max_size();
- 容器判空:
mp.empty();
- 查找键 key 的元素个数:
mp.count(key);
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
mp.insert({ 1, "张三" });
mp.insert({ 2, "李四" });
mp.insert(pair<int, string>{ 3, "隔壁老王" });
cout << mp.size() << endl; // 输出:3
cout << mp.max_size() << endl; // 输出:89478485 // 输出:1
cout << mp.count(2) << endl; // 输出:1
if (mp.empty())
cout << "元素为空" << endl; // 未执行
return 0;
}
3.2 添加函数
- 在容器中插入元素:
mp.insert(const T& x);
- 任意位置插入一个元素:
mp.insert(iterator it, const T& x);
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
// 在容器中插入元素
mp.insert({ 1, "张三" });
mp.insert({ 2, "李四" });
// 任意位置插入一个元素
map<int, string>::iterator it = mp.begin();
mp.insert(it, pair<int, string>{ 3, "隔壁老王" }); // 会自动排序
for (it = mp.begin(); it != mp.end(); it++)
cout << it->first << " " << it->second << endl;
cout << endl;
return 0;
}
/*
1 张三
2 李四
3 隔壁老王
*/
3.3 删除函数
- 删除键值为 keyValue 的元素:
mp.pop_back(const T& keyValue);
- 删除迭代器所指的元素:
mp.erase(iterator it);
- 删除区间[first,last]之间的所有元素:
mp.erase(iterator first, iterator last);
- 清空所有元素:
mp.clear();
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
// 在容器中插入元素
mp.insert({ 1, "张三" });
mp.insert({ 2, "李四" });
mp.insert({ 4, "王五" });
mp.insert({ 5, "小明" });
// 任意位置插入一个元素
mp.insert(mp.begin(), pair<int, string>{ 3, "隔壁老王" }); // 会自动排序
// 删除键值为keyValue的元素
mp.erase(2);
// 删除迭代器所指的元素
mp.erase(mp.begin());
// 删除区间[first,last]之间的所有元素
mp.erase(mp.begin(), ++mp.begin());
// 遍历显示
map<int, string>::iterator it = mp.begin();
for (it = mp.begin(); it != mp.end(); it++)
cout << it->first << " " << it->second << endl;
// 清空容器内的所有元素
mp.clear();
// 判断map是否为空
if (st.empty())
cout << "元素为空" << endl; // 输出:元素为空
return 0;
}
/*
4 王五
5 小明
元素为空
*/
3.4 访问函数
- 查找键 key 是否存在,若存在,返回该键的元素的迭代器;若不存在,返回 map.end():
mp.find(key);
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
// 在容器中插入元素
mp[1] = "张三";
mp[2] = "李四";
mp[3] = "隔壁老王";
// 通过find(key)查找键值
cout << mp.find(1)->first << endl; // 输出:1
cout << mp.find(2)->second << endl; // 输出:李四
return 0;
}
3.5 其他函数
- 交换两个同类型容器的元素:
swap(map&, map&);
或mp.swap(map&);
#include "stdafx.h"
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp1;
// 在容器中插入元素
mp1[1] = "张三";
mp1[2] = "李四";
mp1[3] = "隔壁老王";
map<int, string> mp2;
// 在容器中插入元素
mp2[1] = "tom";
mp2[2] = "jerry";
mp2[3] = "mariy";
// 交换两个容器的元素
mp2.swap(mp1);
// 通过iterator遍历mp1
map<int, string>::iterator it;
for (it = mp1.begin(); it != mp1.end(); it++)
cout << it->second << " "; // 输出:tom jerry mariy
cout << endl;
return 0;
}
四、迭代器与算法
1. 迭代器
- 开始迭代器指针:
mp.begin();
- 末尾迭代器指针:
mp.end();
// 指向最后一个元素的下一个位置 - 指向常量的开始迭代器指针:
mp.cbegin();
// 意思就是不能通过这个指针来修改所指的内容,但还是可以通过其他方式修改的,而且指针也是可以移动的。 - 指向常量的末尾迭代器指针:
mp.cend();
- 反向迭代器指针,指向最后一个元素:
mp.rbegin();
- 反向迭代器指针,指向第一个元素的前一个元素:
mp.rend();
- 返回最后一个 key<=keyElem 元素的迭代器:
mp.lower_bound(keyElem);
- 返回第一个 key>keyElem 元素的迭代器:
mp.upper_bound(keyElem);
- 返回容器中 key 与 keyElem 相等的上下限的两个迭代器,这两个迭代器被放在对组(pair)中:
mp.equal_range(keyElem);
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
map<int,string> mp;
// 在容器中插入元素
mp[1] = "张三";
mp[2] = "李四";
mp[3] = "隔壁老王";
cout << mp.begin()->first << endl; // 输出:1
cout << (--mp.end())->first << endl; // 输出:3
cout << mp.cbegin()->first << endl; // 输出:1
cout << (--mp.cend())->first << endl; // 输出:3
cout << mp.rbegin()->first << endl; // 输出:3
cout << (--mp.rend())->first << endl; // 输出:1
cout << mp.lower_bound(2)->first << endl; // 输出:2
cout << mp.upper_bound(2)->first << endl; // 输出:3
pair<map<int, string>::iterator, map<int, string>::iterator> t_pair = mp.equal_range(2);
cout << t_pair.first->first << endl; // 输出:2
cout << t_pair.second->first << endl; // 输出:3
cout << endl;
return 0;
}
2. 算法
- 遍历元素
map<int>::iterator it;
for (it = mp.begin(); it != mp.end(); it++)
cout << it->second << endl;
五、总结
可以看到,map 与set的用法基本一致,只有以下一处不同:
- map 可以像数组那样插入元素,而 set 不行。
[C++ STL] map使用详解的更多相关文章
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- STL bind1st bind2nd详解
STL bind1st bind2nd详解 先不要被吓到,其实这两个配接器很简单.首先,他们都在头文件<functional>中定义.其次,bind就是绑定的意思,而1st就代表fir ...
- GoLang基础数据类型--->字典(map)详解
GoLang基础数据类型--->字典(map)详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 可能大家刚刚接触Golang的小伙伴都会跟我一样,这个map是干嘛的,是 ...
- Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解
Jquery遍历筛选数组的几种方法和遍历解析json对象|Map()方法详解 一.Jquery遍历筛选数组 1.jquery grep()筛选遍历数组 $().ready( function(){ v ...
- C++ STL bitset 容器详解
C++ STL bitset 容器详解 本篇随笔讲解\(C++STL\)中\(bitset\)容器的用法及常见使用技巧. \(bitset\)容器概论 \(bitset\)容器其实就是个\(01\)串 ...
- map接口详解
1.Map接口详解(1)映射(map)是一个存储键.键值对的对象,给定一个键,可以查询得到它的值,键和值都可以是对象(2)键必须是唯一的,值可以重复(Map接口映射唯一的键到值)(3)有些映射可以接收 ...
- 2.3 C++STL vector容器详解
文章目录 2.3.1 引入 2.3.2 代码实例 2.3.3 运行结果 总结 2.3.1 引入 vector 容器 动态数组 可变数组 vector容器 单口容器(尾部操作效率高) vector动态增 ...
- C++中的STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- (转载) STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
随机推荐
- [luoguP1578] 奶牛浴场(DP)
传送门 O(s2)算法 详见论文 王知昆--浅谈用极大化思想解决最大子矩形问题 我就复制你能把我怎么样QAQ #include <cstdio> #include <iostream ...
- HDU 4960 (水dp)
Another OCD Patient Problem Description Xiaoji is an OCD (obsessive-compulsive disorder) patient. Th ...
- AtCoder Grand Contest 012 B Splatter Painting(记忆化搜索)
题意: 给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点.经过若干个操作, 每次将与某个点vi距离不超过di的所有点染成某种颜色c ...
- Spring Boot实现多个数据源教程收集(待实践)
先收集,后续实践. http://blog.csdn.net/catoop/article/details/50575038 http://blog.csdn.net/neosmith/article ...
- Analyzing Storage Performance using the Windows Performance Analysis ToolKit (WPT)
https://blogs.technet.microsoft.com/robertsmith/2012/02/07/analyzing-storage-performance-using-the-w ...
- [转载]【BlackHat 2017】美国黑客大会首日议题汇总,演讲PPT下载也在这里
今年是 Black Hat 举办的第 20 个年头,高温酷暑也挡不住全世界黑客和安全人员奔赴拉斯维加斯的热情.毕竟这可是一年一度的盛大狂欢啊.今年的 BHUSA 从美国东部时间时间 7 月 22 日( ...
- zabbix学习系列之基础概念
触发器 概念 "监控项"仅负责收集数据,而通常收集数据的目的还包括在某指标对应的数据超出合理范围时给相关人员发送警告信息,"触发器"正式英语为监控项所收集的数据 ...
- BIOS Setup
一般而言,普通的计算机系统应用不必关注BIOS的设置.但是如果涉及到主板集成声卡,网卡,或需要进行远程网络唤醒等操作时,必须在BIOS中设置相应参数才能使电脑正常工作.BIOS能对硬件设备进行初始 ...
- 系统的BIOS与系统安装
今天偶尔看到个介绍电脑BIOS的与各种本子安装系统的介绍:(记录一下) 网络地址:http://blog.sina.com.cn/s/blog_4a1faae60102dyek.html
- TensorFlow的安装与CNN测试
0.说明 在Google开源该框架之后便使用真实K40m卡测试,由于生产环境是CentOS6.6的操作系统,但是该框架需要在Python2.7环境下执行,CentOS6.6下折腾了一天没搞定,后来换成 ...