目录
map和multimap
map提供优秀的一对一数据处理能力.
元素是由key和value两部分组成的队组.key是唯一的.
给定一个key,就能确定相对的value.
map也叫关联数组,只是索引的方式是key不是整数.
map是一个有序的不允许key重复的容器,默认为升序.
multimap是一个有序的允许key重复的容器,默认为升序.
其余map和multimap完全相同.
初始化,遍历
#include <iostream>
#include <map>
#include <string>
using namespace std;
int main()
{
//创建一个空的map
map<int, string> m;
//直接赋值
map<int, string> m2 = {
{1,"a"},
{2,"b"},
{3,"c"}
};
//复制构造函数
map<int, string> m3(m2);
//使用迭代器赋值
map<int, string> m4(m3.begin(), m3.end());
//赋值构造函数
m = m4;
for (auto it = m.begin(); it != m.end(); ++it)
{
cout << it->first << " " << it->second << end