ostream::operator<< ostream
  cplusplus.com  

Declaration prototypes:

class members:
 
ostream& operator<< (bool& val );
ostream& operator<< (short& val );
ostream& operator<< (unsigned short& val );
ostream& operator<< (int& val );
ostream& operator<< (unsigned int& val );
ostream& operator<< (long& val );
ostream& operator<< (unsigned long& val );
ostream& operator<< (float& val );
ostream& operator<< (double& val );
ostream& operator<< (long double& val );
ostream& operator<< (void*& val );
 
ostream& operator<< (streambuf& sb );
 
ostream& operator<< (ostream& ( *pf )(ostream&));
ostream& operator<< (ios& ( *pf )(ios&));
ostream& operator<< (ios_base& ( *pf )(ios_base&));
 
external operator<<  overloaded functions:
 
ostream& operator<< (ostream& os, char ch );
ostream& operator<< (ostream& os, signed char ch );
ostream& operator<< (ostream& os, unsigned char ch );
 
ostream& operator<< (ostream& os, const char* str );
ostream& operator<< (ostream& os, const signed char* str );
ostream& operator<< (ostream& os, const unsigned char* str );

Perform a formatted output operation (insertion).
  Inserts data from the given parameter into the output stream. How data is inserted depends on the type of parameter (see section parameters).
  There are two groups of functions shown above:

Class members
These are member functions of the class ostream. They have only one parameter (the right-value), and may be called by any of the following ways (assuming ostr is an object of class ostream and x an object of one of the supported types):
  ostr.operator<< (x);
  ostr << x;
External operator<< overloaded functions
These are not members of ostream, they are global functions, but their description has been included here because of their similarity of use and complementariety with the members above. They may be called by any of the following ways:
  operator<< (ostr,x);
  ostr << x;
In both cases the most used is the second one shown ( ostr << x ) and it is also the suggested way to avoid certain unwanted implicit conversions.

Parameters.

val
Outputs the numeric value stored in val as a string of numbers. These insertions depend on locale's num_putt<> object to parse data.
sb
Inserts characters to output stream from sb until the End-Of-File is reached or until the output stream fails to successfully insert a character (in which case the character is not inserted).
pf
Calls pf(*this). This is intended to be used with manipulators.
ch
A single character (ch) is inserted to the output stream.
str
Inserts a string of characters whose first character is pointed by str. Insertion ends when the next element to be inserted is a null character.
The output may be padded following the indications of the stream's field width (ios_base::width), filling character (ios::fill) and format flags (ios_base::setf). After a call to this insertion operation the value of the field width is reset to 0.
os
ostream object on which the action is performed. This is a parameter of the external overloaded operator<< functions and means the left-value of the call to that operator.

Return Value.
  class member functions return *this
  external functions return os

Manipulators.
  Certain global functions are specifically designed to be used as a parameter for this member function and modify internal flags of the stream that affect formatted output:

boolalphainsert bool objects as names (true or false).
decinsert integer values in decimal format.
endlinsert new line and flush.
endsinsert null character.
fixedinsert floating-point values in fixed format.
flushflush buffer
hexinsert integer values in hexadecimal format.
internalinternal-justify.
leftleft-justify.
noboolalphainsert bool objects as numeric values.
noshowbasedo not prefix value with its base.
noshowpointdo not show decimal point if not necessary.
noshowposdo not insert plus sign (+) if number is non-negative.
nounitbufdo not flush buffer after each insert operation.
nouppercasedo not replace lowercase letters by uppercase equivalents.
octinsert values in octal format.
rightright-justify.
scientificinsert floating-point values in scientific format.
showbaseprefix value with its base
showpointalways show decimal point when inserting floating-point values
showposinsert plus sign (+) before every non-negative value.
unitbufflush buffer after each insert operation
uppercasereplace lowercase letters by uppercase equivalents.

Manipulators noskipws and skipws may also be applied using operator<< member function, but have no effect on output operations.

If header file <iomanip> is included special manipulators resetiosflags, setbase, setfill, setiosflags, setprecision and setw may be used with this function.

Example.

// example on ostream::operator<<
#include <iostream>
using namespace std;

int main () {

  char str[] = "Test sentence";
  int val = 65;
  char ch = 'A';

  cout << str << endl;     // Insert string

  cout << ch << endl;      // Insert single character

  cout.width (10);
  cout << right;           // Insert manipulator

  cout << val << endl;     // Insert integer

  return 0;
}
  The source demonstrates the use of some of the overloaded operator<< functions shown above using the ostream object cout

Basic template member declarations ( basic_ostream<charT,traits> ):
basic_ostream& operator<< (bool& val );
basic_ostream& operator<< (short& val );
basic_ostream& operator<< (unsigned short& val );
basic_ostream& operator<< (int& val );
basic_ostream& operator<< (unsigned int& val );
basic_ostream& operator<< (long& val );
basic_ostream& operator<< (unsigned long& val );
basic_ostream& operator<< (float& val );
basic_ostream& operator<< (double& val );
basic_ostream& operator<< (long double& val );
basic_ostream& operator<< (void*& val );
basic_ostream& operator<< (basic_streambuf<charT,traits>& sb );
basic_ostream& operator<< (basic_ostream& ( *pf )(istream&));
basic_ostream& operator<< (basic_ios<charT,traits>& ( *pf )(basic_ios<charT,traits>&));
basic_ostream& operator<< (ios_base& ( *pf )(ios_base&));
External operator<<  overloaded template functions:
template <class charT, class traits>
  basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, charT ch );

template <class charT, class traits>
  basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, char ch );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, char ch );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, signed char ch );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, unsigned char ch );

template <class charT, class traits>
  basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, const charT* str );

template <class charT, class traits>
  basic_ostream<charT,traits>& operator<< (basic_ostream<charT,traits>& os, const char* str );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, const char* str );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, const signed char* str );

template <class traits>
  basic_ostream<char,traits>& operator<< (basic_ostream<char,traits>& os, const unsigned char* str );

See also.
  put, istream::operator>>
  ostream class


© The C++ Resources Network, 2001