C ++ Map插入文件数据

时间:2014-03-13 06:38:51

标签: c++ class c++11 map

我尝试在网站上搜索但无法找到解决方案。

我的课程声明如下:

class Movie {
    // data is private by default
    string title, studio;
    short releaseYear, rank[3]; // US, non-US, world
    long long boxOffice[3];     // US, non-US, world
    enum unit {US, NON_US, WORLD};

public:
    Movie();
    Movie(string);
    string getTitle() const;
    string getStudio() const;
    int getReleaseYear() const;
    int getUSRank() const;
    int getNonUSRank() const;
    int getWorldRank() const;
    long long getUSBoxOffice() const;
    long long getNonUSBoxOffice() const;
    long long getWorldBoxOffice() const;
    friend ostream& operator <<(ostream&, const Movie&);
private:
    Movie(const Movie &); // private copy constructor blocks invocation
};

class Movies {
    // data is private by default
    //static const int MAX_MOVIES = 1000;
    //Movie *movies;
    short movieCnt;
    map<string, Movie> movies;

public:
    Movies(string);
    //int getMovieCount() const;
    //const Movie* getMovie(string, int&) const;
    //const Movie * operator[](int) const;
    ~Movies();

private:
    void loadMovies(string);
    string myToLower(string) const;
    void reSize();
};

I am using this copy constuctor to insert data in the movie object in map.

Movie::Movie(string temp) {
    istringstream iS(temp);
    getline(iS, title, '\t');
    getline(iS, studio, '\t');
    iS >> releaseYear >> rank[US] >> boxOffice[US] >> rank[NON_US]
        >> boxOffice[NON_US] >> rank[WORLD] >> boxOffice[WORLD];
}

void Movies::loadMovies(string fn) {
    ifstream iS(fn);
    string s,movieTitle;
    getline(iS, s); // skip heading
    getline(iS, s);
    movieCnt=0;
    //movies = new Movie[MAX_MOVIES];
    while(!iS.eof()) {
        //movies[movieCnt++] = Movie(s);
        getline(iS, movieTitle, '\t');
        movies.insert(movieTitle,Movie(s));
        cout<<movies[movieTitle].title<<" "<<movies[movieTitle].studio;
        getline(iS, s);

    }
    iS.close();
    reSize();
}

我从main函数调用loadMovies,它将文件名作为参数传递。

有谁能告诉我这有什么问题。 我是新来的地图,因此我很挣扎。

我能够在地图中加载数据但我遇到了检索数据的问题。 在这个程序中,我们要求用户输入电影名称,然后从main函数中调用getMovies(string)函数。 getMovies函数如下:

const Movie* Movies::getMovie(string mc) const {

map<string, Movie>::const_iterator it = movies.find(mc);
//Here I want to know how to return Movie object?

}

示例文件数据如下(如果有帮助): 标题Studio ReleaseYear USRank USBoxOffice NonUSRank NonUSBoxOffice WorldRank WorldBoxOffice Avatar Fox 2009 1 760507625 1 2021800000 1 2782300000

0 个答案:

没有答案