用
c
o
u
t
{\rm cout}
cout向屏幕输出时,系统将输出的内容先保存在为其开辟的缓冲区内,当此缓冲区满或遇到
e
n
d
l
{\rm endl}
endl时,才将数据传送到屏幕上显示;利用
c
i
n
{\rm cin}
cin进行输入操作时,数据先保存在键盘输入缓冲区,遇到回车时将数据传送到程序输入缓冲区,当程序遇到
<
<
{\rm <<}
<<操作符时,输入缓冲区的数据会被赋予相关的变量;
系统提供缓冲区机制的目的:为了减少数据传送频度,以提高
I
/
O
{\rm I/O}
I/O效率;
C
{\rm C}
C++中,输入/输出流被定义为一系列的类,这些类称为流类
(
s
t
r
e
a
m
c
l
a
s
s
)
({\rm stream\ class})
(streamclass),这些类被包含在
I
/
O
{\rm I/O}
I/O类库中,其输入输出包括如下方面的内容:
标准
I
/
O
{\rm I/O}
I/O:对系统指定的标准设备,如:显示器、打印机等的输入和输出;
文件
I
/
O
{\rm I/O}
I/O:对外存磁盘或外存储设备文件进行输入和输出;
内存
I
/
O
{\rm I/O}
I/O(串
I
/
O
{\rm I/O}
I/O):对内存中的空间进行输入和输出;
i
o
s
t
r
e
a
m
{\rm iostream}
iostream:基本输入/输出流操作;
s
t
r
e
a
m
{\rm stream}
stream:用于文件的
I
/
O
{\rm I/O}
I/O操作;
s
t
r
s
t
r
e
a
m
{\rm strstream}
strstream:用于字符串流输入/输出;
i
o
m
a
n
i
p
{\rm iomanip}
iomanip:用于对流中的数据进行格式化输入/输出;
12.2 流对象和格式化输出
在
i
o
s
t
r
e
a
m
{\rm iostream}
iostream头文件中,定义了许多相关的类,如:
i
o
s
、
i
s
t
r
e
a
m
、
o
s
t
r
e
a
m
、
i
o
s
t
r
e
a
m
{\rm ios、istream、ostream、iostream}
ios、istream、ostream、iostream等,还定义了
4
4
4种常用的流对象,如下表所示:
对象
对象
对象
含义
含义
含义
对应设备
对应设备
对应设备
对应的类
对应的类
对应的类
c
i
n
{\rm cin}
cin
标准输入流
标准输入流
标准输入流
键盘
键盘
键盘
i
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm istream\_withassign}
istream_withassign
c
o
u
t
{\rm cout}
cout
标准输出流
标准输出流
标准输出流
显示器
显示器
显示器
o
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm ostream\_withassign}
ostream_withassign
c
e
r
r
{\rm cerr}
cerr
标准错误流
标准错误流
标准错误流
显示器
显示器
显示器
o
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm ostream\_withassign}
ostream_withassign
c
l
o
g
{\rm clog}
clog
标准输出流
标准输出流
标准输出流
显示器
显示器
显示器
o
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm ostream\_withassign}
ostream_withassign
c
i
n
{\rm cin}
cin:标准输入流,是类
i
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm istream\_withassign}
istream_withassign的对象,作用为从系统指定的标准输入设备输入数据到程序变量中;
c
o
u
t
{\rm cout}
cout:标准输出流,是类
o
s
t
r
e
a
m
_
w
i
t
h
a
s
s
i
g
n
{\rm ostream\_withassign}
ostream_withassign的对象,作用是将内存数据输出到系统指定的输出设备上;
c
e
r
r
{\rm cerr}
cerr:标准错误流,是类
o
s
t
r
e
a
m
{\rm ostream}
ostream的对象,作用为向系统指定的标准输出设备输出错误信息;
c
l
o
g
{\rm clog}
clog:标准输出流,是类
o
s
t
r
e
a
m
{\rm ostream}
ostream的对象,作用为向系统指定的标准输出设备输出日志信息,
c
e
r
r
{\rm cerr}
cerr没有缓冲区,
c
l
o
g
{\rm clog}
clog