简体   繁体   中英

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.

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(inputFile >> element){
        temp.push_back(element);
        A.push_back(temp);
}

And in the nested for loop do cout<<A[i][j]<<endl;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM