This program refuses to take foom as input, yet if I were to put "matrix.txt" into the function instead of foom, it would work fine.

#include <fstream>
#include <iterator>
#include <sstream>
#include <vector>
#include <string> // needed?

using namespace std;

typedef vector<double> Vec;
typedef vector<Vec> Mat;

Mat readMatrix(string foom){
Mat x;
    ifstream in(foom);
    if (in) {
        for (string line; getline(in, line);) {
            istringstream iss(line);
 
            x.push_back(Vec());
            copy(istream_iterator<double>(iss), 
                istream_iterator<double>(), 
                back_inserter(x.back()));
        }
    }
return x;
}

Thanks so much! Quick and painless! Also, it seems that #include <string> is not needed for the program to run. Is <string> only for some advanced string functions?

Thanks so much! Quick and painless! Also, it seems that #include <string> is not needed for the program to run. Is <string> only for some advanced string functions?

include <string> whenever you need to use std::string.

This question has already been answered. Start a new discussion instead.