水果 HDU - 1263 (map自动排序嵌套使用)

题目链接

一个map相当于一维数组,两个map嵌套相当于二维“数”组,map默认先对key值排序,再对字面值排序。

AC代码:

#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
#include <map>
#include <algorithm>
using namespace std;
const int maxn=1e6+5; 
int main(){
	map<string,map<string,int> >mp;
	map<string,map<string,int> >::iterator it1;
	map<string,int>::iterator it2;
	int t;
	cin>>t;
	while(t--){
		mp.clear();//map一定要初始化!!!! 
		int n;
		cin>>n;
	    for(int i=1;i<=n;i++){
	    	string name,home;
	    	int num;
	    	cin>>name>>home>>num;
	    	mp[home][name]+=num;
		}
		for(it1=mp.begin();it1!=mp.end();it1++){
			cout<<it1->first<<endl;
			for(it2=it1->second.begin();it2!=it1->second.end();it2++){
				cout<<"   |----"<<it2->first<<"("<<it2->second<<")"<<endl;
			}
		}
		if(t) cout<<endl;
	}
} 

猜你喜欢

转载自blog.csdn.net/Alanrookie/article/details/107150225