[英]Unable to read text file with boolean grid into vectors of vectors
我有一个文本文件,该文件由布尔型网格组成,如图所示。 现在,我试图将文本文件读入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,在错误时返回false。
在您的情况下, iss >> tempb
将失败,因为它期望一个布尔值(即,一个位),而是接收到0和1的字符串。
您可以在首先阅读iss >> tempb
之后进行检查,
if (iss.fail()) {
cout << "Failed to read\n";
}
您可以改为逐个遍历字符。
// 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 ++方法。
这个想法是我们想读取一个带有布尔值的行。 因此,我设计了一个新的数据类型,一个类,其中包含此类数据,并且知道如何读取它。 在我看来,数据和方法应该打包在一个类中。
这也将大大减少函数main和总体中的代码行。 在变量的定义中,并通过范围构造器,将读取所有数据。
我添加了其他一些调试输出,以便可以看到结果。 当然,使用索引运算符[][]
访问数据也将起作用。
请参见:
#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;
}
请注意:我确实从用原始字符串初始化的istringstream中读取。 因此,与从文件读取没有区别。
也许有人认为此解决方案很有帮助。
错误“退出状态-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完成
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.