C++:我自己类的动态数组
c++: Dynamic array of my own class
我读过一些关于改变数组大小的文章,但我没有找到适合自己类的东西。我需要的是一个"区域"的动态数组。
第一步(定义类(:
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++
相关文章:
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 输出没有重复元素的动态数组(收缩数组)C++
- 正在插入动态数组
- 如何在动态数组上使用搜索函数
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 静态数组的自由动态数组
- 在 c++ 中对类中的 c 字符串动态数组进行排序的最佳方法是什么?
- 使用 thread 类在 C++ 中构造线程的动态数组时出错
- 如何为 c++ 的不同变量类型的结构元素创建动态数组?
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- 当我使用自定义类型创建动态数组时,即使使用字符串,它似乎也不起作用
- 为什么 c++ 动态数组的大小没有改变?
- 具有自定义构造函数 (C++) 的类型的动态数组分配
- 指针的 C++ 动态数组 - 何时需要使用它?
- 删除动态数组时未定义标识符
- 哈希映射使用 nullptr c++ 初始化节点的动态数组
- 如何将字符串和整数读取到两个单独的动态数组中的程序编写?
- 动态数组的 C++ 重载加运算符
- 基于数组的列表 - 动态数组创建时出错