C++多维数组初始化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3554205/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:06:23  来源:igfitidea点击:

C++ multi-dimensional array initialization

c++pointersinitializationmultidimensional-array

提问by Felix

in C++ I want to initialize a double matrix (2-dimensional double array) like I would normally do without pointers like so:

在 C++ 中,我想初始化一个双矩阵(二维双数组),就像我通常在没有指针的情况下所做的那样:

    double data[4][4] = {
    1,0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,0,1
};

However, since I want to return and pass it to functions, I need it as a double**pointer. So, basically I need to initialize data in a nice way (as above), but then afterwards I need to save the pointer to the 2D-array without losing the data when the function exits.

但是,由于我想返回并将其传递给函数,因此我需要将其作为double**指针。所以,基本上我需要以一种很好的方式初始化数据(如上所述),但之后我需要保存指向二维数组的指针,而不会在函数退出时丢失数据。

Any help on this? :-)

这有什么帮助吗?:-)

回答by Chubsdad

Unless you are particular about pointers, I would prefer a reference here

除非你特别关注指针,否则我更喜欢这里的参考

void init( double (&r)[4][4]){
    // do assignment
    r[0][0] = 1;
}

int main(){
    double data[4][4] = {
        1,0,0,0,
        0,1,0,0,
        0,0,1,0,
        0,0,0,1
    };

    init(data);
}

By the way, if you pass it to a function in this manner, you would be "assigning" rather than "initializing".

顺便说一下,如果你以这种方式将它传递给一个函数,你将是“分配”而不是“初始化”。

回答by fredoverflow

Are all your matrices 4x4? Then I would simply define a class with a double[4][4]member and pass objects of that class around:

你所有的矩阵都是 4x4 的吗?然后我会简单地定义一个带有double[4][4]成员的类并传递该类的对象:

class Matrix
{
    double m[4][4];
    // ...
};

void function(const Matrix& matrix)
{
    // ...
}

If you need matrices of various dimensions, but they are known at compile time, use a template:

如果您需要各种维度的矩阵,但它们在编译时已知,请使用模板:

template <size_t n>
class Matrix
{
    double m[n][n];
    // ...
};

template <size_t n>
void function(const Matrix<n,n>& matrix)
{
    // ...
}

This saves you from dealing with array-to-pointer decay and makes the code more readable IMHO.

这使您免于处理数组到指针的衰减,并使代码更具可读性,恕我直言。

回答by fredoverflow

First, declaration of the double dimensional array is not correct. It needs to be done as follows:

首先,二维数组的声明不正确。需要按如下方式进行:

double data[4][4] = {  
        {1.0,0,0,0},  
        {0,1,0,0},  
        {0,0,1,0},  
        {0,0,0,1}  
    };

Second, for passing it in a function you can do it like

其次,要将它传递给函数,您可以这样做

show(data);

In the function declaration, you need to give the argument as an array with giving all dimensions except the first. So the declaration would look like:

在函数声明中,您需要将参数作为数组给出,并给出除第一个维度之外的所有维度。所以声明看起来像:

void show(double arr[][4])
{
   ...
   ...
}

This passes the array as a reference wihout you needing to use a pointer.

这会将数组作为引用传递,而无需使用指针。

Hope this helped.

希望这有帮助。

回答by Didier Trosset

double (*)[4]is very different from double **

double (*)[4]double **

Just sketch the layout of your doubles in the memory for both and you should understand why you can't use them interchangeably.

只需为两者在内存中勾勒出双打的布局,您就应该明白为什么不能互换使用它们。

回答by Kirill V. Lyadvinsky

Initialize temporary variable in this way and then copy it to the dynamically allocated memory.

以这种方式初始化临时变量,然后将其复制到动态分配的内存中。

回答by SandBag_1996

How about this (with pointers, and does what you asked for)

这个怎么样(带指针,并按照您的要求执行)

#include <iostream>

using namespace std;

int refer(double (*a)[4])
{
   cout<<"First value is "<<(*a)[0];
   (*a)[0] = 37;
   cout<<"changed value is "<<(*a)[0];
}

int main()
{
   double data[4][4] = {
    1.0,0,0,
    0,1,0,0,
    0,0,1,0,
    0,0,0,1
   };
   refer(data);
   return 0;
}