map :映射 ,key、value。(key是唯一的)
//头文件
#include <map>
//定义
map <string,int> mp;
mp["goodplus"] = 99;
mp["lx"] = 100;
cout<<mp["goodplus"]<<endl;//输出 key 为goodplus的value (99)
//遍历输出
map <string,int> :: iterator it;//定义迭代器
it = mp.begin();//初始化
while (it != mp.end()){
cout<<it ->first <<" "<< it ->second;//first -> key; second -> value
it ++;//"++"不要忘记哦
}
//常用方法
//find方法
map <string,int> :: iterator it2 = mp.find("goodplus");//参数为value,返回类型为迭代器
cout<<"key 为 goodplus 的值为" << it2 ->second;
//erase方法
mp.erase(it2);//参数要删除元素的迭代器
//size()方法
cout<<mp.size()<<endl;//元素的个数
//clear()方法
mp.clear();//清除所有元素