构造,无参和带参。
带参只演示这一种,这种最好用。
map<string, string> m;
map<int, int> m({ {1,11},{2,22} });
插入
同样演示两种
m.insert(pair < string, string>("aa", "a"));
m.insert({ {"bb","b"} });
查询map中的value
方法1.使用迭代器
map<string, string>::iterator pos = m.find("aa");
cout << (*pos).second << endl;
方法2.使用重构的【】
cout << m["aa"] << endl;
注意:map并不是数组,【】只是重构过的符号
遍历查询
map<int, int> m({ {1,11},{2,22} });
for (auto& val : m) {
cout << val.first<< val.second << endl;
}