哈希表与关联数组

发布于 2024-09-08 02:04:09 字数 336 浏览 4 评论 0原文

最近,我在一本非常有名的书《算法简介中读到了哈希表一个>”。我还没有在任何实际的应用程序中使用它们,但我想这样做。但我不知道如何开始。
有哪些使用示例,例如如何使用哈希表实现字典应用程序(如 ABBYY Lingvo)?

最后我想知道 PHP 中的哈希表和关联数组有什么区别,我的意思是我应该使用哪种技术以及在什么情况下?

如果我错了(请原谅),请纠正我,因为实际上我是从哈希表开始的,我只有关于它们的基本(理论)知识。

Recently, I have read about hash tables in a very famous book, "Introduction to Algorithms". I haven't used them in any real applications yet, but I want to. But I don't know how to start.
What are some samples of using it, for example, how to realize a dictionary application (like ABBYY Lingvo) using hash tables?

And finally I would like to know what is the difference between hash tables and associative arrays in PHP, I mean which technology should I use and in which situations?

If I am wrong (I beg pardon) please correct me, because actually I am starting with hash tables and I have just basic (theoretical) knowledge about them.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

仙气飘飘 2024-09-15 02:04:09

在 PHP 中,关联数组被实现为哈希表,并具有一些额外的功能。

然而,从技术上讲,关联数组与哈希表并不相同。它只是在幕后通过哈希表部分实现。因为它的大部分实现都是哈希表,所以它可以完成哈希表可以做的所有事情,而且还可以做更多的事情。

例如,您可以使用 for 循环遍历关联数组,而哈希表则无法做到这一点。

因此,虽然它们很相似,但关联数组实际上可以执行哈希表功能的超集,因此它们并不完全相同。将其视为哈希表加上额外的功能。

代码示例:

使用关联数组作为哈希表

$favoriteColor = array();
$favoriteColor['bob'] = 'blue';
$favoriteColor['Peter'] = 'red';
$favoriteColor['Sally'] = 'pink';
echo 'bob likes: ' . $favoriteColor['bob'] . "\n";
echo 'Sally likes: ' . $favoriteColor['Sally'] . "\n";
// Output: bob likes blue
//         Sally likes pink

循环关联数组

$idTable = array();
$idTable['Tyler'] = 1;
$idTable['Bill'] = 20;
$idTable['Marc'] = 4;
// Up until here, we're using the array as a hash table.

// Now we loop through the array - you can't do this with a hash table:
foreach($idTable as $person => $id)
    echo 'id: ' . $id . ' | person: ' . $person . "\n";

// Output: id: 1 | person: Tyler
//         id: 20 | person: Bill
//         id: 4 | person: Marc

特别注意在第二个示例中如何维护每个元素的顺序(TylerBillMarc)基于他们进入数组的顺序。这是关联数组和哈希表之间的主要区别。哈希表不维护其所保存的项之间的任何连接,而 PHP 关联数组则保留(您甚至可以对 PHP 关联数组进行排序)。

In PHP, associative arrays are implemented as hash tables, with a bit of extra functionality.

However, technically speaking, an associative array is not identical to a hash table; it's simply implemented in part with a hash table behind the scenes. Because most of its implementation is a hash table, it can do everything a hash table can, but it can do more, too.

For example, you can loop through an associative array using a for loop, which you can't do with a hash table.

So while they're similar, an associative array can actually do a superset of what a hash table can do, so they're not exactly the same thing. Think of it as hash tables plus extra functionality.

Code examples:

Using an associative array as a hash table:

$favoriteColor = array();
$favoriteColor['bob'] = 'blue';
$favoriteColor['Peter'] = 'red';
$favoriteColor['Sally'] = 'pink';
echo 'bob likes: ' . $favoriteColor['bob'] . "\n";
echo 'Sally likes: ' . $favoriteColor['Sally'] . "\n";
// Output: bob likes blue
//         Sally likes pink

Looping through an associative array:

$idTable = array();
$idTable['Tyler'] = 1;
$idTable['Bill'] = 20;
$idTable['Marc'] = 4;
// Up until here, we're using the array as a hash table.

// Now we loop through the array - you can't do this with a hash table:
foreach($idTable as $person => $id)
    echo 'id: ' . $id . ' | person: ' . $person . "\n";

// Output: id: 1 | person: Tyler
//         id: 20 | person: Bill
//         id: 4 | person: Marc

Note especially how in the second example, the order of each element is maintained (Tyler, Bill, and Marc) based on the order in which they were entered into the array. This is a major difference between associative arrays and hash tables. A hash table does not maintain any connection between the items it holds, whereas a PHP associative array does (you can even sort a PHP associative array).

川水往事 2024-09-15 02:04:09

PHP 数组基本上是哈希表。

PHP arrays are basically hash tables.

陈甜 2024-09-15 02:04:09

关联数组和哈希表之间的区别在于,关联数组是一种数据类型,而哈希表是一种数据实现。显然,关联数组类型在当前的许多编程语言中非常重要:Perl、Python、PHP 等。哈希表是实现关联数组的主要方式,但不是唯一的方式。关联数组是哈希表的主要用途,但并不是唯一的用途。所以这并不是说它们是相同的,但是如果您已经有关联数组,那么您通常不必担心差异。

出于性能原因,了解您最喜欢的语言中的关联数组是作为哈希实现的非常重要。了解该实施的间接费用也很重要。正如您在 C 中看到的那样,散列表比线性数组更慢并且使用更多的内存。Perl

通过将关联数组称为“散列”将这两个概念混为一谈。就像 Perl 的许多功能一样,它并没有完全错误,但它很草率。

The difference between an associative array and a hash table is that an associative array is a data type, while a hash table is a data implementation. Obviously the associative array type is very important in many current programming languages: Perl, Python, PHP, etc. A hash table is the main way to implement an associative array, but not quite the only way. And associative arrays are the main use of hash tables, but not quite the only use. So it's not that they are the same, but if you already have associative arrays, then you usually shouldn't worry about the difference.

For performance reasons, it can be important to know that your associative arrays in your favorite language are implemented as hashes. And it can be important to have some idea of the overhead cost of that implementation. Hash tables are slower and use more memory than linear arrays as you see them in C.

Perl lumps the two concepts together by calling associative arrays "hashes". Like a number of features of Perl, it isn't quite wrong, but it's sloppy.

旧城空念 2024-09-15 02:04:09

PHP 中的数组实际上是一个有序映射,而不是哈希表。

映射和哈希表之间的主要区别在于无法记住添加元素的顺序。另一方面,哈希表比映射快得多。从映射中获取元素的复杂度为 O(n·logn),从哈希表中获取元素的复杂度为 O(1)。

An array in PHP is actually an ordered map, not a hash table.

The main difference between map and hash table consists in inability to remember the order in which elements have been added. On the other hand, hash tables are much faster than maps. The complexity of fetching an element from a map is O(n·logn) and from a hash table it is O(1).

泛滥成性 2024-09-15 02:04:09

关联数组是一种不通过索引而是通过键访问元素的数组。它的内部工作原理是特定于实现的(没有规则它必须如何工作)。关联数组可以通过哈希表来实现(大多数实现都会这样做),但它也可以通过某种树结构或跳跃列表来实现,或者算法只是迭代数组中的所有元素并查找键匹配(这会非常慢,但它有效)。

哈希表是一种存储数据的方法,其中值与键相关联,并且您打算在(通常几乎)恒定时间内查找键的值。这听起来与您对关联数组的期望完全一样,这就是为什么大多数时候使用哈希表来实现这些数组,但这不是强制性的。

An associative array is an array where you don't access elements by an index, but by a key. How this works internally is implementation specific (there is no rule how it must work). An associative array could be implemented by a hash table (most implementations will do that), but it could also be implemented by some sort of tree structure or a skip list or the algorithm just iterates over all elements in the array and looks for a key that matches (this would be awfully slow, but it works).

A hash table is a way how to store data where values are associated to keys and where you intend to find values for keys within a (usually almost) constant time. This sounds exactly like what you expect of an associative array, that's why most of the time hash tables are used for implementing those arrays, but that is not mandatory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文