[Previous] [Contents] [Next]

<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

stringbuf

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

stringbuf::allocator_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

stringbuf::stringbuf

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

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_type

typedef char char_type;

The type is a synonym for char.

stringbuf::int_type

typedef traits_type::int_type int_type;

The type is a synonym for traits_type::int_type.

stringbuf::off_type

typedef traits_type::off_type off_type;

The type is a synonym for traits_type::off_type.

stringbuf::overflow

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

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

If the function cannot succeed, it returns traits_type::eof(). Otherwise, it returns traits_type::not_eof(meta).

stringbuf::pos_type

typedef traits_type::pos_type pos_type;

The type is a synonym for traits_type::pos_type.

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 either or both of the stream positions, 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 either or both of the stream positions, it returns the resultant stream position. Otherwise, it fails and returns an invalid stream position.

stringbuf::str

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

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_type

typedef char_traits traits_type;

The type is a synonym for char_traits.

stringbuf::underflow

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

istringstream

class 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_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

istringstream::istringstream

explicit 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::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(const string& newstr);

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

ostringstream

class ostringstream : public ostream {
public:
    typedef Alloc allocator_type;
    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_type

typedef Alloc allocator_type;

The type is a synonym for the template parameter Alloc.

ostringstream::ostringstream

explicit 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::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(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-2002 by P.J. Plauger. All rights reserved.

[Previous] [Contents] [Next]