문제

The file format looks like:

A 1 2 3
B 1 2 3
...

And I want to read them in a vector of struct Observation that looks like:

struct Observation {
    string category;
    vector<int> variables;
};

How can I read them? I've searched for some solutions here, and tried:

int main() {
    ifstream infile("data.txt");
    string line;
    string category;
    vector<Observation> obs;
    int i = 0;
    while(getline(infile, line)) {
        istringstream iss(line);
        iss >> category;
        int j = 0;
        int value;
        while(iss >> value) {
            obs[i].variables[j] = value;
            j++;
        }
        i++;
    }
}

but it says: error: variable ‘std::istringstream iss’ has initializer but incomplete type my compilier is g++ 4.6.3 on Ubuntu 12.04, complie with command "g++ -std=c++0x io.cpp -g -o io", please help me with that, thanks!

도움이 되었습니까?

해결책

Try including <sstream> and also change the way you add to the vector:

while(iss >> value) {
    obs[i].variables.push_back(value);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top