Thread: [Dev-C++] Integer to std::string?
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Jason H. <jas...@bt...> - 2001-09-21 03:31:27
|
Hi, I'm a bit rusty in my C++ and I just cannot seem to find a way of initialising a std::string with an integer value. While I can write a function to do it quite easily I had thought that there was a either a standard function or member of string that would do it, but I can't find one. I might be missing something with being tired of course. I know in future I will want to be doing similar with float's too, and I hope there is an easy way of doing that. I don't want to have to reinvent the wheel if I can avoid it. I remember with AnsiString in BCB or (or Delphi) would take it as a constructor argument, but std::string doesn't seem to have that ability. Thanx for any help, Jason. |
|
From: Ioannis V. <no...@ya...> - 2001-09-21 07:22:15
|
I do not know string's member functions yet, but i know C well and you
can use sprintf() to convert a number to a C string, so:
#include <cstdio>
#include <iostream>
#include <string>
int main()
{
using namespace std;
char array[100];
sprintf(array, "This is a string with numbers, %d is integer, %f is
float", 2, 5.5);
string s=array;
cout<<s<<endl;
}
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Jason Hardman
> Sent: Friday, September 21, 2001 5:31 AM
> To: Dev-C++
> Subject: [Dev-C++] Integer to std::string?
>
>
> Hi,
>
> I'm a bit rusty in my C++ and I just cannot seem to find a
> way of initialising a std::string with an integer value.
> While I can write a function to do it quite easily I had
> thought that there was a either a standard function or member
> of string that would do it, but I can't find one. I might be
> missing something with being tired of course. I know in
> future I will want to be doing similar with float's too, and
> I hope there is an easy way of doing that. I don't want to
> have to reinvent the wheel if I can avoid it.
>
> I remember with AnsiString in BCB or (or Delphi) would take
> it as a constructor argument, but std::string doesn't seem to
> have that ability.
>
> Thanx for any help, Jason.
>
>
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: Jason H. <jas...@bt...> - 2001-09-21 17:59:46
|
Thanks for that, I had forgotten about sprintf(). I have to say it's not an
ideal solution. It seems odd that the idea of converting from numbers wasn't
put into string. I think I may still write some kind of function to convert
"tostring", using sprintf is a more messy solution for my purpose.
I'll be keeping my eye out for alternatives still though.
Thanx, Jason.
----- Original Message -----
From: "Ioannis Vranos" <no...@ya...>
To: <dev...@li...>
Sent: Friday, September 21, 2001 9:23 AM
Subject: RE: [Dev-C++] Integer to std::string?
> I do not know string's member functions yet, but i know C well and you
> can use sprintf() to convert a number to a C string, so:
>
> #include <cstdio>
> #include <iostream>
> #include <string>
>
> int main()
> {
> using namespace std;
>
> char array[100];
>
> sprintf(array, "This is a string with numbers, %d is integer, %f is
> float", 2, 5.5);
>
> string s=array;
>
> cout<<s<<endl;
> }
>
>
> Ioannis
>
> * Ioannis Vranos
> * Programming pages: http://www.noicys.f2s.com
> * Alternative URL: http://run.to/noicys
|
|
From: Ioannis V. <no...@ya...> - 2001-09-21 18:23:21
|
> -----Original Message----- > From: dev...@li...=20 > [mailto:dev...@li...] On Behalf=20 > Of Jason Hardman > Sent: Friday, September 21, 2001 7:59 PM > To: dev...@li... > Subject: Re: [Dev-C++] Integer to std::string? >=20 >=20 > Thanks for that, I had forgotten about sprintf(). I have to=20 > say it's not an ideal solution. It seems odd that the idea of=20 > converting from numbers wasn't put into string. I think I may=20 I did not say that the idea of converting from numbers to string isn't implemented in string, i said that i do not know it yet. :) > still write some kind of function to convert "tostring",=20 > using sprintf is a more messy solution for my purpose. >=20 > I'll be keeping my eye out for alternatives still though. >=20 > Thanx, Jason. Ioannis * Ioannis Vranos * Programming pages: http://www.noicys.f2s.com * Alternative URL: http://run.to/noicys |
|
From: Jason H. <jas...@bt...> - 2001-09-21 18:56:16
|
I've been looking at it myself to, and AFAICT there isn't standard string
support for converting from int. None of the string members listed in "C++
The Complete Reference" do it.
I have found an alternative way of doing what sprintf does, which is more
C++ like, by using an Array Based Output Stream. e.g.
#include <stdio.h>
#include <stdlib.h>
#include <strstream>
#include <iostream>
#include <string>
int main(int argc, char *argv[])
{
using namespace std;
char str[80];
ostrstream outs(str, sizeof(str));
string mystring;
outs << "An int " << 1024 << " or a float " << 1.234 << "." << ends;
mystring = str;
cout << mystring << endl;
system("PAUSE");
return 0;
}
I tested it just in case, and it seems to work OK, but is also a long winded
way of doing things.
Jason.
----- Original Message -----
From: "Ioannis Vranos" <no...@ya...>
To: <dev...@li...>
Sent: Friday, September 21, 2001 8:24 PM
Subject: RE: [Dev-C++] Integer to std::string?
> -----Original Message-----
> From: dev...@li...
> [mailto:dev...@li...] On Behalf
> Of Jason Hardman
> Sent: Friday, September 21, 2001 7:59 PM
> To: dev...@li...
> Subject: Re: [Dev-C++] Integer to std::string?
>
>
> Thanks for that, I had forgotten about sprintf(). I have to
> say it's not an ideal solution. It seems odd that the idea of
> converting from numbers wasn't put into string. I think I may
I did not say that the idea of converting from numbers to string isn't
implemented in string, i said that i do not know it yet. :)
> still write some kind of function to convert "tostring",
> using sprintf is a more messy solution for my purpose.
>
> I'll be keeping my eye out for alternatives still though.
>
> Thanx, Jason.
Ioannis
* Ioannis Vranos
* Programming pages: http://www.noicys.f2s.com
* Alternative URL: http://run.to/noicys
_______________________________________________
Dev-cpp-users mailing list
Dev...@li...
https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
|