在 IOStreams 库中使用ignore(numeric_limits::max()) 是否可以处理任意大量流?
在 C++ 标准(第 27.6.1.3\24 节)中,对于 IOStreams 库中的 istream ignore()
函数,这意味着如果您为 numeric_limits::max()
的“n”提供参数,它将继续忽略字符 永远直到找到分隔符,甚至远远超出实际 流大小的最大值(即“n”参数被解释为无限)。
对于 gcc 实现来说,这确实是这样的 ignore()
已实现,但我仍然不清楚 这是特定于实现的还是标准强制执行的。 熟悉这一点的人可以确认这是由一个保证的吗? 符合标准的 iostreams 库?
In the C++ standard (section 27.6.1.3\24), for the
istream ignore()
function in the IOStreams library, it implies that if you supply an argument for 'n' of numeric_limits::max()
, it will continue to ignore characters
forever up until the delimiter is found, even way beyond the actual
max value for streamsize (i.e. the 'n' argument is interpreted as infinite).
For the gcc implementation this does indeed appear to be howignore()
is implemented, but I'm still unclear as to
whether this is implementation specific, or mandated by the standard.
Can someone who knows this well confirm that this is guaranteed by a
standard compliant iostreams library?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
标准规定,numeric_limits::max() 是一个特殊值,不会影响跳过的字符数。
The standard says that
numeric_limits<streamsize>::max()
is a special value that doesn't affect the number of characters skipped.根据此处:
提取和丢弃字符
从输入序列中提取字符并丢弃它们。
当 n 个字符被提取并丢弃时或当找到字符 delim 时提取结束,以先到者为准。在后一种情况下,定界字符本身也会被提取。
在您的情况下,当达到 numeric_limits::max() 字符数时,就满足第一个条件。
[Per Bo]
但是,根据规范,仅当 n 不等于
numeric_limits::max()
时才应用上述情况。According to here:
Extract and discard characters
Extracts characters from the input sequence and discards them.
The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.
In your case, when numeric_limits::max() number of characters have been reached, the first condition is met.
[Per Bo]
However, according to spec, the above case is applied only when n is not equal to
numeric_limits<streamsize>::max()
.