Lesson 6
Introduction to Maps and Unordered Maps in C++
Introduction to Map Data Structure

Welcome! Today, we're diving into C++ maps. 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 maps in C++.

Understanding Maps

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:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> map_example = { {"Alice", 25}, {"Bob", 28}, {"Charlie", 30} }; 6}

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".

Creating a Map

Maps are initiated by placing key-value pairs within {} using the initialization list.

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 // An empty map 6 std::map<std::string, int> empty_map; 7 8 // A map of students and their ages 9 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 10}

Keys and values can be of different data types. Here, our key is string, and a value is int. However, they can also be of the same data type. For example:

1std::map<int, int> map;

This map has an integer key and an integer value.

Accessing Map Elements

Unlike arrays, maps are collections of items accessed by their keys, not by their positions.

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 std::cout << student_age["Alice"] << std::endl; // Outputs: 12 7 return 0; 8}

Here, using the "Alice" key, we access the corresponding value. Keys serve as a special indexing system here.

Modifying Maps: Adding New Pairs

C++ maps are mutable, meaning we can add, modify, or delete elements.

We can add a new key-value pair like this:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 student_age["David"] = 14; 7}

Alternatively, we can use the insert() method to add a new key-value pair:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 student_age.insert({"David", 14}); 7}
Modifying Maps: Changing Pairs

We can modify a value by accessing it via its key:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 student_age["Alice"] = 15; 7}

By writing map_name[new_key], you can create a new element with the default value of the mapped type:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 student_age["Eve"]; // This creates a new element with key "Eve" and default value 0 7 std::cout << student_age["Eve"] << std::endl; // Outputs: 0 8}

But how do we understand if it worked? Let's learn how to print the map's contents!

Iterate a map with for loop

Iterating a map is different from iterating a vector. We can still use a simple range-based for loop, but this time each element is a pair of elements: the key and the value. We can access them with .first for the key and .second for the value. Let's see how it works:

C++
1#include <iostream> 2#include <map> 3 4int main() { 5 std::map<std::string, int> student_age = { {"Alice", 12}, {"Bob", 13}, {"Charlie", 11} }; 6 student_age["David"] = 14; 7 student_age["Alice"] = 15; 8 9 // Iterate through the map and print all key-value pairs 10 for (const auto& pair : student_age) { 11 std::cout << pair.first << ": " << pair.second << std::endl; 12 } 13 14 return 0; 15}

Here, we use a range-based for loop to iterate through each key-value pair in the map. The pair variable holds each key-value pair, where pair.first is the key and pair.second is the value. Here is how the output looks like:

Plain text
1Alice: 15 2Bob: 13 3Charlie: 11 4David: 14
Lesson Summary

Great work! We've delved into C++ maps: understanding their structure, creating and using them, and exploring their practical applications. Next, let's reinforce this knowledge with some hands-on exercises!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.