printf的VS用C ++的cout [英] printf vs cout in C++

查看:122
本文介绍了printf的VS用C ++的cout的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是什么的printf() 的区别C $ C> 和 COUT 在C ++?

What is the difference between printf() and cout in C++?

推荐答案

C ++ FAQ

[15.1]为什么我应该用< iostream的> 而不是传统的< cstdio>

[15.1] Why should I use <iostream> instead of the traditional <cstdio>?

增量式的安全性,减少错误,允许可扩展性,并提供可继承。

Increase type safety, reduce errors, allow extensibility, and provide inheritability.

的printf()可以说是不破,而 scanf()的尽管是容易出错也许宜居然而,无论是相对于什么C ++的I / O可以做限制。 C ++ I / O(使用&LT;&LT; &GT;&GT; )是,相对于C(使用的printf() scanf()的

printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()):


      
  • 更类型安全:使用&LT;&iostream的GT; ,对象被I / O类型为
      由编译器静态地已知的。在
      相反,&LT; cstdio&GT; 使用%领域
      动态地指出类型。

  •   
  • 更少的错误倾向:使用&LT;&iostream的GT; ,没有多余的
      %标记的必须是一致的
      与实际物体被I / O。
      去除多余的去除了一类
      的错误。

  •   
  • 扩展:在C ++ &LT;&iostream的GT; 机制允许新的用户定义
      类型被I / O不破
      现有code。试想一下,如果乱
      每个人都同时加入
      新的不相容的%领域
      的printf() scanf()的?!

  •   
  • 可继承:C ++的&LT;&iostream的GT; 机制是从真正的类建
      如的std :: ostream的
      的std :: istream的。不像&LT; cstdio&GT;
       FILE * ,这些都是真正的类和
      因此可继承。这意味着你可以
      有其它用户定义事
      外观和行为像流,然而这
      做什么奇妙的
      你想要的东西。您自动
      获得使用的无数行的
      有用户写的I / O code你不
      甚至不知道,并且它们不需要
      了解你的扩充流
      类。

  •   
  • More type-safe: With <iostream>, the type of object being I/O'd is known statically by the compiler. In contrast, <cstdio> uses "%" fields to figure out the types dynamically.
  • Less error prone: With <iostream>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors.
  • Extensible: The C++ <iostream> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?!
  • Inheritable: The C++ <iostream> mechanism is built from real classes such as std::ostream and std::istream. Unlike <cstdio>'s FILE*, these are real classes and hence inheritable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.

在另一方面,的printf 是显著更快,这可以证明在preference用它来 COUT 中的非常的具体和有限的情况下。总是先轮廓。 (参见,例如,<一href=\"http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout\">http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout/)

On the other hand, printf is significantly faster, which may justify using it in preference to cout in very specific and limited cases. Always profile first. (See, for example, http://programming-designs.com/2009/02/c-speed-test-part-2-printf-vs-cout/)

这篇关于printf的VS用C ++的cout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆