问题描述
我从来没有太多地使用C + + I / O流,并一直选择我知道的。
即printf函数。
我知道使用I / O流有一些好处,但我正在寻找一些提示
stackoverflow社区帮我(或说服我)切换。因为我仍然
喜欢printf,我认为printf风格更容易阅读和更快的输入。
我仍然想熟悉它即使我仍然继续使用printf。
编辑。有趣的是,google C ++编码风格禁止使用流,除了日志记录。
请参阅:
我自己不是流的大用户,所以我只列出我对他们的想法。
- 我喜欢:同质性
我可能有枚举
,
或任何其他,使我的用户定义类型可打印总是通过在我的类型旁边提供相同的运算符<<
:
std :: ostream& operator<<<(std :: ostream& const MyType&);
您可以问自己是否可以打印类型,
b显然,为用户定义的类型提供流传输容量是非常容易的。它也是一个伟大的能够提供我们自己的实现一个流,并使它适合透明地在现有的代码。一旦你的运算符<<
被适当地定义,写入标准输出,内存缓冲区或文件是可以改变的。
- 我不喜欢:格式
code> iomanip 是一团糟。我讨厌写这样的东西,例如(我只是在这里投掷随机操纵器):
std :: cout< std :: left< std :: fixed<< std :: setprecision(0)<< f<< std :: endl;
我认为使用 printf
,但在此很有用。
I've never been much to use C++ I/O streams and have always opted for what I know.i.e. the printf functions.
I know there are some benefits to using I/O streams, but I'm looking for some tipsfrom the stackoverflow community to help me (or convince me) to switch. Because I stillprefer printf and I think the printf style is so much easier to read and quicker to type.
I would still like to be familiar with it even if I still continue to use printf.
Edit. Interestingly, google C++ coding style forbids the use of streams except for logging.See: http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml
I'm not a big user of streams myself, so I'll only list what I think about them. This is really subjective, I'll understand if my answer is voted for deletion.
- I like : homogeneity
I may have a enum
, a class
or anything else, making my user defined type printable is always done by providing the same operator<<
next to my type :
std::ostream &operator<<(std::ostream &, const MyType &);
You may ask yourself if a type is printable, but never how it is printable.
- I like : abstraction
Obviously, it is incredibly easy to provide 'streaming capacities' to a user defined type. It's also a great to be able to provide our own implementation of a stream and have it fit transparently in an existing code. Once your operator<<
are appropriately defined, writing to standard output, a memory buffer or a file is trivially changeable.
- I dislike : formatting
I've always thought iomanip
to be a mess. I hate writing things such as (I'm just throwing random manipulators here) :
std::cout << std::left << std::fixed << std::setprecision(0) << f << std::endl;
I think it was much easier with printf
, but Boost.Format is helpful here.
这篇关于我应该切换到C + + I / O流吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!