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
- Look up the key and return the value. Put the variable
namebetween 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 astring, 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.atis the checked alternative.phoneBook.at(name)throws astd::out_of_rangeexception 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 whyoperator[], which is not allowed on aconstmap, compiles here. - The result is a copy. The function returns a
stringby value, so the caller gets its own copy of the number.
Edge cases and common mistakes
- A single entry. With
{"Max": "555-0100"}andname = "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".
