Map

9.2 - Map basic 2


A map store its elements as pairs. So when we are adding a new element to a map, we need to create a pair which we then store inside the map using the method insert. This is one way to do it, using the grades map from the previous lesson example:

grades.insert({"Tem", 80});

The {"Tem", 80} brace initialization creates the pair which is then inserted to grades.

Your task is to create and insert more elements to the power_level map. You need to put in:

NamePower level
Nappa4000
Goku9000
Vegeta18000
#include <map> #include <iostream> #include <string> int main() { // Create the map power_level std::map<std::string, int> power_level; // This code puts new key and value pairs inside map // Follow this format and put in the others power_level.insert({"Raditz", 1500}); // 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; } }