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

What Is a Map?

After the fights in the arena, Aria goes shopping. The shop has a price for every item. With what you know, you could keep the items and their prices in two vectors, like items and prices on the shopping receipt in the Loops lesson, or in a vector of structs. Either way, to find out what a potion costs, you would go through the items one by one until you find the potion.

A map does that search for you. It stores values under keys: you give it a key, such as the name of an item, and it gives you the value stored under it, such as the price:

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

int main() {
    std::map<std::string, int> prices = {
        {"sword", 50},
        {"helmet", 40},
        {"potion", 10}
    };

    std::cout << "A sword costs " << prices["sword"] << " gold" << std::endl;
    std::cout << "Three potions cost " << prices["potion"] * 3 << " gold" << std::endl;
    return 0;
}

This prints:

A sword costs 50 gold
Three potions cost 30 gold

Let's go through it:

  • Maps need #include <map>.
  • The angle brackets hold two types: first the type of the keys, then the type of the values. std::map<std::string, int> has strings as keys and whole numbers as values.
  • Each element of a map is a key with its value, in curly braces of its own: {"sword", 50}. The elements are separated by commas, and the whole map is in curly braces, as a vector is. This course writes each element on a line of its own.
  • prices["sword"] reads the value stored under the key "sword". It looks like reading an element of a vector, but there is a key in the square brackets instead of an index. The value behaves like a variable of its type, so you can calculate with it.

Each key is in a map only once: the keys are unique. A shop has one price for a sword, not two. If you write the same key twice when you create a map, as in {{"sword", 50}, {"sword", 70}}, the map keeps only the first one, and prices["sword"] is 50. Values can repeat, though: a sword and an axe can both cost 50.

Keys and values can have other types too. A std::map<std::string, std::string> stores strings under strings, for example a description under the name of each item.

Instructions

  1. 1

    Aria is in the shop. Print the price of a helmet, read from the map prices: Helmet: 40. Keep the lines of the map as they are: the exercise will also try other prices.

  2. 2

    The variable gold holds Aria's gold. She buys a helmet and two potions. Below, print how much gold she has left: Gold left: 30. Work it out from gold and the prices in the map, without writing the prices yourself.

Back

1/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.