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

Summary

In this lesson, you learned that:

  • A map stores values under keys. It needs #include <map>, and its angle brackets hold the type of the keys and then the type of the values: std::map<std::string, int> prices = {{"sword", 50}, {"potion", 10}};. Each element is a key with its value, and each key is in the map only once.
  • prices["sword"] reads the value stored under a key. prices["sword"] = 45; changes it, or adds the key when it isn't there yet. +=, -=, ++ and -- work on values too, and size() gives the number of keys.
  • for (const auto& [item, price] : prices) goes through all the elements. auto lets the compiler work out the type, and the names in square brackets are for the key and the value. With auto&, the loop can change the values, but never the keys.
  • A std::map keeps its keys sorted. Strings are sorted by the codes of their characters, so uppercase letters come before lowercase ones, and letters with accents after z.
  • Square brackets add a missing key with 0, so a typo in a key makes a new key instead of an error. count(key) gives 1 or 0 and adds nothing. at(key) stops the program when the key is missing: std::out_of_range: map::at: key not found.
  • Counting is one line in a loop: counts[item]++;.
  • erase(key) removes an element, and empty() says whether the map has no elements left.
  • A function gets a map as a const reference to read it (const std::map<std::string, int>& prices) and as a reference to change it. Square brackets don't compile on a const map, so read it with at() and count().
  • The values can be structs or vectors: bestiary["Troll"].health. For a missing key, square brackets add an "empty" value of the type.
  • std::unordered_map (#include <unordered_map>) is used the same way, but keeps its keys in no order you can rely on. It is usually faster for big maps. When you need the order, or aren't sure, use std::map.

In the next lesson, Algorithms and Lambdas, you will let ready-made functions of the standard library do common work for you: sort a vector, find its largest element, or count the elements that match a condition.

Instructions

  1. 1

    Let's practice. Count the loot from the arena in a map counts, and print each kind of loot with its number:

    bread: 1
    gem: 2
    potion: 2

    Keep the lines of prices and the line with loot as they are: the exercise will also try other data.

  2. 2

    The shop buys only the kinds of loot that are in prices. In place of the comment above main, write a function lootValue that gets counts and prices, both as const references, and returns how much gold the shop pays for all the loot: for each kind it buys, the number times the price. Print the result below the list: The shop pays: 116.

Back

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