Lessons 6–10 · About 60 min per lesson

Maps, sets and counting

Choose map, unordered_map, set or vector based on query requirements.

Lesson breakdown

  1. Lesson 6

    Count arbitrary names with unordered_map.

  2. Lesson 7

    Use map to report counts in key order.

  3. Lesson 8

    Use set for membership checks and deduplication.

  4. Lesson 9

    Look up missing keys with find without inserting defaults.

  5. Lesson 10

    Use a vector when the key range is small and contiguous.

Worked example

Names in the log are Bo, Mia and Bo. Output counts by ascending name.

How to solve it

Count in a map and iterate by key to get Bo 2 and Mia 1. A find lookup can report Ana as missing.

Assignment

Read log names and answer count queries. Missing names yield 0 and queries do not add keys.

Check your result

The example returns 2, 0, 2 for Bo, Ana, Bo and retains only two keys.