繁体   English   中英

从输入文件填充向量的向量?

[英]Populate vector of vectors from 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;
}

我将其用作测试文件,因此尝试打印矩阵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";

将while循环更改为此:

while(inputFile >> element){
        temp.push_back(element);
        A.push_back(temp);
}

并在嵌套的for循环中执行cout<<A[i][j]<<endl;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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