C++ 是否可以允许用户使用键盘输入数组大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8271773/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to allow a user to enter an array size with a keyboard?
提问by Moshe
Is it possible to allow the user to enter the size of an array with a keyboard?
是否可以允许用户使用键盘输入数组的大小?
I know that arrays can't change size. The only solution I could think of is this:
我知道数组不能改变大小。我能想到的唯一解决方案是:
int userSize;
cin >> userSize;
const int SIZE = userSize;
int array[SIZE];
How can I verify that this works? Should I use a vector instead?
我如何验证这是否有效?我应该改用向量吗?
回答by Alok Save
Variable Length Arraysare not approved by C++ standard. C++ Standard mandates that the size of an array mustbe an compile time constant.
You can use VLA's through compiler extension provided in gccbut note the your code is not portable if you do so.
可变长度数组未被 C++ 标准认可。C++ 标准要求数组的大小必须是编译时常量。
您可以通过gcc 中提供的编译器扩展来使用 VLA,但请注意,如果您这样做,您的代码不可移植。
Standard approved way of doing this is to use std::vector.
这样做的标准批准方法是使用std::vector。
回答by Joe McGrath
Many of the other answers are correct, but to show your possible choices. Each one will fit into different situations better.
许多其他答案是正确的,但要显示您可能的选择。每一种都会更好地适应不同的情况。
The first one is most similar to your code. It makes a constant size array and lets the user choose how much of it to use. This is simple, but limiting. It can waste unused space, but has it places to be used. (not likely based on user input though)
第一个与您的代码最相似。它创建一个恒定大小的数组,并让用户选择要使用的数组大小。这很简单,但有局限性。它可以浪费未使用的空间,但有它可以使用的地方。(虽然不太可能基于用户输入)
const int SIZE=100;
int array[SIZE];
int userSize;
cin >> userSize;
if (userSize>SIZE){
cerr << "Array requested too large";
}
// use userSize as size of array
The second choice is dynamic memory allocation and using a regular C pointer to keep track of it. If you forget (or are unable) to delete it you have a memory leak.
第二种选择是动态内存分配并使用常规 C 指针来跟踪它。如果您忘记(或无法)删除它,您就会发生内存泄漏。
cin >> userSize;
int* array = new int[userSize];
...
delete [] array;
The third choice is writing your own smart pointer, boost, or in C++ 0x a unique_ptr
. It will keep track of the pointer for you and delete it when it goes out of scope.
第三种选择是编写自己的智能指针、boost 或在 C++ 0x a 中unique_ptr
。它将为您跟踪指针,并在超出范围时将其删除。
cin >> userSize;
unique_ptr<int[]> array(new int[userSize]);
And finally you probably just want a vector. As you hinted to in your question, a vector is probably the way to go here. They are simple, efficient, and commonly used. Learning one stl container makes learning the others easier. You should research which container fits your current problem best. Vector is a good choice most of the time.
最后你可能只想要一个向量。正如您在问题中暗示的那样,向量可能是这里的方法。它们简单、高效且常用。学习一个 stl 容器会使学习其他容器变得更容易。您应该研究哪种容器最适合您当前的问题。Vector 在大多数情况下都是不错的选择。
#include <vector>
...
std::vector<int> vec;
cin >> userSize;
vec.resize(userSize);
回答by mhelvens
No, arrays cannot be variably sized in C++. The code you have there will not compile, because the value of a constant has to be evaluated at compile-time.
不,数组不能在 C++ 中改变大小。您在那里的代码将无法编译,因为必须在编译时评估常量的值。
You might try the std::vector
class instead and use the resize
method, or pass the size through the constructor. Although, depending on what you need, maybe you can just let it grow naturally by using push_back
.
您可以尝试使用std::vector
该类并使用该resize
方法,或者通过构造函数传递大小。虽然,根据您的需要,也许您可以通过使用push_back
.
A lower-level alternative is to use dynamically allocated arrays:
较低级别的替代方法是使用动态分配的数组:
int userSize;
cin >> userSize;
int* array = new int[userSize];
回答by shehab
Yes you can. Here it is:
是的你可以。这里是:
int n;
cout << "enter degree ";
cin >> n;
int *arr = new int[n];
回答by Chris Eberle
No, you can't have dynamic allocations like that on the stack (yes, I know, some compilers have extensions for this, but generally speaking the answer is no). You'd want to use malloc
or new
to dynamically allocate room on the heap. Or of course there are always structures to make life easier, like vector.
不,您不能在堆栈上进行这样的动态分配(是的,我知道,有些编译器对此进行了扩展,但一般来说答案是否定的)。您想在堆上使用malloc
或new
动态分配空间。或者当然总有一些结构可以让生活更轻松,比如矢量。