I've created code that based of the information that was given to me below.
"The first line will have an integer N denoting the number of entries in the phone book. Each entry consists of two lines: a name(either first-name or first-name last-name) and the corresponding phone number.
After these, there will be some queries. Each query will contain name of a friend. Read the queries until end-of-file."
Also if the name is not found you are supposed to print "Not found"
The problem is on Hackerrank.com and I cant submit because the program was "Terminated due to timeout"
Can someone please help me improve this code and make it run faster?! I barely figured out how to make it run so I'm all out of ideas on how to improve it.
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;
class Number{
public:
string name;
string lname;
long number;
};
class PhoneBook{
public:
void findNumber(){
string tempName;
while (cin >> tempName){
int nf=0;
for(int i=0; i < phoneNumbers.size(); i++){
if (tempName == phoneNumbers[i].name){
cout << phoneNumbers[i].name << "=" << phoneNumbers[i].number << endl;
}
else {
nf +=1;
}
}
if (nf == phoneNumbers.size()){cout << "Not found" << endl;}
}
}
void addNumber(int entries){
for(int numbers = 0; numbers < entries; numbers++){
Number newContact;
long num;
cin >> newContact.name;
cin >> num;
if (!cin){
cin.clear();
cin >> newContact.lname >> newContact.number;
}
else{
newContact.number = num;
}
phoneNumbers.push_back(newContact);
}
}
private:
string PBname;
vector<Number> phoneNumbers;
};
int main(){
int entries;
cin >> entries;
PhoneBook myFriends;
myFriends.addNumber(entries);
myFriends.findNumber();
return 0;
}