I have strings that are something like
3.4meq/kg/day
which I want to parse. The numbers on the front are critical
for later calculations. But I want to pull the descriptors
apart. I can use get() to do this, but if just seems so
sloppy.
This is a code snippet that extracts the numbers. Can't IYou can specify a delimitor when calling getline, but I'd tend
change the delimitor for the istringstream class?
to consider that a bit abuse. I'd just parse the string
directly, rather than using stringstream. Possibly with
boost::regex, but something as simple as the following should
work:
std::vector< std::string >
breakIntoFields(
std::string const& source,
char separator )
{
std::vector< std::string >
result ;
std::string::const_iterator
current = source.begin() ;
while ( current != source.end() ) {
std::string::const_iterator
next
= std::find( current, source,end(), separator ) ;
result.push_back( std::string( current, next ) ) ;
if ( next != source.end() ) {
++ next ;
}
}
return result ;
}
(BTW: the generally acceptable maximum size for a signature is
four lines.)
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34