• 0

[c++] list of arrays


Question

I am having trouble with this. I am using a int array with 4 elements that stores a position and where to move. Think of it as x1, y1 moves to x2,y2. I want to use a array to store this info (int a[4] = {x1,y1,x2,y2}). I want to add this array to a list so I want to use "list<int[4]> moves;". I dont know the number of moves that will be generated so I dont want to use a 2D array and I might be moving the arrays in the list around later on (some moves might have a higher priority).

So I want to use:

list<int[4]> moves;

and add stuff like:

int a[4] = {1,2,3,4};

moves.push_back(a);

but "push_back" does not work, compile error.

I was thinking of just using a struct to hold the array:

typedef struct _move { int a[4]; } move;

and just using "list<move> moves;". I like the struct way since i can add on more like what game piece does the move refer to with an "int type" (if I can get the list of arrays working this would have been another element making it a[5] instead of a[4] ) but I would like to know why I cant do an list of arrays?

Link to comment
Share on other sites

2 answers to this question

Recommended Posts

  • 0

I'm pretty sure it's because you can't define a fixed array size like that for the list. A more general pointer seems to work.

list&lt;int*&gt; moves;

Having a struct would indeed work better though. You could define the struct like

typedef struct _move {
   int x1; 
   int y1;
   int x2;
   int y2;
} move;

This makes more sense when accessing elements of move by doing move.x1 instead of move.a[0]. Also makes it super easy to add stuff.

Edited by Rob2687
Link to comment
Share on other sites

  • 0

Yea the struct way does look better, the things im thinking of adding are the piece that is moving (-4,-3,-2,-1,1,2, 3, 4) and is the new location a capture move (0 or 1). I was just wanted to know why it was not working because c++ error messages are crazy sometimes.

Thanks

Edited by Doli
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.