有人真正使用流提取运算符吗?
我编写了大量 operator<<(std::ostream &, const T &)
函数——它们非常有用。
我从未在实际代码中编写过 operator>>(std::istream &, T &)
函数,甚至从未使用过内置类型的提取运算符(好吧,也许对于 <代码>std::string)。这些仅适用于简短的示例程序和教科书吗? operator>>
是 C++ 的失败功能吗?
有人提出了有关安全重载流运算符的问题。我想知道是否有人在实践中这样做。
即使对于像 从 C++ 文件中读取输入这样简单的事情,我也可以不建议使用operator>>
。编写能够可靠地检测和处理输入错误的代码太困难了(或者我不知道如何)。
如果您不同意,请展示一个使用 operator>>
的好例子——也许可以回答我链接到的最后一个问题。
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'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.如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我认为流提取器运算符在与 std::copy 等 STL 算法和 std::istream_iterator 类结合使用时会非常有用。
阅读这个答案看看我在说什么关于。
I think stream extractor operators can be very useful when combined with STL algorithms like
std::copy
and with thestd::istream_iterator
class.Read this answer to see what I'm talking about.
是的,我确实使用运算符>> (尽管不如操作员<<那么频繁)。它对于将用户定义的类型解析为各自的对象非常有用,从而集中解析和必要的错误处理。它对于解析枚举类型的字符串表示形式也非常有用。
例如,考虑代表水果的枚举类型。您可以使用运算符>>解析字符串(如“apple”、“banana”等)以获得正确的枚举值。
另请注意,在遇到未知字符串时,使用 istream 上的 setstate 方法来设置流的失败状态。使用此运算符时,您可以检查流的失败状态,如下所示:
Yes I do use operator>> (although not as frequently as operator<<). It is very useful for parsing user defined types into their respective objects and hence centralizing the parsing and necessary error processing. It is also very useful for parsing the string representation of an enumerated type.
For example, consider an enumerated type representing a fruit. You can use operator>> to parse a string (like "apple", "banana", etc.) to obtain the correct enumeration value.
Note also the use of the setstate method on the istream to set the failure state of the stream when an unknown string is encountered. When using this operator, you can check the failstate of the stream as follows:
值的打印次数多于读取次数,因此
operator<<
的使用频率高于operator>>
。不过,如果您想读取值,operator>>
很有用。您必须检查错误并不是特定于
operator>>
,显然任何其他读取值的方法都必须以某种方式检测无效输入。Values are more often printed than read, so
operator<<
is used more often thanoperator>>
. Nevertheless, if you want to read values,operator>>
is useful.That you have to check for errors is not specific to
operator>>
, obviously also any other way to read values will have to detect invalid input in some way.我从不写它们,也很少使用“内置”的。提取运算符对于读取交互式用户输入非常无用,因为流很容易变坏。编写自定义解析例程几乎总是更容易、更健壮。当涉及到序列化时,如果我想将某些内容保存为文本,我会采用行业标准格式(例如 XML),而提取运算符尤其不适合读取这种格式。否则,我将数据存储在数据库或二进制文件中,这再次不适合与提取器一起使用。
I never write them, and quite rarely use the "built-in" ones. The extraction operators are pretty useless for reading interactive user input, because it is too easy for a stream to go bad. Writing a custom parsing routine is almost always easier and more robust. And when it comes to serialisation, if I want to save something as text, I do it to an industry-standard format such as XML, which the extraction operators are notably ill-suited to read. Otherwise I store the data on a database or in binary file, which once again are ill-suited for use with extractors.
operator>>
用于将文本形式的数字转换为内部表示形式。它对于加载对象数据也很有用。与不能针对不同类型重载的
scanf
不同,对象可以重载operator>>
。因此,它为加载对象提供了更多的数据隐藏,不需要知道内部表示即可将数据读入对象。operator>>
is useful in converting numbers in text form to an internal representation.It can also be useful in loading data for objects. Unlike
scanf
, which cannot be overloaded for different types, objects can overloadoperator>>
. Thus it provides more data hiding for loading objects, the internal representation does not need to be known in order to read data into the object.运算符>>基本上是反序列化。根据我有限的轶事经验,C++ 中的大多数序列化/反序列化都是在比流库更低的级别实现的。它不必在较低的级别上实现——通常就是这样。
实现自定义反序列化并不总是一个小问题,但即使您不使用流提取语法实现它,您也可能会遇到相同的问题。
这是流提取运算符的一个有趣的用法,至少有一定的用处:
http://www.parashift.com/ c++-faq-lite/misc-technical-issues.html#faq-39.2
在这个有限的范围内,正确的用法似乎相当简单。
Operator >> is basically deserialization. In my limited and anecdotal experience, most serialization/deserialization in C++ is implemented at a lower level than the stream library. It doesn't have to be implemented at a lower level - it just usually is.
Implementing custom deserialization isn't always a trivial problem, but you're likely to run into the same issues even if you don't implement it with stream extraction syntax.
Here's a cheezy use of the stream extraction operator that is at least marginally useful:
http://www.parashift.com/c++-faq-lite/misc-technical-issues.html#faq-39.2
In this limited scope, it seems like correct usage is fairly simple.
我大量使用了运算符<<在我的 OOFILE 数据库中将排序指令、字段列表组装到数据库视图等中API。
由于某种原因,大量用户发现使用操作符>>很直观。 附加一个反向排序的排序字段。我不知道是谁首先建议它的,但它吸引了足够多的人,以至于它出现在 API 中。
例如:
我们还常规使用了运算符>>从流中解析整个 dbTable 。
I made heavy use of operator<< to assemble lists of sort instructions, fields into database views etc. in my OOFILE database API.
For some reason, a large number of users found it intuitive to use the operator>> to append a sort field which was a reverse sort. I don't know who suggested it in the first place but it appealed to enough people that it made it in the API.
eg:
We also made conventional use of operator>> to parse an entire dbTable from a stream.