0
目前即時創建一個簡單的電話簿。搜索矢量時遇到問題。當我需要搜索接近名稱的字符串時,它只能讓我在目錄中搜索確切的鍵。例如,當我搜索「汽車」時,它會聲明它不在目錄中,當它應該拉起卡爾,德里克和卡爾大衛。請幫忙謝謝。建立電話簿搜索矢量使用地圖庫
#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;
}
「接近名稱的字符串」您能準確地指定一個字符串與另一個字符串「接近」的含義嗎?無論如何,'std :: map'不可能有直接的幫助;這不是模糊匹配的業務。 – 2014-09-19 21:39:54