C ++指针向量 [英] C++ Vector of pointers

查看:168
本文介绍了C ++指针向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我最新的CS作业,我需要创建一个名为Movie的类,其中包含标题,导演,年份,评级,演员等。

For my latest CS homework, I am required to create a class called Movie which holds title, director, year, rating, actors etc.

需要读取包含此信息列表的文件,并将其存储在指向Movies的指针向量中。

Then, I am required to read a file which contains a list of this info and store it in a vector of pointers to Movies.

我不知道最后一行是什么意思。是不是意味着,我读这个文件,创建多个Movie对象。然后做一个指针的向量,其中每个元素(指针)指向这些Movie对象之一?

I am not sure what the last line means. Does it mean, I read the file, create multiple Movie objects. Then make a vector of pointers where each element (pointer) points to one of those Movie objects?

我只是做两个向量 - 一个指针和一个电影对这两个向量进行一对一的映射?

Do I just make two vectors - one of pointers and one of Movies and make a one-to-one mapping of the two vectors?

推荐答案

这意味着这样:

std::vector<Movie *> movies;

然后您在阅读行时添加到向量:

Then you add to the vector as you read lines:

movies.push_back(new Movie(...));

请记住删除所有 Movie * 对象一旦你完成了向量。

Remember to delete all of the Movie* objects once you are done with the vector.

这篇关于C ++指针向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆