【发布时间】:2012-07-11 21:20:05
【问题描述】:
我有以下代码,我不太明白为什么结果恰好像下面这样:
#include <iostream>
#include <sstream>
using namespace std;
int main () {
std::stringstream s;
std::streambuf *backup;
backup = cout.rdbuf();
s << "Oh my god" << endl;
cout << "Before1" << endl;
cout << s.rdbuf();
cout << "After1" << endl;
cout << "Before2" << endl;
//s.seekg(0, std::ios_base::beg); // If that is in: After 2 is printed!
cout << s.rdbuf();
//cout.rdbuf(backup); // If that is in, also After 2 is printed!
cout << "After2" << endl;
}
输出:
Before1
Oh my god
After1
Before2
剩下的在哪里??¿ 它只有在我们取消注释上述行时才会被输出...... 内部会发生什么?有人知道吗? =) 会很有趣...
【问题讨论】:
标签: c++ cout stringstream streambuf