动态数组-如何增加数组的大小
Dynamic Arrays - How to increase the size of an array?
我正在从文件中读取数字,然后尝试将它们添加到数组中。我的问题是如何增加数组的大小?例如,我认为可以只做:
#include <iostream>
using namespace std;
int main() {
double *x;
x = new double[1];
x[0]=5;
x = new double[1];
x[1]=6;
cout << x[0] << "," << x[1] << endl;
return 0;
}
但这显然只是覆盖了我最初设置为x[0]的值5,因此输出0.6。我该如何使它输出5,6
请注意,对于我所包含的示例,我不想将其与从文件中读取的代码或从用户那里获取数字的代码混为一谈。在实际的应用程序中,我不知道在编译时需要多大的数组,所以请不要告诉我只制作一个包含两个元素的数组,并将它们分别设置为5和6。
您不想直接使用数组。请考虑使用vector
。然后,您可以调用push_back
函数将内容添加到末尾,它会自动为您调整向量的大小。
#include <iostream>
#include <vector>
int
main() {
double value;
std::vector<double> values;
// Read in values
while (std::cin >> value) {
values.push_back(value);
}
// Print them back out
for (std::size_t i(0), len(values.size()); i != len; ++i) {
std::cout << values[i];
}
}
您应该使用集合类来完成这项工作,而不是自己管理它。看看"vector"类。它本质上是一个动态数组,可以根据需要自动调整大小。
在您的情况下,您将使用"vector"answers"double"类型。您可能还需要阅读C++中的模板。
http://www.cplusplus.com/reference/stl/vector/
或者,如果你不想使用STL或其他动态东西,你可以从一开始就创建正确大小的数组:x=new double[2];
当然,问题是要做多大。如果你不知道,那么你只需要把它做得"足够大"(比如一百个或一千个)。。。在某种程度上,它还不够大,而且会以某种随机的方式失败。因此,你需要调整它的大小。一旦你达到这一点,你就会希望你从一开始就使用STL,就像其他答案告诉你的那样。
#include <iostream>
using namespace std;
int main() {
double *x = new double[2];
x[0]=5;
x[1]=6;
cout << x[0] << "," << x[1] << endl;
return 0;
}
这里有一个示例,可以很好地衡量,因此您可以看到以下模式:
#include <iostream>
using namespace std;
int main() {
// Allocate some memory for a double array of size 1 and store
// an address to the beginning of the memory in mem_address.
double* mem_address = new double[1];
// Assign 5 to the first element in the array.
mem_address[0] = 5;
// Save the address of the memory mem_address is currently
// referencing.
double* saved_address = mem_address;
// Allocate some memory for a double array of size 2 and store
// an address to the beginning of the memory in mem_address.
mem_address = new double[2];
// Copy over the 1 element from the first memory block
// to the new one.
mem_address[0] = saved_address[0];
// Done with the old memory, so clean it up.
delete [] saved_address;
// Assign 6 to the second element in the new array.
mem_address[1] = 6;
// Print out the 2 elements in the new array.
cout << mem_address[0] << "n";
cout << mem_address[1] << "n";
// Done with the new array memory now, so clean it up.
delete [] mem_address;
}
如果由于某种原因您无法访问STL,或者想自己学习如何做到这一点,您可以使用这样的算法:
将数组分配为任意大小,并记住其中有多少元素以及它有多大:
int *a = malloc(int * ARBITRARY_SIZE);
int size = 0;
int allocated = ARBITRARY_SIZE;
每次添加新元素时,都要增加"大小"。如果大小等于ARBITARY_size,请将"allocated"乘以2,然后重新分配数组。无论哪种方式,都可以将新值分配给[size]。
void addElement(int value) {
++size;
if (size == allocated) {
allocated *= 2;
a = realloc(sizeof(int) * allocated);
a = new_a;
}
a[size] = value;
}
请注意,上面的代码至少有一个错误——无论哪种情况,都没有为x[1]分配足够的空间。
同样明显的是,在实际代码中,您会检查malloc&realloc不为null。
数组总是需要一个连续的内存块。在以后可能需要调整阵列大小的情况下,重新分配可能是唯一的解决方案。这就是Moishe和Shadow2531在上面所做的。
重新分配的问题在于,这可能是一项成本高昂的操作。因此,如果您需要在一个5000元素的数组中再添加5个元素,那么最终可能会在内存中复制所有5000元素。
在这种情况下,可以考虑使用链表。
- 数组索引的值没有增加
- C++ 动态数组每次添加时将大小增加 1 - 错误
- 如何增加以前由新运算符分配的 C++ std::list 数组的大小?
- 如何在 turbo C++ 中增加类数组的大小?
- 如何重载运算符+= 要增加动态数组大小,请在末尾添加一个对象
- 在不使用容器或字符串库的情况下,在运行时增加动态数组大小
- 通过数组C 随机增加
- 增加类指针数组
- C++,为什么多维数组的一个元素的增加似乎增加了另一个元素
- 不断增加的指针数组 错误:赋值中的类型不兼容
- 使用数组增加文件
- 数组值无法正确增加
- 如果我们可以将特定数组元件增加/减少1,则最小总移动到平衡阵列
- 数组在方括号中增加元素是什么意思
- 为什么 c++ 字符数组大小增加 1
- 我可以通过增加指针来浏览数组吗?QT
- 当使用动态数组时,循环中的内存使用量不断增加.(C++)
- 如何增加指向字符数组的指针的位置
- C++映射数组键自动增加
- 使用大型数组索引增加访问时间