Wednesday, December 18, 2024

basic_istringstream

Overview
basic_istringstream are designed to receive inputs from sources such as strings.

Details
The class template basic_istringstream provides support for high level input operations on character streams based on strings. The supported operations include formatted input (e.g. integer values or whitespace-separated characters and characters strings) and unformatted input (e.g. raw characters and character arrays). 

Syntax
template<class CharT, class Traits = char_traits<CharT>, class Alloc = allocator<charT>> 
class basic_istringstream: public basic_istream<CharT,Traits> 
where CharT can be char or wchar_t. It's derived from basic_istream class.

member types
Following properties are defined.
NameDefinition
char_typechar,wchar_t
traits_typechar_traits<char_type>
allocator_typeAllocator<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_istringstream
    (ios_base::openmode mode = ios_base::in)
  2. basic_istringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::in)
  1. Constructs an empty basic_istringstream object.
  2. Constructs basic_istringstream object with the content from s.
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