简体   繁体   中英

How to declare a two-dimensional array with 2 variables values

I'm trying to declare a twodimensional array with a value int size but I don't know how to do it.

I want to do this:

int size;

class tile {
public:
    tile() : val( 0 ), blocked( false ) {}
    unsigned int val;
    bool blocked;
};

tile board[size][size];

Thanks for your help :)

std::vector< std::vector< tile> > board( size , std::vector<tile> ( size ) );

will do it.

Alternatively,

tile** board = new (tile*) [size]; //allocate the array as a whole
for (int i = 0; i < size; ++i)     //now allocate each row in it
   board[0] = new tile [size];

But vectors are definitely the way to go.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM