0

writing program to search address book. fast search time being the biggest priority. professor wants match function to look like

Container<const Person*> matches( std::string prefix ) const;

i am having trouble defining a function of that sort. Also, a second part was added later on to add a hash function to the program. how would i go about writing hash function for a struct of strings. i have attached important parts of the code.

`struct Person 
`{
std::string firstName;
std::string lastName;
std::string email;
`};

`class AddressBook
`{
`public:

AddressBook();
void add(Person person);
void match(std::string namesearch);
std::vector<Person> perV;

`};

void AddressBook::match(std::string namesearch)
`{
for (std::vector<Person>::const_iterator itr = perV.begin(); itr <     `    `   perV.end(); ++itr)
{
    if (strstr(itr->firstName.c_str(), namesearch.c_str()) || 
        strstr(itr->lastName.c_str(), namesearch.c_str()))
    {
        std::cout << itr->firstName << ' ' << itr->lastName
            << std::endl << "Email: " << itr->email << std::endl;
    }

  }

`}
  • 1
    What is your question? – UnholySheep Apr 07 '18 at 08:19
  • i need my match function definition to look like this Container matches( std::string prefix ) const; i am having trouble defining it also i need to add a hash function but dont know how to go about that since i have a struct of strings – Amr ELayyan Apr 07 '18 at 21:55

1 Answers1

0

Writing a hash function for the struct of strings does not sound as a good plan to me. But you can form a tree based on each string attribute you have as in the link.

I think another good way for search time efficiency is going with trie structures. You can implement it for all string attributes you have and will work well. Time complexity would be O(W*L), where W is number of unique words, and L is the average length of the words.

CanCode
  • 154
  • 2
  • 10