cout 和 printf
可能的重复:
C++ 中的 printf 与 cout
cout 和 printf 有什么区别?
Possible Duplicate:
printf vs cout in C++
What are the differences between cout and printf?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
cout 自动进行强制转换并找出您要打印的变量的类型。所以你可以这样做:
cout 将检测到 myint 是一个 int 并打印它。使用 printf,您必须指定要尝试打印的变量的类型:
此外,cout 比 printf 慢(因为它执行类型检测...),尽管在大多数实际应用中,您不会注意到性能差异。
cout automatically make casts and finds out the type of the variables you are trying to print. So you can do something like:
And cout will detect that myint is an int and print it. With printf, you have to specify what is the type of the variable you are trying to print:
Also, cout is slower than printf (because it does the type detection...), although in most practical applications, you won't notice the performance difference.
printf
是用于在 C IO 库stdio
库的标准输出上打印数据的函数。它保留在 C++ 中主要是出于遗留原因,尽管有时它仍然存在有用。cout
是来自 iostreams 库的 C++ 流(特别是,它被定义为ostream &
); iostreams 库是执行 IO 的本机 C++ 方式。一般来说,使用 iostream 比旧的类似 printf 的函数更容易、更安全(感谢
<<
运算符重载而不是格式字符串+varargs),并且这是 C++ 的“惯用”执行方式IO,所以你应该使用它,除非你有特定的需要不这样做。printf
is the function used for printing data on the standard output of thestdio
library, the IO library of C. It's kept in C++ mainly for legacy reasons, although sometimes it's still useful.cout
is a C++ stream from the iostreams library (in particular, it's defined to be aostream &
); the iostreams library is the native C++ way to perform IO.In general it's easier and safer to use iostreams than the old printf-like functions (thanks to
<<
operator overloading instead of format strings+varargs), and it's the C++ "idiomatic" way to perform IO, so you should use it unless you have specific needs not to do so.基本上,
cout
是输出到标准输出的 C++ 方式,而printf
是 C 方式。C++ iostream(
cout
就是其中之一)基于 C++ 类,并且可扩展以处理新类。换句话说,您可以创建一个名为foo
的类,然后执行以下操作:另一方面,
printf
无法处理新类型,您必须编写调用的函数printf
用于该类型的每个组件,其中每个组件已经是printf
已知的类型(例如int
或char *)。
确实没有理由在 C++ 代码中使用
printf
。我总是说,如果您要使用 C++,您应该使用它,而不是沉迷于旧世界 :-) 如果您想使用printf
,请坚持使用C。寻找如何允许您的类在 iostream 中使用的示例,请参阅回答 我提供了一个关于执行此操作的代码的早期问题。
Basically,
cout
is the C++ way of outputting to standard output whileprintf
is the C way.C++ iostreams (of which
cout
is one) are based on C++ classes and are extensible to handle new classes. In other words, you can create a class calledfoo
and then do:On the other hand,
printf
cannot handle new types, you have to write functions which callprintf
for each of the components of that type, where each component is already a type known toprintf
(such asint
orchar *
).There's really no excuse for using
printf
in C++ code. I always say that, if you're going to use C++, you should use it, not wallow in the old world :-) If you want to useprintf
, stick with C.If you're looking for an example of how to allow your class to be used in iostreams, see an answer I provided to an earlier question regarding the code that does it.
摘自http://forums.devshed .com/c-programming-42/difference- Between-cout-and-printf-38373.html
cout是C++中iostream的对象。如果你使用C++,那么使用cout,效果很好。
printf 在做一些相同的事情时,它是一个打印到标准输出的格式化函数。这主要用在 C 中。
所以 printf 在某种程度上是 cout 的老大哥,因为它允许您格式化字符串。
taken from http://forums.devshed.com/c-programming-42/difference-between-cout-and-printf-38373.html
cout is an object of the iostream in C++. If you are using C++, then use cout, it works well.
printf while doing some the same things, it is a formatting function that prints to the standard out. This is mainly used in C.
so printf is the big brother of cout in a way, as it allows you to format strings.