使用地图库建立电话目录搜索矢量

时间:2014-09-19 20:50:31

标签: c++ stl

目前我正在创建一个简单的电话目录。我在搜索向量时遇到问题。它只允许我在需要搜索接近名称的字符串时搜索目录中的确切键。例如,当我搜索" car"它会说它不在目录中,它应该拉起Carr,Derek和Carr David。请帮忙谢谢。

#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;
  }

1 个答案:

答案 0 :(得分:0)

它是计算机科学的一个重要领域,与C ++ STL无关(至少现在)。

有关详细信息,请参阅here