11智能在线
新记
C++中数组定义,int x;int a[x];这样不行,那么有什么方法可以实现我所需要的数组,而不浪费空间?
如题所述
举报该文章
相关建议 2012-05-10
可以使用动态数组
用new的方法:
int x;
cin>>x;
int *a=new int [x];
这样就能实现你的要求了
在VC中int a[x] 是不行的,VC中必须已知x大小,因此VC中只能用new的方法动态开辟数组
另外注意数组使用过后(不再使用)一定要释放
示例:
delete a[];
温馨提示:内容为网友见解,仅供参考
当前网址:
https://11.t2y.org/zz/spfmsfpq2.html
其他看法
第1个回答 2012-05-10
int x;
int* a=new int[x]; //然后就可以像数组一样使用a了
第2个回答 2012-05-10
可以使用链表
相似回答
大家正在搜
相关问题
c++里数组int x=10;int A[x];为什么会错?
c/c++中 定义数组时可以定义未知数组吗? 如 int a...
c语言中什么时候可以这样定义数组"int a[]"
在c++中可以定义数组a[x]么 x是随便输入值 c中呢 莫...
我希望用C++定义一个未知长度的数组,大概是x=3*A;in...
怎么理解定义一个数组int x['a'],这个数组的大小是多...
C++中定义一个一维数组,比如int a[10];除了为数组...
C++中,这样定义一个数组为什么运行会出错? int*(p)...