c++ - 你能复制一个方法的内容到 StringStream 中吗?

标签 c++ string methods stringstream

例如:

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/

相关文章:

c++ - << C++ 中的运算符?

c++ - 无法链接到 vtk 6.3/usr/bin/ld : cannot find -lvtkCommon

java - Javac 的 StringBuilder 优化弊大于利吗?

java - 如何在 Java 中捆绑 getter?

当正在读取的文件中的变量等于用户输入时,C++ 为实例创建计数器的方法

c++ - 为什么单例类不能被子类化?

c++ - 在模板类中声明模板成员变量在 Xcode 中不起作用

C++:将负字符值插入字符串文字?

string - 计算列中包含字符串的单元格数的函数

java - 没有找到主要类