哈希表和关联数组的 "key" 和 "value" 术语是否可以互换使用?

Are the "key" and "value" terms for hash tables and associative arrays used interchangeably?

我一直在关注这个 CS 课程介绍。

在学C的时候,学习了hashtables。

哈希 table 是需要哈希函数将 "key" 映射到整数值的数组。 该值将是数组中的索引。

"Key" -> [哈希函数] -> 值

数组[值] = "Key"

现在,我正在学习PHP,我对关联数组的使用很困惑。在 PHP 中,我们传入一个键(例如 $_POST["key"] ,它将为我们提供一个值。所以这里的 "key" 是数组的索引,与 C hash table 其中索引是哈希函数输出的值。

$_POST["key"] = 值

我做了很多搜索,了解到散列 table 和关联数组并非 100% 相同,但我很困惑为什么这两种不同的场景使用术语 "key" 和 "value" 以不同的方式。

我是不是看错了什么?

"key"和"value"不是一个意思

key 是你输入散列 table 或 PHP 关联数组的东西,或者一般来说,"map" 返回.

您 运行 感到困惑的是,您在第一个示例中从哈希 table 中返回的 然后被用作key(数组索引)到 different 事物(数组)。正如一个人可以同时是 parent 和 child 一样,一个数字(或其他任何东西)既可以是键(在一件事上)又可以是值(在另一件事上)。这是一个问题,它的作用与你使用它的东西有关。

一般来说,"key"是一段数据,用于从某种数据结构中唯一标识和检索某些"value"。但是 "value" 这个词当然也可以表示很多其他的意思。

例如,如果您的语言有内置哈希表,如 Python 或 JavaScript,您可以只说 table[key] = value 来存储某些内容,而说 value = table[key] 来检索它。如果您正在制作自己的哈希表,您可能首先必须计算键的哈希函数,然后根据哈希计算数组索引,然后将值放入数组中的该索引处。作者可能将散列函数的结果称为 "hash value",他可能将数组索引称为数组 "key",所以是的,它可能会令人困惑。

我倾向于说在你的 CS 课程的第一个 exampe/statement 中“关键”这个词用错了:

Hash tables are arrays that require a hash function to map a "key" to an integer value. The value would be the index in the array.

声明最好是:

Hash tables are arrays that require a hash function to map an input value of some kind to an integer value, called the hash value. The hash value would be the index in the array.

input value -> [Hash function] -> hash value

进一步声明:

array[value] = "Key"

是错误的,因为哈希函数可以为多个输入值生成相同的哈希值。通常,array[value] 将生成一个 列表 ,其中包含散列函数计算相同散列值的所有输入值。

关联数组 是一种数组符号,告诉机器从数据集(数组名称)中检索与唯一键(数组索引)关联的信息。下面机器将执行一个算法来search/retrieve这个信息。编程语言可以为程序员提供关联数组(符号)来帮助程序员编写他的程序。所以它只是一个数组notation,它不是数组indexing,就像C中的hash数组