无法将带有布尔网格的文本文件读取为向量的向量

Unable to read text file with boolean grid into vectors of vectors

本文关键字:向量 文本 文件 读取 网格 布尔      更新时间:2023-10-16

i有一个文本文件,该文件由如图所示的布尔语网格结构组成。现在,我试图将文本文件读为vector<vector<bool>> grid。但是我无法做到。我的代码正在退出而没有任何错误,并且执行不会在while循环内移动。

文本文件的示例以下:

00000000000000001111111110000
000000100000000010100000100
0000000000000000111111111000
00000000000000011111111111000
0001000000000011111111111110
00000000000000011000000011000
00000000000000100010001000100
00000000000000100000100000
00100011111111111111111001110
00000000000011111000100000001
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
vector<vector<bool> >read_grid(const string &filename)
{
    vector<vector<bool> > gridvector;
    // Open the File
    ifstream in(filename.c_str());
    string str;
    bool tempb;// Check if object is valid
    if(!in)
    {
        cout<< "Cannot open the File : "<<filename<<endl;
        return gridvector;
    }
    // Read the next line from File untill it reaches the end.
    while (getline(in, str))
    {
        istringstream iss(str);
        vector<bool> myvector;
        while(iss>>tempb)
        {
            myvector.push_back(tempb);
        }
        gridvector.push_back(myvector);
    }
    //Close The File
    in.close();
    return gridvector;
}
 void display_grid(vector< vector<bool> >& grid) 
{
// this generates an 8 x 10 grid and sets all cells to ’0’
//vector<vector<bool> >grid(8, vector<bool>(10, 1));// printing the grid
    for(int x = 0; x < grid.size(); x++)
    {
        for(int y = 0;y < grid[x].size();y++)
        {
         // cout<<grid[x].size()<<'n';
            cout << grid[x][y];
        }
        cout << endl;
    }
   cout<<"grid at position [1][2] is: "<< grid[1][2]<<'n';
}
int main ()
{
    const string b_file = "intial_grid.txt";
    vector< vector<bool> > grid_copy = read_grid(b_file);
    display_grid(grid_copy);
    return 0;
}

它正在以"退出状态-1"退出。

字符串流在成功的读取时返回true,而错误的错误。

在您的情况下,iss >> tempb会因为期望布尔值而失败,即有点,但会收到0s和1s的字符串。

您可以在第一次阅读iss >> tempb之后检查一下,

if (iss.fail()) {
    cout << "Failed to readn";
}

您可以单独迭代字符。

// Read the next line from File untill it reaches the end.
    while (getline(in, str))
    {
        istringstream iss(str);
        vector<bool> myvector;
        char bit;
        while(iss >> bit)
        {
            myvector.push_back(bit == '1' ? true : false);
        }
        gridvector.push_back(myvector);
    }

答案被给出并接受。这些错误已在评论中提到。

无论如何,我想使用std算法显示一种"更多" C 方法。

这个想法是我们想阅读带有布尔值的线条。因此,我设计了一种新的数据类型,一个类别,其中包含此类数据,并且也知道如何读取它。以我的拙见,数据和方法应包装在班级中。

这还将大大减少函数和整体函数中的代码线。在变量的定义和通过范围构造函数的定义中,将读取所有数据。

我添加了其他一些调试输出,以便可以看到结果。当然,使用索引操作员[][]访问数据也将起作用。

请参阅:


#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <sstream>
std::istringstream testData(
R"#(00000000000000001111111110000
000000100000000010100000100
0000000000000000111111111000
00000000000000011111111111000
0001000000000011111111111110
00000000000000011000000011000
00000000000000100010001000100
00000000000000100000100000
00100011111111111111111001110
00000000000011111000100000001
)#");

// We want to have a data type for one line with boolean values in a string
struct Line {
    // We overwrite the extractor operator >> . With that we can easily read a complete line
    friend std::istream& operator >> (std::istream& is, Line& l) { 
        std::string line{}; l.lineOfBool.clear(); getline(is, line);
        std::transform(line.begin(), line.end(), std::back_inserter(l.lineOfBool), [](const char c) { return (c == '1') ? true : false; });
        return is; }
    // Type cast operator to expected value
    operator std::vector<bool>() const { return lineOfBool; }
    // The data
    std::vector<bool> lineOfBool{};
};
int main()
{
    // Define the variable that will hold all bool data of the complete file. The range constructor will read the file 
    std::vector<std::vector<bool>> fileAsStrings{std::istream_iterator<Line>(testData),std::istream_iterator<Line>() };
    // For debug purposes: Copy all Data to std::cout
    std::for_each(fileAsStrings.begin(), fileAsStrings.end(), [](const std::vector<bool> & l) {std::copy(l.begin(), l.end(), std::ostream_iterator<bool>(std::cout, " ")); std::cout << 'n'; });
    return 0;
}

请注意:我确实从iSringstream中读取,并用原始字符串启动。因此,从文件中读取没有区别。

也许有人发现此解决方案有帮助。

错误'exit状态-1'引起 display_grid((函数,组件是向量,对向量的访问为 verctorVariable.at();

另一个错误是 while(iss>>tempb),因为您的结果是所有行,您可以使用此代码解决此问题

istringstream iss(str);
vector<bool> myvector;
string value;
iss >> value;
cout << value;
for(char c : value){
   cout << "**** debug print: " << c << endl;
   myvector.push_back((bool)(c-'0'));
}
gridvector.push_back(myvector);

另外,您的方法 display_grid((应该是此

void display_grid(vector< vector<bool> >& grid)
{
// this generates an 8 x 10 grid and sets all cells to ’0’
//vector<vector<bool> >grid(8, vector<bool>(10, 1));// printing the grid
    for(int x = 0; x < grid.size(); x++)
    {
        cout<<"addin another elemen";
        for(int y = 0;y < grid[x].size();y++)
        {
             cout<<"addin another elemen";
            cout << grid.at(x).at(y);
        }
        cout << endl;
    }
    //cout<<"grid at position [1][2] is: "<< grid[1][2]<<'n';
}

此代码返回此退出代码用退出代码0

完成