Namespaces
Variants
Views
Actions

std::basic_ostream::operator<<

From cppreference.com
< cpp‎ | io‎ | basic ostream
Revision as of 09:01, 4 April 2013 by P12 (Talk | contribs)

Template:ddcl list begin <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( short value );
basic_ostream& operator<<( unsigned short value );
</td>

<td > (1) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( int value );
basic_ostream& operator<<( unsigned int value );
</td>

<td > (2) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( long value );
basic_ostream& operator<<( unsigned long value );
</td>

<td > (3) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( long long value );
basic_ostream& operator<<( unsigned long long value );
</td>

<td > (4) </td> <td > (since C++11) </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( float value );

basic_ostream& operator<<( double value );

basic_ostream& operator<<( long double value );
</td>

<td > (5) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( bool value );
</td>

<td > (6) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( const void* value );
</td>

<td > (7) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( std::basic_streambuf<CharT, Traits>* sb);
</td>

<td > (8) </td> <td class="t-dcl-nopad"> </td> </tr> <tr class="t-dcl-sep"><td></td><td></td><td></td></tr>

<tr class="t-dcl ">

<td >
basic_ostream& operator<<( basic_ostream& st,

                           std::ios_base& (*func)(std::ios_base&) );
basic_ostream& operator<<( basic_ostream& st,
                           std::basic_ios<CharT,Traits>& (*func)(std::basic_ios<CharT,Traits>&) );
basic_ostream& operator<<( basic_ostream& st,

                           std::basic_ostream& (*func)(std::basic_ostream&) );
</td>

<td > (9) </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Inserts data into the stream.

1-2) Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, if value is short or int, then casts it to unsigned short or unsigned int if ios_base::flags() & ios_base::basefield is ios_base::oct or ios_base::hex. After that, casts to long in any case and outputs as in (3). If value is unsigned short or unsigned int, casts to unsigned long and outputs as in (3).
3-7) Behaves as an FormattedOutputFunction. After constructing and checking the sentry object, inserts an integer, floating point, boolean or generic pointer value by calling num_put::put(). If the end of file condition was encountered during output (put().failed() == true), sets ios::badbit.
8) Behaves as an UnformattedOutputFunction. After constructing and checking the sentry object, checks if sb is a null pointer. If it is, executes setstate(badbit) and exits. Otherwise, extracts characters from the input sequence controlled by sb and inserts them into *this until one of the following conditions are met:
  • end-of-file occurs on the input sequence;
  • inserting in the output sequence fails (in which case the character to be inserted is not extracted);
  • an exception occurs (in which case the exception is caught).
If no characters were inserted, executes setstate(badbit). If an exception was thrown while extracting, sets failbit
9) Calls func(*this);. This overload is used to implement output I/O manipulators such as std::endl.

Contents

Parameters

value - integer, floating-point, boolean, or pointer value to insert
func - function to call
sb - pointer to the streambuffer to read the data from

Return value

*this

Notes

There are no overload for pointers to volatile or function pointers (other than the ones with signatures accepted by the 9) overload). Attempting to output such objects invokes implicit conversion to bool, and, for any non-null pointer value, the value 1 is printed (unless boolalpha was set).

Example

#include <iostream>
#include <iomanip>
#include <sstream>
int main()
{
    std::istringstream input(" \"Some text.\" ");
    volatile int n = 42;
    double f = 3.14;
    bool b = true;;
    std::cout << n   // int overload
              << ' ' // non-member overload
              << std::boolalpha << b // bool overload
              << " " // non-member overload
              << std::fixed << f // double overload
              << input.rdbuf() // streambuf overload
              << &n // bool overload
              << std::endl; // function overload
}

Output:

42 true 3.140000 "Some text." true

See also

Template:cpp/io/basic ostream/dcl list operator ltlt2Template:cpp/numeric/random/engine/dcl list operator ltltgtgtTemplate:cpp/numeric/random/distribution/dcl list operator ltltgtgtTemplate:cpp/io/basic ostream/dcl list putTemplate:cpp/io/basic ostream/dcl list write
performs stream I/O of strings
(function template)
performs stream input and output of bitsets
(function)
serializes and deserializes a complex number
(function template)