共用方式為


basic_ostream Class

This template class describes an object that controls insertion of elements and encoded objects into a stream buffer with elements of type Elem, also known as char_type, whose character traits are determined by the class Tr, also known as traits_type.

For a list of all members of this type, see basic_ostream Members.

template <class _Elem, class _Tr = char_traits<Elem> >
   class basic_ostream
       : virtual public basic_ios<_Elem, _Tr>

Parameters

  • _Elem
    A char_type.

  • _Tr
    The character traits_type.

Remarks

Most of the member functions that overload operator<< are formatted output functions. They follow the pattern:

   iostate state = goodbit;
   const sentry ok( *this );
   if ( ok )
      {try
         {<convert and insert elements
         accumulate flags in state> }
      catch ( ... )
         {try
           {setstate( badbit ); }
         catch ( ... )
           {}
         if ( ( exceptions( ) & badbit ) != 0 )
           throw; }}
   width( 0 );    // Except for operator<<(Elem)
   setstate( state );
   return ( *this );

Two other member functions are unformatted output functions. They follow the pattern:

   iostate state = goodbit;
   const sentry ok( *this );
   if ( !ok )
      state |= badbit;
   else
      {try
         {<obtain and insert elements
         accumulate flags in state> }
      catch ( ... )
         {try
            {setstate( badbit ); }
         catch ( ... )
            {}
         if ( ( exceptions( ) & badbit ) != 0 )
            throw; }}
   setstate( state );
   return ( *this );

Both groups of functions call setstate(badbit) if they encounter a failure while inserting elements.

An object of class basic_istream<Elem, Tr> stores only a virtual public base object of class basic_ios<Elem, Tr>.

Example

See the example for basic_ofstream Class to learn more about output streams.

Requirements

Header: <ostream>

Namespace: std

See Also

Concepts

basic_ostream Members

<ostream> Members

Thread Safety in the Standard C++ Library

iostream Programming

iostreams Conventions