Can you tell iostreams which characters to treat as whitespace?
Question
So that you could do something like this, for instance:
std::string a("01:22:42.18");
std::stringstream ss(a);
int h, m, s, f;
ss >> h >> m >> s >> f;
Which normally requires the string to be formatted "01 22 42 18"
.
Can you modify the current locale directly to do this?
OTHER TIPS
I don't think you can change the default delimiter without creating a new locale, but that seems hackish. What you can use do is use getline with a third parameter specifying the delimiter character or you could read the delimiters and not do anything with them (e.g. ss >> h >> d >> m >> d >> s >> d >> f).
You could also write your own parsing class that handles splitting strings for you. Or better yet, use boost::split from Boost's String Algorithms Library.
You can do this by creating a locale with a ctype facet classifying :
as whitespace.
Jerry Coffin explains how you can specify whitespace characters in this answer to another question.
char c;
if (!(ss >> h >> c) || c != ':') return;
if (!(ss >> m >> c) || c != ':') return;
if (!(ss >> s >> c) || c != '.') return;
if (!(ss >> f) || ss >> c) return;