如何在C++ crossplatform snprintf中实现?

myW*_*SON 0 c++ boost c++03

我想知道是否有可能以及如何在C++ crossplatform中实现(C99,C++ 0x独立)snprintf?有没有这样的提升?(我想知道要替换的C++习语是snprintf(4)什么?)

hmj*_*mjd 6

std::ostringstream将是一个使用的替代snprintf:

char buf[1024];
snprintf(buf, 1024, "%d%s", 4, "hello");
Run Code Online (Sandbox Code Playgroud)

当量:

#include <sstream>

std::ostringstream s;
s << 4 << "hello";
// s.str().c_str(); // This returns `const char*` to constructed string.
Run Code Online (Sandbox Code Playgroud)

还有boost :: lexical_cast:

std::string s = boost::lexical_cast<std::string>(4) +
                    boost::lexical_cast<std::string>("hello");
Run Code Online (Sandbox Code Playgroud)