Welcome! Today, we're diving into C++ map
s. These store items as key-value pairs, much like a dictionary where you look up a word (key) to find its meaning (value). By the end of this lesson, you’ll have a good grasp of map
s in C++.
Maps in C++ are collections of key-value pairs. This method differentiates them from arrays and vectors, which store elements in a linear order. std::map
stores elements in a sorted order by key.
Here's a simple map
, where names are keys and ages are values:
In map_example
, the keys are "Alice," "Bob," and "Charlie," and the values are 25, 28, and 30, respectively. Key uniqueness is integral to the design of maps — for example, that means we can only have a single key called "Alice"
.
Maps are initiated by placing key-value pairs within {}
using the initialization list.
