Hash tables VS associative arrays

后端 未结 5 1220
南旧
南旧 2020-12-07 14:06

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 want to.

5条回答
  •  感动是毒
    2020-12-07 14:38

    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.

提交回复
热议问题