C ++:创建具有由用户输入的大小的阵列 [英] C++ : Creating an array with a size entered by the user

查看:109
本文介绍了C ++:创建具有由用户输入的大小的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我比很多SO用户是比较新的C ++。

如果我们做一个由用户指定大小的数组我不知道。

I was wondering if we an make an array with a size specified by the user.

例如:

int a;
cout<<"Enter desired size of the array";
cin>>a;
int array[a];

不过,上述方案不会工作作为数组大小必须是一个但对我来说,它是一个变量。

But the above program wont work as array size has to be a constant but in my case, it is a variable.

那么,这可能使一个变量为一个常数并将其指定为数组大小?

So is it possible to make a variable into a constant and assign it as size of an array?

寻找一种简单,易懂的答案,感谢您的时间和精力:)

推荐答案

在C ++中,有两种类型的存储:的基于内存和的基于记忆。在基于堆栈的内存对象的大小必须是静态(即不改变),因此必须在编译时已知。这意味着你可以这样做:

In C++, there are two types of storage: stack-based memory, and heap-based memory. The size of an object in stack-based memory must be static (i.e. not changing), and therefore must be known at compile time. That means you can do this:

int array[10]; // fine, size of array known to be 10 at compile time

但不是这样的:

int size;
// set size at runtime
int array[size]; // error, what is the size of array?

请注意存在的的值和值的在编译时间的已知的,这意味着你甚至不能做到这一点是不同的:

Note there is a difference between a constant value and a value known at compile time, which means you can't even do this:

int i;
// set i at runtime
const int size = i;
int array[size]; // error, size not known at compile time

如果你想有一个动态调整大小的对象,你可以用某种形式的运算符的访问基于堆的内存:

If you want a dynamically-sized object, you can access heap-based memory with some form of the new operator:

int size;
// set size at runtime
int* array = new int[size] // fine, size of array can be determined at runtime

不过,这种原始的使用不建议因为你必须使用删除来恢复分配的内存。

However, this 'raw' usage of new is not recommended as you must use delete to recover the allocated memory.

delete[] array;

这是一种痛苦,因为你必须记得删除您创建一切(且仅删除一次)。幸运的是,C ++有你(这样做有许多数据结构,即他们使用删除幕后动态改变对象的大小)

This is a pain, as you have to remember to delete everything you create with new (and only delete once). Fortunately, C++ has many data structures that do this for you (i.e. they use new and delete behind the scenes to dynamically change the size of the object).

的std ::矢量是这些自我管理的数据结构的一个例子,是一个阵列的直接替代。这意味着你可以这样做:

std::vector is one example of these self-managing data structures, and is a direct replacement for an array. That means you can do this:

int size;
// set size at runtime
std::vector<int> vec(size); // fine, size of vector can be set at runtime

和不必担心删除。它会变得更好,因为的std ::矢量将的自动的当您添加更多的元素调整自身。

and don't have to worry about new or delete. It gets even better, because std::vector will automatically resize itself as you add more elements.

vec.push_back(0); // fine, std::vector will request more memory if needed

在总结:除非你知道在编译时的大小不使用阵列(在这种情况下,不要使用),而使用的std ::矢量

In summary: don't use arrays unless you know the size at compile time (in which case, don't use new), instead use std::vector.

这篇关于C ++:创建具有由用户输入的大小的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆