定义数组,然后更改其大小 [英] Define array, then change its size

查看:122
本文介绍了定义数组,然后更改其大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Java背景,我可以在Java中做一些我需要在C ++中做的事情,但是我不确定该怎么做.

I come from a java background and there's something I could do in Java that I need to do in C++, but I'm not sure how to do it.

我需要声明一个数组,但是目前我不知道大小.一旦知道大小,就可以设置数组的大小.我的Java我只会做类似的事情:

I need to declare an array, but at the moment I don't know the size. Once I know the size, then I set the size of the array. I java I would just do something like:

int [] array;

然后

array = new int[someSize];

如何在C ++中做到这一点?

How do I do this in C++?

推荐答案

在C ++中,您可以执行以下操作:

In C++ you can do:

int *array; // declare a pointer of type int.
array = new int[someSize]; // dynamically allocate memory using new

,一旦使用完内存,即可使用delete as取消分配它:

and once you are done using the memory..de-allocate it using delete as:

delete[]array;

这篇关于定义数组,然后更改其大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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