Course Data Structures Essentials
Map
Hello again ๐. In this class, you will learn about Map and its main functions.
- Introduction
- When to use a Map?
- Map in C++
- Map in Python
Introduction
In certain situations we need to relate elements. For example, in a vector, we have that each vector position holds a value, that is, there is a relationship between the vector position and the element that is saved in that position. However, we have situations that we are not relating positions to the saved element, but more complex relationships like:
- Name (text) and Age (number);
- Name (text) and City (text);
- A sequence of numbers and Name;
For these types of examples, using a vector can be extremely complicated, because it would be necessary to think of some way to represent each thing as a number and that this number would always be unique. Thus, the need arises for a structure that can represent the relationship between elements of different types of data.
The Map, as its name already says, provides a mapping for a given relation between elements. For example: We want to relate a person's name to their age, as the name is unique, we say it is the key of the Map and the age the element that is stored in the key. In addition, this type of structure, keeps the elements ordered by the key for two purposes:
- To keep the structure dynamic;