gpt4 book ai didi

c++ - 比较两个 vector 的字符串元素

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:43:58 25 4
gpt4 key购买 nike

继续我之前的问题:

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

最佳答案

我的猜测是您的输入文件有 DOS 行结尾,这是两个字符的序列:"\r\n"

当您使用 getline 时,它会读取到 \n,因此 \r 会包含在您从文件。这意味着您从第二个文件中读取的成分是这些字符串:

S'mores\r
2\r
4 squares graham crackers\r
1 bar milk chocolate\r
2 large marshmallows

请注意,最后一行后没有 "\r\n",因此读取“marshmallows”时没有 \r 回车符。

在另一个文件中,成分不在行尾,因此 \r 回车符不会读入 name 字符串中营养素。这意味着当您比较正在比较的字符串时:

"graham crackers" == "graham crackers\r" -> false
"milk chocolate" == "milk chocolate\r" -> false
"marshmallows" == "marshmallows" -> true

解决方案是手动删除 \r 回车符,或者转换您的输入文件以删除回车符(或者停止使用 Windows,它有这种愚蠢的约定并且会给初学者带来无穷无尽的问题).

要删除 \r 字符,您可以在每个 getline 之后执行此操作:

while (getline(fin, line)) {
if (!line.empty() && line.back() == '\r')
line.pop_back();

这将从行尾删除 \r(如果存在)。

或者,如果您还停留在过去,无法使用 C++11:

while (getline(fin, line)) {
if (!line.empty() && line[line.length()-1] == '\r')
line.resize(line.length()-1);

关于c++ - 比较两个 vector<Structs> 的字符串元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43058575/

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