I want to know what's the difference between string and stream in c++, and what's stringstream?
istream
and ostream
: interfaces to streaming data (files, sockets, etc.)istringstream
: an istream
that wraps a string and offers its contentsostringstream
: an ostream
that saves the content written to it as a stringExample:
istringstream datastream("1 2 3");
int val;
datastream >> val;
cout << val << endl; // prints 1
datastream >> val;
cout << val << endl; // prints 2
datastream >> val;
cout << val << endl; // prints 3
ostringstream outstream;
outstream << 1 << "+" << 2 << "=" << 3;
cout << outstream.str() << endl; // prints "1+2=3"
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.
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