Writing std :: ostringstream to a file without copying?

For a function that takes an output stream as an argument:

void Foo(const std::ostringstream& _oss);

Is there a way to write a stream buffer to a file WITHOUT calling str ()?

I want to avoid copying the buffer (which str () does).

void Foo(const std::ostringstream& _oss);
{
  std::ofstream f("foo.bin");

  //WANT: write _oss to f without copying the buffer?
}
+3
source share
2 answers

There it operator<<()takes a stream buffer:

f << _oss.rdbuf();

However, the buffer must be really large for this to make a noticeable difference. Typically, writing to a disc will dominate the time required for this in any case.

Please note that I would avoid creating identifiers with leading underscores.

+11
source

- , , .

, , .

0

All Articles