Append an integer to an stl string

C

César

Hi,

I need to append an integer to a stl string. I´ve tried many solutions
and the only one that works is using the c functions 'itoa' or 'sprintf'.
Is there any "smarter" way to do this?

Thanks in advance.
 
J

Jeff Flinn

César said:
Hi,

I need to append an integer to a stl string. I´ve tried many solutions
and the only one that works is using the c functions 'itoa' or 'sprintf'.
Is there any "smarter" way to do this?

std:stringstream lStream; lStream << SomeInt;

MyString += lStream.str();

Or use boost::lexical_cast which basically packages up this into a single
function:

MyString += boos::lexical_cast<std::string>(SomeInt);

Jeff F
 
J

Jeff Flinn

Jeff Flinn said:
std:stringstream lStream; lStream << SomeInt;

Oops, should be std:eek:stringstream;
MyString += lStream.str();

Or use boost::lexical_cast which basically packages up this into a single
function:

MyString += boos::lexical_cast<std::string>(SomeInt);

Which I've just demonstrated is easier to remember than the above.
 
C

César

Thanks to all . At the end I´m going to use the "lexical_cast".
I´m also glad for the links you have written, they will ll be very useful
for me.

César.
 

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,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top