Map

9.3 - Map basic 3


Since map is a associative container, we can retrieve the value of an element by using its key. In the example using grades map, we can access Tem's grade by using the at method as shown here:

std::cout << grades.at("Tem"); // Prints out Tem's grade

The value retrieved using at can also be modified like this:

grades.at("Tem") = 100; // Tem now has 100!!!! :3

Your task is to change Goku's power level to 27000 in line 24.

#include <map> #include <iostream> #include <string> int main() { // Create the map power_level std::map<std::string, int> power_level; // Insert the characters and their power level power_level.insert({"Raditz", 1500}); power_level.insert({"Nappa", 4000}); power_level.insert({"Goku", 9000}); power_level.insert({"Vegeta", 18000}); // This code prints out all the elements in the map // Note that since the key of the map is the name string, it will be sorted alphabetically // We will learn this later too for (const auto& pair : power_level) { std::cout << pair.first << "'s power level is " << pair.second << std::endl; } // Change Goku's power level to 27000, since he used Kaio-ken x3! std::cout << "Goku's power level after using Kaio-ken x3 is: " << power_level.at("Goku") << std::endl; }