Include the iostreams standard header <sstream> to define several template classes that support iostreams operations on sequences stored in an allocated array object. Such sequences are easily converted to and from objects of template class basic_string.
namespace std {
template<class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_stringbuf;
typedef basic_stringbuf<char> stringbuf;
typedef basic_stringbuf<wchar_t> wstringbuf;
template<class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_istringstream;
typedef basic_istringstream<char> istringstream;
typedef basic_istringstream<wchar_t> wistringstream;
template<class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_ostringstream;
typedef basic_ostringstream<char> ostringstream;
typedef basic_ostringstream<wchar_t> wostringstream;
template<class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_stringstream;
typedef basic_stringstream<char> stringstream;
typedef basic_stringstream<wchar_t> wstringstream;
}
The template class describes a stream buffer that controls the transmission of elements of type E, whose character traits are determined by the class T, to and from a sequence of elements stored in an array object. The object is allocated, extended, and freed as necessary to accommodate changes in the sequence.
An object of class basic_stringbuf<E, T, A> stores a copy of the ios_base::openmode argument from its constructor as its stringbuf mode mode:
template <class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_stringbuf
: public basic_streambuf<E, T> {
public:
typedef typename basic_streambuf<E, T>::char_type
char_type;
typedef typename basic_streambuf<E, T>::traits_type
traits_type;
typedef typename basic_streambuf<E, T>::int_type
int_type;
typedef typename basic_streambuf<E, T>::pos_type
pos_type;
typedef typename basic_streambuf<E, T>::off_type
off_type;
basic_stringbuf(ios_base::openmode mode =
ios_base::in | ios_base::out);
basic_stringbuf(basic_string<E, T, A>& x,
ios_base::openmode mode =
ios_base::in | ios_base::out);
basic_string<E, T, A> str() const;
void str(basic_string<E, T, A>& x);
protected:
virtual pos_type seekoff(off_type off,
ios_base::seekdir way,
ios_base::openmode mode =
ios_base::in | ios_base::out);
virtual pos_type seekpos(pos_type sp,
ios_base::openmode mode =
ios_base::in | ios_base::out);
virtual int_type underflow();
virtual int_type pbackfail(int_type c =
traits_type::eof());
virtual int_type overflow(int_type c =
traits_type::eof());
};
basic_stringbuf(ios_base::openmode mode =
ios_base::in | ios_base::out);
basic_stringbuf(basic_string<E, T, A>& x,
ios_base::openmode mode =
ios_base::in | ios_base::out);
The first constructor stores a null pointer in all the pointers controlling the input buffer and the output buffer. It also stores mode as the stringbuf mode.
The second constructor allocates a copy of the sequence controlled by the string object x. If mode & ios_base::in is nonzero, it sets the input buffer to begin reading at the start of the sequence. If mode & ios_base::out is nonzero, it sets the output buffer to begin writing at the start of the sequence. It also stores mode as the stringbuf mode.
typedef E char_type;
The type is a synonym for the template parameter E.
typedef typename traits_type::int_type int_type;
The type is a synonym for traits_type::int_type.
typedef typename traits_type::off_type off_type;
The type is a synonym for traits_type::off_type.
typedef typename traits_type::pos_type pos_type;
The type is a synonym for traits_type::pos_type.
typedef T traits_type;
The type is a synonym for the template parameter T.
virtual int_type overflow(int_type c =
traits_type::eof());
If c does not compare equal to traits_type::eof(), the protected virtual member function endeavors to insert the element traits_type::to_char_type(c) into the output buffer. It can do so in various ways:
If the function cannot succeed, it returns traits_type::eof(). Otherwise, it returns traits_type::not_eof(c).
virtual int_type pbackfail(int_type c =
traits_type::eof());
The protected virtual member function endeavors to put back an element into the input buffer, then make it the current element (pointed to by the next pointer). If c compares equal to traits_type::eof(), the element to push back is effectively the one already in the stream before the current element. Otherwise, that element is replaced by x = traits_type::to_char_type(c). The function can put back an element in various ways:
If the function cannot succeed, it returns traits_type::eof(). Otherwise, it returns traits_type::not_eof(c).
virtual pos_type seekoff(off_type off,
ios_base::seekdir way,
ios_base::openmode mode =
ios_base::in | ios_base::out);
The protected virtual member function endeavors to alter the current positions for the controlled streams. For an object of class basic_stringbuf<E, T, A>, a stream position consists purely of a stream offset. Offset zero designates the first element of the controlled sequence.
The new position is determined as follows:
If mode & ios_base::in is nonzero, the function alters the next position to read in the input buffer. If mode & ios_base::out is nonzero, the function alters the next position to write in the output buffer. For a stream to be affected, its buffer must exist. For a positioning operation to succeed, the resulting stream position must lie within the controlled sequence. If the function affects both stream positions, way must be ios_base::beg or ios_base::end and both streams are positioned at the same element. Otherwise (or if neither position is affected) the positioning operation fails.
If the function succeeds in altering the stream position(s), it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.
virtual pos_type seekpos(pos_type sp,
ios_base::openmode mode =
ios_base::in | ios_base::out);
The protected virtual member function endeavors to alter the current positions for the controlled streams. For an object of class basic_stringbuf<E, T, A>, a stream position consists purely of a stream offset. Offset zero designates the first element of the controlled sequence. The new position is determined by sp.
If mode & ios_base::in is nonzero, the function alters the next position to read in the input buffer. If mode & ios_base::out is nonzero, the function alters the next position to write in the output buffer. For a stream to be affected, its buffer must exist. For a positioning operation to succeed, the resulting stream position must lie within the controlled sequence. Otherwise (or if neither position is affected) the positioning operation fails.
If the function succeeds in altering the stream position(s), it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.
basic_string<E, T, A> str() const; void str(basic_string<E, T, A>& x);
The first member function returns an object of class basic_string<E, T, A>, whose controlled sequence is a copy of the sequence controlled by *this. The sequence copied depends on the stored stringbuf mode mode:
The second member function deallocates any sequence currently controlled by *this. It then allocates a copy of the sequence controlled by x. If mode & ios_base::in is nonzero, it sets the input buffer to begin reading at the beginning of the sequence. If mode & ios_base::out is nonzero, it sets the output buffer to begin writing at the beginning of the sequence.
virtual int_type underflow();
The protected virtual member function endeavors to extract the current element c from the input buffer, then advance the current stream position, and return the element as traits_type::to_int_type(c). It can do so in only one way: If a read position is available, it takes c as the element stored in the read position and advances the next pointer for the input buffer.
If the function cannot succeed, it returns traits_type::eof(). Otherwise, it returns the current element in the input stream, converted as described above.
The template class describes an object that controls extraction of elements and encoded objects from a stream buffer of class basic_stringbuf<E, T, A>, with elements of type E, whose character traits are determined by the class T, and whose elements are allocated by an allocator of class A. The object stores an object of class basic_stringbuf<E, T, A>.
template <class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_istringstream
: public basic_istream<E, T> {
public:
explicit basic_istringstream(
ios_base::openmode mode = ios_base::in);
explicit basic_istringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode = ios_base::in);
basic_stringbuf<E, T, A> *rdbuf() const;
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);
};
explicit basic_istringstream(
ios_base::openmode mode = ios_base::in);
explicit basic_istringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode = ios_base::in);
The first constructor initializes the base class by calling basic_istream(sb), where sb is the stored object of class basic_stringbuf<E, T, A>. It also initializes sb by calling basic_stringbuf<E, T, A>(mode | ios_base::in).
The second constructor initializes the base class by calling basic_istream(sb). It also initializes sb by calling basic_stringbuf<E, T, A>(x, mode | ios_base::in).
basic_stringbuf<E, T, A> *rdbuf() const
The member function returns the address of the stored stream buffer, of type pointer to basic_stringbuf<E, T, A>.
basic_string<E, T, A> str() const; void str(basic_string<E, T, A>& x);
The first member function returns rdbuf()-> str(). The second member function calls rdbuf()-> str(x).
The template class describes an object that controls insertion of elements and encoded objects into a stream buffer of class basic_stringbuf<E, T, A>, with elements of type E, whose character traits are determined by the class T, and whose elements are allocated by an allocator of class A. The object stores an object of class basic_stringbuf<E, T, A>.
template <class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_ostringstream
: public basic_ostream<E, T> {
public:
explicit basic_ostringstream(
ios_base::openmode mode = ios_base::out);
explicit basic_ostringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode = ios_base::out);
basic_stringbuf<E, T, A> *rdbuf() const;
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);
};
explicit basic_ostringstream(
ios_base::openmode mode = ios_base::out);
explicit basic_ostringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode = ios_base::out);
The first constructor initializes the base class by calling basic_ostream(sb), where sb is the stored object of class basic_stringbuf<E, T, A>. It also initializes sb by calling basic_stringbuf<E, T, A>(mode | ios_base::out).
The second constructor initializes the base class by calling basic_ostream(sb). It also initializes sb by calling basic_stringbuf<E, T, A>(x, mode | ios_base::out).
basic_stringbuf<E, T, A> *rdbuf() const
The member function returns the address of the stored stream buffer, of type pointer to basic_stringbuf<E, T, A>.
basic_string<E, T, A> str() const; void str(basic_string<E, T, A>& x);
The first member function returns rdbuf()-> str(). The second member function calls rdbuf()-> str(x).
The template class describes an object that controls insertion and extraction of elements and encoded objects using a stream buffer of class basic_stringbuf<E, T, A>, with elements of type E, whose character traits are determined by the class T, and whose elements are allocated by an allocator of class A. The object stores an object of class basic_stringbuf<E, T, A>.
template <class E,
class T = char_traits<E>,
class A = allocator<E> >
class basic_stringstream
: public basic_iostream<E, T> {
public:
explicit basic_stringstream(
ios_base::openmode mode =
ios_base::in | ios_base::out);
explicit basic_stringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode =
ios_base::in | ios_base::out);
basic_stringbuf<E, T, A> *rdbuf() const;
basic_string<E, T, A>& str();
void str(const basic_string<E, T, A>& x);
};
explicit basic_stringstream(
ios_base::openmode mode =
ios_base::in | ios_base::out);
explicit basic_stringstream(
const basic_string<E, T, A>& x,
ios_base::openmode mode =
ios_base::in | ios_base::out);
The first constructor initializes the base class by calling basic_iostream(sb), where sb is the stored object of class basic_stringbuf<E, T, A>. It also initializes sb by calling basic_stringbuf<E, T, A>(mode).
The second constructor initializes the base class by calling basic_ostream(sb). It also initializes sb by calling basic_stringbuf<E, T, A>(x, mode).
basic_stringbuf<E, T, A> *rdbuf() const
The member function returns the address of the stored stream buffer, of type pointer to basic_stringbuf<E, T, A>.
basic_string<E, T, A> str() const; void str(basic_string<E, T, A>& x);
The first member function returns rdbuf()-> str(). The second member function calls rdbuf()-> str(x).
typedef basic_istringstream<char> istringstream;
The type is a synonym for template class basic_istringstream, specialized for elements of type char.
typedef basic_ostringstream<char> ostringstream;
The type is a synonym for template class basic_ostringstream, specialized for elements of type char.
typedef basic_stringbuf<char> stringbuf;
The type is a synonym for template class basic_stringbuf, specialized for elements of type char.
typedef basic_stringstream<char> stringstream;
The type is a synonym for template class basic_stringstream, specialized for elements of type char.
typedef basic_istringstream<wchar_t> wistringstream;
The type is a synonym for template class basic_istringstream, specialized for elements of type wchar_t.
typedef basic_ostringstream<wchar_t> wostringstream;
The type is a synonym for template class basic_ostringstream, specialized for elements of type wchar_t.
typedef basic_stringbuf<wchar_t> wstringbuf;
The type is a synonym for template class basic_stringbuf, specialized for elements of type wchar_t.
typedef basic_stringstream<wchar_t> wstringstream;
The type is a synonym for template class basic_stringstream, specialized for elements of type wchar_t.
Certain materials included or referred to in this document are copyright P.J. Plauger and/or Dinkumware, Ltd. or are based on materials that are copyright P.J. Plauger and/or Dinkumware, Ltd.
Notwithstanding the meta-data for this document, copyright information for this document is as follows:
Copyright © IBM Corp. 1999, 2010. & Copyright © P.J. Plauger and/or Dinkumware, Ltd. 1992-2006.