C++中数组定义,int x;int a[x];这样不行,那么有什么方法可以实现我所需要的数组,而不浪费空间?

如题所述

可以使用动态数组
用new的方法:
int x;
cin>>x;
int *a=new int [x];
这样就能实现你的要求了
在VC中int a[x] 是不行的,VC中必须已知x大小,因此VC中只能用new的方法动态开辟数组
另外注意数组使用过后(不再使用)一定要释放
示例:
delete a[];
温馨提示:内容为网友见解,仅供参考
第1个回答  2012-05-10
int x;
int* a=new int[x]; //然后就可以像数组一样使用a了
第2个回答  2012-05-10
可以使用链表
相似回答