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++中都是標(biāo)準(zhǔn)IO庫中提供的輸出工具,其中
cout:一個(gè)ostream對(duì)象,向標(biāo)準(zhǔn)輸出寫入數(shù)據(jù);
cerr:一個(gè)ostream對(duì)象,通常用于輸出程序錯(cuò)誤信息,寫入到標(biāo)準(zhǔn)錯(cuò)誤;
clog:也是寫入到標(biāo)準(zhǔn)錯(cuò)誤,這點(diǎn)和cerr有點(diǎn)兒像,平常很少用到。
- 三者的區(qū)別:
1、cout的輸出可以重定向到一個(gè)文件中,而cerr必須輸出在顯示器上。
2、cerr不經(jīng)過緩沖區(qū),直接向顯示器輸出信息, 而clog中的信息存放在緩沖區(qū)內(nèi), 緩沖區(qū)滿或者遇到 endl 時(shí)才輸出。
- 那么,為什么要有cerr和clog呢?
假設(shè)你的程序遇到調(diào)用棧用完了的威脅(無限,沒有出口的遞歸),那么到什么地方借內(nèi)存來存放錯(cuò)誤信息?所以有了cerr。其目的,就是在你最需要它的緊急情況下,還能得到輸出功能的支持。
緩沖區(qū)的目的,就是減少刷屏的次數(shù)——比如,你的程序輸出圣經(jīng)中的一篇文章。不帶緩沖的話,就會(huì)每寫一個(gè)字母,就輸出一個(gè)字母,然后刷屏。有了緩沖,你將看到若干句子“同時(shí)”就出現(xiàn)在了屏幕上(由內(nèi)存翻新到顯存,然后刷新屏幕)。
?
?
參考自:https://blog.csdn.net/qq_38556370/article/details/80640845
轉(zhuǎn)載于:https://www.cnblogs.com/Yanfang20180701/p/10790869.html
總結(jié)
以上是生活随笔為你收集整理的cout,cerr和clog的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【每日一题】8月7日题目精讲—双栈排序
- 下一篇: 牛客网状压dp