我正试图解析一个包含配方成分列表的文本文件。
示例:
1 cup sour cream
1 cup oil
1 teaspoon lemon juice
我不知道如何区分1
、cup
和sour cream
,每行总是只有3个参数。
如果我按空间将它分开,那么sour cream
将被计算为两个参数。
发布于 2013-09-22 20:09:48
double quantity;
string unit;
string ingredient;
input_stream >> quantity >> unit;
getline(input_stream, ingredient);
发布于 2013-09-22 20:11:11
我要做这件事的天真的C++方式是在第二个空格中将字符串分成两部分。第一部分是字符串'1杯‘,第二部分是’酸奶油‘。但是你应该用flex来做这个。
发布于 2013-09-22 20:24:07
所以我不太清楚你在问什么,但是如果你问的是如何把第一个数字和第二个单词一起提取,剩下的部分你就可以做了:
string amount, measurements, recipe;
while (!infile.eof()){
infile >> amount; //this will always give you the number
infile >> measurements; // this will give the second part(cup,teaspoon)
getline(infile,recipe); // this will give you the rest of the line
https://stackoverflow.com/questions/18951562
复制