STL Vector and Classes

Hi everyone, this is my first post here, hope you can help me.

I just started with C++ by myself and I can't figure out how initialise a matrix within a class.

I've just done this so far:
1
2
3
4
5
6
7
8
9
10
...
class Tablero {
    vector< vector<char> > tablero;
    short filas, columnas;
public:
    Tablero(int fila, int columna);
    bool asignar_pos(int x, int y, char ficha);
    void imprimir_tablero();
};
...


and my problem here:
1
2
3
4
5
6
7
8
...
Tablero::Tablero(int fila, int columna)
:tablero(filas, vector<char>(columnas)) //HERE
{
    filas = fila;
    columnas = columna;
}
...


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).

Thanks in advance.
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:
1
2
3
4
5
std::vector<std::vector<char> > board;
board.push_back(std::vector<char>(3));
board.push_back(std::vector<char>(3));
board.push_back(std::vector<char>(3));
board[0][0] = 'd';


The "std::vector<char>(3)" constructor constructs a vector that reserves enough size for 3 chars. This vector is then pushed onto your "board" vector.
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++).

Thanks for your help.
1
2
3
4
5
6
7
8
9
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
Last edited on
Topic archived. No new replies allowed.