Re: delimiter for istringstream
 
Z.L. wrote:
I have following input string:
char* s = "1.0:2.0:3.0  4.0 5.0".
I want the ":" serve as whitespace delimiter just like blank space,
therefore when excuting
istringstream iss(s);
double digit[5];
for (int i=0; i<5; ++i) iss >> digit[i];
",
I can put the double type data into the array.
I remember it seems a function for istream can do this job, but I am
not so sure about that.
What exactly I mean above is whether I can specify the whitespace
delimiter for istringstream. If yes, which function does that job?
It's possible to create a ctype facet which would do this.
Creating and installing a facet is far from trivial, however,
and I would consider using facets for this sort of thing
obfuscation as well---while one expects whitespace to depend on
the locale (e.g. 0xA0 is whitespace in an ISO 8859-1 locale, but
not in an ASCII locale), one expects it to be whitespace, and
not a ':', or other punctuation.
The simplest solution is just to define a manipulator, along the
lines of std::ws, which handles this correctly, and write:
     for ( i = 0 ; i < 5 ; ++ i ) {
         if ( i != 0 ) {
             iss >> separator ;
         }
         iss >> digit[ i ] ;
     }
The manipulator might look something like:
     std::ostream&
     separator( std::ostream& s )
     {
         std::ws( s ) ;
         if ( s && s.peek() == ':' ) {
             s.get() ;
             std::ws( s ) ;
         }
         return s ;
     }
--
James Kanze (Gabi Software)            email: james.kanze@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
-- 
      [ See http://www.gotw.ca/resources/clcm.htm for info about ]
      [ comp.lang.c++.moderated.    First time posters: Do this! ]