I'm writing for class an implementation of Conway's Game of Life in a toroid. The function cargarToroide (loadToroid) should load from a file into the vector the appropriate status (alive or dead - True or False - 1 or 0) of each cell (celda), it's signature is as follows:
toroide cargarToroide(string nombreArchivo, bool &status);
nombreArchivo is the name of the file, and status should be false if there is any problem loading the file or in the format of the file.
The data structure is defined like this (I cannot change it):
typedef vector< vector<bool> > toroide;
the file is structured like this:
numberOfLines numberOfColums
list of the values of the cells
number of live cells
For example:
4 4
1 0 0 0
0 0 1 0
0 0 0 1
0 1 0 0
4
The thing is, I cannot find a way to make it work. I've read online that vector<bool> has problems when you try to load it the usual way, which was the first thing I've tried.
toroide cargarToroide(string nombreArchivo, bool &status)
{
toroide t;
ifstream fi (nombreArchivo);
int cantidadFilas, cantidadColumnas;
int celda;
if(!fi){
status = false;
}
fi >> cantidadFilas;
fi >> cantidadColumnas;
cout << cantidadFilas << endl;
cout << cantidadColumnas << endl;
for(int i = 0; i < cantidadFilas; i++) {
for (int j = 0; j < cantidadColumnas; j++) {
fi >> celda;
if(celda == 1) {
t[i].push_back(true);
}
else if(celda == 0){
t[i].push_back(false);
}
else{
status = false;
return t;
}
}
}
return t;
}
I've also tried defining celda as a boolean and just using
t[i].push_back(celda);
What would be the best way to approach this using C++11?
Aucun commentaire:
Enregistrer un commentaire