'printf' vs. 'cout' in C++

后端 未结 16 1754
梦如初夏
梦如初夏 2020-11-22 07:04

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

16条回答
  •  故里飘歌
    2020-11-22 07:24

    Two points not otherwise mentioned here that I find significant:

    1) cout carries a lot of baggage if you're not already using the STL. It adds over twice as much code to your object file as printf. This is also true for string, and this is the major reason I tend to use my own string library.

    2) cout uses overloaded << operators, which I find unfortunate. This can add confusion if you're also using the << operator for its intended purpose (shift left). I personally don't like to overload operators for purposes tangential to their intended use.

    Bottom line: I'll use cout (and string) if I'm already using the STL. Otherwise, I tend to avoid it.

提交回复
热议问题