Apache C++ Standard Library Reference Guide

## basic_stringstream

Library: Input/output

                               basic_ostream
    basic_stringstreambasic_iostream                 basic_iosios_base
                               basic_istream

- Local Index
- Summary
- Synopsis
- Description
- Interface
- Member Types
- Nonmember Types
- Constructors
- Destructor
- Member Functions
- Example
- See Also
- Standards Conformance

### Local Index

#### Members

allocator_type basic_stringstream() char_type | int_type off_type pos_type | rdbuf() str() string_type | traits_type ~basic_stringstream()

#### Non-Members

stringstream | wstringstream |

### Summary

Class that supports writing and reading objects of specializations of the class template basic_string to and from an array in memory

### Synopsis

    #include <sstream>

    namespace std {
      template<class charT, class traits = char_traits<charT>,
               class Allocator = allocator<charT> >
      class basic_stringstream;
    }

### Description

The class template basic_stringstream reads and writes to an array in memory. It supports writing and reading objects of class basic_string, and uses a basic_stringbuf object to control the associated storage. It inherits from basic_iostream and therefore can use all the formatted and unformatted output and input functions.

### Interface

    namespace std {

      template<class charT, class traits = char_traits<charT>,
               class Allocator = allocator<charT> >
      class basic_stringstream
      : public basic_iostream<charT, traits> {
       public:
        typedef traits traits_type;
        typedef charT char_type;
        typedef Allocator allocator_type;
        typedef typename traits_type::int_type int_type;
        typedef typename traits_type::pos_type pos_type;
        typedef typename traits_type::off_type off_type;

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

        explicit
        basic_stringstream(const basic_string<char_type,
                             traits_type, allocator_type>&,
                           ios_base::openmode =
                             ios_base::out | ios_base::in);

        virtual ~basic_stringstream();

        basic_stringbuf<char_type, traits_type, allocator_type> *
        rdbuf() const;
        basic_string<char_type, traits_type, allocator_type>
        str() const;
        void
        str(const basic_string<char_type, traits_type, allocator_type>&);
      };
    }

### Member Types

    allocator_type

The type `allocator_type` is a synonym for the template parameter `Allocator`.

    char_type

The type `char_type` is a synonym for the template parameter `charT`.

    int_type

The type `int_type` is a synonym of type `traits::in_type`.

    off_type

The type`off_type` is a synonym of type`traits::off_type`.

    pos_type

The type `pos_type` is a synonym of type `traits::pos_type`.

    string_type

The type `string_type` is an instantiation of class basic_string on type `charT`.

    traits_type

The type `traits_type` is a synonym for the template parameter `traits`.

### Nonmember Types

    stringstream

The type `stringstream` is a specialization of class template basic_stringstream on type `char`:

`typedef basic_stringstream<char> stringstream;`

    wstringstream

The type `wstringstream` is a specialization of class template basic_stringstream on type `wchar_t:`

`typedef basic_stringstream<wchar_t> wstringstream;`

### Constructors

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

Constructs an object of class basic_stringstream, initializing the base class basic_iostream with the associated string buffer. This string buffer is initialized by calling the basic_stringbuf constructor `basic_stringbuf<charT,traits,Allocator>(which)`.

    explicit basic_stringstream(const basic_string<char_type,
                       traits_type, allocator_type>& str,
                       ios_base::openmode which =
                       ios_base::in | ios_base::out);

Constructs an object of class basic_stringstream, initializing the base class basic_iostream with the associated string buffer. The string buffer is initialized by calling the basic_stringbuf constructor `basic_stringbuf<char_type, traits_type, allocator_type>(str,which)`.

### Destructor

    virtual ~basic_stringstream();

Destroys an object of class basic_stringstream.

### Member Functions

    basic_stringbuf<char_type, traits_type, allocator_type>*
    rdbuf() const;

Returns a pointer to the basic_stringbuf associated with the stream.

    basic_string<char_type, traits_type, allocator_type>
    str() const;

Returns `rdbuf()->str()`.

    void
    str(const basic_string<char_type, traits_type,
                           allocator_type>& str);

Calls `rdbuf()->str(str)`.

### Example

    //
    //  stringstream.cpp
    //

    #include <iostream>   // for wcout, endl
    #include <sstream>    // for wstringstream

    int main ()
    {
        // create a bi-directional wstringstream object
        std::wstringstream inout;

        // write out three lines to the stream
        inout << "Dieses ist die Geschichte eines Mannes.\n"
              << "C'est l'histoire d'un homme.\n"
              << "This is the story of a man." << std::endl;

        std::wstring line;

        // extract the first line
        std::getline (inout, line);

        // output the first line to stdout
        std::wcout << "\nDeutsch:\n" << line;

        // extract the second line
        std::getline (inout, line);

        // output the second line to stdout
        std::wcout << "\nFrançais:\n" << line;

        // extract the third line
        std::getline (inout, line);

        // output the third line to stdout
        std::wcout << "\nEnglish:\n" << line << std::endl;

        // output the contents of the stream object to stdout
        std::wcout << std::endl << inout.str() << std::endl;

        return 0;
    }

    Program Output:

    Deutsch:
    Dieses ist die Geschichte eines Mannes.
    Français:
    C'est l'histoire d'un homme.
    English:
    This is the story of a man.

    Dieses ist die Geschichte eines Mannes.
    C'est l'histoire d'un homme.
    This is the story of a man.

### See Also

char_traits, ios_base, basic_ios, basic_stringbuf, basic_string, basic_istringstream, basic_ostringstream

### Standards Conformance

ISO/IEC 14882:1998 -- International Standard for Information Systems --Programming Language C++, Section 27.7.4