zoukankan      html  css  js  c++  java
  • STL整理--MAP

    参考https://www.cnblogs.com/ivanovcraft/p/9084315.html

    普通的Map是从键(key)到值(value)的映射,其内部实现是一棵以key为关键码的红黑树

    Map的相关操作

    声明:

    map<key的类型,value的类型>名称;
    比如:
    map<long long,bool>mp;
    map<string,int>mp;
    map<pair<int,int>,vector<int>>mp;

    就像其他需要排序的数据类型一样,key为一个结构体|需要按value排序的map,需要重载小于号

    struct pos{
        int x,y,s;
        string move[100];
    };
    map<pos,int>mp;
    bool operator <(const pos &ai,const pos &bi)
    {
        return (ai.x==bi.x)?ai.y>bi.y:ai.x>bi.x;
    }

    []运算符

    map重载了[]运算符,map[key]返回key到value的引用,时间复杂度O(log n)
    []操作符是map最吸引人(坑人)的地方。我们可以很方便地通过map[key]来得到key对应的value,还可以对map[key]进行赋值操作,改变key对应的value。
    若查找的key不存在,则执行map[key]后,map会自动新建一个二元组(key,zero),并返回zero的引用

    eg.
    map<string,int>mp;
    for(int i=1;i<=n;i++)
    {
        string s;
        int num;
        cin>>s>>num;
        mp[s]=num;
    }
    for(int i=1;i<=m;i++)
    {
        string s;
        cin>>s;
        cout<<mp[s]<<endl;
    }
    注意:
    if(mp[x]!=qqq)
    {...}
    这里如果mp[x]不存在他会自动创建一个mp[x]=0;

    map.size()

    统计map中元素个数,函数返回一个整形变量,表示map中元素个数,时间复杂度O(1)

    用法:名称.size();
    eg.
    int num=mp.size();

    map.empty()

    检查map是否为空,返回一个bool型变量,1表示map为空,否则为非空,时间复杂度O(1)

    用法:名称.empty();
    eg.
    if(mp.empty())
        cout<<"Mymap is Empty."<<endl;

    map.clear()

    清空map,无返回值

    用法:名称.clear();
    eg.
    mp.clear();

    map.count(x)

    返回map中key为x的元素个数,时间复杂度为O(log n)

    用法:名称.count(x)
    eg.
    if(!mp.count(x))
        mp[x]=1;

    迭代器

    双向访问迭代器,不支持随机访问,支持星号解除引用,仅支持“++”,“--”这两个算术操作

    引用和操作:

    map<类型,类型>::iterator it;
    eg.
    map<int,int>::iterator it=mp.begin();
    it++;
    it--;

    若把it++,则it将会指向“下一个”元素。这里的下一个是指在key从小到大排序的结果中,排在it下一名的元素。同理,若把it--,则it会指向排在上一个的元素
    “++”,“--”操作的复杂度均为O(log n)
    对map的迭代器解除引用后,将得到一个二元组pair<...,...>
    遍历map及访问其中的元素

    for(map<int,int>::iterator it=mp.begin();it!=mp.end();it++)
        if(it->second==ans)        //访问二元组中第二个,即value
            cout<<it->first<<endl;        //访问key
    c++11还可以这样:
    for (auto iter:mp)
      iter.second = 5;//这里不会修改map的值
    for (auto &iter:mp)
      iter.second = 5;//这里map内的值会变成5

    map.find(x)

    在map中查找key为x的二元组,并返回指向该二元组的迭代器,若不存在,返回map.end(),时间复杂度为O(log n)

    用法:名称.find(x);
    eg.
    if(mp.find(s)!=mp.end())
        cout<<"Have Found!"<<endl;

    map.insert(pair<...,...>)

    在map中插入,参数是pair<key.type,value.type>,返回插入地址的迭代器和是否插入成功的bool并成的pair,时间复杂度为O(log n)
    PS:insert在进行插入的时候是不允许有重复的键值的,如果新插入的键值与原有的键值重复则插入无效

    用法:名称.insert(pair<key的类型,value的类型>);
    eg.
    mp.insert(make_pair(2,3));

    map.erase(参数)

    删除,参数可以是pair或者迭代器,返回下一个元素的迭代器,时间复杂度为O(log n)

    用法:名称.erase(参数);
    eg.
    map<int,int>::iterator it=mp.begin();
    mp.erase(it);
    mp.erase(make_pair(2,3));

     unordered_map (hash_table) 本质是hash表来存储的,数据结构与map有区别

    因而也导致了在不同场合下他们有不同的优势

    基本用法都差不多...

    unordered_map <int ,int > mp
    // 查找元素
    mp.find(xxx);//返回的是该元素位置的指针,未找到返回end()
    if (it != unorderedFirst.end())
    ...
    mp.count(xxx) //返回0和1 与map不同,返回的不是多少个
    // 赋值元素
    mp[xx]=xxx;
    mp.insert(make_pair(xx,xxx))
    // 获取元素(已知该元素存在)
    mp.[xx]
    mp.at(xx)

    删除什么的也一样

    但是由于数据结构使用的不同,unordered_map 更适合多组对应关系明确时数据的查询是否存在 o(1) (而map查询时log的)

    而重载符号也是重载 ‘=’ 而不是 ‘<’

    map被卡的时候可以试试unordered_map

  • 相关阅读:
    从Oracle提供两种cube产品说开
    Sql Server DWBI的几个学习资料
    Unload Oracle data into text file
    初学Java的几个tips
    我常用的Oracle知识点汇总
    benefits by using svn
    如何在windows上使用putty来显示远端linux的桌面
    building commercial website using Microsoft tech stack
    Understand Thread and Lock
    Update google calendar by sunbird
  • 原文地址:https://www.cnblogs.com/cherrypill/p/12514420.html
Copyright © 2011-2022 走看看