无法将带有布尔网格的文本文件读取为向量的向量
Unable to read text file with boolean grid into vectors of vectors
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
- 在 z3 中使用位向量文本
- 遍历对象向量,并找到与从文本文件中提取的对象匹配的变量
- 通过将文本文件读取为字符串/向量来计算加权/未加权 GPA
- 来自文本文件输入 c++ 的向量
- 无法将带有布尔网格的文本文件读取为向量的向量
- 将C++对的向量写入文本文件
- 如何将文本文件行读为向量
- 使用 C++ 中的向量在新行上显示文本文件中的列
- C - 将文本文件读为一串单词,然后将字符串分为向量
- 试图从文本文件中读取向量
- 如何使用两列C 从文本文件中填充向量
- 将文本文件中的行解析为向量
- 使用C 中的向量从文本文件中读取矩阵
- 如何将文本文件的每一行分配给新向量
- 使用getline()从文本文件中读取行,然后将push_back读取到对象的向量中
- C++ 从文本文件数据类型读取为结构,并将数据存储在列表的向量中
- 如何从文本文件中读取数据并将其加载到向量中
- 从文本文件读取到类中的对象向量 - 在另一个 txt 文件中编写
- 如何将字符串从文本文件读取到要搜索的向量中
- c++如何从文本文件中的表中生成多维向量