0

RT. Typically, the string stream has its own buffer.

std::ostringstream stream;
stream << "Hello";
stream.str(); // It's "Hello"

Then how to redirect the buffer inside the string stream to my own allocated memory like this,

 std::string *buffer = new std::string;
 std::ostringstream stream;
 stream.rdbuf( buffer );
 stream.str(); // It's "Hello"
 buffer->c_str(); // It's also "Hello"
Ringo_D
  • 784
  • 5
  • 18
  • Have you seen [this thread](https://stackoverflow.com/questions/1494182/setting-the-internal-buffer-used-by-a-standard-stream-pubsetbuf)? – lubgr Jun 13 '19 at 09:45
  • Why would you want to do this rather than just using a hard [`std::basic_stringbuf`](https://en.cppreference.com/w/cpp/io/basic_stringbuf) – WhozCraig Jun 13 '19 at 09:45
  • https://www.boost.org/doc/libs/1_70_0/libs/iostreams/doc/tutorial/container_sink.html – Maxim Egorushkin Jun 13 '19 at 09:55

0 Answers0