简体   繁体   English

使用传入的参数在C ++中创建3D数组

[英]Creating a 3D array in C++ using passed in parameters

I have a function that takes in a void* buffer parameter. 我有一个接受void *缓冲区参数的函数。 This function (which is provided by HDF here . From my understanding, it reads info from a dataset into the buffer. I have this working, but only if I create a 3d int array using constant values. I need to be able to do this using values passed in by the user. Here is the start of that function: 该函数(由HDF 在此处提供。据我所知,它将数据集中的信息读取到缓冲区中。我可以正常工作,但是仅当我使用常量值创建3d int数组时,我才需要这样做使用用户传递的值,这是该函数的开始:

void* getDataTest(int countX, int countY)
{
    int NX = countX;
    int NY = countY;
    int NZ = 1;

    int data_out[NX][NY][NZ]; //I know this doesn't work, just posting it for reference

   //.
   //. more code here...
   //.

   // Read function is eventually called...
   h5Dataset.read(data_out, H5::PredType::NATIVE_INT, memspace, h5Dataspace);
}

This constantly fails on me. 这总是使我失败。 However, my previoud implementation that used const int values when creating the data_out array worked fine: 但是,我在创建data_out数组时使用const int值的预先实现效果很好:

void* getDataTest(int countX, int countY)
{
    const int NX = 5;
    const int NY = 5;
    const int NZ = 1;

    int data_out[NX][NY][NZ]; 

   //.
   //. more code here...
   //.

   // Read function is eventually called...
   h5Dataset.read(data_out, H5::PredType::NATIVE_INT, memspace, h5Dataspace);
}

This works fine. 这很好。 From my understanding, this function (which I have no control over) requires dataspaces of the same dimensionality (eg a 3D array will only work with a 3D array while a 2D array will only work with a 2D array when copying over the data to the buffer). 据我了解,此功能(我无法控制)需要相同维度的数据空间(例如,将数据复制到3D数组时,3D数组仅适用于3D数组,而2D数组仅适用于2D数组。缓冲)。

So, my key problem here is that I can't seem to figure out how to create a 3D int array that the read function is happy with (the function parameter is a void* but I can't seem to get anything other than a 3d int array to work). 因此,我的主要问题是,我似乎无法弄清楚如何创建读取函数满意的3D int数组(函数参数为void *,但除3d int数组工作)。 I've tried a 3D int array represented as an array of arrays of arrays using: 我尝试使用以下方法将3D int数组表示为由数组组成的数组:

   int*** data_out = new int**[NX];

but this failed as well. 但这也失败了。 Any ideas on how I can create a 3D int array of the form int arrayName[non-constant value][non-constant value][non-constant value]? 关于如何创建int arrayName [非常数值] [非常数值] [非常数值]形式的3D整数数组的任何想法? I know you can't create an array using non-constant values, but I added them in an attempt to clarify my goal. 我知道您不能使用非恒定值创建数组,但是我添加了它们以阐明我的目标。 Should there be a way in C++ to use function parameters as values for instantiating an array? 在C ++中是否应该有一种方法可以将函数参数用作实例化数组的值?

Do it like this: 像这样做:

    std::vector<int> array;

    array.resize(Nx*Ny*Nz);

    array[z*Ny*Nx + y*Nx + x] = value

It's nice to have the array[z][y][x] syntax, but supporting it is more trouble than it is worth. 拥有array [z] [y] [x]语法是很好的,但是支持它比它值得的麻烦更多。

I think the easiest is to do this: 我认为最简单的方法是:

int* data_out = new int[NX * NY * NZ];

You can then access this 1D array as a 3D array like that: 然后,您可以像下面这样将1D数组作为3D数组访问:

int value = array[z * NX * NY + y * NX + x];

In a more C++11 style, you can use an std::vector : 在更C ++ 11的样式中,您可以使用std::vector

std::vector<int> data_out;
data_out.resize(NX * NY * NZ);

And calling the function like that: 并像这样调用函数:

h5Dataset.read(data_out.begin(), H5::PredType::NATIVE_INT, memspace, h5Dataspace);

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

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