我有以下代码,但我不太明白为什么结果恰好如下所示:
#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
其余的在哪里?¿ 只有当我们取消注释上述行时才会输出它......内部会发生什么?有人知道吗?=) 会很有趣...