2013-10-31 34 views
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 

重点在地图将是第一个字(电影名)。

类defition:

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 >>单词)后,如何填充对象变量并将其分配给映射。

任何帮助将不胜感激。

Devesh

+0

你'删除temp'会留下悬挂指针与当前的代码。此外,您在每次循环迭代中覆盖相同的'movieInfo'对象。 –

回答

0

您必须简化一些事情。我建议加上inserction和提取运算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; 
    } 
}; 
2
 cout << " printing word :->"<< word << endl; 
      //temp->releaseYear = atoi(word); 
      //temp->price = 12.34; 
     //temp->cast = "sahahrukh salman"; 
     //temp->isHD = false; 

在上面的代码中你试图访问直接类,这是不是possible.Hence的私有成员,更好的解决办法是,你应该包括公众每个变量的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循环。

+0

感谢您的回复。 这个任务怎么样 - 这个单词是这行中的第一个单词 mymap [word] = temp; –

+0

映射保存键值对中的数据,其中键用作记录的索引。因此,您可以使用myMap.insert(std :: make_pair(key,temp));其中key可以是你的struct.like temp-> Movie_name的任何唯一数据。所以你也可以在你的结构中引入movie_name。 – MoBaShiR

0

您有效地试图解析CSV file,与和"空间分隔的引号字符。

我建议使用这个库,如this one。 示例代码(在帮助页面获取):

// 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; 
} 
相关问题