c++ - 写入 vector < vector <bool>>

标签 c++ c++11 vector boolean

我正在为类编写康威生命游戏在环形线圈中的实现。函数cargarToroide (loadToroid) 应该将每个单元格 ( celda ) 的适当状态(活着或死亡 - True 或 False - 1 或 0)从文件加载到 vector 中,其签名如下:

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作为 boolean 值并仅使用

t[i].push_back(celda);

使用 C++11 解决此问题的最佳方法是什么?

最佳答案

您需要先调整外部 vector 的大小,然后才能对其使用运算符[]。读取数据时还应该使用正确的类型(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";
        }
    }
}

关于c++ - 写入 vector < vector <bool>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53165601/

相关文章:

C++11。是否可以这样做 : string foo = cout << (. 00000023)*(.00000023) << endl; -精确

c++ - 可变参数模板中的分支

c++ - C++ 11:将 vector 元素作为线程传递到线程函数中

c++ - 类成员初始化是在编译时还是运行时进行?

c++ - 以任意顺序传递参数的构造函数排列

C++ 模板函数 - 多种类型、默认值和...参数?

c++ - 查找数据集 C++ 模式的更好方法

c++ - vector::erase with pointer 成员

c++ - 将 SWI-Prolog 连接到 C++ 的问题

c++ - API 类似于 GLUTesselator?