zoukankan      html  css  js  c++  java
  • 2016.07.13-map的使用(以leetcode1-Two Sum为例)

    map的使用

    1.unordered_map和map的区别

    2.如何用

    3.for (int a : nums1)

    4.to_string()

    5.map的应用

    1.unordered_map和map的区别

    相同点:

      map和unordered_map都是<key,value>的形式,这是固定的,对于map中的每一组映射,first都是key均为键、second均为value值.

      通过key快速索引到value,最后返回的是value。<key,value>可以是<int,int>,<char,int>,<string,int>等等。

    可以统计字母出现的次数,单词出现的次数,那么key就是字母,单词,value就是每次队员的单词字母出现的个数

      无论是map.find(key),map.count(key),map.ereas(key),map.insert(key),均是对key操作,同样返回的map[key]即为value

    区别:

      map的头文件为"map",multimap的头文件也是“map”,unordered_map头文件则是"unodered_map"。

      map中会自动根据key值进行排序(按照二叉搜索树存储,map中自建了一颗红黑树,根据key的准则自动排序),而unodered_map则不排序为乱序。所以unodered_map的效率要高于map,multimap允许键重复出现

      在map中使用make_pair(x,y)将x,y看做一组映射,x为Key,y为value

      m.count(key):由于map不包含重复的key,因此m.count(key)取值为0,或者1,表示是否包含。

    2.unordered_map和map的使用

    使用:

      以leetcode1-Two Sum为例

      1.新建map时,map类型<key值类型,value值类型> 函数名

      2.map中和数组一样用的是方括号“[ ]”

        将vector<int>中或者数组中的元素添加到map中时:map[nums[i]] = i; 这里是用下标作为key,用下标的值对应value。

      3.map.find(需要find的值) 如果找到则返回此元素的迭代器,若没有找到则返回end()的迭代器(即查找到尾部都没有找到)

        所以用map.find(number) != map.end()表示找到元素

          用map.find(number) == map.end()表示没有找到元素

     1 #include "stdafx.h"
     2 #include "iostream"
     3 #include "unordered_map"        //unodered_map的头文件     
     4 #include "vector"
     5 using namespace std;
     6 
     7 class MyClass
     8 {
     9 public:
    10     vector<int>  twoSum(vector<int> &nums, int target)
    11     {
    12         vector<int> res;
    13         unordered_map<int, int> findNum;                //初始化名为hash的hash table,<key,value>均为int型
    14         int size = nums.size();
    15         for (int i = 0; i < size; i++)
    16         {
    17             int numToFind = target - nums[i];
    18             if (findNum.find(numToFind) != findNum.end())
    19             {
    20                 res.push_back(findNum[numToFind]);        //map中和数组一样用的是[ ]
    21                 res.push_back(i);                        //push_back()是vector中的,不是map中的
    22                 return res;                              //只能输出一组,得到后直接跳出程序,返回值
    23             }
    24             findNum[nums[i]] = i;             //由此可以看到下标为键(key),下标对应的值为值(value)
    25         }
    26         return res;
    27     }
    28 };
    29 
    30 int _tmain(int argc, _TCHAR* argv[])
    31 {
    32     vector<int> nums = { 0, 1, 2, 3, 4, 5, 9, 7, 8, 10 };
    33     int target = 5;
    34     vector<int> res;
    35     MyClass solution;
    36     res = solution.twoSum(nums, target);
    37     int size = res.size();
    38     for (int i = 0; i < size; i++)
    39     {
    40         cout << res[i] << " ";
    41     }
    42     cout << endl;
    43     system("pause");
    44     return 0;
    45 }

    3.for (int a : nums1)

    for (int a : nums1)和for(int i = 0;i<size;i++)

      for(int i = 0;i<size;i++)是进行size次循环,每次改变i的值,比如可以比较nums[i]和nums[i+1]的值,也可以改变nums[i]的值,这里与i有关。

      for (int a : nums1)则是直接把nums1这个数组或者vector中的值赋值给a,直到nums值全部执行完

    1 vector<int> nums1 = { 1, 2, 2, 1 };
    2 int len = nums1.size();
    3 unordered_map<int, int>nums;
    4 for (int a : nums1) nums[a];
    5 for (int i = 0; i < len; i++) nums[nums1[i]];

      上述均是把nums1中的值添加到nums这个map中

    4.to_string()

    to_string()

      如果你需要的整型数的操作,但是最后返回的却是string型,那么可以用to_string(x),将整型x变成string型

       to_string(countA) + 'A' + to_string(countB) + 'B';  

    5.map的应用

    map中应用

      如205. Isomorphic Strings同构字符串(paper,title) ,290. Word Pattern(abba,对应dog,cat,cat,dog)

        构建2个map,使其为映射关系,在根据第二map查看映射是否正确

      如217. Contains Duplicate,242. Valid Anagram,218. Contains Duplicate II,299. Bulls and Cows

        则根据vector或是string中规律,建一个map,根据key和value的情况来做。

     

     

  • 相关阅读:
    表达式树作为条件封装多表连查
    EF之结构进一步优化
    EF之ExecuteSqlCommand更新出现无效的解决方案
    dynamic与匿名对象
    webapi 通过dynamic 接收可变参数
    EF INNER JOIN,LEFT JOIN,GROUP JOIN
    Linq join on 多条件
    Excel 行列转置 解决竖向拉,字母跟着递增的问题
    Windows7 安装vs2015 之后 调试Web项目IIS启动不了 aspnetcore.dll未能加载
    Mysql 服务在本机,需要单机调试Mysql数据库 发生 不认识hostname‘localhost’
  • 原文地址:https://www.cnblogs.com/zhuzhu2016/p/5665857.html
Copyright © 2011-2022 走看看