Thursday, April 3, 2025

basic_stringstream

Overview
basic_stringstream class is designed to read from and write to memory stream.

Details
The class template basic_stringstream provides support for high level input and output operations on memory streams. The supported operations include formatted input and output (e.g. integer values or whitespace-separated characters and characters strings) and unformatted input and output(e.g. raw characters and character arrays). 
basic_stringstream class is basically derived class of  basic_istringstream and basic_ostringstream. It has no additional functionality other than constructor. 
Syntax
template<class CharT, class Traits = char_traits<CharT>> 
class basic_stringstream: public basic_iostream<CharT,Traits>
where CharT can be char or wchar_t. It's derived from basic_ios class.

member types
Following properties are defined.
NameDefinition
char_typechar,wchar_t
traits_typechar_traits<char_type>
int_typechar_traits<char_type>::int_type
pos_typechar_traits<char_type>::pos_type
off_typechar_traits<char_type>::off_type

The openmode flags indicate how the streams such as disk files or memory streams can be opened for IO operations such as read, write or append.

Constructor
NameDescription
  1. basic_stringstream
    (ios_base::openmode mode = ios_base::in)
  2. basic_stringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::in)
  3. basic_stringstream
    (basic_stringstream&& buf)
  1. Constructs an empty basic_istringstream object.
  2. Constructs basic_istringstream object with the content from s.
  3. move constructor
The mode constants are defined in ios_base class.

Example
istringstream s, s2 {"hello, world!"};

Methods
NameDescription
basic_streambuf* rdbuf()Returns pointer to the underlying raw file device object.

Example
istringstream s {"hello, world!"};
auto sb = s.rdbuf();
  1. void str
    (
    const basic_string& s) 
  2. basic_string str()
  1. Sets the current contents of the stream to s
  2. Returns the current contents of the stream as string.
Example
istringstream ss,ss2;
ss.str("hello, world!");
ss2.str(ss.str());


No comments:

Post a Comment