0

我想解析文件的内容并加载到地图中。

这是文件内容格式:

Movie-name   release-year   price  cast  ishd 

"DDLG" 2010 20.00 "shahrukh and Kajal" true
"Aamir" 2008 10.00 "abc, xyz and ijkl" false

地图的关键将是第一个单词(电影名称)。

类定义:

class movieInfo
{
        private:
        int releaseYear;
        double price;
        string cast;
        bool isHD;
};

这是我试图实现的功能。

void fill_map_contents (map <string, movieInfo*> &mymap, ifstream& myfile)
{
    string line;
    string word;
    while (getline(myfile, line))
    {
        out << "inside while loop " << line << endl;
        stringstream tokenizer;
        tokenizer << line;
        movieInfo *temp = new movieInfo;
        while  (tokenizer >> word)
        {
            cout << " printing word :->"<< word << endl;
            //temp->releaseYear = atoi(word);
            //temp->price = 12.34;
            //temp->cast = "sahahrukh salman";
            //temp->isHD = false;   

            mymap[word] = temp;
        }
        delete temp;
    }
}

在while(tokenizer >> word)之后,我不知道如何填充对象变量并将其分配给map。

任何帮助将不胜感激。

德韦什

4

3 回答 3

2
        cout << " printing word :->"<< word << endl;
           //temp->releaseYear = atoi(word);
           //temp->price = 12.34;
        //temp->cast = "sahahrukh salman";
        //temp->isHD = false;   

在上面的代码中,您试图直接访问类的私有成员,这是不可能的。因此,更好的解决方案是您应该为每个变量包括公共 getter/setter,如下所示。

       public:
               void setreleaseYear(int sry){releaseYear=sry;}
               void setprice(double pr){price=pr;}
               void setcast(string cast){string=str;}
               void setisHD(bool id){isHD=id;}

现在使用代替注释代码:

               //temp->releaseYear = atoi(word);
                temp->setreleaseYear(atoi(word));
                tokenizer >> word;
                //temp->price = 12.34;
                temp->setprice(atof(word));
                tokenizer >> word;
                //temp->cast = "sahahrukh salman";
               temp->setcast(word);
               tokenizer >> word;
               //temp->isHD = false;  
                temp->setisHD(word);

不需要while循环。

于 2013-10-31T12:16:38.137 回答
0

您正在有效地尝试解析CSV 文件,其中空格作为分隔符和"引号字符。

我建议为此使用一个库,比如这个。示例代码(取自帮助页面):

// Note: I changed mymap to map<string, movieInfo> without a pointer - it's not
// needed

const char field_terminator = ' '; // Use a space
const char line_terminator  = '\n'; // Use line break
const char enclosure_char   = '"'; // Use "
csv_parser file_parser;

file_parser.set_skip_lines(1);
file_parser.init(filename);

file_parser.set_enclosed_char(enclosure_char, ENCLOSURE_OPTIONAL);
file_parser.set_field_term_char(field_terminator);
file_parser.set_line_term_char(line_terminator);

while(file_parser.has_more_rows()) {
    csv_row row = file_parser.get_row();

    movieInfo temp; // No need for pointers
    temp->releaseYear = atoi(row[1]); // C++11: Use std::stoi()
    temp->price = atof(row[2]); // C++11: Use std::stof()
    temp->cast = row[3];
    temp->isHD = row[4].compare("true") == 0;
    mymap[row[0]] = temp;
}
于 2013-10-31T12:17:43.800 回答
0

你必须简化事情。我建议添加插入和提取运算符movieinfo并选择新行作为字段分隔符

DDLG
2010
20.00 
shahrukh and Kajal
true
Aamir
2008
10.00
abc, xyz and ijkl
false

class movieInfo
{
public:
    int releaseYear;
    double price;
    string cast;
    bool isHD;

    friend std::ostream& operator << ( std::ostream& os, const movieinfo& i )
    {
        return os << i.releaseYear << '\n'
                  << i.price << '\n'
                  << i.cast << '\n'
                  << std::boolalpha << i.isHD() << '\n';                      
    }   

    friend std::istream& operator >> ( std::istream& is, movieinfo& i )
    {
        is >> i.releaseYear
           >> i.price;

        getline( is, i.cast );

        return is >> std::boolalpha >> i.isHD;
    }
};

void fill_map_contents (map <string, movieInfo> &mymap, ifstream& myfile)
{
    while ( !myfile.eof )
    {
        string name;
        myfile >> name;

        movieInfo mi;
        myfile >> m1;

        mymap[ name ] = movieInfo;
    }
}

请注意,我更喜欢使用移动语义进行了更改map <string, movieInfo*>map <string, movieInfo>

我将更改 moveinfo 的另一个建议是:

class movieInfo
{
public:
    // ctor and move, assign operator and move operator
    int releaseYear() const { return mReleaseYear; };
    double price() const { return mPrice; };
    const string& cast() const { return mCast; };
    bool isHD() const { return mIsHD; };

private:
    int mReleaseYear;
    double mPrice;
    string mCast;
    bool mIsHD;

    friend std::ostream& operator << ( std::ostream& os, const movieinfo& i )
    {
        return os << i.releaseYear() << '\n'
                  << i.price() << '\n'
                  << i.cast() << '\n'
                  << std::boolalpha << i.isHD() << '\n';                      
    }       

    friend std::istream& operator >> ( std::istream& is, movieinfo& i )
    {
        is >> i.mReleaseYear
           >> i.mPrice;

        getline( is, i.mCast );

        return is >> i.mIsHD;
    }
};
于 2013-10-31T11:18:41.500 回答