C++ 指针向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2853438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Vector of pointers
提问by xbonez
For my latest CS homework, I am required to create a class called Movie which holds title, director, year, rating, actors etc.
对于我最新的 CS 作业,我需要创建一个名为 Movie 的类,其中包含标题、导演、年份、评级、演员等。
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.
然后,我需要读取一个包含此信息列表的文件,并将其存储在指向电影的指针向量中。
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?
我不确定最后一行是什么意思。这是否意味着,我读取文件,创建多个 Movie 对象。然后制作一个指针向量,其中每个元素(指针)都指向那些 Movie 对象之一?
Do I just make two vectors - one of pointers and one of Movies and make a one-to-one mapping of the two vectors?
我是否只制作两个向量 - 一个指针和一个 Movies 并制作两个向量的一对一映射?
回答by Brian R. Bondy
It means something like this:
它的意思是这样的:
std::vector<Movie *> movies;
Then you add to the vector as you read lines:
然后在阅读行时添加到向量中:
movies.push_back(new Movie(...));
Remember to delete all of the Movie*
objects once you are done with the vector.
Movie*
完成矢量后,请记住删除所有对象。
回答by ebasconp
As far as I understand, you create a Movie class:
据我了解,您创建了一个 Movie 类:
class Movie
{
private:
std::string _title;
std::string _director;
int _year;
int _rating;
std::vector<std::string> actors;
};
and having such class, you create a vector instance:
有了这样的类,你就创建了一个向量实例:
std::vector<Movie*> movies;
so, you can add any movie to your movies collection. Since you are creating a vector of pointers to movies, do not forget to free the resources allocated by your movie instances OR you could use some smart pointer to deallocate the movies automatically:
因此,您可以将任何电影添加到您的电影收藏中。由于您正在创建指向电影的指针向量,请不要忘记释放电影实例分配的资源,或者您可以使用一些智能指针自动解除分配电影:
std::vector<shared_ptr<Movie>> movies;
回答by mooware
By dynamically allocating a Movie object with new Movie()
, you get a pointer to the new object. You do not need a second vector for the movies, just store the pointers and you can access them. Like Brian wrote, the vector would be defined as
通过使用 动态分配 Movie 对象new Movie()
,您可以获得指向新对象的指针。电影不需要第二个向量,只需存储指针即可访问它们。就像布莱恩写的那样,向量将被定义为
std::vector<Movie *> movies
std::vector<Movie *> movies
But be aware that the vector will not delete your objects afterwards, which will result in a memory leak. It probably doesn't matter for your homework, but normally you should delete all pointers when you don't need them anymore.
但请注意,vector 之后不会删除您的对象,这会导致内存泄漏。这对您的作业可能无关紧要,但通常您应该在不再需要时删除所有指针。
回答by Jerry Coffin
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?
我不确定最后一行是什么意思。这是否意味着,我读取文件,创建多个 Movie 对象。然后制作一个指针向量,其中每个元素(指针)都指向那些 Movie 对象之一?
I would guess this is what is intended. The intent is probably that you read the data for one movie, allocate an object with new
, fill the object in with the data, and then push the address of the data onto the vector
(probably not the best design, but most likely what's intended anyway).
我想这就是意图。意图可能是您读取一部电影的数据,使用 分配一个对象new
,用数据填充该对象,然后将数据的地址推送到vector
(可能不是最好的设计,但很可能无论如何都是有意的) .