[英]Populate vector of vectors from input file?
I'm trying to populate a vector of vectors from an input file. 我正在尝试从输入文件填充向量的向量。
#include<fstream>
#include<vector>
#include<iostream>
using namespace std;
int main(){
ofstream inputFile;
inputFile.open("input.dat");
if(!inputFile){
cout << "Error in reading" << "input" << endl;
return 0;
}
vector<vector<double> > A;
double element;
vector<double> temp;
while(inputFile << element){
temp.push_back(element);
A.push_back(temp);
}
int len = A.size();
int i, j;
for(i=0; i<len; i++){
for(j=0; j<len; j++){
cout << A[i] <<"\n";
}
}
return 0;
}
I'm using this as a test file, so I am trying to print the matrix A. Any help would be really appreciated. 我将其用作测试文件,因此尝试打印矩阵A。非常感谢您的帮助。
test.cpp:33:9: error: no match for ‘operator<<’ (operand types are
‘std::ostream {aka std::basic_ostream<char>}’ and ‘std::vector<double>’)
cout << A[i] <<"\n";
Change the while loop to this: 将while循环更改为此:
while(inputFile >> element){
temp.push_back(element);
A.push_back(temp);
}
And in the nested for loop do cout<<A[i][j]<<endl;
并在嵌套的for循环中执行cout<<A[i][j]<<endl;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.