C - stl 的 map

xiaoxiao2021-02-28  52

stl 的 map

题目描述

夏天来了~~ 好开心啊,呵呵,好多好多水果 ~~ Joe经营着一个不大的水果店.他认为生存之道就是经营最受顾客欢迎的水果.现在他想要一份水果销售情况的明细表,这样Joe就可以很容易掌握所有水果的销售情况了.

Iutput

第一行正整数N(0

Output

对于每一组测试数据,请你输出一份排版格式正确(请分析样本输出)的水果销售情况明细表.这份明细表包括所有水果的产地,名称和销售数目的信息.水果先按产地分类,产地按字母顺序排列;同一产地的水果按照名称排序,名称按字母顺序排序. 两组测试数据之间有一个空行.最后一组测试数据之后没有空行.

Sample Input

1 5 apple shandong 3 pineapple guangdong 1 sugarcane guangdong 1 pineapple guangdong 3 pineapple guangdong 1

Sample Output

guangdong |—-pineapple(5) |—-sugarcane(1) shandong |—-apple(3)


思路:

定义map键值类型时用一个string 类型和一个map

代码

#include <cstdio> #include <iostream> #include <string> #include <map> #include <algorithm> using namespace std; typedef map<string, map<string, int> > mapp; int main() { int t, n, i, k; string fruit, place; mapp a; mapp::iterator ite; map<string, int>::iterator jte; cin>>t; while(t--){ cin>>n; for(i=0; i<n; i++){ cin>>fruit>>place>>k; a[place][fruit] += k; } for(ite=a.begin(); ite!=a.end(); ite++){ cout<<ite->first<<endl; for(jte=(ite->second).begin(); jte!=(ite->second).end(); jte++){ cout<<" |----"<<jte->first<<'('<<jte->second<<')'<<endl; } } a.clear(); if(t!=0) cout<<endl; } return 0; }
转载请注明原文地址: https://www.6miu.com/read-2628211.html

最新回复(0)