可以将方法的内容复制到StringStream中吗?

Can you copy the content of a method intro a StringStream?

本文关键字:复制 StringStream 中吗 方法      更新时间:2023-10-16

例如:

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);