如何将结构推回向量
How to pushback a struct into a vector
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});
}
}
我正在尝试将结构推回矢量以存储我的数据,并且在如何准确执行此操作时遇到了麻烦。这就是我目前拥有的。
你只需要创建一个结构变量;为它设置属性;然后将该结构推送到向量。
在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++中的结构的多态成员
- NLOHMANN 的 JSON 库将数组转换为结构向量
- 在nlohmann json中,如何将嵌套对象的数组转换为嵌套结构的向量
- 如何在 c++ 中创建结构向量的映射
- 带unique_ptr的结构向量
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- C++,从文件读取到结构,然后读取到向量(结构被推入向量太多次,而不仅仅是一次)
- 将 int 映射到 C++ 中的向量结构
- 如何在 c++ 中的向量结构中使用结构向量
- 无法将值存储到向量结构中
- 无法将.CSV文件读取为代表向量结构的类
- MPI,C,派生类型,向量结构
- 向量结构的打印成员
- 在 c++ 中对向量<结构 S> 进行二分搜索?
- 不需要删除向量<结构A*>
- 在C++中循环向量结构<string>
- 将字符串放入字符串的向量结构中
- 共享内存中的向量结构
- 我的向量结构没有像预期的那样迭代