zoukankan      html  css  js  c++  java
  • map 和 unordered_map

    map就是映射。

    定义

    map<typename,typename>

    注:map的元素是pair。

    特性

    map会对第一个对象自动排序。

    map不允许有两个相同的关键字。

    map可以定义迭代器iterator。当然,map相当于一个像pair的结构体,要访问元素时注意使用->或者(*it).first/second。

    first是key,second才是value。

    typename也可以是结构体。

    关键字

    erase(iterator)

    insert(pair(typename,typename)

    find(typename(first)) 返回值为地址。

    count(typename)查询元素次数。

    栗子:记录学生姓名和成绩,通过姓名查找成绩

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    #include<utility>
    #include<map>
    #include<string>
    using namespace std;
    map <string,int>q;
    const int maxn=5;
    inline int read()
    {
        int x=0,w=0;char c=getchar();
        while(!isdigit(c))w|=c=='-',c=getchar();
        while(isdigit(c))x=(x<<3)+(x<<1)+(c^48),c=getchar();
        return w?-x:x;
    }
    int main()
    {
        int n=read();
        string c;
        for(int i=1;i<=n;i++)
        {
            cin>>c;
            q.insert(make_pair(c,read()));
        }
        int m=read();
        for(int i=1;i<=m;i++)
        {
            cin>>c;
            printf("%d
    ",q[c]);
        }
        return 0;
    }
  • 相关阅读:
    创建一个catkin工作空间
    Ubuntu下安装Python3.4及用python编译py文件
    ubuntu连接kinect v2
    ubuntu循环登录问题的解决
    找到bashrc
    创建一个catkin工作空间
    ts配置
    几个loader源码
    e2e测试
    eslint配置
  • 原文地址:https://www.cnblogs.com/BrotherHood/p/13149640.html
Copyright © 2011-2022 走看看