Wednesday, December 18, 2024

basic_ostringstream

Overview
basic_ostringstream are designed to send outputs to sources such as strings.

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

Syntax
template<class CharT, class Traits = char_traits<CharT>, class Alloc = allocator<charT>> 
class basic_ostringstream: public basic_istream<CharT,Traits> 
where CharT can be char or wchar_t. It's derived from basic_ostream 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_ostringstream
    (ios_base::openmode mode = ios_base::out)
  2. basic_ostringstream
    (const basic_string& s,
    ios_base::openmode mode = ios_base::out)
  1. Constructs an empty basic_istreamstream object.
  2. Constructs basic_istreamstream object with the content from s.
The mode constants are defined in ios_base class.

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

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

Example
ostringstream 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
ostringstream ss, ss2;
ss.str("hello, world!");
ss2.str(ss.str());

No comments:

Post a Comment