cout 和 printf

发布于 2024-10-17 00:58:21 字数 197 浏览 0 评论 0原文

可能的重复:
C++ 中的 printf 与 cout

cout 和 printf 有什么区别?

Possible Duplicate:
printf vs cout in C++

What are the differences between cout and printf?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

笨笨の傻瓜 2024-10-24 00:58:21

cout 自动进行强制转换并找出您要打印的变量的类型。所以你可以这样做:

int myint = 5;
cout << myint;

cout 将检测到 myint 是一个 int 并打印它。使用 printf,您必须指定要尝试打印的变量的类型:

int myint = 5;
printf("%d", myint);

此外,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:

int myint = 5;
cout << myint;

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:

int myint = 5;
printf("%d", myint);

Also, cout is slower than printf (because it does the type detection...), although in most practical applications, you won't notice the performance difference.

稚然 2024-10-24 00:58:21

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 the stdio 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 a ostream &); 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.

哥,最终变帅啦 2024-10-24 00:58:21

基本上,cout 是输出到标准输出的 C++ 方式,而 printf 是 C 方式。

C++ iostream(cout 就是其中之一)基于 C++ 类,并且可扩展以处理新类。换句话说,您可以创建一个名为 foo 的类,然后执行以下操作:

foo bar;
std::cout << bar << std::endl;

另一方面,printf 无法处理新类型,您必须编写调用 的函数printf 用于该类型的每个组件,其中每个组件已经是 printf 已知的类型(例如 intchar *)。

确实没有理由在 C++ 代码中使用 printf。我总是说,如果您要使用 C++,您应该使用它,而不是沉迷于旧世界 :-) 如果您想使用 printf,请坚持使用


C。寻找如何允许您的类在 iostream 中使用的示例,请参阅回答 我提供了一个关于执行此操作的代码的早期问题。

Basically, cout is the C++ way of outputting to standard output while printf 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 called foo and then do:

foo bar;
std::cout << bar << std::endl;

On the other hand, printf cannot handle new types, you have to write functions which call printf for each of the components of that type, where each component is already a type known to printf (such as int or char *).

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 use printf, 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.

心房的律动 2024-10-24 00:58:21

摘自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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文