zoukankan      html  css  js  c++  java
  • hdu 1263 STL的嵌套使用

    做做STL题不为别的,只为学会更好的运用它,用起来的确很方便的。

    中文题题意不多说,字典顺序输出,地区和水果名称都相同的就求一下和。

    #include<stdio.h>
    
    #include<iostream>
    
    #include<map>
    #include<string>
    using namespace std;
    map<string,map<string,int> > m;
    int main()
    {
    int n,nm,t;
    string s1,s2;
    scanf("%d",&n);
    while(n--)
    {
    m.clear();
    scanf("%d",&nm);
    for(int i=0;i<nm;i++)
    {
    cin >> s1 >> s2 >> t;
    m[s2][s1]+=t;
    }
    map<string, map<string,int> > ::iterator it;
    for(it = m.begin();it != m.end(); it++)
    {
    cout << it->first << endl;
    map<string, int> ::iterator it1;
    for(it1 = it->second.begin(); it1 != it->second.end(); it1++)
    cout << " |----" << it1->first << "(" << it1->second << ")" << endl;
    }
    if(n != 0)
    printf("\n");
    }
    return 0;
    }

    (1)第一次用map的镶嵌。网上搜的一段代码段增强理解。

    View Code
    #include<map>  
    #include<iostream>  
    #include <string>  
    using namespace std;  
    int main()  
    {  
        map<int,map<int,string> >multiMap; //对于这样的map嵌套定义,  
        map<int, string> temp;                //定义一个map<int, string>变量,对其定义后在插入multiMap  
        temp[90] = "hi";  
        temp[100] = "maxi";  
        multiMap[10] = temp;  
        multiMap[10][80]="xiaoyu";   
        multiMap[5][30]="xiaoma";  
        map<int,map<int,string> >::iterator multitr;  // 以下是如何遍历本multiMap  
        map<int,string>::iterator intertr;  
        for(multitr=multiMap.begin();multitr!=multiMap.end();multitr++)  
        {  www.2cto.com
            for(intertr= multitr ->second.begin(); intertr != multitr ->second.end(); intertr ++)  
                cout<< multitr ->first<<" "<<intertr->first<<" ("<<intertr -> second <<")"<<endl;  
        }  
        system("pause");  
        return 0;  
    }  
    运行结果:
    (2)写本题还学到一点
    使用 string 类需要用的是 <string> 头文件。不是 <cstring> 也不是 <string.h>。
    (3)漏说了一点map<string,map<string,int>此处无空格会爆错 > m;
    只有多用才能加强记忆啊!
  • 相关阅读:
    《吊打面试官》系列-缓存雪崩、击穿、穿透
    WebGL学习之纹理贴图
    小试小程序云开发
    关于socket.io的使用
    动画函数的绘制及自定义动画函数
    canvas实现俄罗斯方块
    Redis集群
    手工搭建基于ABP的框架
    手工搭建基于ABP的框架(3)
    手工搭建基于ABP的框架(2)
  • 原文地址:https://www.cnblogs.com/XDJjy/p/2992214.html
Copyright © 2011-2022 走看看