借鉴CSDN博主 Linux猿 的文章 仅用于本笨蛋学习
碎碎念~起初我完全不知道map,只是有时候听同学提起过,我也没有想去了解的欲望。但是既然有一个作业题提到了建议使用map,那我就有兴趣来学习这一新容器map了。
一、什么是map?
map 是具有唯一键值对的容器,通常使用红黑树实现。(红黑树!好熟悉的东西,上学期才接触过,不过当时也没有学的很懂)
map 中的键值对是 key value 的形式,比如:每个身份证号对应一个人名(反过来不成立哦!),其中,身份证号就是 key,人名便是 value,是单项的关系,可以与 hash 作类比。(hash函数也是才学过)
二、map的定义
1.头文件
#include <map>
2.初始定义
map<key_type,value_type>变量名;
注意:如果没有 using namespace std, map需要写成 std:map。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "小明";
cout<<"身份证号123456的人叫"<<person[123456]<<endl;
}
在上例中,定义了一个key 为 int ,value 为 string 的 map 容器 person。
三、使用方法
map的常用方法:
//常用如下:
size() // 元素个数
empty() // 判断是否为空,空返回 true
clear() // 清空容器
erase() // 删除元素
find() // 查找元素
insert() // 插入元素
count() // 计算指定元素出现的次数
begin() // 返回迭代器头部
end() // 返回迭代器尾部
//非常用
swap() // 交换两个map容器,类型需要相同
max_size() // 容纳的最大元素个数
rbegin() // 指向map尾部的逆向迭代器
rend() // 指向map头部的逆向迭代器
lower_bound() // 返回键值大于等于指定元素的第一个位置
upper_bound() // 返回键值大于指定元素的第一个位置
equal_range() // 返回等于指定元素的区间
四、使用方式
1.增加数据
方法1:以数组下标的形式直接增加
即:变量名[key] = value 的形式。
如:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "张三";
person[123457] = "李四";
person[123458] = "王五";
cout<<"身份证号123456的人叫"<<person[123456]<<endl;
cout<<"身份证号123457的人叫"<<person[123457]<<endl;
cout<<"身份证号123458的人叫"<<person[123458]<<endl;
}
输出:
身份证号123456的人叫张三
身份证号123457的人叫李四
身份证号123458的人叫王五
方法2:直接插入键值对
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person.insert(pair<int, string>(123456, "张三"));
person.insert(pair<int, string>(123457, "张三"));
person.insert(pair<int, string>(123458, "李四"));
cout<<"身份证号123456的人叫"<<person[123456]<<endl;
cout<<"身份证号123457的人叫"<<person[123457]<<endl;
cout<<"身份证号123458的人叫"<<person[123458]<<endl;
}
输出:
身份证号123456的人叫张三
身份证号123457的人叫张三
身份证号123458的人叫李四
其中,pair 定义了一个键值对,对应 map 的 key 和 value。
2.删除数据
删除数据使用到 map 的 erase 和 clear方法,来看一下例子:
clear:清空容器,
size :获取容器大小。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "张三";
person123457] = "李四";
person[123458] = "王五";
cout<<"size = "<<person.size()<<endl;
//1. 使用 key 删除
person.erase(123456); // 删除 key = 123456 的节点
cout<<"size = "<<person.size()<<endl;
//2. 使用迭代器删除
map<int,string>::iterator it = person.find(123457);
person.erase(it);
cout<<"size = "<<person.size()<<endl;
//3. 清空整个容器
person.clear();
cout<<"size = "<<person.size()<<endl;
}
输出:
size = 3
size = 2
size = 1
size = 0
3.修改数据
修改数据仅能修改 value 的值,key 是不能修改的,可以通过增加和删除来实现修改 key。
如:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "张三";
cout<<"身份证号123456的人叫"<<person[123456]<<endl;
person[123456] = "李四";
cout<<"身份证号123456的人叫"<<person[123456]<<endl;
}
输出为:
身份证号123456的人叫张三
身份证号123456的人叫李四
4.查找数据
利用find函数查找。
如:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "张三";
person[123457] = "李四";
person[123458] = "王五";
map<int, string>::iterator it = person.find(123456);
if(it!= person.end())
{
cout<<"身份证号123456的人叫"<<it->second<<endl;
}
}
输出为:
身份证号123456的人叫张三
find返回map的迭代器。
5.元素遍历
利用迭代器遍历,如下:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>person; // 定义变量
person[123456] = "张三";
person[123457] = "李四";
person[123458] = "王五";
map<int, string>::iterator it; //定义迭代器 iter
for(it = person.begin(); it != person.end(); ++it)
{
cout<<"身份证号"<<it->first<<"的人叫"<<it->second<<endl;
}
}
输出为:
身份证号123456的人叫张三
身份证号123457的人叫李四
身份证号123458的人叫王五
其中,使用迭代器 it 遍历容器,可以将迭代器理解为一个存储了 key 和 value 的一个结构,first 对应 key,second 对应 value。
6.其他扩展
1.swap函数
交换两个容器的内容,这两个map容器类型必须一样。
如:
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node1; // 定义变量
map<int, string>node2;
node1[11] = "张三";
node1[12] = "李四";
node2[21] = "王五";
node2[22] = "赵六";
node2[23] = "孙七";
node1.swap(node2);
map<int, string>::iterator it;
cout<<"node1 :"<<endl;
for(it = node1.begin(); it != node1.end(); ++it)
{
cout<<"key = "<<it->first<<" value = "<<it->second<<endl;
}
cout<<"node2 :"<<endl;
for(it = node2.begin(); it != node2.end(); ++it)
{
cout<<"key = "<<it->first<<" value = "<<it->second<<endl;
}
}
输出为:
node1 :
key = 21 value = 王五
key = 22 value = 赵六
key = 23 value = 孙七
node2 :
key = 11 value = 张三
key = 12 value = 李四
2.max_size函数
返回当前容器的最大可容纳元素个数。
#include <iostream>
#include <map> // 头文件
#include <string>
using namespace std;
int main()
{
map<int, string>node; // 定义变量
cout<<"max_size = "<<node.max_size()<<endl;
node[11] = "张三";
cout<<"max_size = "<<node.max_size()<<endl;
node[12] = "李四";
cout<<"max_size = "<<node.max_size()<<endl;
node[13] = "王五";
cout<<"max_size = "<<node.max_size()<<endl;
}
输出为:
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
max_size = 128102389400760775
五、做题举例
题目:挖掘机技术哪家强
【问题描述】
为了用事实说明挖掘机技术到底哪家强,组织一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。
【输入形式】
输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号、及其比赛成绩(百分制),中间以空格分隔。
【输出形式】
在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。
【样例输入】
6
3 65
2 80
1 100
2 70
3 40
3 0
【样例输出】
2 150
【问题说明】
建议练习使用STL中的map
解题代码举例:
//初识map,尝试使用map
#include <map>
#include <iostream>
using namespace std;
int main()
{
int n;
cin>>n;//参赛人数
int numb,score;
map<int,int>school;
for(int i=0;i<n;i++)//n条信息
{
cin>>numb>>score;
school[numb]+=score;
}
map<int,int>::iterator it=school.begin() ;
map<int,int>::iterator max=school.begin() ;
for(it++;it!=school.end() ;it++)
{
if(it->second > max->second)
{
max=it;
}
}
cout<<max->first<<" "<<max->second<<endl;
return 0;
}