首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >写入vector< vector<bool> >

写入vector< vector<bool> >
EN

Stack Overflow用户
提问于 2018-11-06 04:08:06
回答 2查看 665关注 0票数 2

我正在为课堂写一篇康威的“生命游戏”的圆圈实现。函数cargarToroide (loadToroid)应该将每个单元格(celda)的适当状态(活着或死-真或假-1或0)从文件加载到向量中,其签名如下:

代码语言:javascript
复制
toroide cargarToroide(string nombreArchivo, bool &status);

nombreArchivo是文件的名称,如果加载文件或文件格式有任何问题,status应该是假的。

数据结构是这样定义的(我不能更改它):

代码语言:javascript
复制
typedef vector< vector<bool> > toroide;

该文件的结构如下:

代码语言:javascript
复制
numberOfLines numberOfColums
list of the values of the cells
number of live cells

例如:

代码语言:javascript
复制
4 4
1 0 0 0
0 0 1 0
0 0 0 1
0 1 0 0
4

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

代码语言:javascript
复制
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定义为布尔值,并仅使用

代码语言:javascript
复制
t[i].push_back(celda);

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-06 05:11:15

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

代码语言:javascript
复制
t.resize(cantidadFilas);

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

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

代码语言:javascript
复制
vector<bool> row; 
t.push_back(row)
票数 1
EN

Stack Overflow用户

发布于 2018-11-06 05:39:29

在使用operator[]之前,需要调整外部向量的大小。在读取数据时,还应该使用正确的类型(bool),并检查输入文件中是否有错误。我在代码中评论过:

代码语言:javascript
复制
#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;
}

使用改进的签名:

代码语言:javascript
复制
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";
        }
    }
}

使用您被迫使用的签名:

代码语言:javascript
复制
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";
        }
    }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53165601

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档