C++ 中字符串格式化的首选方法?

ale*_*exm 5 c++ string-formatting

这对我来说有点冗长:

ostrstream ss;
ss << "Selected elements: " << i << "," << j << ".";
string msg(ss.str(), (size_t)ss.pcount());
Run Code Online (Sandbox Code Playgroud)

是否有一种优雅的方法来使用简洁的单行语句(可能带有模板或宏)来格式化文本消息?

Lig*_*ica 3

是的; 您正在寻找Boost.Format

const int i = 3, j = 4;
const std::string msg = (boost::format("Selected elements: %d %d") % i % j).str();
Run Code Online (Sandbox Code Playgroud)

现场演示