簡體   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