How to write a "List<int[,]>" in C++? -


i'm attempting make c++ equivalent code of following c# code, because i'm following tutorial in c#, , i'm more comfortable using c++. thought process maybe making outer array, allocating new array each index represents matrix size. okay or there better way go implementing in c++?

// current c# code list<int[,]> pieces;  pieces = new list<int[,]>();  /* 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} }); 

my initial code making matrix in array. since i'm not looking change number of "pieces" in game itself, fixed array should fine. wrote equivalent. obviously, didn't work, saying cannot assigned entity of 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}              }  /* , on each piece */ 

imo vector of vector of vectors seems little cumbersome such simple construct.

i'd assume 4x4, largest size, , embed pieces in that, in case you'd have

#include <vector> struct piece { int v[4][4]; }; std::vector<piece> pieces {       // piece      { {         {0,0,0,0},         {1,1,1,1},         {0,0,0,0},         {0,0,0,0}      } },      // j piece      { {        {0, 0, 1},  // ok since element 0 initialised default.        {1, 1, 1},        {0, 0, 0}        // dont need row here either 0 initialised.      } }      //others omitted now..   }; 

or non c++11 compliant compilers:

#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},  // ok since element 0 initialised default.        {1, 1, 1},        {0, 0, 0}        // dont need row here either 0 initialised.      } } };   std::vector<piece> pieces(raw_pieces, raw_pieces + sizeof(raw_pieces)/sizeof(piece)); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -