我想使用我的HDF5数据集的维度创建一个数组。我正在使用以下代码来查找数据集的维度。

#include <iostream>
#include <string>
#include <vector>
#include <iomanip>
#include <typeinfo>

#include "H5cpp.h"

using namespace H5;
int main() {
    std::string sFileName;
    sFileName = "test.h5";

    const H5std_string FILE_NAME(sFileName);
    const H5std_string DATASET_NAME("timestep:5.0");
    H5File file(FILE_NAME.c_str(), H5F_ACC_RDONLY);
    DataSet dataset = file.openDataSet(DATASET_NAME.c_str());

    DataSpace dataspace = dataset.getSpace();
    int rank = dataspace.getSimpleExtentNdims();

    // Get the dimension size of each dimension in the dataspace and display them.
    hsize_t dims_out[2];
    int ndims = dataspace.getSimpleExtentDims(dims_out, NULL);
    std::cout << "rank " << rank << ", dimensions " <<
        (unsigned long)(dims_out[0]) << " x " <<
        (unsigned long)(dims_out[1]) << std::endl;

    const int xrows = static_cast<int>(dims_out[0]); //120
    const int yrows = static_cast<int>(dims_out[1]); //100

    std::cout << xrows * yrows << std::endl; //12000

    double myArr[xrows] // this also produces an error saying xrows is not a constant value
}


但是,当我尝试使用创建数组时

double myArr[xrows*yrows];


我收到一个错误,说xrow和yrow不是常数。我该如何解决?

最佳答案

double array[c]仅在c是恒定值时才有效:

const int c = 10;
double array[c]; //an array of 10 doubles


如果c是动态的,则使用new

int c = 5;
c *= 2; //c=10
double *array = new double(c);

关于c++ - C++ HDF5将数据集的维用作const int,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44789168/

10-10 16:38