searching vector with maps

Currently im creating a simple phone directory. I am having a problem when searching the vector. It only lets me search for the exact key in the directory when I need to to search for strings that are close to the name. For example when I search "car" it will state that its not in the directory when it should pull up Carr, Derek and Carr David. Please help Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <string>
#include <map>
#include <vector>
#include <ostream>
#include <iostream>

int main()
{
std::map<std::string, std::vector <std::string> > directory;

directory["Carr"].push_back("Derek 916-667-6761");
directory["Carr"].push_back("David 916-667-6766");
directory["Mcfadden"].push_back("Darren 510-667-0000");
directory["Streater"].push_back("Rod 510-667-0001");
directory["Jones"].push_back("James 510-667-0020");
directory["Mack"].push_back("Khalil 707-557-6700");
directory["Woodson"].push_back("Charles 707-557-0061");
directory["Tuck"].push_back("Justin 707-557-6001");
directory["Moore"].push_back("Sio 415-600-5551");
directory["Roach"].push_back("Nick 415-600-4461");
directory["Woodson"].push_back("Rod 415-600-4441");

std::string str;
int yesno;
std::cout <<"Would you like to search a name in the phone directory?(yes=1, no=0)" <<std::endl;
std::cin>>yesno;
while (yesno==1)
{
    std::cout << "Enter name you would like to find: ";
    std::cin >> str;

    std::map<std::string, std::vector <std::string> >::iterator p;

    p = directory.find(str);
    if(p != directory.end())
    {
      std::string key = p->first;
      std::vector<std::string> names = p->second;

      for (int i = 0; i < names.size(); ++i)
         std::cout << key << " " << names[i] << std::endl;

    }
    else
    {
        std::cout << "Name not in phone directory.\n";
    }
   std::cout <<"Do you have another name you would like to look up?(yes=1, no=0)"<<std::endl;
   std::cin>>yesno;
   }
   system("pause");
   return 0;
  }
It is not trivial matter. I suggest you to read on Levenshtein distance: http://en.wikipedia.org/wiki/Levenshtein_distance

Topic archived. No new replies allowed.