【问题标题】:Can you copy the content of a method intro a StringStream?您可以将方法的内容复制到 StringStream 中吗?
【发布时间】:2013-04-07 22:38:48
【问题描述】:

例如:

void Display()
{
    cout << "Hello World" << endl;
}

stringstream ss;

如何将display 方法输入到ss 字符串流中?

【问题讨论】:

  • ss

标签: c++ string methods stringstream


【解决方案1】:
// 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);

cmets 本身的解释。

【讨论】:

  • 谢谢大家的回答。我是一个非常新的程序员,实际上我对 streambuf 或任何流一无所知。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 2018-07-22
  • 2013-08-08
  • 2014-05-22
  • 2018-01-26
相关资源
最近更新 更多