Please someone help me to undersatnd the difference between " streambuf, stringbuf and stringstream".
void f1(std::string const& text) { std::stringstream inStream(text); cout<<inStream.str()<<endl; }
or if I write
void f2(std::string const& text) { std::stringbuf inStream(text); cout<<inStream.str()<<endl; }
Both shows the same result. When should I use stringbuf or stringstream? Thanks in advance.
Very Informally: A string is a collection of characters, a stream is a tool to manipulate moving data around. A string stream is a c++ class that lets you use a string as the source and destination of data for a stream.
A stringstream associates a string object with a stream allowing you to read from the string as if it were a stream (like cin). To use stringstream, we need to include sstream header file. The stringstream class is extremely useful in parsing input.
A stringstream class in C++ is a Stream Class to Operate on strings. The stringstream class Implements the Input/Output Operations on Memory Bases streams i.e. string: The stringstream class in C++ allows a string object to be treated as a stream. It is used to operate on strings.
The StringStream class in C++ is derived from the iostream class. Similar to other stream-based classes, StringStream in C++ allows performing insertion, extraction, and other operations. It is commonly used in parsing inputs and converting strings to numbers, and vice-versa.
A stringbuf
is used by a stringstream
, it provides the buffer for the stream. A streambuf
is just the base class for a stringbuf
. What you want is a stringstream
if you need formatted input/output into/out of the stream (like putting numbers in a string), otherwise you want to continue using a simple string
. You should never need to bother with the streambuf
family yourself.
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