• Create Account

### #ActualÁlvaro

Posted 01 December 2012 - 12:12 PM

One could still do what we used to do in C, and then put the result in a string:
char buffer[100];
std::snprintf(buffer, 100, "Hi, I am %d years old.", x);
std::string my_string(buffer);
That's kind of ugly, because it requires you to come up with an upper bound for the space needed for the string, and because it formats the string in one buffer and then you need to copy it if you want it as a string. But if you need to do any formatting of the numbers, it's much easier to use the printf family of functions than <iomanip>. You can also pick different formatting strings dynamically, which is much more flexible than the other methods.

Another interesting alternative for formatting is Boost Format..

### #1Álvaro

Posted 01 December 2012 - 12:12 PM

One could still do what we used to do in C, and then put the result in a string:
char buffer[100];
std::snprintf(buffer, 100, "Hi, I am %d years old.", x);
std::string my_string(buffer);
That's kind of ugly, because it requires you to come up with an upper bound for the space needed for the string, and because it formats the string in one buffer and then you need to copy it if you want it as a string. But if you need to do any formatting of the numbers, it's much easier to use the printf family of functions than <iomanip>. You can also pick different formatting strings dynamically, which is much more flexible than the other methods.

Another interesting alternative for formatting is Boost Format..

PARTNERS