如何将2D动态数组复制到3D数组 [英] How to copy 2D dynamic array to 3D array

查看:89
本文介绍了如何将2D动态数组复制到3D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


有什么方法可以将2D动态数组(指针)传递给以3D动态数组作为参数的函数吗?

Hi,
Is there any way to pass 2D dynamic array (pointer) to a function that has 3D dynamic array as parameter?

void func(double*** s)
{
    //do something with 3D dynamic array
}

void main()
{
    double** s=new double*[10];
    for(int i=0;i<10;i++)
            s[i]=new double[10];

}



更新:

上面的代码只是一个示例,而不是我的孔代码,因此
当我们拥有无界数组时,问题出在c#中:
int [,] t;
我们可以像这样获取数组的长度:
for(int i = 0; i< array.getlenght(); i ++)>
我知道一种在c ++中获取此类数组长度的方法,但首先我需要将2D数组复制到3D数组中.
因此,如果有办法,请提供帮助.



update:

the code above is just a sample, not the hole code I have, so
the problem is in c# when we have unbounded array:
int[,] t;
we can get length of the array to work with like:
for(int i=0;i<array.getlenght();i++)>
I know a way to get the length of such arrays in c++, but first I need to copy my 2D array in a 3D array.
So if there is a way to do this, please help.

推荐答案

CPallini绝对正确;另请参阅我的评论—在讨论中对该问题的评论.

到目前为止,您的签名或参数类型没有多大意义.您只需要先学习数组,然后决定要对它们做些什么以及为什么做.请参阅:
http://www.cplusplus.com/doc/tutorial/arrays/ [
您还需要了解锯齿状数组的工作方式:
http://en.wikipedia.org/wiki/Jagged_array [ http://www.functionx.com/cppcli/Lesson26.htm [ http://cboard.cprogramming.com/c-programming/139938- 2d-array-pointer-crash-exe.html [ ^ ],
http://bytes.com/topic/c/answers/852849-array-pointers-objects [^ ],
http://bytes.com/topic/c/answers/778705-array-pointers [ ^ ],
http://www.experts-exchange.com/Programming/Misc/Q_20790138.html [ ^ ].

(令我惊讶的是,在锯齿状数组上找到一篇简单的文章并不容易.对于那些知道如何使用指针的人来说,这个话题很简单,但是……看起来大多数人都在讨论这个话题.这些天都在.NET中;甚至我发现在C ++/CLI方面,与C ++相比,我也在讨论这个话题.)


—SA
CPallini is absolutely right; see also my comment — in the discussion in comments to the question.

So far, your signature or parameter type make no much sense. You just need to learn arrays first, and then decide what you want to do with them and why. Please see:
http://www.cplusplus.com/doc/tutorial/arrays/[^].

Pay attention for the section "Multidimensional arrays" and this part: "Multidimensional arrays are just an abstraction for programmers, since we can obtain the same results with a simple array just by putting a factor between its indices".

You also need to understand how jagged arrays work:
http://en.wikipedia.org/wiki/Jagged_array[^],
http://www.functionx.com/cppcli/Lesson26.htm[^],
http://cboard.cprogramming.com/c-programming/139938-2d-array-pointer-crash-exe.html[^],
http://bytes.com/topic/c/answers/852849-array-pointers-objects[^],
http://bytes.com/topic/c/answers/778705-array-pointers[^],
http://www.experts-exchange.com/Programming/Misc/Q_20790138.html[^].

(To my surprise, it wasn''t easy to find a single simple article on jagged arrays. This topic is pretty simple for those who knows how to work with pointers, but… it looks like most discusses topics these days are all in .NET; even C++/CLI I found to be discussed in connection with this topic than C++.)


—SA


//是否可以将2D动态数组(指针)传递给以3D动态数组作为参数的函数?
只需在先前的尺寸"的指针处加上&"号即可:):
// Is there any way to pass 2D dynamic array (pointer) to a function that has 3D dynamic array as parameter?
Just apply an ampersand at the pointer of the "previous dimesions" :) :
void func(double*** s,
          int iSlotCount,
          int iRowCount,
          int iColCount)
{
    //do something with 3D dynamic array
}

void main()
{
    double** s = new double*[10];
    for (int i = 0; i < 10; i++) {
      s[i] = new double[10];
    }

    func(&s, 1, 10, 10);

    for (i = 0; i < 10; i++) {
      delete[] s[i];
    }
    delete[] s;
}


这篇关于如何将2D动态数组复制到3D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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