我正在尝试将真值表复制到二维 vector 中。第一个数字是表的输入数,所以我输入第一行,然后开始填充 vector 。这是来自 sample.txt 的真值表:
4
0 0 0 0 1
0 0 0 1 0
0 0 1 0 1
0 0 1 1 1
0 1 0 0 0
0 1 0 1 1
0 1 1 0 0
0 1 1 1 0
1 0 0 0 1
1 0 0 1 0
1 0 1 0 0
1 0 1 1 1
1 1 0 0 1
1 1 0 1 0
1 1 1 0 1
1 1 1 1 0
这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <math.h>
#include <vector>
#include <fstream>
using namespace std;
string getFileName();
string userFile;
fstream file;
int width;
vector < vector <int> >newArray; // 2d array as a vector of vectors
int main() {
vector < vector <int> >array; // 2d array as a vector of vectors
int row = 0; // Row counter
int col = 0;
int count = 0;
int add = 0;
userFile = getFileName();
// Read file
file.open(userFile.c_str(), ios::in); // Open file
if (file.is_open()) { // If file has correctly opened...
// Output debug message
cout << "File correctly opened" << endl;
string nLine;
string line;
getline(file, nLine);
width = atoi(nLine.c_str());
cout << "Width: " << width << endl;
// Dynamically store data into array
while (getline(file, nLine)) { // ... and while there are no errors,
vector <int> rowVector(width-1); // vector to add into 'array' (represents a row)
array.push_back(rowVector); // add a new row,
cout << "row" << row <<": ";
col = 0;
for(std::string::size_type i = 0; i < nLine.size(); ++i) { //iterate string
if(nLine[i] == '1'){
array[row][col] = 1;
cout << " Col " << col << ": " << array[row][col];
col++;
}
else if(nLine[i] == '0'){
array[row][col] = 0;
cout << " Col " << col << ": " << array[row][col];
col++;
}
else{ //skip spaces
}
}
cout << endl;
row++; // Keep track of actual row
}
}
else cout << "Unable to open file" << endl;
file.close();
cout << "Copied" << endl;
for(int i = 0; i < row; i++){
for(int j = 0; j <= width; j++){
cout << array[i][j] << " ";
}
cout << endl;
}
//erase lines that have output of 0
// for(int i = 0; i < row; i++){
// if(array[i][width] == 0){
// cout << i << " " << array[i][width]<< endl;
// array.erase(array.begin() + i);
// count++;
// }
// }
row -= count;
cout << "New rows: " << row << endl;
return 0;
}
string getFileName(){
cout << "Enter file name pls: " << endl;
cin >> userFile;
file.open(userFile.c_str(), ios::in); // Open file
if (file.is_open()) { // If file has correctly opened...
cout << "File correctly opened" << endl;
}
else {
cout << "Unable to open file" << endl;
getFileName();
}
file.close();
return userFile;
}
这是执行的屏幕截图:
当我将行和列放入 vector 中输出时,行和列似乎都是正确的,但是当我返回时,前 8 行左右完全错误,其余的都是正确的...我是不知道为什么。我在关闭文件(第 67 行)后立即打印它,在 if(file.is_open()) 语句之后。
提前感谢您的帮助, 一切顺利
最佳答案
在这一行
vector <int> rowVector(width-1); // vector to add into 'array' (represents a row)
你声明 rowVector
的大小是 width-1
,在这个测试用例中你的 width
是 4
,所以 rowVector
的大小是 3。但是您的测试用例有 5 列,0
或 1
。因此,您试图访问显示未定义行为的越界索引。
要解决此问题,请使 rowVector
大小等于 width+1
vector <int> rowVector(width+1); // vector to add into 'array' (represents a row)
关于c++ - 将真值表加载到二维 vector 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33272841/