Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > typecast int to string

Reply
Thread Tools

typecast int to string

 
 
Venkat
Guest
Posts: n/a
 
      01-07-2004
Hi All,

I want to typecast int to std::string how can i do it.

Here is the sample code.

int NewList[500];

//Fill the NewList with integers values.
.......
.......

//Replace the file contents with new list values at a specified location.

int i=0;
std::string line;
ifstream inFile(sample);//opens a file to read
while (getline (inFile, line) )
{

int comma1Pos = line.find(',');
int comma2Pos = line.find(',', comma1Pos+1);

int numChars = comma2Pos - comma1Pos - 1;

line.erase(comma1Pos+1, numChars); //
line.insert(comma1Pos+1, (std::string)NewList[i]);
i++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList[i] to string.
The complier throws a type cast error saying type cast int to string is not
possible.

Is there any way i can type cast the same.


regards,
Venkat




 
Reply With Quote
 
 
 
 
Hendrik Belitz
Guest
Posts: n/a
 
      01-07-2004
Try something like

> int i=0;
> std::string line;
> ifstream inFile(sample);//opens a file to read
> while (getline (inFile, line) )
> {
>
> int comma1Pos = line.find(',');
> int comma2Pos = line.find(',', comma1Pos+1);
>
> int numChars = comma2Pos - comma1Pos - 1;
>
> line.erase(comma1Pos+1, numChars); //

std:stringstream os;
os << NewList[i];
line.insert(comma1Pos+1, os.str());
> i++;
> }
> inFile.close();
>


--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
 
Reply With Quote
 
 
 
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-07-2004
On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:

> Hi All,
>
> I want to typecast int to std::string how can i do it.
>
> Here is the sample code.
>
> int NewList[500];
>
> //Fill the NewList with integers values.
> ......
> ......
>
> //Replace the file contents with new list values at a specified location.
>
> int i=0;
> std::string line;
> ifstream inFile(sample);//opens a file to read
> while (getline (inFile, line) )
> {
>
> int comma1Pos = line.find(',');
> int comma2Pos = line.find(',', comma1Pos+1);
>
> int numChars = comma2Pos - comma1Pos - 1;
>
> line.erase(comma1Pos+1, numChars); //
> line.insert(comma1Pos+1, (std::string)NewList[i]);
> i++;
> }
> inFile.close();
>
> In the above code the function insert takes 2nd argument as a string so i
> was trying to type cast NewList[i] to string.
> The complier throws a type cast error saying type cast int to string is not
> possible.
>
> Is there any way i can type cast the same.


No, that is not what casting is about. Casting can change something to
something related. Although for humans integers and their
string-representations may be related, for computers they are very
different.

As a side note, you should never use C-style casts in C++, C++ has much
better casts: static_cast<>, dynamic_cast<>, const_cast<> and
reinterpret_cast<>. Familiarize yourself with those and never use the
C-style casts again. It will save you a lot of grief.

So the question now becomes, how to convert a number to a string. Thee are
a number of ways to do so, the easiest and most C++ish:

#include <sstream>

std::string toString(int i)
{
std::stringstream s;
s << i;
return s.c_str();
}

OK, this works, but maybe you want to use this for unsigned ints as well.
Or for longs. You could create the same function multiple times,
overloading on the argument:

std::string toString(long) { ... }
std::string toString(unsigned int) { ... }
std::string toString(unsigned long) { ... }
std::string toString(float) { ... }
std::string toString(double) { ... }

Fortunately, there is an easier way. We can get the compiler to do it for
us by using the magic of templates:

template<typename T>
std::string toString(T t)
{
std::stringstream s;
s << t;
return s.c_str();
}

This will make the compiler produce all of the above functions
automagically, but only the ones we actually use! (Do note that this code
must be 'seen' by the compiler before you use it, you cannot just use a
prototype and define the function in another C++ file. So this typically
goes in some header.)

So if you use toString(i), where i is an integer, the compiler will
substitute int for T and we end up with exectly the same as above. But if
we use toString(l), where l is a long, the compiler automagically
generates the above for a long. Templates can be so incredibly powerful!

HTH,
M4


 
Reply With Quote
 
Evan Carew
Guest
Posts: n/a
 
      01-07-2004
Venkat,

Probably, you want to try looking at Boost's lexical cast facility. The
boost designers (a free add on to the STL) have provided save means to
do what you are asking. so see an example, take a look at

http://www.boost.org/libs/conversion/lexical_cast.htm

Evan Carew

Venkat wrote:
> Hi All,
>
> I want to typecast int to std::string how can i do it.
>
> Here is the sample code.
>
> int NewList[500];
>
> //Fill the NewList with integers values.
> ......
> ......
>
> //Replace the file contents with new list values at a specified location.
>
> int i=0;
> std::string line;
> ifstream inFile(sample);//opens a file to read
> while (getline (inFile, line) )
> {
>
> int comma1Pos = line.find(',');
> int comma2Pos = line.find(',', comma1Pos+1);
>
> int numChars = comma2Pos - comma1Pos - 1;
>
> line.erase(comma1Pos+1, numChars); //
> line.insert(comma1Pos+1, (std::string)NewList[i]);
> i++;
> }
> inFile.close();
>
> In the above code the function insert takes 2nd argument as a string so i
> was trying to type cast NewList[i] to string.
> The complier throws a type cast error saying type cast int to string is not
> possible.
>
> Is there any way i can type cast the same.
>
>
> regards,
> Venkat
>
>
>
>


 
Reply With Quote
 
The Directive
Guest
Posts: n/a
 
      01-08-2004
[Snip]

> #include <sstream>
>
> std::string toString(int i)
> {
> std::stringstream s;
> s << i;
> return s.c_str();
> }
>


Shouldn't "std::stringstream s" be "std:stringstream s"? Which one is better? Why?

--The Directive

> OK, this works, but maybe you want to use this for unsigned ints as well.
> Or for longs. You could create the same function multiple times,
> overloading on the argument:
>
> std::string toString(long) { ... }
> std::string toString(unsigned int) { ... }
> std::string toString(unsigned long) { ... }
> std::string toString(float) { ... }
> std::string toString(double) { ... }
>
> Fortunately, there is an easier way. We can get the compiler to do it for
> us by using the magic of templates:
>
> template<typename T>
> std::string toString(T t)
> {
> std::stringstream s;
> s << t;
> return s.c_str();
> }
>
> This will make the compiler produce all of the above functions
> automagically, but only the ones we actually use! (Do note that this code
> must be 'seen' by the compiler before you use it, you cannot just use a
> prototype and define the function in another C++ file. So this typically
> goes in some header.)
>
> So if you use toString(i), where i is an integer, the compiler will
> substitute int for T and we end up with exectly the same as above. But if
> we use toString(l), where l is a long, the compiler automagically
> generates the above for a long. Templates can be so incredibly powerful!
>
> HTH,
> M4

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-08-2004
On Wed, 07 Jan 2004 18:37:07 -0800, The Directive wrote:

> [Snip]
>
>> #include <sstream>
>>
>> std::string toString(int i)
>> {
>> std::stringstream s;
>> s << i;
>> return s.c_str();
>> }
>>
>>

> Shouldn't "std::stringstream s" be "std:stringstream s"? Which one is
> better? Why?


Yes.. Somehow I have a problem remembering which stringstream to use,
while I now find it obvious. I thought about it, but did not want any risk
of getting it wrong. Stupid of me really.

M4
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      01-08-2004
Martijn Lievaart wrote:

> On Wed, 07 Jan 2004 18:37:07 -0800, The Directive wrote:
>
>> [Snip]
>>
>>> #include <sstream>
>>>
>>> std::string toString(int i)
>>> {
>>> std::stringstream s;
>>> s << i;
>>> return s.c_str();
>>> }
>>>
>>>

>> Shouldn't "std::stringstream s" be "std:stringstream s"? Which one
>> is better? Why?

>
> Yes.. Somehow I have a problem remembering which stringstream to use,
> while I now find it obvious. I thought about it, but did not want any
> risk of getting it wrong. Stupid of me really.


I was always wondering why there is an istringstream and an
ostringstream if a stringstream already does what both can do. So
what's the actual advantage of using an ostringstream over a
stringstream?

 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-08-2004
On Thu, 08 Jan 2004 10:48:40 +0100, Rolf Magnus wrote:

> I was always wondering why there is an istringstream and an
> ostringstream if a stringstream already does what both can do. So
> what's the actual advantage of using an ostringstream over a
> stringstream?


I'm guessing, efficiency. An istream and an ostream both need to maintain
state, so you'll shave of a few bytes and operations by using the best
suitable class.

Also, on implementations that link complete objects (I guess most
implementations do this), as opposed to linking only the parts of an
object you need, you might link in much more than you actually need.

HTH,
M4

 
Reply With Quote
 
Gavin Deane
Guest
Posts: n/a
 
      01-08-2004
Martijn Lievaart <(E-Mail Removed)> wrote in message news:<(E-Mail Removed) art.rtij.nl>...
> On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:


<snip>

> template<typename T>
> std::string toString(T t)
> {
> std::stringstream s;
> s << t;
> return s.c_str();


ITYM
return s.str();

> }


--
GJD
 
Reply With Quote
 
Martijn Lievaart
Guest
Posts: n/a
 
      01-08-2004
On Thu, 08 Jan 2004 04:26:12 -0800, Gavin Deane wrote:

> Martijn Lievaart <(E-Mail Removed)> wrote in message
> news:<(E-Mail Removed) art.rtij.nl>...
>> On Wed, 07 Jan 2004 19:39:14 +0530, Venkat wrote:

>
> <snip>
>
>> template<typename T>
>> std::string toString(T t)
>> {
>> std::stringstream s;
>> s << t;
>> return s.c_str();

>
> ITYM
> return s.str();
>
>> }


Yup, thanks.

M4
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
map question - need to find a string from an int but also need the int from the string? Angus C++ 3 05-03-2008 02:27 PM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
Typecast illegal from int to pointer? Ulrich Hobelmann C++ 2 06-04-2006 05:13 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments