我有一个地图变量,我试图使用" find"用于发现该地图中是否存在某些内容的功能。那东西是用户输入的字符串。 如果我喜欢下面发布的代码,"它" (迭代器)总是失败,即使" typedRoadName"存在于地图变量中。
但是,例如,如果我写" John's Road"迭代器不是使用输入变量而是找到它,但是如果我使用变量,即使我写了相同的变量,也会失败。
知道为什么吗?
string typedRoadName;
map<string, int> roadInfo;
vector<string> roadNames;
StringAlgorithms *algorithm = new StringAlgorithms();
gps->getRoadNames(roadInfo);
//roadInfo is filled using the line above
map<string, int>::iterator it;
for (it = roadInfo.begin(); it != roadInfo.end(); it++)
roadNames.push_back(it->first);
cout << "\nType the desired road name:\n>>";
cin >> typedRoadName;
it = roadInfo.find(typedRoadName);
//it = roadInfo.find("John's Road"); --> it works!
if (it == roadInfo.end())
cout << "Failed!\n";
else {
gps->setsourceID(it->second);
cout << "You've set the origin to '" << it->first << "' road, node --> " << it->second << endl;
}