+ 6
What is string stream and it uses in c++
with what I know string stream requires the header <sstream> and using the keyword stringstream but don't really understand how to use it and it usefulness instead of the "cout"
1 Antwort
+ 3
@DUDE The class stringstream allows the user to treat a string as a buffer of a stream. In classes like ostream and istream, of which cout and cin are objects, a buffer is maintained to store the data.
When you call, for example : cout<<5;
The operator << converts the integer 5 to its character equivalent (each digit to ASCII char) and stores it to the set buffer. Then the buffer is flushed when it becomes full and the contents are transferred to stdout, and that is when you see the 5 as output.
More information on this can be found here: https://www.codeproject.com/Questions/342008/how-the-cout-statement-works-in-cplusplus-and-how
Now, if we use a string as a buffer, we can use most of the stream functions with the string itself, like <<, >>, getline(), setw(), etc.
Now, since all conversions are done by the operators which are pre-defined for other streams, conversion between types and string formatting becomes relatively easier.
Thus, with stringstream, you can manipulate strings easily. But to print them in the end, you need to use cout.