【发布时间】:2017-03-28 00:18:13
【问题描述】:
在这里继续我之前的问题:
Accessing local variables from void functions
我已经能够获得我需要的数据,现在我正在尝试比较传递给函数的每个 struct 元素的 string name 属性。
这是我当前的代码:
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
struct Nutrient {
string name, units;
double amount, calories;
};
struct Recipe {
string title;
double servings;
};
struct Ingredient {
string name, units;
double amount;
};
vector<Nutrient> readNutrients(istream& fin) {
vector<Nutrient> nutrients;
/**ifstream in(input_file.c_str());*/
string line;
while (getline(fin, line)) {
Nutrient n;
int pos = line.find(';');
n.name = line.substr(0, pos);
line = line.substr(pos + 1);
istringstream iss(line);
iss >> n.amount >> n.units >> n.calories;
nutrients.push_back(n);
}
return nutrients;
}
Recipe readRecipe(istream& fin) {
Recipe recipe;
string line;
int lineCount = 0;
while (getline(fin, line)) {
lineCount++;
istringstream iss(line);
if (lineCount == 1) {
iss >> recipe.title;
}
else if (lineCount == 2) {
iss >> recipe.servings;
}
}
return recipe;
}
vector<Ingredient> readIngredients(istream& fin) {
vector<Ingredient> ingredients;
string line;
string title; // Just grabs, doesnt return
double servings; // Just grabs, doesnt return
int lineCount = 0;
while (getline(fin, line)) {
Ingredient g;
lineCount++;
istringstream iss(line);
if (lineCount == 1) {
iss >> title;
}
else if (lineCount == 2) {
iss >> servings;
}
else {
iss >> g.amount >> g.units >> ws;
getline(iss, g.name, '\n');
cout << g.name << "\n";
ingredients.push_back(g);
}
}
return ingredients;
}
bool itemsMatch(vector<Nutrient>& nut, vector<Ingredient>& ing) {
int matchCount = 0;
for (int i = 0; i < nut.size(); i++) {
for (int j = 0; j < ing.size(); j++) {
if (nut[i].name == ing[j].name) {
cout << nut[i].name << " matched " << ing[j].name << endl;
cout << "\n";
}
else {
cout << nut[i].name << " didnt match " << ing[j].name << endl;
}
}
}
return true;
}
int main(int argc, char** argv) {
vector<Nutrient> nutri;
vector<Ingredient> ingri;
Recipe rec;
bool match;
ifstream finNutr(argv[1]);
ifstream finIngr(argv[2]);
ifstream finReci(argv[2]);
nutri = readNutrients(finNutr);
ingri = readIngredients(finIngr);
rec = readRecipe(finReci);
match = itemsMatch(nutri, ingri);
return 0;
}
bool itemsMatch() 是造成麻烦的原因。这是最后的输出:
graham crackers
milk chocolate
marshmallows
graham crackers didnt match graham crackers
graham crackers didnt match milk chocolate
graham crackers didnt match marshmallows
milk chocolate didnt match graham crackers
milk chocolate didnt match milk chocolate
milk chocolate didnt match marshmallows
cheese, swiss didnt match graham crackers
cheese, swiss didnt match milk chocolate
cheese, swiss didnt match marshmallows
marshmallows didnt match graham crackers
marshmallows didnt match milk chocolate
marshmallows matched marshmallows
可见,有几个字符串匹配,但出于某种原因,它说它们不匹配,我不知道为什么。
arg1 内容
graham crackers; 2 squares 59
milk chocolate; 1 bar 235
cheese, swiss; 1 oz 108
marshmallows; 1 cup 159
arg2 内容
S'mores
2
4 squares graham crackers
1 bar milk chocolate
2 large marshmallows
【问题讨论】:
-
使用调试器逐步完成。
-
@chris 调试器?我不熟悉 C++ 调试器。它是内置的吗?
-
取决于您使用的是什么。大多数 IDE 都内置了调试器,编译器附带了一个可以从命令行使用的调试器。阅读有关如何使用调试器的教程非常值得。
-
您是否验证了这些向量包含您认为它们包含的内容?如果您没有调试器,请使用循环打印每个内容。此外,您应该编辑您的问题以提供minimal reproducible example。
-
附带说明,您将按值 传递给
itemsMatch(),因此它将在内存中复制它们的数据。您应该通过 by reference 传递向量。另外,请查看 STL 的std::find_if()算法,而不是手动搜索。
标签: c++ string function vector struct