Is QMap a hash table?

487 Views Asked by At

I used Qmap many times but perhaps never used QHash. Now I'm reading about hash tables.

Is QMap a hash table?

I presume down there in a QHash we will find the ideas of Hash Maps. Should I say QHash is the implementation of a hash map (or hash table) data structure? Is QMap also the implementation of a hash table?

Can I use the terms map and table interchangeably?

1

There are 1 best solutions below

0
On

No QMap is not a hash table. Per the documentation:

The QMap class is a template class that provides a red-black-tree-based dictionary.

In other words it is a binary sort tree that uses the red-black tree algorithm to maintain balance. Meaning that searches will take O(logN) rather than O(1) as in the case of QHash.

It also means that QMap will keep the data sorted.

From the documentation you quoted:

QMap and QHash provide very similar functionality. The differences are:

  • QHash provides average faster lookups than QMap. (See Algorithmic Complexity for details.)
  • When iterating over a QHash, the items are arbitrarily ordered. With QMap, the items are always sorted by key.
  • The key type of a QHash must provide operator==() and a global qHash(Key) function. The key type of a QMap must provide operator<() specifying a total order. Since Qt 5.8.1 it is also safe to use a pointer type as key, even if the underlying operator<() does not provide a total order.

QHash is a hash table. QMap is a binary sort tree using the red black tree algorithm.