What to use instead of sprintf?

C

cppaddict

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
 
N

Nomak

Le 15/06/2004 à 01:16:49 said:
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
 
P

Phlip

Nothing personal, dude, but do you, like, own a C++ tutorial on paper??
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();
 
C

cppaddict

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.
 
M

Misha Polatov

cppaddict said:
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
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top