将堆上的3D数组存储为结构成员 [英] Storing 3D Array on Heap as Structure Member

查看:95
本文介绍了将堆上的3D数组存储为结构成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用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;

    };
}

这篇关于将堆上的3D数组存储为结构成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆