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:
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.
- 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;
Parameters.
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:
boolalpha insert bool objects as names (true or false). dec insert integer values in decimal format. endl insert new line and flush. ends insert null character. fixed insert floating-point values in fixed format. flush flush buffer hex insert integer values in hexadecimal format. internal internal-justify. left left-justify. noboolalpha insert bool objects as numeric values. noshowbase do not prefix value with its base. noshowpoint do not show decimal point if not necessary. noshowpos do not insert plus sign (+) if number is non-negative. nounitbuf do not flush buffer after each insert operation. nouppercase do not replace lowercase letters by uppercase equivalents. oct insert values in octal format. right right-justify. scientific insert floating-point values in scientific format. showbase prefix value with its base showpoint always show decimal point when inserting floating-point values showpos insert plus sign (+) before every non-negative value. unitbuf flush buffer after each insert operation uppercase replace 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&)); |
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