如何传递动态分配的多维数组? [英] how to pass a dynamically allocated multidimensioned array ?

查看:84
本文介绍了如何传递动态分配的多维数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我声明了一个数组z [m] [n],m = 100,n是对话框中的输入变量,我使用此方法:

I declared an array z[m][n], m=100,n is an input variable from the dialog box, I use this method:

double **z = new double *[m];
for (i=0;i<m;i++)
{
   z[i]=new double [n];
}
///////////
///////////

for (i=0;i<m;i++)
{
   delete []z[i];
}
delete []z;


我想通过


I want to pass z[m][n] by

func(z)

传递z [m] [n],我该如何写"func()"
声明如下是错误的:

how can I write "func()"
it is wrong to declare as below:

void func(double z[][n])


声明为以下内容也是错误的:


it is also wrong to declare as:

void fun(double **z)

如何解决该问题,谢谢您的建议.

how can I solve the problem, thank you for your advice.

推荐答案

您可以使用以下两种形式之一:
You can use either of the following forms:
void func(double z[][])
{
}

void func(double **z)
{
}


函数的唯一问题是如何确定数组每个维度中元素的数量,因此您需要在每个级别中添加一些标记元素,或发送其他参数以给出维度.

如果您正在谈论向该函数发送特定元素,则可能是这样的:


The only issue for your function is how it determines the number of elements in each dimension of the array, so you would need to add some marker element in each level, or send some other parameters giving the dimensions.

If you are talking of sending a specific element to the function then it would be something like:

void func(double d)
{
}

// called by
    func(z[x][y]);


不要在C ++中使用原始数组,这是不值得的.使用向量的向量,它们将更快,更容易地传递给您的程序.您也不必搞乱手动内存管理.您几乎可以像在代码中一样初始化它们:
Don''t use raw arrays in C++, it''s just not worth the pain. Use a vector of vectors, they''ll be as fast and far easier to pass around your program. You don''t have to mess around with manual memory management either. You can initialise them in virtually the same way as you do in your code:
std::vector<std::vector<double>> z( m );
std::for_each( begin( z ), end( z ) [n]( std::vector<double> &v )
{
    v.resize( n );
} );

,或者直接复制得多,但复制更多:

or be a lot more direct but with a bit more copying:

std::vector<double> y( n );
++>std::vector<std::vector<double>>; z( m, y );

当您要将数据传递给另一个函数时,请将函数声明为:

When you want to pass the data to another function you declare the function as:

void do_something( std::vector<std::vector<double>> &v )
{
    // Damage to taste in here
}

,您不必担心从支柱到支柱传递的数据大小,因为您总是可以询问向量.如果使用实际"数组,则存在一个问题,即对象的阵列性"仅衰减到指向double的指针,这会使函数变得复杂,因为您必须对数据大小进行假设,或者将两个尺寸的大小传递给函数.

因此,通常来说,如果您必须将数组用于任何事情,除非您与操作系统或硬件进行交互,否则可能会做错事情.

and you don''t have to worry about the size of the data you pass around from pillar to post as you can always ask the vectors. If you use "real" arrays you have a problem in that the "arrayness" of the objects decays to nothing more than a pointer to pointer to double, which complicates the function as you have to either make assumptions about the size of the data OR pass the size of both dimensions into the function.

So generally if you have to use arrays for anything you''re probably doing something wrong unless you''re interacting with the OS or hardware.


尝试一下:):
Try it :) :
class CDblMtxPar
{
  double** m_ppData;
  int m_iRows;
  int m_iCols;

public:
  CDblMtxPar(double** ppData, int iRows, int iCols)
    : m_ppData(ppData), m_iRows(iRows), m_iCols(iCols) {}

  double** GetData() const { return m_ppData; }
  int GetRowsCount() const { return m_iRows; }
  int GetColsCount() const { return m_iCols; }
};

void yourFunction(const CDblMtxPar& cPar)
{
  // use the dimensions and data here
}

void test()
{
  // double** z, int m, int n
  //..

  CDblMtxPar cPar(z, m, n);
  yourFunction(cPar);
}


这篇关于如何传递动态分配的多维数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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