cout,cerr和clog的区别

  • 官方解释:

cout——Standard output stream

Object of class ostream that represents the standard output stream oriented to narrow characters (of type char). It corresponds to the C stream stdout

cerr——Standard output stream for errors

Object of class ostream that represents the standard error stream oriented to narrow characters (of type char). It corresponds to the C stream stderr

clog——Standard output stream for errors

Object of class ostream that represents the standard logging stream oriented to narrow characters (of type char). It corresponds, along with cerr to the C stream stderr

可以看出这三者在C++中都是标准IO库中提供的输出工具,其中

cout:一个ostream对象,向标准输出写入数据;

cerr:一个ostream对象,通常用于输出程序错误信息,写入到标准错误;

clog:也是写入到标准错误,这点和cerr有点儿像,平常很少用到。

  • 三者的区别:

1、cout的输出可以重定向到一个文件中,而cerr必须输出在显示器上

2、cerr不经过缓冲区,直接向显示器输出信息, 而clog中的信息存放在缓冲区内, 缓冲区满或者遇到 endl 时才输出。

  • 那么,为什么要有cerrclog呢?

假设你的程序遇到调用栈用完了的威胁(无限,没有出口的递归),那么到什么地方借内存来存放错误信息?所以有了cerr。其目的,就是在你最需要它的紧急情况下,还能得到输出功能的支持

缓冲区的目的,就是减少刷屏的次数——比如,你的程序输出圣经中的一篇文章。不带缓冲的话,就会每写一个字母,就输出一个字母,然后刷屏。有了缓冲,你将看到若干句子“同时”就出现在了屏幕上(由内存翻新到显存,然后刷新屏幕)。

 

 

参考自:https://blog.csdn.net/qq_38556370/article/details/80640845

转载于:https://www.cnblogs.com/Yanfang20180701/p/10790869.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值