4

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

map <char, int> dict;

Is it a hash table?

jogojapan
  • 68,383
  • 11
  • 101
  • 131
programmingIsFun
  • 1,057
  • 2
  • 11
  • 20

4 Answers4

6

std::unordered_map uses hashing to store its objects.

Rapptz
  • 20,807
  • 5
  • 72
  • 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
  • 58,701
  • 10
  • 113
  • 156
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,891
  • 4
  • 39
  • 63
  • 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
  • 802
  • 3
  • 10
  • 22