Map是STL的一个关联容器
数据一对一储存,自动建立key-value对应,可根据key来快速查找记录,时间复杂的Log(N);
key,value可以为任意类型;
key-value可以快速插入删除;
#include <map>
map为模板类,创建时需要关键字与储存对象两种模板参数,例 std:map<string,int> teao;数据插入
teao.insert(make_pair("a1",1);teao.emplace("a0",9);
teao["a2"]=0;
数据修改
teao["a1"]=3;数据读取
map<string,int>::iterator ismap=teao.begin();cout<<ismap->first<<ismap->second<<endl;
cout<<teao.at("a1")<<endl;//at会检查下标存在与否
cout<<teao["a2"]<<endl;//不检查下标越界
数据查找
map<string,int>::iterator it=teao.find{"a1"};数据删除
teao.erase(it);teao.erase("a2");
teao.erase("a1",teao.end());
teao.clear()//删除map中所有数据
map交换
std::map<char,int> a,b;a.swap(b);
map容量查询
teao.size();顺序比较
map<string,int>::key_compare teaocomp=teao.key_comp();teao["a5"]=4;
teao["a6"]=5;
teaocomp("a5","a6");//a5在a6前,返回true
迭代器
begin, end, rbegin,rend 以及对应的 cbegin, cend, crbegin,crend后四个返回const_iterator