0

所以我在 C++ 中遇到了一些问题(我的第一个编程语言是 C)。

假设我有以下课程:

2个标题(矩形和网格,假设点类很好,另一个假设是我们目前不需要打印功能)

网格.h

#ifndef GRID_H
#define GRID_H
#ifndef RECT_H
#include "Rectangle.h"

#endif

class Grid
{
public:
    Grid(int tileW, int tileH, int width, int height, int color);
    ~Grid();
    Rectangle& getRectAt(const Point &p);
    void print() const;
private:
    int count;
    Rectangle **recs;
};
#endif

矩形.h

#ifndef RECT_H
#define RECT_H
#ifndef POINT_H
#include "Point.h"
#endif

class Rectangle
{
public:
        Rectangle(int l, int u, int w, int h, int color);
        int getColor() const;
        void setColor(int color);
        bool contains(const Point &p) const;
        void print() const;
private:
        const Point topLeft, bottomRight;
        int color;
};

#endif

和2个cpp:

矩形.cpp

#include "Rectangle.h"

Rectangle::Rectangle(int l, int u, int w, int h, int color) : topLeft(l, u), bottomRight(l + w, u + h) { this->color = color; }

int Rectangle::getColor() const
{
    return this->color;
}

void Rectangle::setColor(int color)
{
    this->color = color;
}

bool Rectangle::contains(const Point &p) const
{
    return (this->topLeft.getX < p.getX && p.getX < this->bottomRight.getX
        && this->bottomRight.getY < p.getY && p.getY < this->bottomRight.getY);
}

void Rectangle::print() const
{
    /**/
}

网格.cpp

#include "Grid.h"

Grid::Grid(int tileW, int tileH, int width, int height, int color)
{
    int index, index_c=0;
    recs = new Rectangle *[width];

    for (int index = 0; index < width; index++)
    {
        recs[index] = new Rectangle [height];
    }

}

(假设我们不需要其他 Grid 函数并且构造函数没有完成)。
现在我要做的是,在 Grid.cpp 构造函数中,我试图动态分配数组数组,但我无法理解 cpp 中类的内存分配背后的逻辑。如果有人可以向我解释 cpp 中的“新”功能如何在类和 n 维数组(类和一般情况下)上发挥作用,我将不胜感激。

我希望你能理解我在这里遇到的问题。

提前致谢。

4

2 回答 2

0
Grid::Grid(int tileW, int tileH, int width, int height, int color)  // האם ניתן להניח את תקינות הקלט ? 
{
  int i ,j ; 
  i=0;
  count=height*width;
  recs=new Rectangle* [count];
 
  for(i=0;i<count;i++)
   for(j=0;j<width;j++)
    {
        recs[i+j]=new Rectangle (tileW*j,tileH*i,tileW,1,tileH);
    }

}

您需要使用 1 个长数组并像二维一样处理它。

于 2016-11-21T20:28:18.460 回答
0

正如评论中提到的,它更好/更容易使用一些容器模板,例如:

                vector< Rectangle >     recs_1D;
        vector< vector< Rectangle > >   recs_2D;
vector< vector< vector< Rectangle > > > recs_3D;

vector<T>自分配一维数组的任何容器类模板在哪里std::vector?请确保对于嵌套模板,您在<前后都有空间,>否则某些编译器可能会抛出语法错误。

您甚至可以像这样创建自己的容器,请参阅:

并寻找List_static模板(有完整的代码)它的静态(为了简单起见,没有动态分配),但您可以将它用作模板代码来添加动态分配。

但是,如果您坚持使用new,delete(在某些情况下更快/更好/更安全/必要)而不是像这样在第 3 方模板代码上中继(直接写在这里,因此它可能包含拼写错误,并且为了使其简单快速您需要定义没有操作数的矩形构造函数Rectangle::Rectangle(){}!!!):

// some variables for 2D grid
int xs=0,ys=0;                // size of 2D grid [rectangles]
Rectangle **rec=NULL;

// allocation
xs=100; ys=50;                // wanted size
rec=new Rectangle*[xs];       // pointers for each column
rec[0]=new Rectangle[xs*ys];  // whole grid xs*yus rectangles (and also the first column)
for (int x=1;x<xs;x++)        // set all pointers to point to their column
 rec[x]=rec[x-1]+ys;

// now you can use rec[x][y]
rec[3][7]=Rectangle(3,7,1,1,55);

// releasing of memory
if (rec)
 {
 if (rec[0]) delete[] rec[0]; // release the grid
 delete[] rec;                // release pointers
 }
xs=0; ys=0; rec=NULL;

如果您不能在没有操作数的情况下使用构造函数(出于任何原因),那么您需要将 of 更改new/delete[]rec[0]2 个嵌套的 for(x,y)循环并rec[x][y]改用它会慢得多并破坏堆和 C++ 引擎内存管理。

您可以在任何维度上使用它......只需添加*到指针,添加分辨率变量和每个轴的另一个 for 循环,还有一个new,delete[]......

于 2021-01-26T09:00:22.970 回答