码迷,mamicode.com
首页 > 其他好文 > 详细

计蒜客练习题:水果店(嵌套map)

时间:2019-10-26 20:58:48      阅读:83      评论:0      收藏:0      [点我收藏+]

标签:str   show   while   cstring   nbsp   map   注意   嵌套   info   

技术图片技术图片

这道题主要就是考察嵌套map的使用

这道题我一开始用的map+pair但是怎么写都不对。

之后就求助万能的百度发现网上都是用嵌套map做的。

别说我还真不知道map该怎么嵌套,以及怎么调用嵌套map。

map<string, map<string,int> > m; 定义在第二维,插入的话就跟二维数组一样(m[tm1][tm2] = tm3;)。

调用的话需要定义两个不同的iterator一个是外层的,一个是内层的。

具体看本题代码。

PS:注意输出格式!

附AC代码:

技术图片
#include <iostream>
#include <cstring>
#include <map>
using namespace std;
int main()
{
    int n;
    cin >> n;
    map<string,map<string,int> >m;
    string temp1, temp2;
    while (n--)
    {
        int temp3;
        cin >> temp1 >> temp2 >> temp3;
        m[temp2][temp1] += temp3;
    }
    for (map<string, map<string, int> > ::iterator it = m.begin(); it != m.end(); it++)
    {
        cout << it->first <<endl;
        for (map<string, int> :: iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
        cout << "   |----" << it2->first << "(" << it2->second<<")"<< endl;
    }
    if (n != 0) cout <<endl;
    return 0;
}
View Code

 

计蒜客练习题:水果店(嵌套map)

标签:str   show   while   cstring   nbsp   map   注意   嵌套   info   

原文地址:https://www.cnblogs.com/Vikyanite/p/11745259.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!