4

What is the data structure used for following line of code in C++?

map <char, int> dict;

Is it a hash table?

jogojapan
  • 65,927
  • 10
  • 94
  • 129
programmingIsFun
  • 997
  • 2
  • 10
  • 19

4 Answers4

6

std::unordered_map uses hashing to store its objects.

Rapptz
  • 20,187
  • 4
  • 71
  • 86
4

The standard does not impose any specific implementation on std::map. It only gives the required operations and their complexity. Those factors lead to the actual implementation choice which is usually a Red-black Tree.

The chapter listing the requirements for std::map is 23.2.4 Associative Containers in C++11.

pmr
  • 56,784
  • 10
  • 109
  • 154
0

It is usually implemented with a self-balancing BST. Implementation is actually compiler specific.

std::map<char, int> dict;

A char is the key while an int is the corresponding value.

Chris Dargis
  • 5,541
  • 4
  • 37
  • 61
  • 4
    I don't think there is any requirement for it to be Red-Black tree implementation. It could be anything under the hood if the semantics are followed. – TJD Aug 31 '12 at 01:15
0

It uses Red-Black Tree to organize keys in order.

That's why you can iterate it in ascending order, and the key object has to have operator< overloaded.

FrostNovaZzz
  • 780
  • 3
  • 8
  • 22