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:
If you like concision - here's another option that (ab?)uses &&
to get cout
done only when a number's been parsed successfully, and when a number isn't parsed it uses the comma operator to be able to clear()
stream error state inside the conditional before reading a character to be ignored...
#include
#include
#include
int main()
{
std::istringstream iss("2.832 1.3067 nana1.678 x-1E2 xxx.05 meh.ugh");
double num = 0;
char ignored;
while (iss >> num && std::cout << num << '\n' ||
(iss.clear(), iss) >> ignored)
;
}
http://ideone.com/WvtvfU