integer to string conversion

S

sam

Hi,

Does STL has Integer class?
I want to convert an integer to string.
Of course I can use sprintfs C API, but I m wondering whether STL has
Integer class to be utilsed.

Thanks
Sam.
 
C

Chris \( Val \)

| Hi,
|
| Does STL has Integer class?
| I want to convert an integer to string.
| Of course I can use sprintfs C API, but I m wondering whether STL has
| Integer class to be utilsed.

There is no 'Integer' class in C++, however you can write
one yourself.

C++ has std::stringstream(s) to do the job for you.

Look up the many examples posted on usenet in the past.
You'll want to use an 'std::eek:stringstream' object.

Cheers,
Chris Val
 
R

Rolf Magnus

sam said:
Hi,

Does STL has Integer class?

No. There is no need for one.
I want to convert an integer to string.

Use std::istringstream for that.

std::istringstream stream;
int i = 4;
stream << i;
std::string s = stream.str();
 
A

Angad

Have you tried the itoa() library function? It return char * to string
for the corresponding integer. It's arguments include destination
string, number to be converted, and base of the number. (I don't
remember the order).
 
A

Artie Gold

Angad said:
Have you tried the itoa() library function? It return char * to string
for the corresponding integer. It's arguments include destination
string, number to be converted, and base of the number. (I don't
remember the order).
There *is* no such function in standard C++.

--ag
 
R

Ron Natalie

Angad said:
Have you tried the itoa() library function? It return char * to string
for the corresponding integer. It's arguments include destination
string, number to be converted, and base of the number. (I don't
remember the order).
There is no such function in the standard C or C++ library.
 
J

Jan Eickmann

Hi,

take a look at boost::lexical_cast. It wraps the entire stringstream
idea and as far as I know it's header-only, so no need to build the lib.

Jan Eickmann
 
K

Karl Heinz Buchegger

Angad said:

It doesn't matter that on some systems there is a function
called 'itoa'.

It is not available on all systems. This group is about *standard*
C++. That is: The language as defined by the corresponding ISO document.
And in those document no function 'itoa' is mentioned.

When working in these group, please restrict yourself to recommend
only things that are described in the language standard.
Thank you.
 
A

Angad

Ok guys, my mistake. It won't happen again. You see, I'm an undergrad,
not fully aware of *standard* c++. So pardon me.
Could anyone give a link to the ISO document defining standard C++?
 
C

Chris Croughton

Ok guys, my mistake. It won't happen again. You see, I'm an undergrad,
not fully aware of *standard* c++. So pardon me.

You might ask your instructors why they aren't teaching standard C++,
and also ask in your library (assuming universities still have libraries
these days, they did when I was an undergrad 30 years ago).
Could anyone give a link to the ISO document defining standard C++?

Go to http://webstore.ansi.org/ansidocstore/ and search for 14882 (the
ISO/IEC number for the document). They have it for download as a PDF
for US$18 (the usage requirements are very strict, though).

Stroustrup's "The C++ Standard: Incorporating Technical Corrigendum No. 1"
is available from Amazon.com price $56.11 and contains the standard up
to 2003 in a hardcover edition published by Wiley (normal price $70).
ISBN 0470846747, if your library wants to order it...

Chris C
 

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,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top