gpt4 book ai didi

c++ - 在文本文件中搜索某个单词 C++

转载 作者:行者123 更新时间:2023-11-28 05:56:44 24 4
gpt4 key购买 nike

我正在尝试按项目的成分创建一个可搜索的食谱数据库。我正在尝试创建遍历字符串 vector (其中保存了每种成分)的 for 循环并搜索文件并比较它们。现在,我只想让它输出“你好!”如果有比赛。在我的所有摆弄中,要么有 100 个 Hello!s(绝对不正确),要么没有。这是代码:

int main()
{
int y;
cout << "Hello! Welcome to Abby's Recipe Calculator." << endl << endl;
cout << "Please select an option: 1 to search by ingredient or 2 to browse recipes..." << endl;
cin >> y;

vector <string> ingreds;
ingreds.reserve(4);

if (y == 1)
{
ingredientvector(ingreds); // calls function to fill vector w/ ingredients
}
//else if (y == 2)
//{
//call recipe function...
//}

Search x1(ingreds); //assigns ingredients to object vector

recipesearch(x1.getingreds());

system("pause");
return 0;
}

void ingredientvector(vector<string>& x)
{
cout << "SEARCH BY INGREDIENT" << endl;
cout << "Please enter up to three ingredients... " << endl;

for (int i = 0; i < 4; i++)
{
x.push_back(" ");
getline(cin, x[i]);
if (x[i] == "1")
{
break;
}

}

}

void recipesearch(const vector<string>& ingredientlist) //ifstream& recipes)
{

ifstream myrecipes;
string line;
string ingredient;
myrecipes.open("recipes.txt");
if (myrecipes.is_open())
{
for (int i = 0; i < 4; i++)
{
ingredient = ingredientlist[i];
while(getline(myrecipes, line)){
if (ingredient == line)
{
cout << "Hello!" << endl;
}
else
{
break;
}
}
}
}
else cout << "Unable to open recipe file!";

myrecipes.close();
}

这是一个使用的配方示例:

Cheese-y Ramen

Prep Time: 5 minutes
Cook Time: 20 minutes
Total Time: 25 minutes
Servings: 2
Ingredients:
8 oz cheddar cheese
1 tablespoon cornstarch
¾ cup milk
2 packages ramen noodles
Directions:
1. Grate cheddar cheese and add with cornstarch into a small bowl
2. Combine with milk in a medium saucepan and cook on medium to low heat until consistent. Keep warm until serving.
3. In a separate pan boil ramen noodles. Set aside the included flavor packets.
4. Once boiling, drain the noodles and combine with cheese.
Recipe from Buzzfeed

最佳答案

这会将整个配方文件读入一个字符串,然后在字符串中查找每种成分。注意:这是极其蛮力的。它很快,但不会很准确。例如,如果在边栏中提到奇多,而不是在食谱本身中,它们仍会被列出。

归功于它,这个答案提升了从 Read whole ASCII file into C++ std::string 中读取的文件

void recipesearch(const vector<string>& ingredientlist)
{

ifstream myrecipes;
string file;
myrecipes.open("recipes.txt");
if (myrecipes.is_open())
{
// read entire file into string
myrecipes.seekg(0, std::ios::end);
file.reserve(myrecipes.tellg());
myrecipes.seekg(0, std::ios::beg);

file.assign((std::istreambuf_iterator<char>(myrecipes)),
std::istreambuf_iterator<char>());

// look inside file string for each ingredient
for (const string & ingredient: ingredientlist)
{

if (file.find(ingredient) != file.npos)
{ // found ingredient in file
cout << ingredient << endl;
}
}
}
else
cout << "Unable to open recipe file!";

}

警告:您将从网络上提取的许多文件都采用多字节字符集编码以获得更漂亮的结果和国际化,而不是大多数标准 C++ 工具默认使用的 7 位 ASCII,包括上面示例代码中使用的那些。

正确解释使用哪些潜在的多字节字符集以及如何使用它们本身就是一个讨论主题,但出于此分配的目的,OP 可能能够确保所有输入文件都保存ASCII 编码。

关于c++ - 在文本文件中搜索某个单词 C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34009667/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com