C++:我自己类的动态数组

c++: Dynamic array of my own class

本文关键字:动态 数组 我自己 C++      更新时间:2023-10-16

我读过一些关于改变数组大小的文章,但我没有找到适合自己类的东西。我需要的是一个"区域"的动态数组。

第一步(定义类(:

class Region{
private:
int start;
int end;
public:
Region(){
start=0;
end=0;
}
// some get and set functions
}

第二步(定义区域的 emtpy 数组(:

Region regions[0];

步骤 3(添加新区域(

// Pseudo-Code
generate a help array with dimension 1 (start is 0)
add old regions values to help (none, because it was empty in the beginning)
add the new region to help
delete the regions array
initilize a regions array with dimension 1 (old dimension+1)
copy help to region
delete help

我认为第 2 步已经不正确了。我想在步骤 2 和步骤 3 中获得一些帮助。

一个好处(在我理解步骤 2 和 3 之后(可能是:如何删除特定的索引区域。

问候马丁

对于动态数组,您正在寻找std::vector<Region>(请参阅 cppreference 中的参考(。这包括插入、删除和推送操作。

你可以自己用new[]delete[]来模仿这种行为,但不建议这样做C++