1. map定义
1. 定义一个map : map<type1,type2>mp ,
type1是映射前的类型(键key),typename2是映射后的类型
(值 value),mp为映射的名字。
2.map表达映射关系 , 其实数组也是一种映射
例如 int a[100] 定义了int 到 int 的映射关系
a[5]=10 ,将 5 与10 建立了 5 到映射到10 关系
但是 map 可以映射 任何基本类型 (包括STL 容器)
map<string ,int >mp ; // 将string 映射为 int
map<string ,string >mp; // 将string 映射为 string
map<set<int >,string >mp; // 将set容器 映射到string
2.map的访问
1.通过下标访问
#include<iostream>
#include<cstdio>
#include<algorithm>
#include <map> // 使用map 的必要头文件
using namespace std;
int main()
{
map<char,int>mp;
mp['c']=100;
mp['d']=124;
cout<<mp['c']<<endl;
return 0;
}
// 输出结果为 100
2.通过迭代器访问 (重中之重)
map<type1,type2>::iterator it; //正向迭代,从第一个键元素开始访问
type1和type2就是定义map时填写的类型,这样就得到了迭代器it
通过 使用it->first来访问键,用it->second来访问值。// map<键,值>mp
#