Name

basic_stringstream class template — Base class for input and output string streams

Synopsis

template <class charT, class traits = char_traits<charT>,
          class Alloc = allocator<charT> >
class basic_stringstream: public basic_iostream<charT,traits>
{
public:
  typedef charT char_type;
  typedef typename traits::int_type int_type;
  typedef typename traits::pos_type pos_type;
  typedef typename traits::off_type off_type;
  typedef traits traits_type;
   
  explicit basic_stringstream(ios_base::openmode which = 
                              ios_base::out|ios_base::in);
  explicit basic_stringstream(const basic_string<charT,traits,Alloc>& str,
                              ios_base::openmode which = 
                              ios_base::out|ios_base::in);
   
  basic_stringbuf<charT,traits,Alloc>* rdbuf(  ) const;
  basic_string<charT,traits,Alloc> str(  ) const;
  void str(const basic_string<charT,traits,Alloc>& str);
};

The basic_stringstream class template is the base class for string streams that permit input and output. You can start with an empty string and write to the stream, or start with a string and read from the stream. If you initialize the stream with a string and start writing, the output overwrites the string. You can switch between reading and writing at any time and set the read and write positions independently.

The following are the methods of basic_stringstream:

explicit basic_stringstream (ios_base::openmode which = ios_base::in | ios_base::out)

Initializes an empty string stream by constructing an internal basic_stringbuf object, passing which | ios_base::in | ios_base::out to that object's constructor and the address of the string buffer to the base-class constructor for basic_iostream.

explicit basic_stringstream (const basic_string<charT,traits,Alloc>& str, ios_base::openmode which = ios_base::in|ios_base::out)

Initializes a string stream with str as the initial string contents by constructing an internal basic_stringbuf object, passing str and which | ios_base::in | ios_base::out to that object's constructor and the address of the string buffer to the base-class constructor for basic_iostream.

basic_stringbuf<charT,traits,Alloc>* rdbuf ( ) const

Returns a pointer to the internal basic_stringbuf object.

basic_string<charT,traits,Alloc> str ( ) const

Returns the buffer contents as a string, that is, rdbuf( )->str( ).

void str (const basic_string<charT,traits,Alloc>& s)

Calls rdbuf( )->str(s) to set the buffer contents.