using namespace std;
struct Movie {
string title;
string director;
string genre;
string yearRelease;
string duration;
};
int main(){
cout << "Hi";
ifstream fin;
string line;
vector <Movie> m;
fin.open("Movie_entries.txt");
while (getline(fin, line)) {
cout << line << endl;
stringstream lineStream(line);
getline(lineStream, m.title, ',');
getline(lineStream, m.director, ',');
getline(lineStream, m.genre, ',');
getline(lineStream, m.yearRelease, ',');
getline(lineStream, m.duration, ',');
m.push_back({title, director, genre, yearRelease, duration});
}
}
我试图将结构推回 vector 中以存储我的数据,并且在如何准确地做到这一点方面遇到麻烦。这就是我目前所拥有的。
最佳答案
您只需要创建一个struct变量;为它设置属性;然后将该结构推入 vector 。
在C++中,用Movie aMovie;
声明一个结构变量就足够了。无需struct Movie aMovie;
。
using namespace std;
struct Movie {
string title;
string director;
string genre;
string yearRelease;
string duration;
};
int main(){
cout << "Hi";
ifstream fin;
string line;
vector <Movie> m;
fin.open("Movie_entries.txt");
while (getline(fin, line)) {
cout << line << endl;
stringstream lineStream(line);
struct Movie aMovie;
getline(lineStream, aMovie.title, ',');
getline(lineStream, aMovie.director, ',');
getline(lineStream, aMovie.genre, ',');
getline(lineStream, aMovie.yearRelease, ',');
getline(lineStream, aMovie.duration, ',');
m.push_back(aMovie);
}
}
关于c++ - 如何将结构推回 vector ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41905875/