- 自动建立Key-value的对应。key 和 value可以是任意你需要的类型。
- 根据key值快速查找记录,查找的复杂度基本是log(N)。
- 快速插入Key -Value 记录。
- 快速删除记录。
- 根据Key 修改value记录。
- 遍历所有记录。
常见用法
#include<bits/stdc++.h>
#include<map>
using namespace std;
map<int, char> mp;
int main() {
for(int i=0; i<3; i++) mp[i] = (char)(i + 'a');
map<int, char>::iterator it;
for(it=mp.begin(); it!=mp.end(); it++){
cout << it->first << " " << it->second << endl;
}
cout << mp[1] << endl;
for(auto it: mp){
cout << it.first << " " << it.second << endl;
}
map<int, string> mp1;
for(int i=0; i<3; i++) mp1[i] = "abcd";
map<int, string>::iterator it1;
for(it1=mp1.begin(); it1!=mp1.end(); it1++){
cout << it1->first << " " << it1->second << endl;
}
cout << mp1[1][0] << endl;
map<int, vector<int> > mp2;
for(int i=1; i<=10; i++){
mp2[1].push_back(i);
}
int len = mp2[1].size();
for(int i=0; i<len; i++){
cout << mp2[1][i] << " ";
}
return 0;
}