Learning STL, this question is a mapping of str -> str -> int. If you are familiar with STL, it is easy to do, but if you don’t write it for a long time, you can forget about it. After seeing how someone else does it, I still feel that STL is strong and I am used to python. Just mess with C++ and write a bunch of bugs by myself.
Then we understand that it is the mapping relationship of str -> str -> int. Use map to store and open an iterator to iterate. Since auto is disabled, you can use iterator.
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
map<string,map<string,int> > fruit;
string type,place;
int num;
for(int i = 0; i < n; i ++){
cin >> type >> place >> num;
fruit[place][type] += num;
}
map<string,map<string,int> > :: iterator it;
map<string,int> :: iterator ti;
for(it = fruit.begin(); it != fruit.end(); it ++){
cout << it -> first << endl;
for (ti = it->second.begin(); ti != it->second.end(); ti ++){
cout << " |----" << ti->first << "(" << ti->second << ")" << endl;
}
}
return 0;
}
The picture originated from Ji Suanke, invaded and deleted.