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()表示没有找到元素

 #include "stdafx.h"
#include "iostream"
#include "unordered_map"        //unodered_map的头文件     
#include "vector"
using namespace std; class MyClass
{
public:
vector<int> twoSum(vector<int> &nums, int target)
{
vector<int> res;
unordered_map<int, int> findNum; //初始化名为hash的hash table,<key,value>均为int型
int size = nums.size();
for (int i = ; i < size; i++)
{
int numToFind = target - nums[i];
if (findNum.find(numToFind) != findNum.end())
{
res.push_back(findNum[numToFind]); //map中和数组一样用的是[ ]
res.push_back(i); //push_back()是vector中的,不是map中的
return res; //只能输出一组,得到后直接跳出程序,返回值
}
findNum[nums[i]] = i;             //由此可以看到下标为键(key),下标对应的值为值(value)
}
return res;
}
}; int _tmain(int argc, _TCHAR* argv[])
{
vector<int> nums = { , , , , , , , , , };
int target = ;
vector<int> res;
MyClass solution;
res = solution.twoSum(nums, target);
int size = res.size();
for (int i = ; i < size; i++)
{
cout << res[i] << " ";
}
cout << endl;
system("pause");
return ;
}

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值全部执行完

 vector<int> nums1 = { , , ,  };
int len = nums1.size();
unordered_map<int, int>nums;
for (int a : nums1) nums[a];
for (int i = ; 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的情况来做。

 

2016.07.13-map的使用(以leetcode1-Two Sum为例)的更多相关文章

  1. http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html

    http://www.cnbc.com/2016/07/12/tensions-in-south-china-sea-to-persist-even-after-court-ruling.html T ...

  2. 多线程博文地址 http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html

    http://www.cnblogs.com/nokiaguy/archive/2008/07/13/1241817.html

  3. 2016/07/11 PHP接口的介绍与实现

        接口定义了实现某种服务的一般规范,声明了所需的函数和常量,但不指定如何实现.之所以不给出实现的细节,是因为不同的实体可能需要用不同的方式来实现公共的方法定义.关键是要建立必须实现的一组一般原则 ...

  4. 2016.08.13/2/index/_d_Lucene54_0.dvm: Too many open files

    er[file_system_exception: /elk/elasticsearch/data/es_cluster/nodes/0/indices/logstash-zjzc-frontend- ...

  5. 2016.07.13-vector<vector<int>>应用2——Two Sum扩展

    收获: vector<vector<int> >res,不能直接用res[j].push_back(number),因为res[j]是空的,没有初始化 可以先定义 vector ...

  6. 2016.2.13 (年初六) oracle两张表update方法

    A表customers和B表tmp_cust_city有3个相同字段, customer_id,city_name,customer_type 现要根据b表更新a表 更新一个字段情况: update ...

  7. Murano Weekly Meeting 2016.07.26

    Meeting time: 2016.July.26 1:00~2:00 Chairperson:  Nikolay_St, from Mirantis Meeting summary: 1.Masc ...

  8. Murano Weekly Meeting 2016.07.19

    Meeting time: 2016.July.19 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  9. Murano Weekly Meeting 2016.07.12

    Meeting time: 2016.July.12 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

随机推荐

  1. 深入理解JAVA虚拟机阅读笔记2——垃圾回收

    线程私有的程序计数器.虚拟机栈和本地方法栈随线程而生,随线程而灭.栈中的栈帧随方法的进入和退出有条不紊的入栈和出栈. 而Java堆和方法区因为需要多大内存.创建多少对象都是不确定的,因此这两个区域是垃 ...

  2. redis压力测试工具-----redis-benchmark

    redis做压测可以用自带的redis-benchmark工具,使用简单 压测命令:redis-benchmark -h 127.0.0.1 -p 6379 -c 50 -n 10000 压测需要一段 ...

  3. ZOJ3435_Ideal Puzzle Bobble

    把L,H,W分别减一就变成上面一个题目了. 不多说,也不召唤代码君了.

  4. 【Java并发编程】之六:Runnable和Thread实现多线程的区别

    Java中实现多线程有两种方法:继承Thread类.实现Runnable接口,在程序开发中只要是多线程,肯定永远以实现Runnable接口为主,因为实现Runnable接口相比继承Thread类有如下 ...

  5. url基础知识

    浏览器通过url访问服务器步骤 ①浏览器解析出url中的服务器名称 ②浏览器将服务器名称解析成ip(DNS解析) ③浏览器解析出url中的服务器端口(如果有端口的话) ④浏览器建立和web服务器的TC ...

  6. 读取Maven项目下resources目录下的配置文件(properties为例)

    配置文件:xxxxx.properties a.url=******************** b.url=---------------------------------- 读取配置文件: im ...

  7. 【bzoj4013】 HNOI2015—实验比较

    http://www.lydsy.com/JudgeOnline/problem.php?id=4013 (题目链接) 题意 给出$n$个数的$m$个大小关系,问它们之间可以形成的单调不降的序列有多少 ...

  8. C函数调用机制及栈帧指针

    http://blog.csdn.net/jjiss318/article/details/7185802

  9. 二、Linux学习之centOS的的setup

    因为我是在VMware上安装的centOS,网络设置选择的是桥接模式,这样可以和实体机使用同样的网络,但是也使用同样的IP,因此就需要设置一下IP了,否则使用ifconfig查询ip的时候显示的是12 ...

  10. Qt error ------ no matching function for call to QObject::connect(QSpinBox*&, <unresolved overloaded function type>, QSlider*&, void (QAbstractSlider::*)(int))

    connect(ui->spinBox_luminosity,&QSpinBox::valueChanged, ui->horizontalSlider_luminosity, & ...