istringstream问题

时间:2014-04-11 23:26:35

标签: c++ delimiter istringstream

问题:

我在" txt文件"下面有一个包含信息行的文本文件。我正在尝试映射这些项目,以便完成我的任务。在映射它们时我使用的是istringstream。当我想要保存在一个字符串中的项目中有多个单词时,我的问题就出现了。例如"不加糖的苹果酱"我希望它是一个字符串(item.setNewItem)。任何帮助都会非常感激,因为我是现在的学生,任何为我着想的人都会非常感激。 =)

txt文件:

1杯糖| 1杯无糖苹果酱|热量

代码:

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs)
{
    string line;
    while (getline (foodReplace, line));
    {
        Substitutions item;
        istringstream readLine(line);

        readLine << item.setOldAmount
                 << item.setOldMeasurement
                 << item.setOldItem
                 << item.setNewAmount
                 << item.setNewMeasurement
                 << item.setNewItem;

        subs.insert(pair<string, Substitutions>(item.getOldItem, item));
    }

}

2 个答案:

答案 0 :(得分:0)

您可以为getline提供第三个参数来指定分隔符:

http://www.cplusplus.com/reference/string/string/getline/

然后你可以阅读第一和第二个字段到&#39; &#39;,第三个字段为&#39; |&#39;。

电话会写着:

void mapList(ifstream &foodReplace, map<string, Substitutions> &subs)
{
    string line;
    while (getline (foodReplace, line));
    {
        Substitutions item;
        istringstream readLine(line);

        getline(readLine, item.setOldAmount, ' '); //you may need to read this in to a temp string if setOldAmount is an int
        getline(readLine, item.setOldMeasurement, ' ');
        getline(readLine, item.setOldItem, '|');

        getline(readLine, item.setNewAmount, ' '); //you may need to read this in to a temp string if setNewAmount is an int
        getline(readLine, item.setNewMeasurement, ' ');
        getline(readLine, item.setNewItem, '|');

        subs.insert(pair<string, Substitutions>(item.getOldItem, item));
    }

}

答案 1 :(得分:0)

非常感谢所有人的帮助,它确实帮助我到达了我需要的地方,即使我没有使用完全相同的代码,下面是我使用的解决方案再次,谢谢大家。 =)

//function to populate map and update object info
void mapList(fstream &foodReplace, map<string, Substitutions> &subs) 
{
    string line;
    while (getline (foodReplace, line)) //gets the line and saves it to line
    {
        Substitutions item;
        istringstream readLine(line); //reads it into readLine

        //declaring variables
        double amount;
        string unit;
        string ingredient;
        string benefit;

        //gets old ingredient and saves in object
        getIngredient(readLine, amount, unit, ingredient);
        item.setOldAmount(amount);
        item.setOldMeasurement(unit);
        item.setOldItem(ingredient);

        //gets new ingredient and saves to object
        getIngredient(readLine, amount, unit, ingredient);
        item.setNewAmount(amount);
        item.setNewMeasurement(unit);
        item.setNewItem(ingredient);

        //reads in last piece and saves in object
        readLine >> benefit;
        item.setBenefit(benefit);

        //inserts object into map
        subs.insert(pair<string, Substitutions>(item.getOldItem(), item));
    }
}

//function to extract amount-units-ingredient
void getIngredient(istringstream &stream, double &amount, string &unit, string &ingredient)
{
    //resetting variables
    amount = 0;
    unit = "";
    ingredient = "";
    string temp;

    //setting variables
    stream >> amount;
    stream >> unit;
    stream >> temp;

    //read until delimiter is hit
    while (temp != "|")
    {
        ingredient += temp + " ";
        stream >> temp;
    }

    //removes the space at the end of the ingredient
    ingredient = ingredient.substr(0, ingredient.length() - 1);
}