例如:
void Display()
{
cout << "Hello World" << endl;
}
stringstream ss;
如何将 display
方法输入到 ss
字符串流中?
最佳答案
// Save the old cout's streambuf
streambuf* old = cout.rdbuf();
ostringstream oss;
// replace cout's streambuf with the ostringstream's
cout.rdbuf(oss.rdbuf());
// Call Display
Display();
// Restore the old cout
cout.rdbuf(old);
评论本身的解释。
关于c++ - 你能复制一个方法的内容到 StringStream 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16021553/