<sstream>


// DECLARATIONS
    class stringbuf;
    class istringstream;
    class ostringstream;
// END OF DECLARATIONS

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.

stringbuf

class stringbuf : public streambuf {
public:
    stringbuf(ios_base::openmode mode =
        ios_base::in | ios_base::out);
    stringbuf(string& x,
        ios_base::openmode mode =
            ios_base::in | ios_base::out);
    string str() const;
    void str(string& 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());
    };

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:

stringbuf::stringbuf

stringbuf(ios_base::openmode mode =
    ios_base::in | ios_base::out);
stringbuf(string& 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.

stringbuf::overflow

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).

stringbuf::pbackfail

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).

stringbuf::seekoff

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 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:

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.

stringbuf::seekpos

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 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 the stream position(s), it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.

stringbuf::str

string str() const;
void str(string& x);

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:

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.

stringbuf::underflow

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.

istringstream

class istringstream : public istream {
public:
    explicit istringstream(ios_base::openmode mode =
        ios_base::in);
    explicit istringstream(const string& x,
        ios_base::openmode mode = ios_base::in);
    stringbuf *rdbuf() const;
    string& str();
    void str(const string& x);
    };

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::istringstream

explicit istringstream(ios_base::openmode mode =
    ios_base::in);
explicit istringstream(const string& x,
    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(x, mode | ios_base::in).

istringstream::rdbuf

stringbuf *rdbuf() const

The member function returns the address of the stored stream buffer, of type pointer to stringbuf.

istringstream::str

string str() const;
void str(string& x);

The first member function returns rdbuf()-> str(). The second member function calls rdbuf()-> str(x).

ostringstream

class ostringstream : public ostream {
public:
    explicit ostringstream(ios_base::openmode mode =
        ios_base::out);
    explicit ostringstream(const string& x,
        ios_base::openmode mode = ios_base::out);
    stringbuf *rdbuf() const;
    string& str();
    void str(const string& x);
    };

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::ostringstream

explicit ostringstream(ios_base::openmode mode =
    ios_base::out);
explicit ostringstream(const string& x,
    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(x, mode | ios_base::out).

ostringstream::rdbuf

stringbuf *rdbuf() const

The member function returns the address of the stored stream buffer, of type pointer to stringbuf.

ostringstream::str

string str() const;
void str(string& x);

The first member function returns rdbuf()-> str(). The second member function calls rdbuf()-> str(x).


See also the Table of Contents and the Index.

Copyright © 1992-1996 by P.J. Plauger. All rights reserved.