且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将结构添加到STL列表

更新时间:2023-09-06 19:59:46

要声明任何类型的向量,您需要将类型作为模板参数传递。在你的情况下,类型是 MachineList

To declare a vector of any type, you need to pass the type as a template parameter. In your case, the type is MachineList:

vector<MachineList> SS;

要添加一个实例,可以使用 push_back code>:

To add an instance, you can use for example push_back():

SS.push_back(MACHINELIST); // add the MACHIHELIST instance
SS.push_back(MachineList()); // add a default constructed MachineList.

在您的代码中, MACHINELIST MachineList

指针,你需要一个指针的向量:

Edit in response to comments: if you wanted to store pointers, you would need a vector of pointers:

vector<MachineList*> SS;

然后您可以添加条目:

MachineList* m = new MachineList;
SS.push_back(m);

您有责任删除指针,在适当的时候释放动态分配的资源。我建议使用智能指针,而不是原始指针,但你的代码中有更基本的问题。

You are responsible for deleting the pointer to deallocate the dynamically allocated resources at the appropriate moment. i would suggest using smart pointers instead of raw ones, but you have more fundamental problems in your code.