是C格式化的I / O功能(printf,sprintf等)比iOStream更流行,如果是,为什么? [英] Are the C formatted I/O functions (printf, sprintf, etc) more popular than IOStream, and if so, why?
问题描述
我一直在通过他人最近的许多代码,并发生了注意每个人都使用printf样式C函数很多,但在学校(特别是cout)学习的C ++函数似乎不那么受欢迎。
这是一个有效的观察,是否有理由呢?
Convention
感谢,
R
就个人而言,我使用 printf
over iostream
当你使用 iostream 格式化时
cout
/ code>,你必须<
各种古怪,如 setiosflags
和 setf
。我永远不会记得这些东西所在的命名空间,更不用说它是什么。
printf
另一个优点是
code> printf 是无状态的:与 cout
不同,我不需要记住在<$ c上调用了哪些成员函数$ c> printf ,或者标记的拜占庭控制已经被<<
这是一个很大的可读性。 I've been looking through a lot of code made by others lately and happened to notice everyone uses "printf" style C functions a lot, but the C++ functions learned in school (cout, specifically) don't seem so popular.
Is this a valid observation, and is there a reason for this? Convention?
Thanks,
R
Personally, I use printf
over the iostream
stuff (like cout
) because I think it's clearer.
When you do formatting with iostream
, you have to <<
all sorts of weirdness like setiosflags
and setf
. I can never remember which namespace all this stuff lives in, let alone what it all does. Even when I do, I'm disappointed with how verbose and unintuitive the code looks.
The formatting options with printf
may seem illegible at first, but they're concise, clearly documented in a single manual page, and common to a wide range of languages.
Another advanage is that printf
is stateless: Unlike with cout
, I don't need to remember which member functions have been called on printf
, or which byzantine concoction of flags has been <<
'ed into it. This is a big plus for readability.
这篇关于是C格式化的I / O功能(printf,sprintf等)比iOStream更流行,如果是,为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!