#include<iostream>
#include<string>
#include<vector>
#include<map>
using namespace std;
int main() {
map<int, int> m;//定义map
m[1] = 1;
m[1] = 2;//第一种插入方式 使用下标插入
//key不存在时创建 存在时更新
cout << m[1]<<endl;
pair<int, int> p1;//定义键值对
p1 = make_pair(2, 7);
pair<int, int> p2(2, 4);//直接构造键值对
m.insert(p1);
m.insert(p2);//第二种插入方式 使用insert接口
//key不存在时创建 存在时放弃
cout << m[2]<<endl;
map<int, int>::iterator itr;
for (itr = m.begin(); itr != m.end(); itr++) { //遍历
cout << itr->first << " " << itr->second << endl;
}
if ((itr = m.find(3)) != m.end()) {
cout << "finded" << endl;
}
else {
cout << "no found";
}
return 0;
}
网友评论