【HDU1263 水果】 STL之map应用经典好题

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1263

 

题目大意:对同一地方的同一水果进行归类。

 

解题思路:  map真心强大,以前只知道map的一维运用,今天了解了map的二维运用(map映射map)。

 

View Code
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <map>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int main()
 9 {
10     int T, m;
11     cin >> T;
12     while(T--)
13     {
14        map<string,map<string,int> >mp;   ///注意格式
15        string place, name;
16        int num;
17        cin >> m;
18        for(int i=0; i<m; i++)
19        {
20            cin >> name >> place >> num;
21            mp[place][name]+=num;;
22        }
23        for(map<string,map<string,int> >::iterator iter1=mp.begin(); iter1!=mp.end(); iter1++) ///第一关键字(map里居然会自动排序,难道和hash表一样的?真心感觉自己肤浅)
24        {
25            cout << iter1->first <<endl;
26            for(map<string,int >::iterator iter2=iter1->second.begin(); iter2!=iter1->second.end(); iter2++)  ///第二关键字
27              cout << "   |----" << iter2->first << "(" << iter2->second << ")" <<endl;
28        }
29        if(T) cout << endl;
30     }
31     return 0;
32 }

 

posted @ 2013-04-17 17:46  Mr. Ant  阅读(1526)  评论(1编辑  收藏  举报