<\/span><\/h2>\n\n\n\nToday, we’re learning about Key-Value pair mappings using a Map<\/em> or Dictionary<\/em> data structure. <\/p>\n\n\n\n<\/span>Task<\/strong><\/span><\/h2>\n\n\n\nGiven n<\/em><\/strong> 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<\/strong><\/em> queried, print the associated entry from your phone book on a new line in the form name=phoneNumber<\/code>; if an entry for name<\/strong><\/em> is not found, print Not found<\/code> instead.<\/p>\n\n\n\nNote:<\/strong> Your phone book should be a Dictionary\/Map\/HashMap data structure.<\/p>\n\n\n\n<\/span>Input Format<\/strong><\/span><\/h2>\n\n\n\nThe first line contains an integer, <\/em><\/strong>n<\/strong><\/em>, denoting the number of entries in the phone book.
Each of the n<\/strong><\/em> subsequent lines describes an entry in the form of 2<\/strong> space-separated values on a single line. The first value is a friend’s name, and the second value is an 8<\/strong>-digit phone number.<\/p>\n\n\n\nAfter the n<\/strong><\/em> lines of phone book entries, there are an unknown number of lines of queries<\/em>. Each line (query) contains a name<\/em><\/strong> to look up, and you must continue reading lines until there is no more input.<\/p>\n\n\n\nNote:<\/strong> Names consist of lowercase English alphabetic letters and are first names<\/em> only.<\/p>\n\n\n\n<\/span>Constraints<\/strong><\/span><\/h2>\n\n\n\n- 1 <= n<\/em> <= 105<\/sup><\/strong><\/li>
- 1 <= queries<\/em> <= 105<\/sup><\/strong><\/strong><\/li><\/ul>\n\n\n\n
<\/span>Output Format<\/strong><\/span><\/h2>\n\n\n\nOn a new line for each query, print Not found<\/code> if the name has no corresponding entry in the phone book; otherwise, print the full name<\/em><\/strong> and phoneNumber<\/strong><\/em> in the format name=phoneNumber<\/code>.<\/p>\n\n\n\nSample Input<\/strong><\/p>\n\n\n\n3\nsam 99912222\ntom 11122222\nharry 12299933\nsam\nedward\nharry<\/code><\/pre>\n\n\n\nSample Output<\/strong><\/p>\n\n\n\nsam=99912222\nNot found\nharry=12299933<\/code><\/pre>\n\n\n\nExplanation<\/strong><\/p>\n\n\n\nWe add the following n<\/em> = 3<\/strong> (Key,Value)<\/em> pairs to our map so it looks like this:<\/p>\n\n\n\nphoneBook<\/em> = {(sam<\/em>, 99912222), (tom<\/em>, 11122222), (harry<\/em>, 12299933)}<\/strong><\/p>\n\n\n\nWe then process each query and print key=value<\/code> if the queried key<\/em><\/strong> is found in the map; otherwise, we print Not found<\/code>.<\/p>\n\n\n\nQuery 0:<\/em> sam<\/code>
Sam is one of the keys in our dictionary, so we print sam=99912222<\/code>.<\/p>\n\n\n\nQuery 1:<\/em> edward<\/code>
Edward is not one of the keys in our dictionary, so we print Not found<\/code>.<\/p>\n\n\n\nQuery 2:<\/em> harry<\/code>
Harry is one of the keys in our dictionary, so we print harry=12299933<\/code>.<\/p>\n\n\n\n<\/span>Solution – Day 8: Dictionaries and Maps<\/strong><\/span><\/h2>\n\n\n\n<\/span>C++<\/strong><\/span><\/h3>\n\n\n\n#include <iostream>\n#include <map>\nusing namespace std;\n\nint main() {\n std::map<string, string> phoneBook;\n int n;\n cin >> n;\n \n \/\/ Read names and numbers, add to phoneBook:\n for(int i = 0; i < n; i++){\n string name;\n string phone;\n cin >> name;\n cin >> phone;\n phoneBook[name] = phone;\n }\n \n \/\/ Execute queries:\n std::map<string,string>::iterator it;\n string query;\n while( cin >> query ){\n it = phoneBook.find(query);\n \n if ( it != phoneBook.end() ){ \/\/ key is found in phoneBook \n cout << it->first << \"=\" << it->second << '\\n';\n }\n else{ \/\/ the iterator hit the end of the phone book without finding key\n cout << \"Not found\" << '\\n';\n }\n }\n \n return 0;\n}<\/pre>\n\n\n\n