简体   繁体   English

如何制作一个大小可变的数组?

[英]How to make an array that has a variable as the size?

For example: 例如:

int size;
cout << "Enter array size" <<endl;
cin >> size;
int myarray[size];

I want the user to be able to enter the size of the array, but I keep getting an error message saying I'm not using a constant variable. 我希望用户能够输入数组的大小,但是我不断收到一条错误消息,提示我未使用常量。 When ever I search for an answer to this question, I get information about how to store a variable in an array (not what I'm looking for). 每当我搜索此问题的答案时,我都会得到有关如何在数组中存储变量的信息(不是我要查找的信息)。

Variable length arrays are not part of the C++ standard, although they are part of the C99 standard and several compilers support them as an extension in C++ including gcc and clang , Visual Studio being one of the notable exceptions though. 可变长度数组不是C ++标准的一部分,尽管它们是C99标准的一部分,并且一些编译器支持它们作为C ++的扩展,包括gccclang ,但是Visual Studio是值得注意的例外之一

The obvious C++ solution would be to use std::vector or possibly new although that means you have to worry about delete'ing the memory as well. 显而易见的C ++解决方案是使用std :: vector或可能是new,尽管这意味着您还必须担心删除内存。

In C++ you can simply use std::vector , in your case a std::vector<int> . 在C ++中,您可以简单地使用std::vector ,在您的情况下为std::vector<int>

You then need to include the <vector> header. 然后,您需要包括<vector>标头。

using std::vector;
int size;
cout << "Enter array size ";
cin >> size;
vector<int> myarray(size);

A std::vector takes care of managing memory for you, and it also has a handy push_back method to add an item at the end, expanding the array as necessary. 一个std::vector负责为您管理内存,它还有一个方便的push_back方法,可以在最后添加一个项,并根据需要扩展数组。

This means that you do not have to decide up-front on a specific size, but can simply add more items. 这意味着您不必预先决定特定的大小,而只需添加更多项目。

The proper way to handles this in C++03 and C++11 is to use a managed dynamic array type such as std::vector : 在C ++ 03和C ++ 11中处理此问题的正确方法是使用托管动态数组类型,例如std::vector

int size;
std::cout << "Enter array size" << std::endl;
std::cin >> size;
std::vector<int> myarray;
myarray.resize(size);

std::vector<int> behaves a lot like a raw array of int s. std::vector<int>行为很像int的原始数组。 You can use [3] to access elements, as an example. 例如,您可以使用[3]访问元素。

This is superior to managing the memory yourself, as ever experienced programmers lose track of memory. 这胜过自己管理内存,因为有经验的程序员会失去对内存的跟踪。

There are proposals to add dynamically sized arrays to C++1y (the next iteration of C++) or later in an extension in a way that is somewhat compatible with C99 (an important difference is that sizeof(variable length array) in C99 is runtime evaluated, but not in C++). 有提案添加动态大小的阵列,以C ++ 1Y(C ++的下一次迭代)或更高版本中的方式,是用C99 有些兼容的延伸部(一个重要的区别是, sizeof(variable length array)在C99是运行时评估,但不在C ++中)。 There are also proposals to add std::dynarray , which is a slightly less heavyweight std::vector style class with some possibility of automatic storage optimization. 也有建议添加std::dynarray ,这是一个std::dynarray稍小的std::vector样式类,可以自动进行存储优化。 None of this is really needed to solve your problem. 解决您的问题实际上并不需要这些。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM