Re: convert string into an array
On Jul 3, 10:46 pm, "Jim Langston" <tazmas...@rocketmail.com> wrote:
I would use a stringstream.
Just curious, but why a stringstream, and not an istringstream?
[...]
std::string str_array = "{201,23,240,56,23,45,34,23}";
std::stringstream Buffer;
Buffer << str_array;
Why those two lines, instead of simply:
std::istringstream Buffer( str_array ) ;
char Trash;
Buffer >> Trash; // Throw away (
Throw away, or verify, as a syntax check? (In my experience,
you can never verify input too much.)
int Value;
std::vector<int> Data;
while ( Buffer >> Value )
while ( Buffer >> Value && Trash != '}' )
{
And
if ( Trash != (Data.empty() ? '{' : ',') ) {
Buffer.setstate( std::ios::failbit ) ;
}
here.
Data.push_back( Value );
Buffer >> Trash; // Throw away , or )
}
And a final error check:
if ( ! (Buffer >> std::ws && Buffer.get() == EOF ) {
error ...
}
// Display vector data
for ( std::vector<int>::iterator it = Data.begin(); it != Data.en=
d();
++it )
std::cout << *it << " ";
}
(But I like your basic algorithm better than the one I
suggested.)
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34