C++将输出看作字节流,但在程序中,很多数据被组织成比字节更大的单位,例如int类型由16位或者32位的二进制表示,但在将字节流发送给屏幕时,希望每个字节表示一个字符值,也就是说,要在屏幕上显示-2.34,需要将5个字符(-,2,.,3,4),而不是这个值的32位内部浮点数表示发送到屏幕上,因此,ostream的重要任务之一就是将数值类型转换成以文本形式表示的字符流,也就是说,ostream类将数据内部表示转换成由字符字节组成的输出流。

__PURE_APPDOMAIN_GLOBAL extern ostream cout;    // 标准输出
__PURE_APPDOMAIN_GLOBAL extern ostream cerr; // 标准错误输出
__PURE_APPDOMAIN_GLOBAL extern ostream clog; // 标准日志输出

一些ostream方法

put方法:用来显示(插入)一个字符

basic_ostream& __CLR_OR_THIS_CALL put(_Elem _Ch) { // insert a character
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (!_Ok) {
_State |= ios_base::badbit;
} else { // state okay, insert character
_TRY_IO_BEGIN
if (_Traits::eq_int_type(_Traits::eof(), _Myios::rdbuf()->sputc(_Ch))) {
_State |= ios_base::badbit;
}
_CATCH_IO_END
}

_Myios::setstate(_State);
return *this;
}

write方法:用来显示(插入)一个字符串

basic_ostream& __CLR_OR_THIS_CALL write(const _Elem* _Str,
streamsize _Count) { // insert _Count characters from array _Str
ios_base::iostate _State = ios_base::goodbit;
const sentry _Ok(*this);

if (!_Ok) {
_State |= ios_base::badbit;
} else if (0 < _Count) { // state okay, insert characters
_TRY_IO_BEGIN
if (_Myios::rdbuf()->sputn(_Str, _Count) != _Count) {
_State |= ios_base::badbit;
}
_CATCH_IO_END
}

_Myios::setstate(_State);
return *this;
}

flush方法:刷新输出缓冲区

basic_ostream& __CLR_OR_THIS_CALL flush() { // flush output stream
const auto _Rdbuf = _Myios::rdbuf();
if (_Rdbuf) { // buffer exists, flush it
const sentry _Ok(*this);

if (_Ok && _Rdbuf->pubsync() == -1) {
_Myios::setstate(ios_base::badbit); // sync failed
}
}
return *this;
}

用cout进行格式化

主要是用ios这个类进行实现,而ios类内以前是包括ios_base类的,现在ios_base成为了ios的基类

关于C++的cout输出流全笔记_ios

#include <ios>

boolalpha方法:输入输出bool值,可以为true或false

inline ios_base& __CLRCALL_OR_CDECL boolalpha(ios_base& _Iosbase) { // set boolalpha
_Iosbase.setf(ios_base::boolalpha);
return _Iosbase;
}

dec方法:以十进制数据显示

inline ios_base& __CLRCALL_OR_CDECL dec(ios_base& _Iosbase) { // set basefield to dec
_Iosbase.setf(ios_base::dec, ios_base::basefield);
return _Iosbase;
}

default方法:以浮点数方式显示

inline ios_base& __CLRCALL_OR_CDECL defaultfloat(ios_base& _Iosbase) { // clear floatfield
_Iosbase.unsetf(ios_base::floatfield);
return _Iosbase;
}

fixed方法:以数字末尾的小数或0进行显示

inline ios_base& __CLRCALL_OR_CDECL fixed(ios_base& _Iosbase) { // set floatfield to fixed
_Iosbase.setf(ios_base::fixed, ios_base::floatfield);
return _Iosbase;
}

hex方法:以十六进制显示

inline ios_base& __CLRCALL_OR_CDECL hex(ios_base& _Iosbase) { // set basefield to hex
_Iosbase.setf(ios_base::hex, ios_base::basefield);
return _Iosbase;
}

hexfloat方法:以十六进制的浮点数显示

inline ios_base& __CLRCALL_OR_CDECL hexfloat(ios_base& _Iosbase) { // set floatfield to hexfloat
_Iosbase.setf(ios_base::hexfloat, ios_base::floatfield);
return _Iosbase;
}

internal方法:符号或基数前缀左对齐,值右对齐

inline ios_base& __CLRCALL_OR_CDECL internal(ios_base& _Iosbase) { // set adjustfield to internal
_Iosbase.setf(ios_base::internal, ios_base::adjustfield);
return _Iosbase;
}

left方法:使用左对齐

inline ios_base& __CLRCALL_OR_CDECL left(ios_base& _Iosbase) { // set adjustfield to left
_Iosbase.setf(ios_base::left, ios_base::adjustfield);
return _Iosbase;
}

unboolalpha方法:和boolalpha相反

inline ios_base& __CLRCALL_OR_CDECL noboolalpha(ios_base& _Iosbase) { // clear boolalpha
_Iosbase.unsetf(ios_base::boolalpha);
return _Iosbase;
}

noshowbase方法:不使用C++基数前缀

inline ios_base& __CLRCALL_OR_CDECL noshowbase(ios_base& _Iosbase) { // clear showbase
_Iosbase.unsetf(ios_base::showbase);
return _Iosbase;
}

noshowpoint方法:不显示末尾小数点

inline ios_base& __CLRCALL_OR_CDECL noshowpoint(ios_base& _Iosbase) { // clear showpoint
_Iosbase.unsetf(ios_base::showpoint);
return _Iosbase;
}

noshowpos方法:不在正数前加+

inline ios_base& __CLRCALL_OR_CDECL noshowpos(ios_base& _Iosbase) { // clear showpos
_Iosbase.unsetf(ios_base::showpos);
return _Iosbase;
}

nouppercase方法:对于16进制输出,不使用大写字母E表示法

inline ios_base& __CLRCALL_OR_CDECL nouppercase(ios_base& _Iosbase) { // clear uppercase
_Iosbase.unsetf(ios_base::uppercase);
return _Iosbase;
}

oct方法:以八进制方式输出

inline ios_base& __CLRCALL_OR_CDECL oct(ios_base& _Iosbase) { // set oct in basefield
_Iosbase.setf(ios_base::oct, ios_base::basefield);
return _Iosbase;
}

right方法:右对齐

inline ios_base& __CLRCALL_OR_CDECL right(ios_base& _Iosbase) { // set right in adjustfield
_Iosbase.setf(ios_base::right, ios_base::adjustfield);
return _Iosbase;
}

scientific方法:以科学计数法表示

inline ios_base& __CLRCALL_OR_CDECL scientific(ios_base& _Iosbase) { // set scientific in floatfield
_Iosbase.setf(ios_base::scientific, ios_base::floatfield);
return _Iosbase;
}

showbase方法:和showbase相反

inline ios_base& __CLRCALL_OR_CDECL showbase(ios_base& _Iosbase) { // set showbase
_Iosbase.setf(ios_base::showbase);
return _Iosbase;
}

showpoint方法:输出末尾小数点

inline ios_base& __CLRCALL_OR_CDECL showpoint(ios_base& _Iosbase) { // set showpoint
_Iosbase.setf(ios_base::showpoint);
return _Iosbase;
}

showpos方法:输出正数前的+

inline ios_base& __CLRCALL_OR_CDECL showpos(ios_base& _Iosbase) { // set showpos
_Iosbase.setf(ios_base::showpos);
return _Iosbase;
}

uppercase方法:对于16进制,使用大写字母E

inline ios_base& __CLRCALL_OR_CDECL uppercase(ios_base& _Iosbase) { // set uppercase
_Iosbase.setf(ios_base::uppercase);
return _Iosbase;
}