V
V
vipermagi2016-10-13 14:59:24
C++ / C#
vipermagi, 2016-10-13 14:59:24

How to take stringstream data without copying?

There is a task: to take data from a file or a pipe conveyor and, based on it, do something new, for example, archive or compose a large string from several lines.
I use the stringstream type for the buffer. But that's not the problem, through the str () method, it returns a copy of its internal field. And I expected to get the rvalue of this line, so that one full copy could be bypassed; that is, I need to deliberately make the old stringstream not valid.
What is the best way to proceed in such a case?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MiiNiPaa, 2016-10-13
@MiiNiPaa

If only reading is needed, then istreambuf_iterator can be used .

M
Mercury13, 2016-10-13
@Mercury13

stringstream is not required to store data in a string. So the str() function simply collects the information into a string.
https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++...
Judging by the G++ sources, their sstream stores information in a long buffer, but not in a string. But this is not necessary either; there can be many buffers.
So, unfortunately, to save memory, you have to make your own bike. True, as MiiNiPaa says , some tasks can be performed on a stringstream, using, for example, an iterator.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question