what is the difference between set and map in C ++ (STL)

advertisements

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?


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.