GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 10-Oct-2005, 22:39
monalin monalin is offline
Junior Member
 
Join Date: Sep 2005
Posts: 35
monalin is on a distinguished road

Is there a better way of converting Int to std::string


I'm curious if theres a better way of converting int data types into std::string. What I have works but i'm almost sure there's a better way of doing it. Let me know if you guys know of anything easier or any suggestions u have. Cause my way is somewhat limited.

I've tried this code and it works but its still kinda inefficiant I think its the only thing i could think of.

CPP / C++ / C Code:
#define BUFF 10

using namespace std;
int main()
{
	int i = 2001;
	int count;
	char ch[BUFF];
	string s;

	itoa(i,ch,BUFF);

	for(count=0;ch[count] != NULL; count++)
		s=s+ch[count];

	cout << s;
	return 0;
}
__________________
There are 10 kinds of people in this world. Those who speak binary and those who dont.
  #2  
Old 11-Oct-2005, 01:11
LuciWiz's Avatar
LuciWiz LuciWiz is online now
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 961
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough

Re: Is there a better way of converting Int to std::string


If you will include the string header (on some compilers there may not be the need) you can just assign the char array to the std::string, since there is an assignment operator defined for this:

CPP / C++ / C Code:
#include <string>
//...
/*
for(count=0;ch[count] != NULL; count++)
	s = s+ch[count];
*/
s = ch;

But if you don't want to use this C hack and do it the "C++ way", you can include sstream and use the stringstreams for conversion:

CPP / C++ / C Code:
#include <sstream>
using namespace std;

int main()
{
	int i = 2001;
	string s;
	ostringstream convStream;
	convStream << i;
	s = convStream.str();
	cout << s;
	return 0;
}

You can use stringstreams just like you use cin and cout. This method is actually a C++ mirror for the C sprintf method for conversion.

Best regards,
Lucian
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 11-Oct-2005, 01:18
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Is there a better way of converting Int to std::string


Hi monalin,

You can find more information about this in the following links:

Converting Int to a string 1
Converting int to string 2
Converting int to string 3
Converting int to string 4

Paramesh.
__________________

Don't walk in front of me, I may not follow.
Don't walk behind me, I may not lead.
Just walk beside me and be my friend.
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Issues converting Fahrenheit to celsius Skampy C++ Forum 7 25-Feb-2006 09:04
Converting a constant to a string Darth Predator C++ Forum 2 26-Sep-2005 18:11
Converting string to float CT++ C++ Forum 2 10-May-2005 12:29
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26
converting binary to integer... tru504187211 C++ Forum 3 10-Feb-2004 08:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:28.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.