c++:我自己的动态数组class
c++: Dynamic array of my own class
我已经阅读了一些关于更改数组大小的帖子,但我没有找到适合自己的 classes。我需要的是一个"region"的动态数组。
第一步(定义class):
class Region{
private:
int start;
int end;
public:
Region(){
start=0;
end=0;
}
// some get and set functions
}
第二步(定义一个空的区域数组):
Region regions[0];
第三步(添加新区域)
// 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++ 中不推荐这样做
我已经阅读了一些关于更改数组大小的帖子,但我没有找到适合自己的 classes。我需要的是一个"region"的动态数组。
第一步(定义class):
class Region{
private:
int start;
int end;
public:
Region(){
start=0;
end=0;
}
// some get and set functions
}
第二步(定义一个空的区域数组):
Region regions[0];
第三步(添加新区域)
// 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++ 中不推荐这样做