【数据结构1-3】集合-map
一.概览
二.常用操作
同map篇,这里不过多赘述
代码:
1 #include<iostream>
2 #include<string>
3 #include<map>
4 using namespace std;
5 int main()
6 {
7 map<string, int>m;
8 int n;
9 cin >> n;
10 for (int i = 0; i < n; i++)
11 {
12 int tmp, num;
13 string name;
14 cin >> tmp;
15 switch (tmp)
16 {
17 case 1:
18 cin >> name >> num;
19 m[name] = num;
20 cout << "OK" << endl;
21 break;
22 case 2:
23 cin >> name;
24 if (m.find(name) == m.end())
25 cout << "Not found" << endl;
26 else
27 cout << m[name] << endl;
28 break;
29 case 3:
30 cin >> name;
31 if (m.find(name) != m.end())
32 {
33 m.erase(name);
34 cout << "Deleted successfully" << endl;
35 }
36 else
37 cout << "Not found" << endl;
38 break;
39 case 4:
40 cout << m.size() << endl;
41 break;
42 }
43 }
44 return 0;
45 }