C++ cung cấp std::ostringstream (thư viện <sstream>) để format chuỗi kiểu sprintf nhưng an toàn kiểu và hỗ trợ stream operator.
Ví dụ
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
int main() {
ostringstream os;
os << "Store " << 50 << " and " << 52.32 << " in string.";
string str = os.str();
cout << str;
}Kết quả
Store 50 and 52.32 in string.