473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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

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

What is the best way to do this?

Aug 8 '05 #1
8 1440
br**************@hotmail.com wrote:
I have an integer. I need to return it as a string.

What is the best way to do this?


Using 'ostringstream', probably. Read the FAQ.
Aug 8 '05 #2
Monday 08 of August 2005 16:05, br**************@hotmail.com :
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" ? ;-)

--
empty
Aug 8 '05 #3
On 8 Aug 2005 07:05:34 -0700, <br**************@hotmail.com> wrote:
I have an integer. I need to return it as a string.

What is the best way to do this?


one way is

int i;
string s = boost::lexical_cast<string>(i);

(www.boost.org)

Aug 8 '05 #4
ulrich wrote:
On 8 Aug 2005 07:05:34 -0700, <br**************@hotmail.com> wrote:
I have an integer. I need to return it as a string.

What is the best way to do this?


one way is

int i;
string s = boost::lexical_cast<string>(i);

Boost is not part of the C++ language at this time.

Brian
Aug 8 '05 #5
Boost is not part of the C++ language at this time.

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

ben
Aug 9 '05 #6

<br**************@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g44g2000cwa.googlegr oups.com...
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


Aug 9 '05 #7
In message <3l*************@individual.net>, Gernot Frisch
<Me@Privacy.net> writes

<br**************@hotmail.com> schrieb im Newsbeitrag
news:11**********************@g44g2000cwa.googleg roups.com...
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:


And a good thing too:

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


--
Richard Herring
Aug 9 '05 #8
benben wrote:
Boost is not part of the C++ language at this time.


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).

--
Mike Smith
Aug 9 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
10
by: Angus Leeming | last post by:
Hello, Could someone explain to me why the Standard conveners chose to typedef std::string rather than derive it from std::basic_string<char, ...>? The result of course is that it is...
7
by: Sims | last post by:
Hi, if i have a code const char * GetValue() { std::string szVectorValue = ...// get a std::string from the vector return szVectorValue.c_str(); }
14
by: brad | last post by:
I've got a multithreaded application using std::string in Linux. Performance is not very good so I ran Quantify(tm) to look at what is happening. Most of the time my app was calling...
22
by: Michael Rasmussen | last post by:
Hi all, Not sure if this is OT? I have a function in a library written in C++ which returns a strdup(s.c_str()) to an application written i C. Running Valgrind on my C-application shows this...
2
by: FBergemann | last post by:
if i compile following sample: #include <iostream> #include <string> int main(int argc, char **argv) { std::string test = "hallo9811111z"; std::string::size_type ret;
1
by: TBass | last post by:
Quick and stupid question: I want to replace a function: const char * Address() with std::string Address()
12
by: Eric Kaplan | last post by:
I have a function like this ============= bool Table::Get(char* FieldName, char* FieldValue) and I call the function like the follow ============= while(!tbl.ISEOF()) { char id2;...
8
by: Edson Manoel | last post by:
I have some C++ unmanaged code that takes std::string& arguments (as reference), and fills them (possibly growing the string). I want to call this code through PInvoke (DllImport), possibly using...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.