C++ 标准模板库(STL):map

4. map

  • map翻译为映射,也是常用的STL容器。
  • 在定义数组时,实际上是定义了一个int型到int型的映射,或者int型到double型的映射等。
  • hashtable[max_size]不适合数据太大,导致不好设计散列函数和浪费空间。但可以选择map建立映射。

4.1 map的定义

map<typenameKey,typenameValue> mp;

map的值和键也可以是STL容器。

4.2 map容器内元素的访问

  1. 通过下标访问

    • map的值是唯一的
    #include <stdio.h>
    #include <map>
    using namespace std;
    int main(){
        map<char, int> mp;
        mp['c'] = 20;
        mp['c'] = 30;
        printf("%d\n", mp['c']);
        return 0;
    }
    
  2. 通过迭代器访问

    map<typenameKey,typenameValue>::iterator it;
    
    • map可以用it->first来访问键,it->second来访问值!
    • map会以键从小到大的顺序自动排序。(这是由于map内部是使用红黑树实现的,在建立映射的过程中会自动实现从小大到的排序功能。)
    #include <stdio.h>
    #include <map>
    using  namespace std;
    int main(){
        map<char, int> mp;
        mp['m'] = 20;
        mp['r'] = 30;
        mp['a'] = 40;
        for(map<char, int>::iterator it = mp.begin();it != mp.end(); it++){
            printf("%c %d\n", it->first,it->second);
        }
        return 0;
    }
    

4.3 map常用函数实例解析

  1. find()

    find(key)返回键值key的映射的迭代器,时间复杂度为\(O(logN)\)

    #include <stdio.h>
    #include <map>
    using  namespace  std;
    int main(){
        map<char ,int> mp;
        mp['a'] = 1;
        mp['b'] = 2;
        mp['c'] = 3;
        map<char ,int>::iterator it = mp.find('b');
        printf("%c %d", it->first, it->second);
        return 0;
    }
    
  2. erase()

    • 删除单个元素

      • mp.erase(it),it为需要删除的元素的迭代器。时间复杂度为\(O(1)\)
      #include <stdio.h>
      #include <map>
      using  namespace std;
      int main(){
          map<char, int> mp;
          mp['a'] = 1;
          mp['b'] = 2;
          mp['c'] = 3;
          //这种删除方法,需要先根据键值查找到迭代器指针的位置。
          auto it = mp.find('b');
          mp.erase(it);
          for(auto it = mp.begin(); it != mp.end(); it++){
              printf("%c %d\n", it->first, it->second);
          }
          return 0;
      }
      
      • mp.erase(key),key为欲删除的映射的键。时间复杂度为\(O(logN)\)
      #include <stdio.h>
      #include <map>
      using  namespace  std;
      int main(){
          map<char, int> mp;
          mp['a'] = 1;
          mp['b'] = 2;
          mp['c'] = 3;
          //总体来说删除特定键值的映射关系的时间复杂度都是O(logN)
          mp.erase('b');
          for(auto it = mp.begin(); it != mp.end(); it++){
              printf("%c %d\n", it->first, it->second);
          }
          return 0;
      }
      
    • 删除一个区间所有元素

      mp.erase(first,last) 左开右闭!

      #include <stdio.h>
      #include <map>
      using namespace std;
      int main(){
          map<char, int> mp;
          mp['a'] = 1;
          mp['b'] = 2;
          mp['c'] = 3;
          auto it = mp.find('b');
          mp.erase(it, mp.end()); //删除b之后的所有映射
          for(auto it = mp.begin(); it != mp.end(); it++){
              printf("%c %d\n" ,it->first, it->second);
          }
          return 0;
      }
      
  3. size()

    \(O(1)\)

    #include <stdio.h>
    #include <map>
    using namespace  std;
    int main(){
        map<char, int> mp;
        mp['a'] = 10;
        mp['b'] = 20;
        mp['c'] = 30;
        printf("%d\n",mp.size());
        return 0;
    }
    
  4. clear()

    \(O(N)\)

    #include <stdio.h>
    #include <map>
    using namespace std;
    int main(){
        map<char, int> mp;
        mp['a'] = 1;
        mp['b'] = 2;
        mp.clear();
        printf("%d\n", mp.size());
        return 0;
    }
    

4.4 map的常见用途

  • 需要建立字符或字符串与整数之间的映射关系的题目,使用map可以减少代码量。
  • 判断大整数或其他类型数据是否存在的题目
  • 字符串和字符串的映射也可能会遇到。
posted @ 2021-01-30 22:23  张吱吱  阅读(300)  评论(0编辑  收藏  举报