C++摘要——什么时候使用std::cout,什么时候用std::cerr

本文探讨了C++中标准输出流cout、cerr与clog的区别及使用场景。阐述了它们之间的不同之处,并建议通常情况下将正常输出发送到cout,而错误信息和日志则通过cerr进行输出。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

What is the difference between cout, cerr, clog of iostream header in c++? When to use which one?

stdout and stderr are different streams, even though they both refer to console output by default. Redirecting (piping) one of them (e.g. program.exe >out.txt) would not affect the other.

Generally, stdout should be used for actual program output, while all information and error messages should be printed to stderr, so that if the user redirects output to a file, information messages are still printed on the screen and not to the output file.

以前关于什么时候用cout, 什么时候用cerr有比较大的随意性质,总结一下:显式print 一定要用std::cout, 其余情况,用情况主要用std::cerr

class ClassA

{

void print()

{

        std::cout<<....

}

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值