我有2种结构,颜色1:

struct Color
{
    unsigned char red;
    unsigned char green;
    unsigned char blue;
};

和图像结构:
struct matrixImage
{
    Image Img;
    struct Color T[MAX][MAX];
    int Width;
    int Height;
};

T表中,我正在存储图像的像素。但是数组的最大大小很小(堆栈上),所以我不能存储图像的每个像素。我知道如何在堆中定义这样的数组

-> struct Color *newTable = new struct Color[anyNumber];
但是我该如何用struct matrixImage编写代码,有帮助吗?

最佳答案

我会做这样的事情:

//Switched to a class because we need some encapsulation or this will be bug-prone.
class matrixImage
{
    //Do you need this? What kind of object is "Image"?
    //Image Img;
    std::vector<Color> pixel_data;
    int Width;
    int Height;
public:
    matrixImage(int width = 1, int height = 1) :
    Width(width), Height(height), pixel_data(width * height)
    {
    }

    //You can add bounds-checking if you need it, i.e. make sure y is less than Height,
    //make sure x is less than Width. Not every application needs it, and you need a
    //clear semantic of "what should happen" if you specify an invalid index.
    Color & get_color(int x, int y) {
        return pixel_data[y * Width + x];
    }
    //We have two versions to handle the case where your object is made "const"
    Color const& get_color(int x, int y) const{
        return pixel_data[y * Width + x];
    }

    //Hey, we can use the function we just defined above this one!
    void set_color(int x, int y, Color c) {
        get_color(x, y) = c;
    }

    //This only resizes the canvas. Doing proper "Resizing" is beyond the scope of what
    //we're discussing here.
    void set_size(int new_width, int new_height) {
        std::vector<Color> new_data(new_width * new_height);

        //This case is pretty easy
        if(new_width == Width) {
            std::copy(
                pixel_data.begin(),
                pixel_data.begin() + std::min(pixel_data.size(), new_data.size()),
                new_data.begin()
            );
        //This gets complicated
        } else if(new_width < Width) {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + y * Width + new_width,
                    new_data.begin() + y * new_width
                );
            }
        //Similar logic, but slightly different.
        } else {
            for(size_t y = 0; y < std::min(Height, new_height); y++) {
                std::copy(
                    pixel_data.begin() + y * Width,
                    pixel_data.begin() + (y + 1) * Width,
                    new_data.begin() + y * new_width
                );
            }
        }
        pixel_data = new_data;
        Width = new_width;
        Height = new_height;
    }

    //I leave this last one as an exercise to the reader, as it's beyond my expertise.
    void resize(int new_width, int new_height);
};

这将为您处理动态内存分配(当然还有释放),并在您绝对需要时为您提供一些基本功能,以直接处理基础数据。

关于c++ - 如何使用C++在结构体中的堆内存中定义变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41148354/

10-11 22:40
查看更多