如何在 turbo C++ 中增加类数组的大小?
How do I Increase the size of a Class array? in turbo c++
我写了这个类:
class Spacewalk {
private:
char mission[50];
char date[50];
char astronaut[50];
char startingAt[50];
char endingAt[50];
public:
void list() {
// lists the values.
}
void addValue(miss, da, astro, start, end) {
// adds value to the private items.
}
};
我创建了一个此类数组,如下所示 -
Spacewalk list[1];
比方说,我已经用完了这个数组的空间,我将如何增加它的大小?
数组非常适合学习编码的概念,因此我比任何其他标准模板库(在学习代码时(都更认可它们。
注意:
使用vector
是明智的,但是学校不教这个的原因是因为他们希望您了解诸如vector
,stack
或queue
之类的事物背后的基本概念。 如果不了解汽车的各个部分,就无法制造汽车。
可悲的是,在调整数组大小时,除了创建一个新数组并传输元素之外,没有简单的方法。 执行此操作的最佳方法是保持数组动态。
请注意,我的示例适用于int
(s(,因此您必须将其制作成模板或将其更改为所需的类。
#include <iostream>
#include <stdio.h>
using namespace std;
static const int INCREASE_BY = 10;
void resize(int * pArray, int & size);
int main() {
// your code goes here
int * pArray = new int[10];
pArray[1] = 1;
pArray[2] = 2;
int size = 10;
resize(pArray, size);
pArray[10] = 23;
pArray[11] = 101;
for (int i = 0; i < size; i++)
cout << pArray[i] << endl;
return 0;
}
void resize(int * pArray, int & size)
{
size += INCREASE_BY;
int * temp = (int *) realloc(pArray, size);
delete [] pArray;
pArray = temp;
}
相关文章:
- 数组索引的值没有增加
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 如何在 turbo C++ 中增加类数组的大小?
- 如何重载运算符+= 要增加动态数组大小,请在末尾添加一个对象
- 在不使用容器或字符串库的情况下,在运行时增加动态数组大小
- 通过数组C 随机增加
- 增加类指针数组
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 使用数组增加文件
- 数组值无法正确增加
- 如果我们可以将特定数组元件增加/减少1,则最小总移动到平衡阵列
- 数组在方括号中增加元素是什么意思
- 为什么 c++ 字符数组大小增加 1
- 我可以通过增加指针来浏览数组吗?QT
- 当使用动态数组时,循环中的内存使用量不断增加.(C++)
- 如何增加指向字符数组的指针的位置
- C++映射数组键自动增加
- 使用大型数组索引增加访问时间