Start learning free

LESSON 128 OF 667 · DICTIONARIES - CHALLENGES

Phone Book Lookup in C++: Reading an unordered_map

Practice Read the full explanation

Given a phone book phoneBook, an unordered_map from names to phone numbers, and a name, return the phone number of name.

Every name in the phone book is a key, and its value is that person's phone number, a string like "555-0142". The name you look up is always in the phone book.

 

Example 1:

Input: phoneBook = {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"}, name = "Ben"
Output: "555-0199"
Explanation: The key "Ben" maps to "555-0199".

Example 2:

Input: phoneBook = {"Max": "555-0100"}, name = "Max"
Output: "555-0100"
Explanation: The phone book has a single entry, and it is the one we look up.

Example 3:

Input: phoneBook = {"Tom": "555-0111", "Tim": "555-0111"}, name = "Tim"
Output: "555-0111"
Explanation: Tom and Tim share a phone number. We only need the value of the key "Tim".

 

Constraints:

  • phoneBook has at least one entry.
  • name is always a key of phoneBook.
  • Names are single words made of English letters, and they are all different. Two people can share a phone number.
  • Every phone number is a string of three digits, a dash and four digits, like "555-0142".
STEP 1 OF 1
    Ctrl + Enter to check

    C++ · DICTIONARIES - CHALLENGES

    Phone Book Lookup in C++, explained

    Problem overview

    The function phoneBookLookup receives phoneBook, an unordered_map<string, string> from names to phone numbers, and a name. It returns the phone number stored for that name. The name is always a key of the map, and each number is a string such as "555-0142".

    For {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} and name = "Ben", the answer is "555-0199". Two people may share a number: with {"Tom": "555-0111", "Tim": "555-0111"} and name = "Tim", the answer is "555-0111". Only keys have to be different, never values.

    Approach

    An unordered_map stores key and value pairs and finds a value from its key directly, without scanning every entry. The square brackets do that lookup: phoneBook[name] is the value stored under the key held in the variable name. The lesson has a single step, because that expression is already the answer.

    Both phoneBook.count("Ana") and phoneBook["Ana"] work on a key that exists: count says how many entries have that key (1 here), and the brackets give its value. The difference shows up only for missing keys, which the solution section below covers.

    Step by step

    1. Look up the key and return the value. Put the variable name between the brackets, without quotes. Writing "name" would search for the key made of the letters n, a, m, e, which isn't in the map.

    The solution in C++

    class Solution {
    public:
        string phoneBookLookup(unordered_map<string, string>& phoneBook, string name) {
            return phoneBook[name];
        }
    };
    

    The details that matter:

    • operator[] can modify the map. When the key is missing, phoneBook[name] inserts it with a default value, which is an empty string for a string, and returns that. The lookup of a missing name "Zed" in the three-entry book above returns "", and afterwards the map holds four entries instead of three. This lesson never looks up a missing name, so the side effect doesn't show up here.
    • at is the checked alternative. phoneBook.at(name) throws a std::out_of_range exception for a missing key instead of inserting one. Use it when a missing key would be a bug you want to see.
    • The map is passed by reference. The parameter type ends in &, so the map isn't copied. That is why operator[], which is not allowed on a const map, compiles here.
    • The result is a copy. The function returns a string by value, so the caller gets its own copy of the number.

    Edge cases and common mistakes

    • A single entry. With {"Max": "555-0100"} and name = "Max", the lookup returns "555-0100". The book has at least one entry, so there is no empty case.
    • Shared numbers. Two keys can hold the same value. The lookup follows the key you give it and nothing else.
    • Quotes around the variable. phoneBook["name"] looks for the literal key "name". It compiles, inserts that key with an empty value, and returns an empty string. On a map with two entries, that lookup returns "" and leaves the map with three.
    • Case matters. Keys are compared exactly, so "ben" is not "Ben".

    Testing

    • {"Ana": "555-0142", "Ben": "555-0199", "Cara": "555-0123"} with "Ben" gives "555-0199".
    • {"Max": "555-0100"} with "Max" gives "555-0100".
    • {"Tom": "555-0111", "Tim": "555-0111"} with "Tim" gives "555-0111".
    Keep going with C++650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free