Sign in to follow this  

Converting int to string

This topic is 4359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

This topic is 4359 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this