3D 数组 C++使用 int[] 运算符
我是 C/C++ 新手,我一直在绞尽脑汁,但仍然不知道如何制作这样的“结构”
它应该是一个使用指针的 3D 动态数组。
我是这样开始的,但被困在那里
int x=5,y=4,z=3;
int ***sec=new int **[x];
知道如何使其成为 y 和 z 的静态大小就足够了;
拜托,我很感激你能帮助我。
提前致谢。
I'm new to C/C++ and I've been cracking my head but still got no idea how to make an "structure" like this
It's supposed to be a 3D dynamic array using pointers.
I started like this, but got stuck there
int x=5,y=4,z=3;
int ***sec=new int **[x];
It would be enough to know how to make it for a static size of y and z;
Please, I'd appreciate that you help me.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
要动态创建 3D 整数数组,最好先了解 1D 和 2D 数组。
一维数组:您可以通过以下方式轻松完成此操作:
这里,我们创建一个 int 指针,它将指向可以存储整数的内存块。
二维数组:您可以使用上述一维数组的解决方案来创建二维数组。首先,创建一个指针,该指针应指向一个内存块,其中仅保存其他最终指向实际数据的整数指针。由于我们的第一个指针指向一个指针数组,因此这将被称为指针到指针(双指针)。
3D 阵列:这就是您想要做的。这里你可以尝试上面两种情况下使用的方案。应用与二维数组相同的逻辑。有问题的图表解释了一切。第一个数组将是指针到指针到指针(int*** - 因为它指向双指针)。解决方法如下:
To create dynamically 3D array of integers, it's better you understand 1D and 2D array first.
1D array: You can do this very easily by
Here, we are creating an int-pointer which will point to a chunk of memory where integers can be stored.
2D array: You may use the solution of above 1D array to create a 2D array. First, create a pointer which should point to a memory block where only other integer pointers are held which ultimately point to actual data. Since our first pointer points to an array of pointers so this will be called as pointer-to-pointer (double pointer).
3D Array: This is what you want to do. Here you may try both the scheme used in above two cases. Apply the same logic as 2D array. Diagram in question explains all. The first array will be pointer-to-pointer-to-pointer (int*** - since it points to double pointers). The solution is as below:
(毕竟,这是标记为 c++ 的)
编辑以回应乔的问题,
你好,乔=)当然。这是例子:
(this is tagged c++, after all)
EDIT in response to Joe's question
hello again Joe =) sure. here's the example:
您可以尝试:
一旦您使用完该内存,您可以将其释放为:
You can try:
And once you are done using this memory you can deallocate it as:
综合解答。
如果你真的是用 C++(而不是粗略的 C)编写的,我认为你应该再看看这个复杂的数据结构。 IMO 重新设计,同时记住你正在尝试做的事情会更好。
Comprehensive answers.
If you are really writing this in C++ (not rough C) I think you should take another look at this complicated data structure. IMO redesign while keeping in mind what you are trying to do would be better.
你想要做的事情在 C++ 中不是惯用的。当然,您可以为此使用
int***pointer
,但强烈建议不要这样做。在 C++ 中,我们有更好的方法来实现这一目标。这将导致内存布局与您要求的类似。它支持动态调整大小和内部向量具有不同的大小,就像您的图片一样。此外,您不必担心手动分配/删除其中任何一个。此外,向量知道它们的大小,因此您不必在某处记住它。
但是,如果您只想要一个“矩形”3D 数组,其中所有元素连续存储在同一个内存块中,您可以使用 boost::multiarray。
What you're trying to do is not idiomatic in C++. Of course, you can use a
int***pointer
for this, but this is strongly discouraged. In C++ we have better ways to get there.This will result in something with the memory layout similar to what you asked for. It supports dynamic resizing and inner vectors to have different sizes just like in your picture. In addition, you don't have to worry about manual allocation / deletion of any of it. Also, the vectors know their size so you don't have to remember it somewhere.
But if you just want a "rectangular" 3D array where all the elements are consecutivly stored in the same memory block, you could use a boost::multiarray.
好吧,让我们开始吧
sec 现在是一个长度为 x 的 int**s 数组,所以现在我只专注于使第零个元素成为你想要的
现在 sec[0] 指向 int*s 的数组长度 y,现在只需要完成树的最后一点,所以
最后将其变成图表中的形式
这看起来确实有点像一个家庭作业问题,请确保您真正理解答案及其工作原理。
OK let us take your beginnings
sec is now an array of int**s of length x, so now I am just going to focus on making the zeroeth element be what you want
Now sec[0] points to array of int*s of length y, now just need to get the last bit of the tree done, so
And finally to get it to the form in your diagram
This does seem a little like a homework question, make sure you actually understand the answer and why it works.
如果这是您遇到问题的实际数组,请查看此处: 声明指向的指针多维数组和分配数组
不确定你到底想要什么,但你可能想阅读有关链表的内容。
If it's the actual arrays you'r having problems with look here: Declaring a pointer to multidimensional array and allocating the array
Not sure exactly what you want but you might want to read up on about linked lists.