【发布时间】:2016-05-07 10:49:23
【问题描述】:
我被要求编写一个搜索清单函数,该函数将根据其模型搜索向量,并将其索引位置返回给用户。我一生都无法弄清楚如何使用数组以外的向量来做到这一点。请帮忙!
//this function will search the inventory and return its index position.
void searchInventory(vector<Vehicle> &vehicles)
{
int invSize = vehicles.size();
string vModel;
int position = -1;
int index = 0; //position of search
bool found = false; //flag
cout << "Please enter the model number of the vehicle you are searching for: " << endl;
getline(cin, vModel);
while (index < invSize && !found)
{
if (vehicles[index].getModel == vModel)
{
found = true;
position = index;
}
index++;
}
return position;
cin.ignore();
}
【问题讨论】:
标签: c++ vector binary-search