HDOJ.1263 水果(map)

水果

点我跳转到题面
点我一起学习STL-MAP

题意分析

给出多组测试数据,每组数据有多条信息。分别是水果种类,地点,和水果数目。每组信息要按照样例输出,并且输出要按照地点->水果种类的字典序。
可以采用map的嵌套,来实现题目要求。但要注意一下,在循环的时候迭代器内外层是的类型是不一样的,所以要重新写两种typedef。
(此题也是学习了别的大牛的做法,这题有结构体排序也可以是实现)

代码总览

/*
    Title:HDOJ.1263
    Author:pengwill
    Date:2016-11-21
*/
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
using namespace std;
string s1,s2;
int main()
{
   //freopen("in.txt","r",stdin);
    typedef map<string,map<string,int> > mmp;
    typedef map<string,int> mp;
    mmp p;
    int t,n,num,flag = 0;
    cin>>t;
    while(t--){
        p.clear();
        cin>>n;
        while(n--){
            cin>>s2>>s1>>num;
            p[s1][s2]+=num;
       }
       mmp::iterator iter1;
       mp::iterator iter2;
       for(iter1 = p.begin(); iter1!= p.end();iter1++){
            cout<<iter1->first<<endl;
            for(iter2 = iter1->second.begin();iter2 != iter1->second.end();iter2++){
                cout<<"   |----"<<iter2->first<<"("<<iter2->second<<")"<<endl;
            }
       }
       if(t){
        cout<<endl;
       }
    }

    return 0;
//fclose(stdin);
}
原文地址:https://www.cnblogs.com/pengwill/p/7367227.html