即使是简单的例如我不能建议使用 operator>> 。编写在检测和处理输入错误时非常强大的代码太困难了(或者我不知道如何)。
如果您不同意,请展示一个很好的例子使用 operator>> - 也许通过回答我链接到的最后一个问题。
总结:感谢大家的回应,很多好的意见。 Manuel的回答让我重新考虑我不愿意使用 op>> ,所以我接受了那个。
我认为流提取器操作符在与 std :: copy 和 std: :istream_iterator 类。
阅读,看看我在说什么。
I've written tons of operator<<(std::ostream &, const T &) functions -- they're incredibly useful.
I've never written an operator>>(std::istream &, T &) function in real code or even used the extraction operators for built-in types (OK, maybe for std::string). Are these appropriate only for short example programs and textbooks? Is operator>> a failed feature of C++?
Questions have been asked about safely overloading stream operators. What I wonder is if anyone does this in practice.
Even for something simple like reading input from a file in C++ I can't suggest using operator>>. It's too difficult to write code that is robust in detecting and handling errors in input (or I don't know how).
If you disagree please show a good example of using operator>> -- maybe by answering that last question I linked to.Wrapup: Thanks for the responses everyone, lots of good opinions. Manuel's answer made me reconsider my reluctance to using op>> so I accepted that one.
I think stream extractor operators can be very useful when combined with STL algorithms like std::copy and with the std::istream_iterator class.
Read this answer to see what I'm talking about.
这篇关于有人真的使用流提取运算符吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!