如何使不定尺寸的多维数组成为c ++中的类的成员? [英] How do I make a multidimensional array of undetermined size a member of a class in c++?

查看:140
本文介绍了如何使不定尺寸的多维数组成为c ++中的类的成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从boost中查看多维数组库,我无法找出一种在头文件中使用未定义形状声明一个multi_array成员的方法,例如 A 以下示例:



Class.h

  std :: size_t nX; 
std :: size_t nY;
std :: size_t nZ;

boost :: multi_array< double,3>一个;

这些文件在源文件中被实例化为一定大小:



Class.c ++

  nX = 3 
nY = 4
nZ = 2

A = boost :: multi_array< double,3>(boost :: extent [nX] [nY] [nZ]);

但这给我一个不平等的形状错误。有没有办法做我正在尝试的事情?



最终,我需要一个成员容器来存储一个要确定的后期大小的三维数据。这个工作与 boost :: numeric :: ublas :: matrix 有两个维度没有问题,但现在我需要一些可用于三维的东西。



任何人都可以告诉我如何使用multi_array类可以实现,或者通知我另一个可能会工作的库?

解决方案

而不是

  A = boost :: multi_array< double,3& ::区段[NX] [NY] [nZ上]); 

您需要调用 resize()函数作为

  A.resize(boost :: extents [nX] [nY] [nZ]); 


I am looking at the multidimensional array library from boost, and I'm having trouble figuring out a way to declare a multi_array member in the header file with an undefined shape, like "A" in the example below:

Class.h

std::size_t nX;
std::size_t nY;
std::size_t nZ;

boost::multi_array<double, 3> A;

which is later instantiated to a certain size in the source file:

Class.c++

nX = 3
nY = 4
nZ = 2

A = boost::multi_array<double, 3>(boost::extents[nX][nY][nZ]);

but this gives me an unequal shape error. Is there any way to do what I am attempting?

Ultimately, I need a member container to store three-dimensional data of a to-be-determined-later size. This worked with boost::numeric::ublas::matrix for two dimensions without a problem, but now I need something that will work for three dimensions.

Can anyone tell me how/if this is possible with the multi_array class, or inform me of another library that might work instead?

解决方案

instead of

A = boost::multi_array<double, 3>(boost::extents[nX][nY][nZ]);

you need to call resize() function as

A.resize(boost::extents[nX][nY][nZ]);

这篇关于如何使不定尺寸的多维数组成为c ++中的类的成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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