简体   繁体   English

C ++多维数组初始化

[英]C++ multi-dimensional array initialization

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. 但是,由于我想返回并将其传递给函数,我需要它作为double**指针。 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. 所以,基本上我需要以一种很好的方式初始化数据(如上所述),但之后我需要将指针保存到2D数组,而不会在函数退出时丢失数据。

Any help on this? 对此有何帮助? :-) :-)

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". 顺便说一句,如果你以这种方式将它传递给一个函数,你将“分配”而不是“初始化”。

Are all your matrices 4x4? 你的所有矩阵都是4x4吗? Then I would simply define a class with a double[4][4] member and pass objects of that class around: 然后我只需用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. 这样可以避免处理数组到指针的衰减,并使代码更具可读性。

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. 希望这有帮助。

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. 只是在内存中勾勒出双打的布局,你应该理解为什么你不能互换使用它们。

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

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;
}

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

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