Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Please help me to understand the difference between streambuf, stringbuf and stringstream?

Tags:

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.

like image 390
Alok Avatar asked Jun 17 '11 20:06

Alok


People also ask

What is the difference between string and Stringstream?

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.

What is a Stringstream?

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.

What type is Stringstream?

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.

What is CPP Stringstream?

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.


1 Answers

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.

like image 116
Xeo Avatar answered Sep 20 '22 22:09

Xeo