将堆上的3D数组存储为结构成员

人气:716 发布:2022-10-16 标签: struct new-operator multidimensional-array c++ heap-memory

问题描述

我最近开始使用C++进行数值计算,我希望在模拟过程中使用Struct Operators来存储3D字段。 我用

在堆上创建了3D数组
const unsigned int RES = 256;
auto arr3D = new double [RES][RES][RES];

因为根据我的测试,这种方法比使用Boost_Multiarr、Eigen张量或嵌套向量都要快。 到目前为止,这在我的极简主义Test.cpp上运行得很好,但当我尝试将这些相同的3D数组实现为我的Struct Operators成员时,我不能再使用auto命令:

const unsigned int RES = 256;

struct Operators {
public:
    std::complex<double>*** wfc;         // edited, see 'spitconsumers' comment

    Operators(Settings &set) {        // just another structure used by Operators
        wfc = new std::complex<double> [RES][RES][RES];

        // ...Initializing wfc using Settings

};

在这种情况下,我找不到声明wfc的方法,这样我就不会收到类型错误

错误:无法在赋值中将‘std::Complex(*)[256][256]’转换为‘std::Complex*’

所以我的问题是如何正确声明3D数组wfc以及维护这种结构方法是否可能/有用。如果wfc不是结构的成员,访问wfc[i][j][k]通常会更快吗?(我必须这样做~10^6次)

提前谢谢!

推荐答案

错误消息返回正确的声明std::complex<double>(*wfc)[RES][RES];

const unsigned int RES = 256;
struct Settings {};
struct Operators {
public:
    std::complex<double>(*wfc)[RES][RES];         // edited, see 'spitconsumers' comment

    Operators(Settings& set) {        // just another structure used by Operators
        wfc = new std::complex<double>[RES][RES][RES];

        // ...Initializing wfc using Settings
        // Setting the last element
        wfc[254][254[254] = 42;

    };
}

173