构造,无参和带参。
带参只演示这一种,这种最好用。
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;
}
本文详细介绍了C++中map容器的无参和带参构造,以及如何插入元素、通过迭代器和重构符号查询值和遍历map。特别指出map并非数组,【】符号是重构的访问方式。
801





