用c ++初始化一些数据数组

Xun*_*ang 3 c++ arrays

我想建立所有俄罗斯方块的定义:

#include <iostream>
#include <vector>

struct Tetris{
    std::vector<int> data;
    int getAt(int x, int y){return data.at(y*4+x);};
} tetris[6];

int main(){
    //stick
    tetris[0].data.resize(32);
    int array0[32]={0,0,0,0,
                    0,0,0,0,
                    1,1,1,1,
                    0,0,0,0,
                    0,1,0,0,
                    0,1,0,0,
                    0,1,0,0,
                    0,1,0,0};
    tetris[0].data.assign(array0,array0+32);

    //brick
    tetris[1].data.resize(16);
    int array1[16]={0,0,0,0,
                    0,1,1,0,
                    0,1,1,0,
                    0,0,0,0};
    tetris[1].data.assign(array1,array1+16);

    ...

}
Run Code Online (Sandbox Code Playgroud)

通过这种方式,我需要定义6个数组,仅用于存储初始化数据(array0,array1...),这在初始化后是无用的.这看起来效率很低,浪费内存.我想知道每次使用后是否有办法删除这些数据?

更新:

如果我想重用array0,比方说

    tetris[0].data.resize(32);
    int array0[32]={...};
    tetris[0].data.assign(array0,array0+32);

    //brick
    tetris[1].data.resize(16);
    delete array0;
    int array0[16]={...};
    tetris[1].data.assign(array0,array0+16);

    ...
Run Code Online (Sandbox Code Playgroud)

编译器将报告"array0重定义"的错误.是delete不是在这种情况下工作?

Chr*_*sCM 5

如果要控制静态分配类型的生命周期,可以添加任意范围分辨率运算符来执行此操作.

int main()
{
    {
        //stick
        tetris[0].data.resize(16);
        int array0[16]={0,0,0,0,
                    0,0,0,0,
                    1,1,1,1,
                    0,0,0,0};
        tetris[0].data.assign(array0,array0+16);

        //brick
        tetris[1].data.resize(16);
        int array1[16]={0,0,0,0,
                    0,1,1,0,
                    0,1,1,0,
                    0,0,0,0};
        tetris[1].data.assign(array1,array1+16);
    }//THISONE
    //Do the rest of the work of main, without the pesky arrays sticking around.

}
Run Code Online (Sandbox Code Playgroud)

在这个示例main中,您的tetris变量仍然存在,因为它是全局的.但是你声明的那些数组不会超过"THISONE".

请注意,我不一定推荐这种方法.最好是创建一个类,或者使用其他更标准的方法.但偶尔这是一个不错的小技巧,以避免使用不必要的动态分配.

编辑:这种方法可能更好,但要确保你了解正在发生的一切,否则你可能最好坚持你所知道的.特别是如果你不理解我在这里做的所有不好的事情,为了快速给你一个例子.

#include <iostream>
using namespace std;

class stick;//Forward declaration so shape can make stick a "friend"

class shape {//This classe declaration should go in a .h file.
    static const int WIDTH = 4;
    static const int HEIGHT = 4;
    int array[WIDTH][HEIGHT];

public:


    shape(){
        for(int i = 0; i < HEIGHT; i++) {
            for(int j = 0; j < WIDTH; j++) {
                array[i][j] = 0;
            }
            array[i][i] = 1;
        }
    }

    void printShape() {
        for(int i = 0; i < HEIGHT; i++) {
            for(int j = 0; j < WIDTH; j++) {
                cout << array[i][j] << " ";
            }
            cout << endl;
        }
    }

    friend class stick;
};

class stick: public shape {//This declaration should also go in a header file
public:
    stick() {
        for(int i = 0; i < HEIGHT; i++) {
            for(int j = 0; j < WIDTH; j++) {
                if(i == 0) array[i][j] = 1;
                else array[i][j] = 0;
            }
        }
    }
};



int main(){

    shape planeShape;
    stick stickShape;
    cout << "PLAIN SHAPE: " << endl;
    planeShape.printShape();

    cout << endl << "STICK SHAPE: " << endl;
    stickShape.printShape();



    cout << endl << "PLANE SHAPE POINTER:" << endl;
    shape* shapePointer = &planeShape;
    shapePointer->printShape();

    cout << endl << "STICK SHAPE POINTER:" << endl;
    shapePointer = &stickShape;
    shapePointer->printShape();

}
Run Code Online (Sandbox Code Playgroud)