ostringstream is to be the C++ replacement for sprintf. ostringstream is the char specialization of the template class basic_ostringstream (that is, it is: std::basic_ostringstream<char>). #include <sstream>
#include <iostream>
#include <string>
int main() {
std::ostringstream oss;
oss << "Hello, world" << std::endl;
oss << "42" << std::endl;
std::string str = oss.str();
std::cout << str;
}