I use to write code like this:
void fun(char *buff, unsigned size)
{
std::strstream str(buff, size);
str << "hello world: " << 5;
}
so I can use stream output over an arbitrary buffer. I've found this technique both efficient (no allocations) and useful (streams!). Now that std::strstream is deprecated, how can I get the same speed+flexibility that I can get with this code?
The standard library doesn't provide that functionality. However, Boost does with its generic streams and array sources/sinks.
Boost Generic Stream
Boost Array Devices
char buff[size];
boost::stream<boost::array_sink> out(buff, size);
out << "Hello, world!";
(Code untested)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With