//http://stackoverflow.com/questions/6010864/copying-stringstream/6010930#6010930
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
stringstream ss;
streambuf *coutbuf = cout.rdbuf();
cout.rdbuf(ss.rdbuf());
cout << "this goes to the stringstream" << endl;
string s = ss.str();
cout.rdbuf(coutbuf);
cout << "after cour.rdbuf : " << s;
return 0;
}
Ly9odHRwOi8vc3RhY2tvdmVyZmxvdy5jb20vcXVlc3Rpb25zLzYwMTA4NjQvY29weWluZy1zdHJpbmdzdHJlYW0vNjAxMDkzMCM2MDEwOTMwCiAKI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3N0cmVhbT4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKIAppbnQgbWFpbigpIAp7CiAgICAgICAgc3RyaW5nc3RyZWFtIHNzOwogICAgICAgIHN0cmVhbWJ1ZiAqY291dGJ1ZiA9IGNvdXQucmRidWYoKTsKICAgICAgICBjb3V0LnJkYnVmKHNzLnJkYnVmKCkpOwogICAgICAgIGNvdXQgPDwgInRoaXMgZ29lcyB0byB0aGUgc3RyaW5nc3RyZWFtIiA8PCBlbmRsOwogICAgICAgIHN0cmluZyBzID0gc3Muc3RyKCk7CiAgICAgICAgY291dC5yZGJ1Zihjb3V0YnVmKTsKICAgICAgICBjb3V0IDw8ICJhZnRlciBjb3VyLnJkYnVmIDogIiA8PCBzOwogICAgICAgIHJldHVybiAwOwp9Cg==