I decided to take on competition and challenges across the internet. My place of choice at the moment is hacker rank. I'm stuck on hacker rank's 30 day challenge day 8. I need to create a map key data structure for a phone book. The maps need to be the names of the people and the keys are their numbers. Does anyone know how to complete this challenge and could someone explain to me why test case 1 would fail with my code?
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;
int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int it = 0;
int n;
int number;
string name;
std::map<string,int> phoneBook;
//Number of names + phone numbers
cin >> n;
//Fill in Map and keys
while (it < n)
{
cin >> name;
cin >> number;
if(phoneBook.insert(std::make_pair(name, number)).second != false)
{
phoneBook.insert(std::make_pair(name, number));
it++;
}
}
//Find the keys using the map
for (int i = 0; i < n; i++)
{
cin >> name;
if(phoneBook.find(name) != phoneBook.end())
{
cout<<name<<"="<<phoneBook[name]<<endl;
}
else if(phoneBook.find(name) == phoneBook.end())
{
cout<<"Not found"<<endl;
}
}
return 0;
}
↧