在 C++ 处使用 for 循环初始化一系列数组
我尝试使用 for 循环创建一系列多维数组。用户应输入要初始化的数组的数量。我想要这样的东西:
int i;
printf("Enter the number of arrays you want: ");
scanf("%d",i)
for(int j=0;j<i;j++){
long double x(j)[size][size];
}
如果我输入 5,我想要 x0、x1、x2、x3 和 x4。
有可能吗?
感谢您的帮助。
此致, 埃姆雷.
I try to create series of multidimensional arrays with for loops. User should input the number of arrays to be initialized. I want something like this:
int i;
printf("Enter the number of arrays you want: ");
scanf("%d",i)
for(int j=0;j<i;j++){
long double x(j)[size][size];
}
if I input 5, I want to have x0, x1, x2, x3 and x4.
Is it possible at all??
Thank you for any help.
Best Regards,
Emre.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
编辑:刚刚注意到您正在使用 C++。在这种情况下,std::vector绝对是最方便的解决方案。如果您对如何使用 C 风格的普通数组执行此操作感到好奇,请继续阅读...
您不能这样做。你需要做的是创建一个 3 维数组(或一个 2 维数组的数组,这取决于你的观点......)
与 C 的情况一样,内存分配是一个痛苦......你可以静态地allocate:
(N 在编译时确定。确保它足够大。
另外,如果 N 确实很大,请以静态方式声明数组(作为全局变量或使用
static
关键字)。如果你不这样做,你可能会得到堆栈溢出。)或者你可以使用动态内存分配:
也许使用 typedef 看起来会更好(即使我不能 100% 确定我在上一个例子中得到了正确的数组声明...... ):
顺便说一句,您当前的代码不起作用的原因是
x(j)
语法是虚构的:)Edit: Just noticed you are using C++. In this case, std::vector is definitely the most convenient solution. If you are curious about how to do this with plain arrays, C style, then read on...
You can't do it like this. What you need to do is create a 3 dimensional array (or an array of 2-dimensional arrays, depending on your point of view...)
As is always the case with C, memory allocation is a pain... You can statically allocate:
(N is determined at compile time. Make sure you have it large enough.
Also if N is really big, declare the array stactically (either as a global variable or with the
static
keyword). If you don't you can get a stack-overflow.)Or you can use dynamic memory allocation:
Maybe things look better with a typedef (even I am not 100% sure I got that array ceclaration right in the last example...):
BTW, the reason your current code doesn't work is
x(j)
syntax is ficticious :)如果您使用 C++,您最好使用
std::vector
作为:If you're working in C++, you better should be using
std::vector
as:一组多维数组与多一个维度的多维数组是一样的。
另外,关于内置数组的一些事情:
malloc
,并确保稍后对从malloc
获得的所有内容调用free
(有时很难做到正确)如果您使用的是 C++,并且不需要使用 C 编译器编译代码,我建议使用
vector
而不是内置数组。向量不必具有恒定的大小,并且可以在您想要调整大小时调整大小,通常无需在代码中调用malloc
或new
一种方法这看起来像:
编辑:
上述解决方案的唯一问题是您可能能够也可能无法在需要内置数组的代码中使用它。从你的评论来看,你需要它。
如果您现有的代码可以一次接受一行,那么上述解决方案将起作用。如果它只能接受二维数组,则必须使用不同的向量结构。请参阅此问题了解原因:
传递嵌套 C++ 向量作为内置样式多维数组
如果您需要一组连续的二维数组,这里有一些选项:
或者:
A set of multi-dimensional arrays is the same thing as a multi-dimensional array with one more dimension.
Also, a couple of things about built-in arrays:
malloc
, and ensure that later you callfree
on everything you got frommalloc
(which can sometimes be very hard to get right)If you are using C++, and don't need to compile your code with a C compiler, I recommend using a
vector
instead of a built-in array. Vectors don't have to have a constant size, and can be resized whenever you feel like resizing them, often without callingmalloc
ornew
in your codeOne way to go about this would look like:
Edit:
The only problem with the solution above is that you may or may not be able to use it in code that expects a built-in array. Judging from your comments, you need it to.
If your existing code can accept one row at a time, then the above solution will work. If it can only accept a 2d array, you'll have to use a different vector structure. See this question for why:
Pass nested C++ vector as built-in style multi-dimensional array
If you need a set of contiguous 2d arrays, here are some options:
Or: