13.2 浅析哈希表和STL map。对比哈希表和STL map。哈希表是怎么实现的?如果输入数据规模不大, 我们可以使用什么数据结构来代替哈希表。

解答

对比哈希表和STL map

在哈希表中,实值的存储位置由其键值对应的哈希函数值决定。因此, 存储在哈希表中的值是无序的。在哈希表中插入元素和查找元素的时间复杂度都是O(1)。 (假设冲突很少)。实现一个哈希表,冲突处理是必须要考虑的。

对于STL中的map,键/值对在其中是根据键进行排序的。它使用一根红黑树来保存数据, 因此插入和查找元素的时间复杂度都是O(logn)。而且不需要处理冲突问题。 STL中的map适合以下情况使用:

  1. 查找最小元素
  2. 查找最大元素
  3. 有序地输出元素
  4. 查找某个元素,或是当元素找不到时,查找比它大的最小元素

哈希表是怎么实现的

  1. 首先需要一个好的哈希函数来确保哈希值是均匀分布的。比如:对大质数取模
  2. 其次需要一个好的冲突解决方法:链表法(chaining,表中元素比较密集时用此法), 探测法(probing,开放地址法,表中元素比较稀疏时用此法)。
  3. 动态地增加或减少哈希表的大小。比如,(表中元素数量)/(表大小)大于一个阈值时, 就增加哈希表的大小。我们新建一个大的哈希表,然后将旧表中的元素值, 通过新的哈希函数映射到新表。

如果输入数据规模不大,我们可以使用什么数据结构来代替哈希表。

你可以使用STL map来代替哈希表,尽管插入和查找元素的时间复杂度是O(logn), 但由于输入数据的规模不大,因此这点时间差别可以忽略不计。

careercup-C和C++ 13.2的更多相关文章

  1. [CareerCup] 17.13 BiNode 双向节点

    17.13 Consider a simple node-like data structure called BiNode, which has pointers to two other node ...

  2. [CareerCup] 18.13 Largest Rectangle of Letters

    18.13 Given a list of millions of words, design an algorithm to create the largest possible rectangl ...

  3. [CareerCup] 13.1 Print Last K Lines 打印最后K行

    13.1 Write a method to print the last K lines of an input file using C++. 这道题让我们用C++来打印一个输入文本的最后K行,最 ...

  4. [CareerCup] 13.2 Compare Hash Table and STL Map 比较哈希表和Map

    13.2 Compare and contrast a hash table and an STL map. How is a hash table implemented? If the numbe ...

  5. [CareerCup] 13.3 Virtual Functions 虚函数

    13.3 How do virtual functions work in C++? 这道题问我们虚函数在C++中的工作原理.虚函数的工作机制主要依赖于虚表格vtable,即Virtual Table ...

  6. [CareerCup] 13.4 Depp Copy and Shallow Copy 深拷贝和浅拷贝

    13.4 What is the difference between deep copy and shallow copy? Explain how you would use each. 这道题问 ...

  7. [CareerCup] 13.5 Volatile Keyword 关键字volatile

    13.5 What is the significance of the keyword "volatile" in C 这道题考察我们对于关键字volatile的理解,顾名思义, ...

  8. [CareerCup] 13.6 Virtual Destructor 虚析构函数

    13.6 Why does a destructor in base class need to be declared virtual? 这道题问我们为啥基类中的析构函数要定义为虚函数.首先来看下面 ...

  9. [CareerCup] 13.7 Node Pointer 节点指针

    13.7 Write a method that takes a pointer to a Node structure as a parameter and returns a complete c ...

  10. [CareerCup] 13.8 Smart Pointer 智能指针

    13.8 Write a smart pointer class. A smart pointer is a data type, usually implemented with templates ...

随机推荐

  1. 剑指Offer:连续子数组的最大和

    题目: 输入一个整型数组, 数组里有正数也有负数. 数组中的一个或连续的多个整数组成一个子数组. 求所有子数组的和的最大值. 要求时间复杂度为O(n) #include <stdio.h> ...

  2. C# 中 string.Empty、""、null的区别

    原文C# 中 string.Empty."".null的区别 一.string.Empty 和 "" 1.Empty是string类中的一个静态的只读字段,它是 ...

  3. [Bhatia.Matrix Analysis.Solutions to Exercises and Problems]ExI.4.4

    (1). There is a natural isomorphism between the spaces $\scrH\otimes \scrH^*$ and $\scrL(\scrH,\scrK ...

  4. 自动化测试(四):VBScript脚本语言

    VBS基于对象,只能利用现成的对象,不能封装.继承等,意味着不是真正的面向对象 语言的学习: 1.数据定义,变量定义,表达式 2.程序控制结构 3.函数,方法,类 4.异常处理 VBScript的数据 ...

  5. 开源了一个iOS输入控件【原】

    1.Github 地址:https://github.com/linyc/InputBarFollowKeyboard 2.说明文档:https://github.com/linyc/InputBar ...

  6. poj 2104 K-th Number(主席树)

    Description You are working for Macrohard company in data structures department. After failing your ...

  7. leetcode–Binary Tree Maximum Path Sum

    1.题目说明 Given a binary tree, find the maximum path sum.   The path may start and end at any node in t ...

  8. 浅析Netty的异步事件驱动(一)

    本篇文章着重于浅析一下Netty的事件处理流程,Netty版本为netty-3.6.6.Final. Netty定义了非常丰富的事件类型,代表了网络交互的各个阶段.并且当各个阶段发生时,触发相应的事件 ...

  9. leetcode@ [241] Different Ways to Add Parentheses (Divide and Conquer)

    https://leetcode.com/problems/different-ways-to-add-parentheses/ Given a string of numbers and opera ...

  10. linux内存负载分析

    衡量内存负载的一个很重要的指标就是页面置换的频率.当linux系统频繁的对页进行换进换出 的时候,说明物理内存不过,不得不进行频繁的置换页面. 使用vmstat(virtual memory stat ...