I need a matrix[3][3], but I don't know how to create it within the class itself, I've tried in many ways with the constructor but I got errors in every try.
I need it this way because I will need to assign it char values (using another class) to compare and determinate a winner (tic-tac-toe game).
You could just declare a 3 by 3 char matrix inside the class:
1 2 3 4 5
class Foo
{
private:
char board[3][3];
}
If you do want to use std::vector, you can do something like this, although it should be taken with a grain of salt, since I've never had to use nested vectors:
Thanks for your response (now to implement it). So, would it be better to use standard C-style matrix instead of using STL vector in this particular problem?.
I've read that it is a safest and better way to do arrays, and for that reason I wanted to do my try with them, but I'm not sure yet.
I want to stick with STL vectors, mostly for educational purposes :P (I know some C and I want to do the jump to the C++).
Tablero::Tablero(int fila, int columna)
:tablero(columna) // assuming columna is 3 we have a vector of 3 empty vectors.
{
filas = fila;
columnas = columna;
for ( unsigned i=0; i<columna ; ++i )
tablero[i].resize(fila) ; // each empty vector is resized to size fila.
}
It may make more sense to you to do the opposite with columna and fila, and it's perfectly fine to do so. You just need to treat it consistently within the class.
For something simple and static like a tic tac toe board, I think it's easiest to use a simple C style array. If you need to do any re-sizing, or anything dynamic at all, a vector is generally much easier to use than new[] and delete[].
@cire: Whenever I post any sort of code on here, someone else always seems to be able to do it much neater and more efficiently :P Just goes to show how much I don't know about the language yet.
Nice, @BlackSheep I have now my vector full working, I think I will stick with that (just 20 lines on that), but I will take your advice in future :P thanks.
@cire nice code, I didn't think this that way.
Thanks for help, I think now I understand more about vectors :P