Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between strstream and stringstream

Tags:

I was going through these two class implementations and found out that the strstream class is deprecated.

And if I use the stringstream class as replacement, then there is big difference between how they log in the buffer since the stringstream class object maintains a deep copy of the buffer.

Has anyone faced any problem while replacing strstream with stringstream class?

What would be the output of this code and why?

#include<iostream> #include <sstream> #include <strstream>    int main(){      char strArr[] = "Soheb Khan is great";      char stringArr[] = "TurboCharging";      std::strstream strStream(strArr,19);      std::stringstream stringStream(std::string(stringArr,19));      std::cout<<"Before Modification strArr= "<<strArr<<" & stringArr= "<<stringArr<<std::endl;      strStream << "Fifa 2012 is nice";       stringStream << "Sometimes its sucks";       std::cout<<"After Modification strArr= "<<strArr<<" & stringArr= "<<stringArr<<std::endl;      return 0;   } 
like image 572
Soheb Khan Avatar asked Dec 15 '12 14:12

Soheb Khan


People also ask

What is the difference between Stringstream and Istringstream?

A stringstream is an iostream object that uses a std::string as a backing store. An ostringstream writes to a std::string . An istringstream reads from a std::string . You read & write from & to an istringstream or ostringstream using << and >> , just like any other iostream object.

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.

Is Stringstream deprecated?

strstream has been deprecated since C++98, std::stringstream and boost::iostreams::array are the recommended replacements.

What is Stringstream in C++ used for?

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

The classes from <strstream> are hideous to use. When they were more popular I haven't seen any correct production used (well, they got corrected when I spotted the problems). Either people didn't terminate the string using std::ends or they didn't release the memory using s.freeze(0) (or, most often, both). Although the <sstream> class do create a copy I haven't found this to be a problem.

In case memory allocation actually matters for your use case, either because you need to allocate large chunks or because you have many of them, you can take control easily and have data read from or written to buffers you provide using a custom stream buffer. For example, a stream buffer writing to a readily allocate chunk of memory is trivial to write:

struct omembuf     : std::streambuf { {     omembuf(char* base, std::size_t size) {         this->setp(base, base + size);     }     char* begin() const { return this->pbase(); }     char* end() const { return this->pptr(); } }; 
like image 164
Dietmar Kühl Avatar answered Sep 28 '22 05:09

Dietmar Kühl