Hi,
Currently, whenever I need to work with numbers as strings, I am using
sprintf to do the conversion. This seems a little prehistoric. Is
there a current STL alternative?
One would think that std::string would support it somehow. But things
like:
std::string test = "hello";
test += 99;
std::cout << test;
do not result in "test99" as I would like. Can anyone help me out, or
is something in the printf family of functions still required?
Thanks,
cpp 5 21751
Le 15/06/2004 à 01:16:49, cppaddict <he***@hello.com> a écrit: Hi,
Hi
[...]
$ cat ss.cc
#include <iostream>
#include <sstream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::stringstream;
int
main()
{
stringstream ss;
string test = "hello";
ss << test << 99;
cout << ss.str() << endl;
return 0;
}
$ g++ -W -Wall -std=c++98 ss.cc
$ ./a.out
hello99
--
Nomak
Nothing personal, dude, but do you, like, own a C++ tutorial on paper??
cppaddict wrote: Currently, whenever I need to work with numbers as strings, I am using sprintf to do the conversion. This seems a little prehistoric. Is there a current STL alternative?
One would think that std::string would support it somehow. But things like:
std::string test = "hello"; test += 99; std::cout << test;
#include <sstream>
....
std::stringstream z;
z << "hello " << 99;
std::cout << z.str();
--
Phlip http://industrialxp.org/community/bi...UserInterfaces main() { stringstream ss;
string test = "hello";
ss << test << 99;
cout << ss.str() << endl;
return 0; }
Yes, sstream. I knew there was something.
thank you Nomak and Philip.
cppaddict <he***@hello.com> writes: Hi,
Currently, whenever I need to work with numbers as strings, I am using sprintf to do the conversion. This seems a little prehistoric. Is there a current STL alternative?
One would think that std::string would support it somehow. But things like:
std::string test = "hello"; test += 99; std::cout << test;
do not result in "test99" as I would like. Can anyone help me out, or is something in the printf family of functions still required?
Thanks, cpp
Take a look at boost format library. http://boost.org/libs/format/index.html
--
Regards,
Misha Polatov
MetaCommunications Engineering This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: huey_jiang |
last post by:
Hi All,
I am trying to figure out a right syntax to convert an integer array
into hex array. sprintf worked for me on doing single integer:
int i, Iarray, n=15;
char buf;
sprintf(buf,...
|
by: jt |
last post by:
Here is my code:
unsigned long listenip;
short tlen;
sprintf(tmp,"len:%x listenip:%x",htonl(tlen),listenip);
If the value on "tlen" is less than 2 bytes and the value on "listenip" is...
|
by: jimjim |
last post by:
Hello,
I was wondering about the implications of giving as an argument to sprintf a
different data type from the one specified in the format argument. This type
of question along with some...
|
by: babak |
last post by:
Hi everyone
I want to format a string (using sprintf) and put it in a messagebox
but however I try to do it, it doesn't seem to work.
Here is an example sample of what i try to do:
char msg;...
|
by: Neal Barney |
last post by:
I have a C program which runs on a device using a Zilog Z180
microprocessor. While it can address 1MB of RAM, it can only address
64KB at any given time. And of that only 16KB can be used for...
|
by: tjay |
last post by:
Hi. I wrote some code using sprintf and atof to store a double as a
string of fixed length and to convert it back to a double variable. The
string is stored in a char buffer global variable. I'm...
|
by: ujjwaltrivedi |
last post by:
Can I use any method to allocate /deallocate memory other than new/
delete operators? Actually the problem is while using delete operator
in my ".sqc" files the application crashes. It seems the...
|
by: satty888 |
last post by:
i've got a bunch of questions for coursework at uni and theres no good notes that i have or been given.
can someone please tell me what a sprintf function does if it was used like this:
...
|
by: krister |
last post by:
Hello,
I'm working in a quite large system that has some limitations. One of
those is that I can't use printf() to get an output on a screen. I'm
forced to use a special function, let's call it...
|
by: google |
last post by:
Consider the following code:
char str;
char str2;
strcpy(str, "%alfa% %beta% d%100%d %gamma% %delta%");
printf("printf: ");
printf("1%s2", str);
printf("\nsprintf: ");
sprintf(str2, "1%s2",...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |