using lexical_cast

S

Sarath

I've a doubt on using lexical_cast

suppose if I have a string as follows.

string s( "123 456 789 1011 1213");

Can i separate the each numbers separated by space using lexical_cast?
I think it's not possible like it. Do we need to manually separate
each numbers using stringstream or any other parser classes?
 
S

sonison.james

I've a doubt on using lexical_cast

suppose if I have a string as follows.

string s( "123 456 789 1011 1213");

Can i separate the each numbers separated by space using lexical_cast?
I think it's not possible like it. Do we need to manually separate
each numbers using stringstream or any other parser classes?

I think you are talking about boost::lexical_cast. If you are, you
could use the boost tokenizer (http://www.boost.org/libs/tokenizer/
char_separator.htm)

Thanks and regards
SJ
 
S

Sarath

I am interested to know, what's the advantage of using lexical_cast
and tokenizeer than the old style C++ stream operators to extract
values?

Regards,
Sarath
 
M

Michael DOUBEZ

Sarath a écrit :
I've a doubt on using lexical_cast

suppose if I have a string as follows.

string s( "123 456 789 1011 1213");

Can i separate the each numbers separated by space using lexical_cast?
I think it's not possible like it. Do we need to manually separate
each numbers using stringstream or any other parser classes?

stringstream is a safe bet and it is what lexical_cast does ,or did not
so long ago (I think there are some optimisation today).

Michael
 
J

Joe Greer

I am interested to know, what's the advantage of using lexical_cast
and tokenizeer than the old style C++ stream operators to extract
values?

Absolutely none. That is not what it's for. Much like any other cast,
lexical_cast is for converting a value to some other type. That is:

int i = 5;

std::string iAsString = lexical_cast<std::string>(i);

double x = lexical_cast<double>(iAsString);

See? It allows you to treat conversion to a from a character
representation by casting rather than invoking some other function or
declaring a stringstream yourself. Any effects it may have along the
parsing line is completely coincidental. Many/most implementations of
lexical_cast use a streamstream internally to do the conversion.

Hope that helps,
joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top