Best way to return an int as a std::string

  • Thread starter brightoceanlight
  • Start date
B

brightoceanlight

I have an integer. I need to return it as a string.

What is the best way to do this?
 
K

Kuba_O

Monday 08 of August 2005 16:05, (e-mail address removed) :
I have an integer. I need to return it as a string.

#include<iostream>
#include<sstream>
using namespace ::std;
//-----------------------------------------------------
int main(int argc,char*argv[])
{
stringstream stream;
string str;
int i=911;
stream<<i;
str="int: ";
str+=stream.str();
str+=" inside string ;-)";
cout<<str<<endl;
return 0;
}

What is the best way to do this?
What is "the best way" ? ;-)
 
G

Gernot Frisch

I have an integer. I need to return it as a string.

What is the best way to do this?

something they really forgot when writing std::string is the
printf-function:

#include <superstring>
int main()
{
int integer = 42;
std::superstring str;
str.printf("%d", integer);
}

<sigh>
-Gernot
 
R

Richard Herring

Gernot Frisch said:
something they really forgot when writing std::string is the
printf-function:

And a good thing too:

#include <superstring>
int main()
{
double integer = 42;
std::superstring str;
str.printf("%d", integer);
}
 
M

Mike Smith

benben said:
Of course! Boost is a library and C++ language is a language...

Well, this NG is for discussion of the C++ *language*, not this or that
library that might be available for C++ (except for the Standard Library).
 

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

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top