I'm browsing the sstream library and I've come across stringstream. Googling I come to the conclusion that it is the same as string, but I can't understand it.
As I understand it, it is to create a string and use it as "cin" to concatenate?
#include <string> // std::string
#include <iostream> // std::cout
#include <sstream> // std::stringstream
int main () {
std::stringstream ss;
ss << 100 << ' ' << 200;
int foo,bar;
ss >> foo >> bar;
std::cout << "foo: " << foo << '\n';
std::cout << "bar: " << bar << '\n';
return 0;
}
Roughly speaking, a
stringstream
is basically a buffer in memory that pretends to behave like a file. Simple and easy.It allows the same operations that a stream would allow , but it is not tied to any physical device ; all operations are performed in memory, using a string as a store .
While a stream does not allow access to the underlying physical device, a stringstream does allow access to the stream it uses as a buffer , via the operations
to get a copy of the buffer , and
to establish it ourselves.
Streams are output and/or input buffers. Through them, a more or less unified interface is presented that allows you to access different data sources in a homogeneous way.
You can use an output stream to store data in a file, to write that same information to the screen, or to output it to the printer, and the same is true for input streams .
stringstream
it is a stream that works on an internal buffer. It does not write to files or read from keyboards. What you send to that stream ends up in memory. What is it for? Generally for internal uses.The class
string
instead physically represents a character string and its interface is designed to store and manage character strings.Some differences between both classes:
string
you don't have the insert/remove operator overloaded. If it's not a buffer, it doesn't need to behave like one.string
it is not capable of converting native types.stringstream
does not have iterators. A buffer is a store of information, to use it first you have to unpack it. This feature prevents it from being used directly with the vast majority of STL functions.stringstream
does not support explicit resizing of its internal buffer.stringstream
into another buffer. To do this you have to extract its content (for example using thetoString()
) method.iomanip
are applicable withstringstream
but not withstring
.The
stringstream
are primarily used for formatting operations onstring
s, just asfstream
s are for files.You can use all the functors that
iostream
s have to manipulate the stream such as input/output numbers (std::hex
,std::scientific
, etc...), strings (std::quoted
), padding (std::setw,std::setfill
), location settings (.imbue()
,std::put_money
), etc.In other words, if
iostream
together with its overloads ofoperator<</>>
s it is the C++ alternative ofprintf
,fstream
it is offprintf
, andstringstream
it is ofsprintf
, as well as type safe and with much more comfortable syntax, as well as iterable (std::istream_iterator
andstd::ostream_iterator
, to be able to use streams as if were containers).