printf()和cout的区别
2010-12-29 09:45
423 查看
printf()和cout的区别
printf is a function that takes a variable number of arguments, the first argument being a format string explaining how to treat further arguments. cout is an object of the std::ostream class. The two are completely different in form, yet the result is the same, data sent to stdout.
This question is difficult to answer completely as often phrased since cout and printf are wildly different, but most often one of three questions are meant:
Q: "Which is faster?" A: printf, but the difference is too slight to worry about.
Q: "Which is better?" A: Neither, it depends heavily on personal preference and what needs to be done.
Q: "Which should I use?" A: You should use whichever you feel most comfortable with.
It is preferred that you use iostreams with C++ instead of the C functions because iostreams are type safe and extensible for user defined types, both of which are very good reasons.
printf是个函数,输出既是一个函数调用;
cout是个变量,一个对象,和" < <"操作符结合使用;输出既是一个表达式;
当你用 cout 输出的时候,系统并不是将其送入屏幕,而是转存到缓冲区,待缓冲区满时一次性显示
解决的办法是,调用一次 cout < < flush;
C语言中为 fflush(stdout);
还有就是 cout < < "/n"; 和 cout < < endl; 的区别
前者仅仅只将回车符送入输出缓存,而后者多一个操作,即立即显示
printf is a function that takes a variable number of arguments, the first argument being a format string explaining how to treat further arguments. cout is an object of the std::ostream class. The two are completely different in form, yet the result is the same, data sent to stdout.
This question is difficult to answer completely as often phrased since cout and printf are wildly different, but most often one of three questions are meant:
Q: "Which is faster?" A: printf, but the difference is too slight to worry about.
Q: "Which is better?" A: Neither, it depends heavily on personal preference and what needs to be done.
Q: "Which should I use?" A: You should use whichever you feel most comfortable with.
It is preferred that you use iostreams with C++ instead of the C functions because iostreams are type safe and extensible for user defined types, both of which are very good reasons.
printf是个函数,输出既是一个函数调用;
cout是个变量,一个对象,和" < <"操作符结合使用;输出既是一个表达式;
当你用 cout 输出的时候,系统并不是将其送入屏幕,而是转存到缓冲区,待缓冲区满时一次性显示
解决的办法是,调用一次 cout < < flush;
C语言中为 fflush(stdout);
还有就是 cout < < "/n"; 和 cout < < endl; 的区别
前者仅仅只将回车符送入输出缓存,而后者多一个操作,即立即显示
相关文章推荐
- c中的printf和c++中的cout有什么区别
- cout和printf的区别
- cout和printf是有区别的
- cout和printf的区别
- printf和cout的区别
- ACM---------printf和cout区别
- cout,print,printf 和sprintf的区别
- C++中换行符‘\n’和控制符‘endl’的区别(cout和printf区别)
- cout和printf的区别
- HDU1215 与1406类似,但是要注意超时(cout 和 printf的区别!!!)
- cout与printf的区别
- PRINTF 与 COUT 区别
- C++中cout和printf在vc6.0中的区别
- 关于流文件输出cout与标准输出函数printf的区别
- c++中cin/cout与scanf/printf的区别比较
- cout与printf区别
- 浅谈cout和printf的区别
- 指针和printf、cout中使用++a和a++的细节,从右向左压栈(++a压寄存器变量)
- 关于printf/scanf 与 cin/cout 输入输出的速度研究
- 研究c语言中 printf()、sprintf()、scanf()、sscanf()、之间的区别