Quote:Original post by EasilyConfused
Doubtful. When you define the type of a 2D array, you have to provide the size of the second subscript, so that the compiler can do the necessary math to resolve the index when you dereference:
int f(int a[][3]){ cout << a[1][2]; // couldn't work this out without the 3 above}
I just tried:
std::vector<int[][3]> v;
and got a load of "not an allowed type" errors.
The thing is that in your first example with a 1D array vector, you are not really pushing a 1D array into the vector, you are just pushing a pointer to the start of the array. The array name decomposes down to a pointer to the start of the array when it is passed as a parameter of such type to push_back.
A pointer can be used pretty much interchangabley with a 1D array. The same is not true of a 2D array so I think you are out of luck.
I'm not sure if you will like this, but the following code shows how you can create a class that uses a single normal pointer to memory but allows for access via [][] like a 2D array. You could use a technique like this, or find a third-party container (boost?) that does a similar thing, then "silently" replace all your 2D arrays at declaration with such a class so the rest of your code is not affected:
*** Source Snippet Removed ***
With something like that, you could certainly have a std::vector<array>, although you'd need to supply array with a default constructor and a resize method so you could resize the individual arrays after the vector had been initialised.
You could then do:
std::vector<array> v;v.resize(1);v[0].resize(3,3);v[0][1][2]=3;
However, just because something is possible doesn't mean it is a good design [smile].
HTH Paul
Thank you for writing all that code up. But from what it sounds from the last thread when I asked for something else, this thing that i'm about to do should be quite simple.
I'm currently making a Tetris game and want to make rotation for the blocks. So far, this is how it looks.
// the current piece (4x4)vector<vector<int> > piece(4, vector<int>(4));// the blocks i'm using in their first form int LBlock0[4][4] = {{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 1, 0}}; int IBlock0[4][4] = {{0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}, {0, 1, 0, 0}}; int LBlockMirror0[4][4] = {{0, 0, 1, 0}, {0, 0, 1, 0}, {0, 0, 1, 0}, {0, 1, 1, 0}}; int SquareBlock0[4][4] = {{0, 0, 0, 0}, {0, 1, 1, 0}, {0, 1, 1, 0}, {0, 0, 0, 0}}; int ArrowBlock0[4][4] = {{1, 1, 1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
Going from an advice from another thread, I will make the rotation by changing the index of an array that holds all the blocks of the current kind. My way of solving this is to make a global variable named "rotation" which is going to have a value between 0-3 which represents the index/rotation of the current block.
Then I thought I could do it some way like this. LBlock[rotation]; supposed the LBlock vector/array contains all the 4 different forms of the LBlock.
I also have a switch statement in the function that generates a new block. That switch checks which block is going to be next. If the switch is for example 0 (random number), the currentblock is set to LBlock[0]; where 0 is the normal rotation of the block. When I later on Blit the blocks on the screen I use the rotation variable like this, LBlock[rotation]. rotation=0 if the player hasn't pushed the up button which changes the value.
The reason I mentioned the switch case is because I want to somehow set the vector to the block like this:
Vec = LBlock[0];
Vec = SquareBlock[0];
...
Well, anyways, the important part is how to store the 2d arrays somewhere so I can use them like I mentioned in combination with the rotation variable.
How would you make this in a simple way? I realized I should've asked this instead, because my first attempt may be too odd where I wanted to store 2d arrays in vectors.