直接上AC代码:
#include<iostream>
#include<map>
#include<string>
using namespace std;
int main(){
int t;
scanf("%d", &t);
while(t--){
int n;
scanf("%d",&n);
map<string, map<string, int> > mp;//可以理解为二维的map,有两个key,一个value
map<string, map<string, int> >:: iterator it1;
map<string, int>:: iterator it2;
string place, fruit;
int number;
for(int i = 1; i <= n; i++){
cin>>fruit>>place>>number;
mp[place][fruit] += number;
}
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; //注意一下输出格式
}
}
// it2 = (*it1).second.begin(); 可以写成 it2 = it1->second.begin();
if(t)
cout<<endl;
}
return 0;
}