每个项目的C 字符串数组搜索输出
C++ String Array search outputs for every item
我知道这有点令人困惑,但我确实需要帮助。我需要在数组中找到一个带有许多字符串的字符串。如果找不到字符串,则显示适当的消息。但是,当我用于循环时,它显示了阵列中每个字符串的此消息,尽管它也显示了找到的字符串...我希望您理解我的意思,如果我没有意义,则很抱歉。这是我的代码:
void Store::search() {
string name;
cout << "Enter name of product you're searching: " << endl;
getline(cin, name);
for (int i = 0; i < quantity; i++) {
if (name.compare(database[i].name) == 0){
cout << "-------------<Product found!>-------------" << endl;
cout << "name: " << database[i].name << endl;
cout << "supplier: " << database[i].supplier << endl;
cout << "available quantity: " << database[i].quantity<< endl;
cout << "price per unit: " << database[i].price<< endl;
cout << "------------------------------------------" << endl;
}
else
{
cout << "Product doesn't exist in database!" << endl;
}
}
}
代码可用于搜索,但是如何停止输出"数据库中不存在的产品!"对于未找到的数组中的每个项目(即使找到了搜索的项目)?
预先感谢您
您可以使用语句标志:
void Store::search()
{
string name;
bool found = false
cout << "Enter name of product you're searching: " << endl;
getline(cin, name);
for (int i = 0; i < quantity; i++)
{
if (name.compare(database[i].name) == 0){
cout << "-------------<Product found!>-------------" << endl;
cout << "name: " << database[i].name << endl;
cout << "supplier: " << database[i].supplier << endl;
cout << "available quantity: " << database[i].quantity<< endl;
cout << "price per unit: " << database[i].price<< endl;
cout << "------------------------------------------" << endl;
found = true;
break;
}
if (!found)
cout << "Product doesn't exist in database!" << endl;
}
您也可以使用std :: find_if,这将使您的代码看起来像:
auto it = std::find_if(databases.begin(), databases.end(), [&name](const auto &database) {return name.compare(database.name) == 0; });
if (it != databases.end())
{
cout << it->name << endl;
cout << "found" << endl;
}
else
{
cout << "not found" << endl;
}
一般而言,C 提供了许多此类功能,这些功能通常会使您的代码缩短,提高可读性和保证功能
您可以:
1.如果在for loop中找到该项目,请保持Bool变量以设置为true
2.在找到项目
时,添加break
立即退出循环 3.删除其他部分,因为它将打印出"数据库中不存在的产品!"对于每个循环周期,如果项目不匹配
4. for循环后,检查 found
是否为false以检查集合中是否不存在
bool found = false;
for (int i = 0; i < quantity; i++)
{
if (name.compare(database[i].name) == 0)
{
cout << "-------------<Product found!>-------------" << endl;
cout << "name: " << database[i].name << endl;
cout << "supplier: " << database[i].supplier << endl;
cout << "available quantity: " << database[i].quantity<< endl;
cout << "price per unit: " << database[i].price<< endl;
cout << "------------------------------------------" << endl;
found = true; // set "found" to true
break; // add a break to immediately exit for loop when item is found
}
}
if (!found)
{
cout << "Product doesn't exist in database!" << endl;
}
我假设您想在数据库中搜索产品,并在找到其详细信息中。否则,您想通知用户找不到该产品。如果我正确理解您,那么您需要将其他语句从" for"循环中移出,例如:
void Store::search() {
string name;
cout << "Enter name of product you're searching: " << endl;
getline(cin, name);
bool found = false;
for (int i = 0; i < quantity; i++) {
if (name.compare(database[i].name) == 0){
cout << "-------------<Product found!>-------------" << endl;
cout << "name: " << database[i].name << endl;
cout << "supplier: " << database[i].supplier << endl;
cout << "available quantity: " << database[i].quantity<< endl;
cout << "price per unit: " << database[i].price<< endl;
cout << "------------------------------------------" << endl;
found = true;
break;
}
}
if (!found)
{
cout << "Product doesn't exist in database!" << endl;
}
}
如果您的数据库可能包含更多具有相同名称的产品,请删除" Break;";语句。
一种更"现代的C "方法是利用C 算法(例如STD :: find_if),Lambdas和Auto Specifier。
为例(假设数据库是std ::向量或某种STL容器):
auto it = std::find_if(database.begin(), database.end(), [&name](const auto& item) { return name.compare(item.name) == 0; });
if (it != database.end())
{
cout << it->name << endl;
cout << "found" << endl;
}
else
{
cout << "not found" << endl;
}
相关文章:
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 如何在动态数组上使用搜索函数
- 在 C++ 中搜索动态分配的数组中的出现次数
- 当您在此单词中搜索单词时调整字符数组的大小?
- 在对象数组中搜索字符串并返回相应值的函数
- C++线性搜索算法,确定数组中元素的数量
- 递归二进制搜索与字符串数组
- 变量未在此范围内声明 数组线性搜索
- 如果你有一个固定大小的数组,你需要遍历它!n次,使用二进制搜索如何改变时间复杂性
- 运行搜索数组
- 从数组中搜索元素的索引
- 在数组中搜索复杂度高于 O(n) 的元素
- 比较/搜索数组中多个整数的最佳解决方案
- 如何搜索和排序 2D 数组
- 如何在数组中搜索之前已输入的 int 值
- 数组搜索和唯一增值
- 矢量搜索与数组搜索的时间成本
- 每个项目的C 字符串数组搜索输出
- 简单的C++数组搜索
- 针对多个字符串数组搜索字符串