typecast int to string

V

Venkat

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++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList 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
 
H

Hendrik Belitz

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::eek:stringstream os;
os << NewList;
line.insert(comma1Pos+1, os.str());
 
M

Martijn Lievaart

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++;
}
inFile.close();

In the above code the function insert takes 2nd argument as a string so i
was trying to type cast NewList 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
 
T

The Directive

[Snip]
#include <sstream>

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

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

--The Directive
 
M

Martijn Lievaart

[Snip]
#include <sstream>

std::string toString(int i)
{
std::stringstream s;
s << i;
return s.c_str();
}
Shouldn't "std::stringstream s" be "std::eek: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
 
R

Rolf Magnus

Martijn said:
[Snip]
#include <sstream>

std::string toString(int i)
{
std::stringstream s;
s << i;
return s.c_str();
}
Shouldn't "std::stringstream s" be "std::eek: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?
 
M

Martijn Lievaart

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
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top