如何在C ++中编写“List <int [,]>”?</int [,]>

时间:2013-03-13 06:25:34

标签: c++

我正在尝试制作以下C#代码的C ++等效代码,主要是因为我正在使用C#中的教程,而且我更习惯使用C ++。我的思考过程可能是创建一个外部数组,然后在表示矩阵大小的每个索引中分配一个新数组。这样可以,还是有更好的方法在C ++中实现它?

// current C# code
List<int[,]> pieces;

pieces = new List<int[,]>();

/* I Piece */
pieces.Add(new int[4, 4] {
    {0, 0, 0, 0},
    {1, 1, 1, 1},
    {0, 0, 0, 0},
    {0, 0, 0, 0}
});

/* J Piece */
pieces.Add(new int[3, 3] {
    {0, 0, 1},
    {1, 1, 1},
    {0, 0, 0}
});

/* O Piece */
pieces.Add(new int[2, 2] {
    {1, 1},
    {1, 1}
});

/* S Piece */
pieces.Add(new int[3, 3] {
    {0, 1, 1},
    {1, 1, 0},
    {0, 0, 0}
});


/* T Piece */
pieces.Add(new int[3, 3] {
    {0, 1, 0},
    {1, 1, 1},
    {0, 0, 0}
});

/* Z Piece */
pieces.Add(new int[3, 3] {
    {1, 1, 0},
    {0, 1, 1},
    {0, 0, 0}
});

我的初始代码是在数组中制作矩阵。因为我不打算改变游戏本身中“碎片”的数量,所以固定阵列应该没问题。所以这就是我写的等价物。显然,它没有用,说它不能分配给int的实体:

int pieces [7];

//I piece
pieces [0] = new int [4][4];
pieces [0] = {
                {0, 0, 0, 0},
                {1, 1, 1, 1},
                {0, 0, 0, 0},
                {0, 0, 0, 0}
             }

/* And so on for each piece */

2 个答案:

答案 0 :(得分:3)

对于这样一个简单的构造,IMO向量载体的载体似乎有点麻烦。

我可能会认为一切都是4x4,最大的尺寸,并且只是嵌入了那些,在这种情况下你会有

#include <vector>
struct Piece { int v[4][4]; };
std::vector<Piece> pieces { 
     // I piece
     { {
        {0,0,0,0},
        {1,1,1,1},
        {0,0,0,0},
        {0,0,0,0}
     } },
     // J piece
     { {
       {0, 0, 1},  // This is OK since extra element will be 0 initialised by default.
       {1, 1, 1},
       {0, 0, 0}
       // Dont need a row here either as it will be 0 initialised.
     } }
     //Others omitted for now..
  };

或者对于非C ++ 11兼容的编译器:

#include <vector>
struct Piece { int v[4][4]; };
Piece raw_pieces[] = {
     { {
        {0,0,0,0},
        {1,1,1,1},
        {0,0,0,0},
        {0,0,0,0}
     } },
     { {
       {0, 0, 1},  // This is OK since extra element will be 0 initialised by default.
       {1, 1, 1},
       {0, 0, 0}
       // Dont need a row here either as it will be 0 initialised.
     } }
};


std::vector<Piece> pieces(raw_pieces, raw_pieces + sizeof(raw_pieces)/sizeof(Piece));

答案 1 :(得分:2)

尝试std::list<std::vector<std::vector<int> > >或按照建议尝试std::vector<std::vector<std::vector<int> > >

以下是vector<vector<vector<int>>>的完整示例,但请记住,您必须按照以下方式进行编译: g++ -std=c++0x t.cpp

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<vector<vector<int>>> A = {
      {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
      },
      {
        {0, 0, 1},
        {1, 1, 1},
        {0, 0, 0}
      },
      {
        {1, 1},
        {1, 1}
      },
      {
        {0, 1, 1},
        {1, 1, 0},
        {0, 0, 0}
      },
      {
        {0, 1, 0},
        {1, 1, 1},
        {0, 0, 0}
      },
      {
        {1, 1, 0},
        {0, 1, 1},
        {0, 0, 0}
      }
    };


    for(int i=0;i<A.size();i++) {
      for(int j=0;j<A[i].size();j++) {
        for(int k=0;k<A[i][j].size();k++) {
          std::cout<<A[i][j][k];
        };
        std::cout<<std::endl;
      };
      std::cout<<std::endl;
    };
}

或者,如果您不喜欢一次性初始化所有内容,并且您希望在运行时执行此操作。您始终可以使用push_back附带的std::vector方法

#include <vector>
#include <iostream>

using namespace std;

int main()
{
    vector<vector<vector<int>>> A;
    A.push_back(vector<vector<int>>(
      {
        {0, 0, 0, 0},
        {1, 1, 1, 1},
        {0, 0, 0, 0},
        {0, 0, 0, 0}
      }
    ));


    A.push_back(vector<vector<int>>(
      {
        {0, 0, 1},
        {1, 1, 1},
        {0, 0, 0}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {1, 1},
        {1, 1}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {0, 1, 1},
        {1, 1, 0},
        {0, 0, 0}
      }
    ));


    A.push_back(vector<vector<int>>(
      {
        {0, 1, 0},
        {1, 1, 1},
        {0, 0, 0}
      }
    ));

    A.push_back(vector<vector<int>>(
      {
        {1, 1, 0},
        {0, 1, 1},
        {0, 0, 0}
      }
    ));


    for(int i=0;i<A.size();i++) {
      for(int j=0;j<A[i].size();j++) {
        for(int k=0;k<A[i][j].size();k++) {
          std::cout<<A[i][j][k];
        };
        std::cout<<std::endl;
      };
      std::cout<<std::endl;
    };
}