c++ How to istream struct contains vector -
how istream struct contains vector of integer member, tried following code failed read file
struct ss{ std::vector<int> a; double b; double c; }; std::istream& operator>>(std::istream &is, ss &d) { int x; while (is >> x) d.a.push_back(x); >> d.b; >> d.c; return is; } std::vector <std::vector<ss >> aarr; void scandata() { ifstream in; in.open(fileinp); std::string line; while (std::getline(in, line)) { std::stringstream v(line); ss s1; std::vector<ss > inner; while (v >> s1) inner.push_back(std::move(s1)); aarr.push_back(std::move(inner)); } }
i did search similar problems not find one.
the immediate problem here same condition terminates while
loop prevents successive reads working. stream in error state. double
values never read.
actually, integer part of first double
read integer, leaving fractional part in stream. can't recover this.
one way solve might read in values strings, converting them integers using stoi
, putting them in vector. before integer conversion, check decimal point in string. if contains one, break out of loop. convert double values using stod
.
Comments
Post a Comment