Map

9.5 - Map basic 5


Just like with vector, we can also iterate through every element in a map using a range-based for loop and structured binding. For example:

for (auto [key, value] : my_map) {
    std::cout << "Key: " << key << std::endl;
    std::cout << "Value: " << value << std::endl;
}

This will print out the key and value of each element inside the map. And since map is sorted, the printing order will also be sorted according to the key.

Your task is to use the range-based for loop to print out each element in a line in the power_level map. The format is:

<name>'s power level is <power>
#include <map> #include <iostream> #include <string> int main() { // Create the map power_level std::map<std::string, int> power_level{ {"Raditz", 1500}, {"Nappa", 4000}, {"Goku", 9000}, {"Vegeta", 18000}, {"Piccolo", 3500}, {"Krillin", 1770}, {"Yamcha", 1480}, {"TienShinhan", 1830}, {"Chiaotzu", 610}, {"Yajirobe", 970}, {"Gohan", 2800}, {"Saibamen", 1200}, }; // Write your code after this line! }