1.什么是关联式容器
类似于vector,list,deque,等我们称之为序列式容器,它的底层是线性结构,存放的是数值本身,而关联式容器存放的是key-value键值对。
- 键值对:用来表示具有一一对应关系的一种结构,该结构中一般只包含两个成员变量key和value,key代表键值, value表示与key对应的信息。
2.map
map是最常用的关联式容器之一,它会按照key来比较顺序存储元素,支持下标访问。
2.1 map的使用
#include<map>
#include<string>
#include <iostream>
int main()
{
std::map<std::string,std::string>m1;
//列表初始化
std::map<std::string,std::string>m2{
{
"1","一"},{
"2","二"},{
"3","三"}
};
std::cout << m2["1"] << std::endl;
//拷贝构造
std::map<std::string,std::string>m3(m2);
std::cout << m3["1"] << std::endl;
//使用迭代器遍历
for(auto it = m2.begin(); it != m2.end(); it++){
std::cout << (*it).first <<"---" << it->second << std::endl;
}
//修改
m2["1"