1263 水果(自定义map)

#include<iostream>
#include<string>
#include<map>
using namespace std;
 //map<string,map<string,int>>这种比较新奇,解决了三个变量的键值对应
struct MyStruct
{
	map <string, int>MyStructma;  //存放水果名以及该种水果的数量
};
int main()
{
	map <string, MyStruct>ma;    //地名   
	map <string, MyStruct>::iterator it;
	map <string, int>::iterator MyStructmait;
	string fruit,place;
	int count;
    int n,t;
	cin>>t;
	while(t--)
	{
		ma.clear();
		cin>>n;
		while(n--)
		{
			cin>>fruit>>place>>count;
			ma[place].MyStructma[fruit] += count;
		}
		for (it = ma.begin(); it != ma.end(); it++)
		{
			cout<<it->first<<endl;
			for (MyStructmait = it->second.MyStructma.begin(); MyStructmait != it->second.MyStructma.end(); MyStructmait++)
			{
				
				cout<<"   |----"<<MyStructmait->first<<"("<<MyStructmait->second<<")"<<endl;
			}
		}
		if(t != 0)cout<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40061421/article/details/80996860