I am interested in discussing methods for using stringstream
to parse a line with multiple types. I would begin by looking at the following line:
Few minor differences to πάντα ῥεῖ's answer - makes it also handle e.g. negative number representations etc., as well as being - IMHO - a little simpler to read.
#include
#include
#include
int main()
{
std::istringstream iss("2.832 1.3067 nana1.678 x-1E2 xxx.05 meh.ugh");
double num = 0;
for (; iss; )
if (iss >> num)
std::cout << num << '\n';
else if (!iss.eof())
{
iss.clear();
iss.ignore(1);
}
}
Output:
2.832
1.3067
1.678
-100
0.05
(see it running here)