|
|
|
作者: 蓝天伟 文章类别: 蓝天开发 发表时间: 2008-1-26 20:37:00
|
阅读(615) |
1.main函数的返回值可以是任何类型,并且可有可无,因此C++ Primer的说法是错误的。
2.在DOS环境下编译源代码用cl -GX 源代码.cpp,查询相关的命令可用cl -?,运行可直接输入源代码名.exe,如要查看返回值可用echo %ERRORLEVEL%;在UNIX环境下编译用CC 源代码.cc,运行用./源代码名,查看运行的返回值可用echo $?
3.IO的四个对象:cin,cout,cerr,clog。 对于cout和cerr的疑惑解答: Cout prints output to standard out (stdout) while cerr does to standard error (stderr). Normally, they both print out to terminal/monitor. Standard out is line buffered but standard error is never buffered. On UNIX both of them can be redirected to other output devices, for example: a.out >& /dev/null redirects both to /dev/null devices which is no device (that is ignore the output). a.out > myFile redirect standard out to file myFile, but standard error still the default. Output redirection syntax is also shell dependent. So, 1) is wrong on UNIX, cerr can be redirected as well. 2) is talking about the third standard input/output, that is standard in (stdin). By default standard input device is keyboard but you can redirect it as well.
c++里关于cerr,clog,cout三者的区别: cerr(无缓冲标准错误)----------没有缓冲,发送给它的内容立即被输出 clog(缓冲标准错误)------------有缓冲,缓冲区满时输出 cout--------------------------------标准输出 三个都是ostream类定义的输出流对象. cout是在终端显示器输出,cout流在内存中对应开辟了一个缓冲区,用来存放流中的数据,当向cout流插入一个endl,不论缓冲区是否漫了,都立即输出流中所有数据,然后插入一个换行符. cerr流对象是标准错误流,指定为和显示器关联,和cout作用差不多,有点不同就是cout通常是传到显示器输出,但可以被重定向输出到文件,而cerr流中的信息只能在显示器输出. clog流也是标准错误流,作用和cerr一样,区别在于cerr不经过缓冲区,直接向显示器输出信息,而clog中的信息存放在缓冲区,缓冲区满或者遇到endl时才输出. 还是有一疑问,cout和clog有什么区别呢?
什么是重定向?=>重定向就是重新定义其输出的位置,显示的结果不一定是显示在屏幕上,可以放到其他地方(比如文件中间)。(在WINDOWS环境下)cerr不能重定向,即使你这么做了,它还是显示在屏幕上面。
cout是先把数据存放到buffer中再输出,而cerr是直接输出,降低了I/O效率。为什么呢?因为cout是存放到buffer中是将一个buffer存满了再输出的,这样可以减少I/O的次数,所以可以增加效率。 4.endl是一个特殊值,称为操纵符(manipulator),将它写入输出流时,具有输出换行的效果,并刷新与设备相关联的缓冲区(buffer)。通过刷新缓冲区,保证用户立即看到写入到流中的输出。程序员经常在调试过程中插入输出语句,这些语句都应该刷新输出流。忘记刷新输出流可能会造成输出停留在缓冲区中,如果程序崩溃,将会导致程序错误推断崩溃位置。
|
|
|