4

I know that the STL containers like set and map are sorted but how are they actually sorted? What is the underlying structure?

I couldn't find any books about it.


I'm a C++ beginner please don't judge me. :)

Shalomi90
  • 736
  • 4
  • 9
  • 33

1 Answers1

4

For both std::map and std::set, it is implementation defined how the sorting is being done. The underlying data structure should sort the elements somehow:

Internally, the elements in a map are always sorted by its key following a specific strict weak ordering criterion indicated by its internal comparison object (of type Compare).

(same holds for set.)

A typical data structure for these containers is red black tree or a binary search tree.

gsamaras
  • 71,951
  • 46
  • 188
  • 305