+ [1.5、map 查找和统计](#15map__163)
🔥前言
继
set
容器后,今天总结一下map
容器的功能,从零到一快速掌握基本使用与常用接口。map
在STL
编程中与vector
、list
、set
具有同等重要的地位,键值对的方式存储元素在查找时很是高效,那么下面正式开始map
容器的学习之旅。
1、map 容器基本操作,从构造到查找统计
1.1、map/ multimap 基本概念
特点:
- map中所有元素都是二元组
pair
- 二元组中第一个元素为
key
(键),起到索引作用,第二个元素为value
(值)- 优点:可以根据key值快速找到value值
- 所有元素都会根据元素的键值自动升序排序
本质:
- map/multimap属于关联式容器,底层结构是用二叉树实现
二者区别:
map
不允许容器中有重复key值元素multimap
允许容器中有重复key值元素
1.2、map 赋值和构造
功能:
- 对map容器进行构造和赋值操作
函数原型:
map<T1, T2> mp;
默认构造函数:map(const map &mp);
拷贝构造函数map& operator=(const map &mp);
重载等号操作符
代码示例:
// 遍历容器
void printInfo(map<int,int>& mp)
{
for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
cout << "key = " << it->first << " value = " << it->second << endl;
}
cout << endl;
}
// 构造和赋值
void test01()
{
// 默认构造
map<int, int> mp1;
// 利用二元组显示创建
mp1.insert(make\_pair(1,1));
mp1.insert(make\_pair(5,2));
mp1.insert(make\_pair(4,5));
printInfo(mp1);
// 拷贝构造
map<int, int> mp2(mp1);
printInfo(mp2);
// 赋值
map<int, int> mp3;
mp3 = mp2;
printInfo(mp3);
}
1.3、map 大小和交换
功能:
- 统计map容器大小以及交换map容器
函数原型:
size();
返回容器中元素的数目empty();
判断容器是否为空swap(st);
交换两个集合容器
代码示例:
// 大小和交换
void test02()
{
// 大小
map<int, int>mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(3, 30));
mp.insert(pair<int, int>(2, 20));
if (mp.empty())
{
cout << "m为空" << endl;
}
else
{
cout << "m的大小为: " << mp.size() << endl;
}
// 交换
map<int, int>m2;
m2.insert(pair<int, int>(4, 100));
m2.insert(pair<int, int>(5, 200));
m2.insert(pair<int, int>(6, 300));
cout << "交换前" << endl;
printInfo(mp);
printInfo(m2);
cout << "交换后" << endl;
mp.swap(m2);
printInfo(mp);
printInfo(m2);
}
1.4、map 插入和删除
功能:
- map容器进行插入数据和删除数据
函数原型:
insert(elem);
在容器中插入元素。clear();
清除所有元素erase(pos);
删除pos迭代器所指的元素,返回下一个元素的迭代器。erase(beg, end);
删除区间[beg,end)的所有元素 ,返回下一个元素的迭代器。erase(key);
删除容器中值为key的元素。
代码示例:
// 插入和删除
void test03()
{
//插入操作
map<int, int> mp;
//第一种插入方式
mp.insert(pair<int, int>(1, 10));
//第二种插入方式,推荐
mp.insert(make\_pair(2, 20));
//第三种插入方式
mp.insert(map<int, int>::value\_type(3, 30));
//第四种插入方式,不推荐:当不存在此值就会自动创建键,值为0
mp[4] = 40;
// 模拟误操作
cout << "下标为6的值为:"<<mp[6] << endl;
printInfo(mp);
//删除,按迭代器
mp.erase(mp.begin());
printInfo(mp);
// 删除,按下标
mp.erase(3);
mp.erase(6);
printInfo(mp);
//清空
mp.erase(mp.begin(), mp.end());
mp.clear();
printInfo(mp);
}
1.5、map 查找和统计
功能:
- 对map容器进行查找数据以及统计数据
函数原型:
find(key);
查找key是否存在,若存在,返回该键对应的迭代器;若不存在,返回end()
;count(key);
统计key的元素个数
代码示例:
// 查找和统计
void test04()
{
map<int, int>mp;
mp.insert(pair<int, int>(1, 10));
mp.insert(pair<int, int>(2, 20));
mp.insert(pair<int, int>(4, 40));
mp.insert(pair<int, int>(3, 30));
![img](https://img-blog.csdnimg.cn/img_convert/f17f29291e8cfa0d9ad01f8c7e43f142.png)
![img](https://img-blog.csdnimg.cn/img_convert/bf88977b6d5047bc766a6f47a5800d3c.png)
![img](https://img-blog.csdnimg.cn/img_convert/c4321552f6c87ec27b78a3acaf025ced.png)
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**
42)]
[外链图片转存中...(img-piSwEEvn-1714843664242)]
**既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上大数据知识点,真正体系化!**
**由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新**
**[需要这份系统化资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618545628)**