====C++map(映射)的常用用法

====C++map(映射)的常用用法
Cmap(映射)的常用用法前提头文件#include1.map 的定义1普通定义mapmap type1, type2 mp ;2嵌套定义mapmap套set一对多,自动去重// 一个key对应一堆不重复元素mapstring, setstring ms;map套map二位映射// 二维键值像二维数组mapint, mapint, int mm;map string, int mp;map的键和值也可以是STL容器type1 键类型:必须是支持比较 string ,int, set, vector,自定义结构体type2 值类型mapsetint, string mp;特点注1.键唯一不能重复重复会覆盖2.自动按键从小到大排序3.底层:红黑树map内部是由红黑树实现的set也是在建立映射的过程中会自动实现从小到大排序功能。4.如果是字符串类型到整型的映射必须是string而不能是char数组不能作为键值5.访问不存在的键会自动创建 所以使用前一定要判断 if ( mp.count (x) )mapchar, int mp; mp[c] 20; mp[c] 30; //30覆盖了20 mp[c] 666; //666覆盖了30 cout mp[c]; //答案输出6662、map容器内元素的访问两种访问方式通过下标访问或通过迭代器访问1通过下标访问和普通数组一样mp[a] 100;cout mp[a];2通过迭代器访问map 键类型 , 值类型 :: iterator 迭代器名;it-first来访问键it-second来访问值for(auto it mp.begin(); it ! mp.end(); it){cout it-first it-second endl;}代码#includeiostream#includemapusing namespace std;int main(){mapchar, int mp;mp[a] 222;mp[b] 333;mp[c] 444;for(mapchar, int::iterator it mp.begin(); it ! mp.end(); it){cout it-first;cout ;cout it-second;cout endl;}return 0;}输出结果a 222b 333c 444输入 输出mp[c] 222; a 333mp[a] 333; b 444mp[b] 444; c 2223、map常用函数1mp.findkeyauto it mp.find(a);#includeiostream#includemapusing namespace std;int main(){mapchar, int mp;mp[a] 222;mp[b] 333;mp[c] 444;mapchar, int::iterator it mp.find(b);cout it-first it-second;return 0;}2mp.erase① 删除单个元素*mp.erase(it)*it为需要删除的元素的迭代器#includeiostream#includemapusing namespace std;int main(){mapchar, int mp;mp[a] 222;mp[b] 333;mp[c] 444;mapchar, int::iterator it mp.find(b);mp.erase(it);for(mapchar, int::iterator it mp.begin(); it ! mp.end(); it){cout it-first it-second endl;}return 0;}输出a 222c 444mp.erase(key)key为要删除的映射的键#includeiostream#includemapusing namespace std;int main(){mapchar, int mp;mp[a] 222;mp[b] 333;mp[c] 444;//mapchar, int::iterator it mp.find(b);//mp.erase(it);mp.erase(b);for(mapchar, int::iterator it mp.begin(); it ! mp.end(); it){cout it-first it-second endl;}return 0;}输出a 222c 444② 删除一个区间内所有的元素*mp.erase(first, last)*其中first为需要删除的区间的起始迭代器last为需要删除的区间末尾迭代器的下一个地址即为删除左闭右开的区间[first, last)#includeiostream#includemapusing namespace std;int main(){mapchar, int mp;mp[a] 222;mp[b] 333;mp[c] 444;mapchar, int::iterator it mp.find(b);mp.erase(it, mp.end()); //删除it之后的所有映射即b 333和 c 444//mp.erase(it);//mp.erase(b);for(mapchar, int::iterator it mp.begin(); it ! mp.end(); it){cout it-first it-second endl;}return 0;}3mp.size()返回键值对数量4mp.count(key)判断key是否存在存在返回1 不存在返回05mp.clear()清空map嵌套例题//B万年沉睡的宝藏#includebits/stdc.h#define endl \n#define ll long long// 输入输出加速#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);using namespace std;ll q; // 总操作次数ll op; // 操作编号string x,y;int main(){IOS; // 开启快读快写防止超时cinq;// map岛屿名 - 该岛屿拥有的宝藏集合// set 自动去重同一个宝藏不会重复统计mapstring ,setstringbao;while(q--){cinop;​// 操作1给岛屿 x 新增一个宝藏 y​if(op1)​{​cinxy;​bao[x].insert(y); // 往岛屿x的宝藏集合里加入宝藏y​}​// 操作2查询岛屿 x 有多少个不同宝藏​if(op2)​{​cinx;​// 如果岛屿不存在输出0否则输出宝藏个数​if(!bao.count(x))​{​cout0endl;​}​else​{​coutbao[x].size()endl;​}​}​// 操作3查询岛屿 x 是否拥有宝藏 y​if(op3)​{​cinxy;​// 岛屿都不存在直接输出0​if(!bao.count(x))​{​cout0endl;​}​else​{​// set.count 有就返回1没有返回0​coutbao[x].count(y)endl;​}​}​// 操作4查询一共有多少个出现过的岛屿​if(op4)​{​// map.size() 就是岛屿总数​coutbao.size()endl;​}}return 0;}