Information about cookies on this page

Our website uses cookies to ensure the functionality of the website, to collect and analyze information about the performance and usage of the website. Privacy Policy

  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in
  
  • Courses
    • Discord
    • Twitter
    • Linkedin
    • Facebook
    • Instagram
  • Pricing
  • English
  • Čeština
  • Deutsch
  • Русский
Sign inStart free
  • Courses
  • Pricing

Community

Sign in

Adding and Changing Values

You change a value in a map the way you read it: with its key in square brackets. Assign the new value to it, as you would to a variable. When the key isn't in the map yet, the same statement adds it:

#include <iostream>
#include <map>
#include <string>

int main() {
    std::map<std::string, int> inventory;
    inventory["potion"] = 3;
    inventory["bread"] = 2;
    inventory["potion"] = 5;

    std::cout << "Potions: " << inventory["potion"] << std::endl;
    std::cout << "Bread: " << inventory["bread"] << std::endl;
    return 0;
}

This prints:

Potions: 5
Bread: 2

std::map<std::string, int> inventory; creates an empty map. The first assignment adds the key "potion" with the value 3, and the second one adds "bread". The third one finds "potion" already in the map, so it only changes its value. A map has no push_back(): you add elements with square brackets.

A value in a map behaves like a variable, so +=, -=, ++ and -- work on it too. When you add these lines at the end of main above, before return 0;:

inventory["potion"] += 2;
inventory["bread"]--;
std::cout << "Potions: " << inventory["potion"] << std::endl;
std::cout << "Bread: " << inventory["bread"] << std::endl;
std::cout << "Different items: " << inventory.size() << std::endl;

they print:

Potions: 7
Bread: 1
Different items: 2

The method size() gives the number of elements, as it does for a vector. In a map, that is the number of keys: Aria carries 8 things, but only 2 different items.

Instructions

  1. 1

    In a cave, Aria finds three potions. Below the comment, add 3 to the value stored under "potion". The program prints Potions: 4. Keep the lines of the map as they are: the exercise will also try another number of potions.

  2. 2

    There is a gem in the cave too. Below the line you wrote, add the key "gem" to the map with the value 1. At the end of the program (before return 0;), print the number of different items with size(): Different items: 3.

Back

2/12

  1. 1. What Is a Map?
  2. 2. Adding and Changing Values
  3. 3. Looping over a Map
  4. 4. Missing Keys
  5. 5. Counting with a Map
  6. 6. Quiz: Counting Loot
  7. 7. Removing Elements
  8. 8. Maps and Functions
  9. 9. Quiz: Restocking
  10. 10. Structs as Values
  11. 11. Unordered Maps
  12. 12. Summary
Next

Try it yourself

Do this exercise right here in your browser. No account needed.