So istringstream
copies the contents of a string when initialised, e.g
string moo("one two three four");
istringstream iss(moo.c_str());
I was wondering if there's a way to make std::istringstream
use the given c_str
as its buffer without copying things. This way, it won't have to copy large bits of memory before passing the std::istringstream&
to functions that take istream&
as an argument.
What I've been trying to do is convert some functions which only take std::ifstream&
arguments (they're mostly parsers) into taking istream&
as well. Would I have to make my own istream
subclass for this?
It's fairly trivial to write a basic std::streambuf
class that reads from a given memory area. You can then construct an istream
from this and read from that.
initializing a C++ std::istringstream from an in memory buffer?
Note that the lifetime of the buffer pointed to be c_str()
is very limited, though, and there's no guarantee that a call to c_str()
want cause some copying although I don't know of any implementations where it does.