STL之双重map(map的second值也是一个map容器)

来源:互联网 发布:逆光源网络剧百度云盘 编辑:程序博客网 时间:2024/06/06 19:03

map容器还是很好用的,虽然效率不是亲手编写的那种高效率得应用,但是在acm中的编码效率和正确率却能够大大的提高,下面是一道双重map容器的应用

http://acm.hdu.edu.cn/showproblem.php?pid=1263


题意是中文,而且很简单,但是麻烦,用map嵌套就好了

[cpp] view plaincopyprint?
  1. #include <iostream>  
  2. #include <cstdio>  
  3. #include <string>  
  4. #include <map>  
  5. #include <algorithm>  
  6. #include <iomanip>  
  7. using namespace std;  
  8.   
  9. map<string, map<string, int> > Map;  
  10. map<string, int> mmap;  
  11.   
  12. int main()  
  13. {  
  14.     int ncase, m;  
  15.     cin >> ncase;  
  16.     while(ncase--)  
  17.     {  
  18.         Map.clear();  
  19.         cin >> m;  
  20.         for(int i = 0; i < m; i++)  
  21.         {  
  22.             string name, loc;  
  23.             int num;  
  24.             cin >> name >> loc >> num;  
  25.             (Map[loc])[name] += num;  
  26.         }  
  27.         for(map<string, map<string, int> >::iterator it = Map.begin(); it != Map.end(); it++)  
  28.         {  
  29.             cout << it->first <<endl;  
  30.             for(map<string, int> ::iterator ii = it->second.begin(); ii != it->second.end(); ii++)  
  31.             {  
  32.                 cout << setw(4) << "|";  
  33.                 cout << "----";  
  34.                 cout << ii->first <<"(" << ii->second << ")" << endl;  
  35.             }  
  36.         }  
  37.         if(ncase != 0)  
  38.             cout << endl;  
  39.     }  
  40.     return 0;  
  41. }  
0 0
原创粉丝点击