简体   繁体   English

多维数组

[英]Multi-dimensional array

I need to create a function that has a parameter which is a multi-dimensional array with two dimensions being user-specified, eg 我需要创建一个函数,该函数的参数是一个多维数组,其中二维是用户指定的,例如

int function(int a, int b, int array[a][b])
{
 ...
}

How would I do that in C++ ? 我将如何在C ++中做到这一点?

Are the dimensions known at compile-time? 尺寸在编译时是否已知? In that case, turn them into template parameters and pass the array by reference: 在这种情况下,请将它们转换为模板参数,然后按引用传递数组:

template<int a, int b>
int function(int(&array)[a][b])
{
    ...
}

Example client code: 客户端代码示例:

int x[3][7];
function(x);

int y[6][2];
function(y);

Assuming the dimensions are not known at compile time, you emulate a two dimensional array with a one dimensional array: 假设在编译时不知道维数,则用一维数组模拟二维数组:

int& getat(int x, int y, int r, int c, int *array) {return array[y*c+x];}
int function(int a, int b, int *array) {
    getat(4, 2, a, b, array) = 32; //array[4,2] = 32
}

or, for safety, wrap it all in a class: 或者,为了安全起见,将其全部包装在一个类中:

template <class T>
class array2d {
    std::vector<T> data;
    unsigned cols, rows;
public:
    array2d() : data(), cols(0), rows(0) {}
    array2d(unsigned c, unsigned r) : data(c*r), cols(c), rows(r) {}
    T& operator()(unsigned c, unsigned r) {
        assert(c<cols&&r<rows); 
        return data[r*cols+c];
    }
};

or, best yet, use Boost's Multidimensional Array , which will be better than anything mere mortals could write. 或者最好的方法是使用Boost的多维数组 ,它比凡人都能写的任何东西都要好。

I'm not sure if this work, because your question and code are not the same, according to your code the function can have 3 parameters, so this would work: 我不确定是否可以正常工作,因为您的问题和代码不同,根据您的代码,该函数可以具有3个参数,因此可以正常工作:

int function(int a, int b, int** &array)
{
    array = new int*[a];
    for (int i =0;i<a;i++)
        array[i] = new int[b];

    // I don't know why you are returning int, probably doing something here....
}

However your question says that your function can take only one parameter, so: 但是您的问题说您的函数只能接受一个参数,因此:

  1. if the dimensions are known at compile time, then Fred's Answer is the best (it charmed me in fact! :) ). 如果在编译时知道尺寸,那么弗雷德的答案是最好的(实际上让我着迷!:))。
  2. if not, I can't see any possible solution that allows passing more than one user-specified value other than encapsulating all these values in one object. 如果没有,除了将所有这些值封装在一个对象中之外,我看不到任何允许传递多个用户指定值的可能解决方案。

Like this: 像这样:

class Foo {
public:
    Foo(int d1, int d2)
    { a = d1; b = d2; }
    int a,b;
    int** array;
};

int function(Foo &f)
{
    f.array = new int*[f.a];
    for (int i = 0;i<f.a;i++)
        f.array[i] = new int[f.b];
    // I don't know why you are returning int, probably doing something here....
}

Though I find it a bad idea, in fact the function could be a parameterless method instead: 尽管我发现这是一个坏主意,但实际上该function可能是无参数方法:

class Foo {
public:
    Foo(int d1, int d2)
    { a = d1; b = d2; }

    void Create()   // Or could do this right in the Constructor
    {
        array = new int*[a];
        for (int i = 0;i<a;i++)
            array[i] = new int[b];
    }

private:
    int a,b;
    int** array;

};

Still this is a bad idea, because you are reinventing the wheel, as there are a perfect class in the STL to do all the work for you: 但这仍然不是一个好主意,因为您正在重新发明轮子,因为STL中有一个完美的类可以为您完成所有工作:

vector< vector<int> > v;    // Now v is a 2D array

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM