Need help with an exercise

Can someone please explain how this code could work? I can't find a way to approach this problem.


Given n names and phone numbers, assemble a phone book that maps friends' names to their respective phone numbers. You will then be given an unknown number of names to query your phone book for. For each name queried, print the associated entry from your phone book on a new line in the form name=phoneNumber; if an entry for name is not found, print Not found instead.



Example:
Sample Input

3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry

Sample Output

sam=99912222
Not found
harry=12299933




Explanation

We add the following (Key,Value) pairs to our map so it looks like this:
phoneBook={(sam,99912222),(tom,11122222),(harry,12299933)}


We then process each query and print key=value if the queried is found in the map; otherwise, we print Not found.

Query 0: sam
Sam is one of the keys in our dictionary, so we print sam=99912222.

Query 1: edward
Edward is not one of the keys in our dictionary, so we print Not found.

Query 2: harry
Harry is one of the keys in our dictionary, so we print harry=12299933.
Last edited on
For how map works see this:

http://www.cplusplus.com/reference/map/map/?kw=map

You can find several example, e.g. here:

http://www.cplusplus.com/reference/map/map/operator[]/
http://www.cplusplus.com/reference/map/map/find/

Note that map.find(...) will return map.end() if not found.
Topic archived. No new replies allowed.