-1

Thinking about creating a phone book but confused that which STL container is better choice. Please explain the difference between set and map. Which one is better choice in which scenario.

For example when names and numbers are stored in map then if make names as key then search cannot be done with numbers and vice-versa. So which container/data structure would exactly serve this purpose?

jww
  • 97,681
  • 90
  • 411
  • 885
Vinod
  • 115
  • 11
  • http://stackoverflow.com/questions/21804086/what-is-the-difference-between-stdset-and-stdmap – Steephen Mar 24 '16 at 17:46
  • 5
    Possible duplicate of [What is the difference between set vs map in C++?](http://stackoverflow.com/questions/22088607/what-is-the-difference-between-set-vs-map-in-c) – user222031 Mar 24 '16 at 17:47
  • Possible duplicate of [What is the difference between set vs map in C++?](https://stackoverflow.com/q/22088607/608639), [What is the difference between std::set and std::map](https://stackoverflow.com/q/21804086/608639), etc. – jww Oct 15 '18 at 01:16

2 Answers2

3

Very little internally. They are both red-black trees.

map though contains a pair as its value type. The tree operations then operate on only the first element in that pair. Map then adds additional functionality to search based on this first element and retrieve just the second.

Use map when you need key->value pairs. Use set when you need to track unique values as a whole.

Edward Strange
  • 40,307
  • 7
  • 73
  • 125
0

Think of std::set as a std::map in which the key is also the value.

R Sahu
  • 204,454
  • 14
  • 159
  • 270