![]() |
![]() |
![]() |
<sstream>Include the iostreams
standard header <sstream>
to define several classes that support
iostreams operations on
sequences stored in an allocated array object.
Such sequences are easily converted to and from objects of
class
string.
// DECLARATIONS
class stringbuf;
class istringstream;
class ostringstream;
// END OF DECLARATIONS
stringbufclass stringbuf : public streambuf {
public:
stringbuf(ios_base::openmode mode =
ios_base::in | ios_base::out);
stringbuf(const string& str,
ios_base::openmode mode =
ios_base::in | ios_base::out);
string str() const;
void str(const string& newstr);
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 meta =
traits_type::eof());
virtual int_type overflow(int_type meta =
traits_type::eof());
};
The class describes a stream buffer that controls the transmission of elements 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
stringbuf
stores a copy of the
ios_base::openmode
argument from its constructor as its
stringbuf mode mode:
mode &
ios_base::in is nonzero,
the input buffer
is accessible.mode &
ios_base::out is nonzero,
the output buffer
is accessible.stringbuf::allocator_typetypedef Alloc allocator_type;
The type is a synonym for the template parameter Alloc.
stringbuf::stringbufstringbuf(ios_base::openmode mode =
ios_base::in | ios_base::out);
stringbuf(const string& str,
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 str.
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.
stringbuf::char_typetypedef char char_type;
The type is a synonym for char.
stringbuf::int_typetypedef traits_type::int_type int_type;
The type is a synonym for
traits_type::int_type.
stringbuf::off_typetypedef traits_type::off_type off_type;
The type is a synonym for
traits_type::off_type.
stringbuf::overflowvirtual int_type overflow(int_type meta =
traits_type::eof());
If meta does not compare equal to
traits_type::eof(),
the protected virtual member function endeavors to insert the element
traits_type::to_char_type(meta)
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(meta).
stringbuf::pbackfailvirtual int_type pbackfail(int_type meta =
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 meta 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
byte =
traits_type::to_char_type(meta).
The function can put back an element in various ways:
byte,
it can simply decrement the next pointer for the input buffer.mode &
ios_base::out is nonzero),
it can store byte into the putback position and decrement the
next pointer for the input buffer.If the function cannot succeed, it returns
traits_type::eof(). Otherwise, it returns
traits_type::not_eof(meta).
stringbuf::pos_typetypedef traits_type::pos_type pos_type;
The type is a synonym for
traits_type::pos_type.
stringbuf::seekoffvirtual 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
stringbuf,
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:
way ==
ios_base::beg,
the new position is the beginning of the stream plus off.
way ==
ios_base::cur,
the new position is the current stream position plus off.
way ==
ios_base::end,
the new position is the end of the stream plus off.
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 either or both of the stream positions, it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.
stringbuf::seekposvirtual 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
stringbuf,
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 either or both of the stream positions, it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.
stringbuf::strstring str() const; void str(const string& newstr);
The first member function returns an object of class
string,
whose controlled sequence is a copy of the sequence controlled
by *this. The sequence copied depends on the stored
stringbuf mode mode:
mode &
ios_base::out is nonzero
and an output buffer exists,
the sequence is the entire output buffer
(epptr() -
pbase()
elements beginning with pbase()).mode &
ios_base::in is nonzero
and an input buffer exists,
the sequence is the entire input buffer
(egptr() -
eback()
elements beginning with eback()).The second member function deallocates any sequence currently
controlled by *this. It then
allocates a copy of the sequence controlled
by newstr. 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.
stringbuf::traits_typetypedef char_traits traits_type;
The type is a synonym for
char_traits.
stringbuf::underflowvirtual int_type underflow();
The protected virtual member function endeavors to extract the current
element byte from the
input buffer,
then advance the current stream position, and return the element as
traits_type::to_int_type(byte).
It can do so in only one way:
If a read position
is available, it takes byte 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.
istringstreamclass istringstream : public istream {
public:
explicit istringstream(ios_base::openmode mode =
ios_base::in);
explicit istringstream(const string& str,
ios_base::openmode mode = ios_base::in);
stringbuf *rdbuf() const;
string str();
void str(const string& newstr);
};
The class describes an object that controls
extraction of elements and encoded objects from a
stream buffer of class
stringbuf.
The object stores an object of class
stringbuf.
istringstream::allocator_typetypedef Alloc allocator_type;
The type is a synonym for the template parameter Alloc.
istringstream::istringstreamexplicit istringstream(ios_base::openmode mode =
ios_base::in);
explicit istringstream(const string& str,
ios_base::openmode mode = ios_base::in);
The first constructor initializes the base class by calling
istream(sb),
where sb is the stored object of class
stringbuf.
It also initializes sb by calling
stringbuf(mode | ios_base::in).
The second constructor initializes the base class by calling
istream(sb).
It also initializes sb by calling
stringbuf(str,
mode | ios_base::in).
istringstream::rdbufstringbuf *rdbuf() const
The member function returns the address of the stored
stream buffer, of type pointer to
stringbuf.
istringstream::strstring str() const; void str(const string& newstr);
The first member function returns
rdbuf()->
str().
The second member function calls rdbuf()-> str(newstr).
ostringstreamclass ostringstream : public ostream {
public:
explicit ostringstream(ios_base::openmode mode =
ios_base::out);
explicit ostringstream(const string& str,
ios_base::openmode mode = ios_base::out);
stringbuf *rdbuf() const;
string str();
void str(const string& newstr);
};
The class describes an object that controls
insertion of elements and encoded objects into a
stream buffer of class
stringbuf.
The object stores an object of class
stringbuf.
ostringstream::allocator_typetypedef Alloc allocator_type;
The type is a synonym for the template parameter Alloc.
ostringstream::ostringstreamexplicit ostringstream(ios_base::openmode mode =
ios_base::out);
explicit ostringstream(const string& str,
ios_base::openmode mode = ios_base::out);
The first constructor initializes the base class by calling
ostream(sb),
where sb is the stored object of class
stringbuf.
It also initializes sb by calling
stringbuf(mode | ios_base::out).
The second constructor initializes the base class by calling
ostream(sb).
It also initializes sb by calling
stringbuf(str,
mode | ios_base::out).
ostringstream::rdbufstringbuf *rdbuf() const
The member function returns the address of the stored
stream buffer, of type pointer to
stringbuf.
ostringstream::strstring str() const; void str(const string& newstr);
The first member function returns
rdbuf()->
str().
The second member function calls rdbuf()-> str(newstr).
See also the Table of Contents and the Index.
Copyright © 1992-2006 by P.J. Plauger. All rights reserved.
![]() |
![]() |
![]() |