我有一个程序,它从文本文件中读取一部电影的名称和与该电影相关的字段。我想将这些电影包含为
string line;
while (getline(z, line))
{
istringstream iss(line);
int ane = line.find(";");
string roa = line.erase(0, ane + 1);
string actor = line.substr(0, ane);
int mns = line.find_first_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM");
string mn = roa.substr(mns);
movies movie;
construct(movie, actor);
}
我的
struct movies {
int rank;
string title;
string actor;
float rating;
};
我想用一个void函数来进行初始化,但是它似乎不起作用,所以我想问一下,是否可以这样做。也许通过使用向量或指针,但我不知道怎么做,所以如果你能告诉我它是如何工作的,我会很感激。
像这样的东西
struct movie {
int rank;
string title;
string actor;
float rating;
};
vector<movie> all_movies; // this vector will hold all the movies
string line;
while (getline(z, line))
{
...
movie m; // this is one movie that we've read from line
m.rank = ...; // set the rank
m.title = ...; // set the title
m.actor = ...; // set the actor
m.rating = ...; // set the rating
all_movies.push_back(m); // add the movie to the vector
}
您可以创建一个以movies类型为主的向量,然后将所有单独的结构(movies)push_back()放入您创建的向量中