2013-10-31 56 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; 
} 
相關問題