C++ istringstream问题

C++ istringstream问题,c++,delimiter,istringstream,C++,Delimiter,Istringstream,问题: 我有一个文本文件与行的信息,它需要下面的txt文件。我正在尝试映射项目,以便完成我的作业。在映射它们时,我使用的是istringstream。我的问题是,当一个项目中有多个我想保存在一个字符串中的单词时,如何让它工作。例如,我希望未加糖的苹果酱是一个字符串item.setNewItem。任何帮助都会非常感激,因为我现在是一名学生,任何为了我而沉默的人都会非常感激= txt文件: 1杯糖| 1杯无糖苹果酱|卡路里 代码: 您可以只向getline提供第三个参数来指定分隔符: 然后可以将第一

问题:

我有一个文本文件与行的信息,它需要下面的txt文件。我正在尝试映射项目,以便完成我的作业。在映射它们时,我使用的是istringstream。我的问题是,当一个项目中有多个我想保存在一个字符串中的单词时,如何让它工作。例如,我希望未加糖的苹果酱是一个字符串item.setNewItem。任何帮助都会非常感激,因为我现在是一名学生,任何为了我而沉默的人都会非常感激=

txt文件:

1杯糖| 1杯无糖苹果酱|卡路里

代码:


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

然后可以将第一个和第二个字段读取到“”,将第三个字段读取到“|”

电话内容如下:

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));
    }

}
我真的很感谢大家的帮助,它确实帮助我到达了我需要的地方,尽管我没有使用完全相同的代码,下面是我使用的解决方案,再次感谢大家=

//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);
}

将输入更改为在单词之间使用分隔符,以便可以在逻辑段之间拆分字符串。比如食物不加糖的苹果酱endFood这是穷人的XML:-众所周知,XML是穷人的csv。
//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);
}