如何使用 3 维数据作为类属性?

发布于 2024-09-18 22:01:57 字数 682 浏览 3 评论 0原文

我已经很长时间没有使用 C++ 了,但我有一个使用 3 维数据的类,但我不知道如何才能完成这项工作。我需要在构造函数中定义尺寸的大小。我在标题中尝试了这个:

class CImage
{
public:
    float values[][][];
...
}

在构造函数中尝试了这个:

CImage::CImage(int cols, int rows, int depth)
{
    values[cols][rows][depth];
}

但这返回错误:“将‘值’声明为多维数组必须具有除第一个维度之外的所有维度的界限”。

在构造函数中使用它也不起作用:

values = new float[cols][rows][depth];

我也尝试使用向量,但没有取得多大成功。标题:

vector<vector<vector<float> > > values;

构造函数中没有任何内容。没有编译器错误,但是当我尝试设置一个值时:

values[c][r][d] = value;

程序崩溃。

看起来很基础,但我就是不明白......

It's been a long time since I worked with C++, but I have a class that uses 3-dimensional data and I can't figure out how I can make this work. I need the sizes of the dimensions to be defined in the constructor. I tried this in the header:

class CImage
{
public:
    float values[][][];
...
}

and this in the constructor:

CImage::CImage(int cols, int rows, int depth)
{
    values[cols][rows][depth];
}

but this returns the error: "declaration of `values' as multidimensional array must have bounds for all dimensions except the first".

Also using this in the constructor does not work:

values = new float[cols][rows][depth];

I also tried using vector, but without much success. Header:

vector<vector<vector<float> > > values;

Nothing in constructor. No compiler errors, but when I try to set a value:

values[c][r][d] = value;

the program crashes.

It seems so basic, but I just can't figure it out...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紙鸢 2024-09-25 22:01:57

访问该向量时程序崩溃,因为它是空的,即这些索引处没有元素。

解决这个问题的最佳方法是创建一个线性的、一维的向量(甚至是一个数组),并使用一对运算符()来访问它,请参阅C++FAQ Lite 了解详细信息。或者使用 boost::multi_array

例如:

#include <vector>
#include <iostream>
class CImage
{
        int X, Y, Z;
        std::vector<float> values;
public:
        CImage(int cols, int rows, int depth)
                : X(cols), Y(rows), Z(depth),
                values(cols*rows*depth) {}
        float operator()(int x, int y, int z) const
        {
                return values[Z*Y*x + Z*y + z];
                // or you lay it out differently in memory
                // if you please, transparent to the user:
                // return values[x + X*y + X*Y*z];
        }
        float& operator()(int x, int y, int z)
        {
                return values[Z*Y*x + Z*y + z];
                // likewise, in a different layout
                // return values[x + X*y + X*Y*z];
        }
};

int main()
{
        CImage ci(3,3,3);
        ci(2,2,2) = 7.0;
        std::cout << ci(2,2,2) << '\n';
}

The program crashes when accessing that vector because it is empty, i.e. there are no elements at those indexes.

The best way to go about this is to make a linear, one-dimensional, vector (or even an array), and access it with a pair of operator()'s, see C++FAQ Lite for details. Or use boost::multi_array.

For example:

#include <vector>
#include <iostream>
class CImage
{
        int X, Y, Z;
        std::vector<float> values;
public:
        CImage(int cols, int rows, int depth)
                : X(cols), Y(rows), Z(depth),
                values(cols*rows*depth) {}
        float operator()(int x, int y, int z) const
        {
                return values[Z*Y*x + Z*y + z];
                // or you lay it out differently in memory
                // if you please, transparent to the user:
                // return values[x + X*y + X*Y*z];
        }
        float& operator()(int x, int y, int z)
        {
                return values[Z*Y*x + Z*y + z];
                // likewise, in a different layout
                // return values[x + X*y + X*Y*z];
        }
};

int main()
{
        CImage ci(3,3,3);
        ci(2,2,2) = 7.0;
        std::cout << ci(2,2,2) << '\n';
}
慕烟庭风 2024-09-25 22:01:57

只是为了指出为什么 Cubbi 是正确的,这将是 3d 向量的构造函数:

vector<vector<vector<float>>> values;
// create vector [dim0][dim1][dim2]
// init value: init
size_t dim0 = 3;
size_t dim1 = 3;
size_t dim2 = 3;
float init = 0.42f;
values = vector<vector<vector<float>>>
         (
            dim0,
            vector<vector<float>>
            (
                dim1,
                vector<float>
                (
                    dim0,
                    init
                )
            )
         );

很好,不是吗?

此外,您不能声明浮点值[][][];,因为数组驻留在堆栈上,因此编译器必须在编译时知道该数组的大小(例外:C99变量length 数组,但这不是 C++)。你可以声明一个float***values;(在ctor中使用new float[c][r][d];),但这也很糟糕。

Just to poiny out why Cubbi is right, this would be the constructor of the 3d-vector:

vector<vector<vector<float>>> values;
// create vector [dim0][dim1][dim2]
// init value: init
size_t dim0 = 3;
size_t dim1 = 3;
size_t dim2 = 3;
float init = 0.42f;
values = vector<vector<vector<float>>>
         (
            dim0,
            vector<vector<float>>
            (
                dim1,
                vector<float>
                (
                    dim0,
                    init
                )
            )
         );

Nice, isn't it?

Besides, you cannot declare a float values[][][]; because arrays reside on the stack, so the compiler has to know at compile time what size that array has (exception to this: C99 variable length arrays, but this would'nt be C++). You could declare a float*** values; (using new float[c][r][d]; in the ctor), but this is awful, too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文