在大多数情况下,我创建的以下二进制搜索功能有效。但是,有时候没有明显的原因,当我知道它存在时,搜索将找不到条目。有时,search
的变量将具有用户输入的内容,但缺少第一个字母。我添加了cin.clear()
和cin.ignore()
以尝试解决这个奇怪的错误。到目前为止,在添加cin.clear()
之后,我无法重新创建该问题。
查看以下程序:
void searchInventory(vector<Vehicle> &carList)
{
cout << "You have chosen to search for a vehicle.\n\n";
int first = 0, // first, last, and middle define the vector element subscripts
last = (carList.size() - 1),
middle,
position = -1, // Needed to keep track of current position
searchResult = 0; // Used copy the data from position for use in displaying confirmation messages to the screen
bool found = false; // Flag to tell program if search has been successful
string search; // Used to hold user's input on what to search
do
{
// Prompt to user so they can enter the make they are searching for
cout << "Please enter the make of the vehicle you are searching for: ";
cin.ignore();
cin.clear();
getline(cin, search);
cout << endl;
// Binary search algorithm begins...
while (!found && first <= last)
{
middle = (first + last) / 2;
if (carList[middle].getMake() == search)
{
found = true;
position = middle;
}
else if (carList[middle].getMake() > search)
last = middle - 1;
else
first = middle + 1;
}
searchResult = position; // position is copied into searchResult
if (searchResult == -1) // if searchResult is -1, then the user specified word was not found
{
cout << "Sorry, there is no vehicles under the make of " << search << " in your inventory\n"
<< "Please try again...\n\n";
}
else
{
// If the make was found, the information about the first vehicle with the specified model is given
cout << search << "has been found...\n";
}
} while (searchResult == -1);
}
}
此函数的一些背景知识:我正在向矢量对象中搜索包含汽车品牌的字符串数据类型的成员变量。
例如,如果我搜索&#34;福特&#34;什么时候&#34;福特&#34;存在于向量中,程序大部分都会说
已找到福特
但有时候程序会说
对不起,您的库存中没有福特制造的车辆
有时候程序会说
抱歉,您的广告资源中没有ord制作的车辆
注意&#34; ord&#34;而不是&#34;福特&#34;在最后一个。
有没有人知道为什么这个二进制搜索功能如此不可预测?
答案 0 :(得分:0)
要了解代码无法正常工作的原因,您需要对其进行调试或至少添加一些调试输出。如果您添加类似
的内容cout << "Now compare: " << carList[middle].getMake() << endl;
在if (carList[middle].getMake() == search)
之前,您有时会(在第一次输入后)看到您的程序没有比较任何内容,因为您没有重新初始化变量found
,first
和{ {1}}。
您需要删除last
,这就是您错过第一个字符的原因。
更正后的代码:
cin.ignore();
也不要将输入与算法结合起来。您最好创建更多通用函数,例如void searchInventory(vector<Vehicle> &carList) {
cout << "You have chosen to search for a vehicle.\n\n";
string search; // Used to hold user's input on what to search
int searchResult = 0;
do {
// Prompt to user so they can enter the make they are searching for
cout << "Please enter the make of the vehicle you are searching for: ";
cin.clear();
getline(cin, search);
cout << endl;
int first = 0, // first, last, and middle define the vector element subscripts
last = (carList.size() - 1),
middle,
position = -1; // Used copy the data from position for use in displaying confirmation messages to the screen
bool found = false; // Flag to tell program if search has been successful
// Binary search algorithm begins...
while (!found && first <= last) {
middle = (first + last) / 2;
if (carList[middle].getMake() == search) {
found = true;
position = middle;
} else if (carList[middle].getMake() > search) {
last = middle - 1;
} else {
first = middle + 1;
}
}
searchResult = position; // position is copied into searchResult
if (searchResult == -1) { // if searchResult is -1, then the user specified word was not found
cout << "Sorry, there is no vehicles under the make of " << search << " in your inventory\n"
<< "Please try again...\n\n";
} else {
// If the make was found, the information about the first vehicle with the specified model is given
cout << search << " has been found...\n";
}
} while (searchResult == -1);
}
,它返回向量中的车辆索引,如果找不到,则返回int vehicle_search(vector<Vehicle>& list, const string& search)
。 (仅作为示例,您还可以返回指向元素或迭代器的指针,并传递-1
Vehicle
的内容。然后你将从你程序的某个地方调用它,检查返回值和输出结果。