How to print into std::string like sprintf using C++11?
Posted on 2016-10-26
In the past when I've needed a formatted string, I've created a char buffer and then did sprintf into that buffer. Now that C++ has the std string class, I'd like to use it more, but it doesn't seem to play nice all the time.
I know I can still create the buffer, sprintf into that buffer and then convert to string, but is there a better way? For example, if I have the following (junk code):
sprintf(buf,"Pair (%d,%d) = %f\n",i,j,(double)i/j);
Pair (1,4) = 0.25
How can I print that string directly into a std::string instead of going through a char buffer?
Using: C++11, Visual Studio 2013