Converting int to string

Started by
6 comments, last by Zahlman 18 years, 3 months ago
My TTF wrapper takes a string to print on the screen. But now I want to print an int. I could use a stringstream but thats quite tedious as I want to print three numbers now, I have to clear the stringstream between every print. Isnt there an easy operator that could be written to convert an int to a string?
Advertisement
There's always boost::lexical_cast.
The easiest way would probably be to write a simple convienience function:
std::string intToString(int i){std::ostringstream out_s;out_s << i;return out_s.str();}

Although this may not be as efficient as using one stringstream for all conversions.
You could also install the Boost C++ Libraries, and use boost::lexical_cast, which uses the same method as my function (but supports many different conversions).

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Good idea swiftcoder, Ill go with that one. Feel a bit stupid I hadnt considered it myself.
Something else to consider would be boost::format, which is a type-safe "printf-like" formatting construct. Assuming you already have some function like
MyTTFWrapper::Print(const std::string &str) { ... }

you could simply provide an overload like this
MyTTFWrapper::Print(const boost::format &fmt){  Print(fmt.str());}

and then you could invoke it like
MyTFFWrapper  ttf;    ttf.Print(boost::format("Hello, $1$! I am $2$ years old.") % "world" % 25);


boost::format() can handle anything that is streamable. Of course, so can lexical_cast. The main difference in your situation would be if you want to send formatted strings more often that just regular strings, or numbers, et cetera, to your Print method.

EDIT: Oh well, I'm too late. How typical of me.
Glad to have been of service.
However, you probably will soon find a need for a floatToString() function, and maybe a doubleToString(), at which point it might be a good idea to seriously consider boost::lexical_cast (although if this is a library, I appriciate the need to keep external dependencies down).

jpetrie: A lot of people seem to regard boost::format as having terrible performance compared to snprintf(), I haven't tried it personally, but that might make it unattractive for games.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Quote:
jpetrie: A lot of people seem to regard boost::format as having terrible performance compared to snprintf(), I haven't tried it personally, but that might make it unattractive for games.


Really? Interesting. I haven't noticed much impact. I use boost::format heavily for producing strings that are then sent to a logging mechanism (so if there is a performance impact, it will vanish when compiled without logging enabled). I notice differences with/without logging enabled, but I imagine the bulk of that has to do with my inefficient logging system writing to disk constantly.

The best thing about boost::format versus snprintf() in my mind is the type-safety and ability to defer the feeding of arguments or reset the format object, et cetera. Obviously boost::format's performance will be worse than snprintf because of the extra bookkeeping and such that it does... but it having "terrible" performance is somewhat surprising. I'll have to look into that.

(Sorry for the minor thread hijack).
Quote:Original post by Mizipzor
My TTF wrapper takes a string to print on the screen. But now I want to print an int. I could use a stringstream but thats quite tedious as I want to print three numbers now, I have to clear the stringstream between every print.


The usual solution for tedium is to wrap it up in a function ;)

class leetThingy {  std::stringstream streamer;  public:  void showTheThreeImportantNumbers(int x, int y, int z) {    streamer.str("");    streamer << x << " " << y << " " << z;    doMagicPrintingStuffWith(streamer.str().c_str());  }};


See here and here (philosophical; not specific to the problem at hand).

This topic is closed to new replies.

Advertisement