写入向量<向量<bool>>

writing to a vector< vector<bool> >

本文关键字:gt 向量 lt bool      更新时间:2023-10-16

我正在为类编写Conway在环面中的生命游戏的一个实现。函数cargarToroide(loadToroid(应将每个单元格(celda(的适当状态(活动或死亡-真或假-1或0(从文件加载到向量中,其签名如下:

toroide cargarToroide(string nombreArchivo, bool &status);

nombreArchivo是文件名,如果加载文件或文件格式有任何问题,则status应为false。

数据结构定义如下(我无法更改(:

typedef vector< vector<bool> > toroide;

该文件的结构如下:

numberOfLines numberOfColums
list of the values of the cells
number of live cells

例如:

4 4
1 0 0 0
0 0 1 0
0 0 0 1
0 1 0 0
4

问题是,我找不到让它发挥作用的方法。我在网上读到,当你试图以通常的方式加载vector<bool>时,它会出现问题,这是我尝试的第一件事。

toroide cargarToroide(string nombreArchivo, bool &status)
{
toroide t;
ifstream fi (nombreArchivo);
int cantidadFilas, cantidadColumnas;
int celda;
if(!fi){
status = false;
}
fi >> cantidadFilas;
fi >> cantidadColumnas;
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;
}

我还尝试将celda定义为布尔值,并仅使用

t[i].push_back(celda);

使用C++11实现这一点的最佳方法是什么?

您需要调整外部向量的大小,然后才能对其使用运算符[]。在读取数据时还应该使用正确的类型(bool(,并检查输入文件中的错误。我在代码中评论道:

#include <iostream>
#include <fstream>
#include <vector>
typedef std::vector< std::vector<bool> > toroide;
// Both cargarToroide and cargarToroide_improved can be used
bool cargarToroide_improved(const std::string& nombreArchivo, toroide& in_toroide)
{
std::ifstream fi(nombreArchivo);
if(!fi) return false;
int cantidadFilas, cantidadColumnas, liveCells=0;
// use a bool to read the bool data
bool celda;
fi >> cantidadFilas;
fi >> cantidadColumnas;
// check if the stream is in a failed state
if(fi.fail()) return false;
// Temporary used to not mess with in_toroide until we're finished.
// Create it with cantidadFilas default inserted rows
toroide t(cantidadFilas);
for(auto& row : t) {
// default insert columns into the row
row.resize(cantidadColumnas);
for (int col = 0; col < cantidadColumnas; ++col) {
fi >> celda;
// check if the stream is in a failed state
// (non-bool read or the file reached eof())
if(fi.fail()) return false;
// set column value in the row
row[col] = celda;
// count live cells
liveCells += celda;
}
}
// compare live cells in matrix with checksum
int cmpLive;
fi >> cmpLive;
if(fi.fail() || cmpLive!=liveCells) return false;
// a successful toroide was read, swap your temporary
// toroide with the user supplied one
std::swap(t, in_toroide);
return true;
}
// if the signature of this function really can't be changed (which it should),
// make it a proxy for the function with a slightly nicer interface
// Like this:
toroide cargarToroide(std::string nombreArchivo, bool &status)
{
toroide rv;
status = cargarToroide_improved(nombreArchivo, rv);
return rv;
}

使用改进的签名:

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
for(auto& file : args) {
toroide my_toroide;
if(cargarToroide_improved(file, my_toroide)) {
for(auto& r : my_toroide) {
for(auto c : r) {
std::cout << c << " ";
}
std::cout << "n";
}
} else {
std::clog << "failed loading " << file << "n";
}
}
}

使用您被迫使用的签名:

int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
for(auto& file : args) {
bool status;
toroide my_toroide = cargarToroide(file, status);
if(status) {
for(auto& r : my_toroide) {
for(auto c : r) {
std::cout << c << " ";
}
std::cout << "n";
}
} else {
std::clog << "failed loading " << file << "n";
}
}
}

如果您知道编译时的行数(就像在本例中所做的那样(,则可以使用resize。在这种情况下,您必须在两个for循环之前执行以下操作。

t.resize(cantidadFilas);

事实上,您也可以对列执行同样的操作。这样,您就不再需要在内部for循环中使用push_back了。

如果不知道行数,则只使用push_back并将行添加到toroide。然后在第二个for循环之前添加以下行。

vector<bool> row; 
t.push_back(row)