大家好,我有一个文件要读取到向量数组中。我检查了其他几篇文章,了解到我的意思。我一直遇到错误,不允许我使用put_back()函数插入字符串。我不断收到char错误。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using std::vector;
using namespace std;
string outPutFileName;
vector<vector<string> > array2D;
#define HEIGHT 32
#define WIDTH 9
int main() {
string x;
string line;
string filename;
ifstream infile;
infile.open("file.txt");
if (infile.fail()) {
cerr << " The file you are trying to access cannot be found or opened";
exit(1);
}
array2D.resize(HEIGHT);
for (int i = 0; i < HEIGHT; ++i) {
array2D[i].resize(WIDTH);
}
while (getline(infile, line)) {
istringstream streamA(line);
while (streamA >> x) {
for (int row = 0; row < HEIGHT; row++) {
for (int col= 0; col < WIDTH; col++) {
array2D[row][col].push_back(x);
col++;
}
row++;
}
}
}
for (int i = 0; i <HEIGHT; i++) {
for (int j = 0; j <WIDTH; j++) {
cout << array2D[i][j] << " ";
}
cout << endl;
}
return 0;
}
类型array2D[row][col]
为std::string
,您正尝试调用push_back
该向量,而不是调用其中一个向量。您可能的意思是:
array2D[row][col] = x;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句