Luoyoooo

与其感慨路难行,不如马上出发

【数据结构1-3】集合-map

一.概览

 

 

二.常用操作

同map篇,这里不过多赘述

 

例题:【深基17.例6】学籍管理

代码:

 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 }

 

posted @ 2020-03-24 18:57  Luoyoooo  阅读(145)  评论(0编辑  收藏  举报