为什么我需要哈希表? [英] Why would I need the Hash table?

查看:131
本文介绍了为什么我需要哈希表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来学习计算机编程的人.

最近,我研究了哈希表.在本文中说,哈希函数将值转换为索引.这将有助于减少更少的时间,并使访问成本为O(1).

该文本中的实现是将数组封装到类模板中.那是我不明白的地方.由于使用数组,为什么他不直接使用数组的索引.这将访问要随机查找的元素,这样可以快速进行并节省探测时间(解决碰撞问题的方法).

Hi, I''m new to learn the computer programming.

Recently, I study about the Hash table. It is said in the text that the Hash functions transform the value into an index. That will help reduce much less time and make the access cost O(1).

The implementation in that text is to encapsulate an array into a class template. That''s where I don''t understand. Since using an array, why wouldn''t he just use the index of the array directly. This accesses the elements to be found randomly so that will be fast and saves the time probing(the way to solve collision problem).

推荐答案

让我们假设您有一个整数数组.

为此,您将采用一个数组,其中每个整数值都可能包含一个元素(4个字节):其中所有4,294,967,296个-一个数组有16 GB的内存,其中许多将不使用. br/>
如果您开始谈论字符串,那么潜在的数字将变得巨大.散列意味着您不需要为所有这些字符串分配空间,可以为更少的空间分配空间,只需根据需要扩展散列表即可.而且,当您谈论字符串时,生成哈希然后在有序索引中查找哈希要快得多,而不是比较每个字符串直到找到它!
Let''s assume you have an array of integers.

To do it your way would take an array with an element (4 bytes) for each possible value of the integer: all 4,294,967,296 of them - that''s 16 gigabytes of memory for one array, much of which will not be used.

If you start talking about strings, the potential number gets enormous. hashing means that you don''t need to allocate space for all these strings, you can allocate space for far less, and just expand the hash table as necessary. And, when you are talking strings, it is much, much quicker to generate a hash then look for that in an ordered index, than it is to compare each string until you find it!


frankgt40写道:
frankgt40 wrote:

为什么需要哈希表?

例如,因为您要实现

Because, for instance, you want to implement associative arrays[^].


这篇关于为什么我需要哈希表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆