使用std :: istream_iterator读取最多N个值 [英] Using std::istream_iterator to read up to N values
本文介绍了使用std :: istream_iterator读取最多N个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果我确定我的输入流包含10个值,则可以使用
If I know for certain that my input stream contains 10 values, I can read them with
std::copy_n(std::istream_iterator<T>(input), 10, output);
如果我不知道自己有多少值,我可以用
If I don't know how much values I have, I can read all of them with
std::copy(std::istream_iterator<T>(input), std::istream_iterator<T>(), output);
我的问题是如何最多读取10个值.我正在尝试在这里针对I/O错误进行增强,但是看来 copy_n
会尝试读取输入结尾之后的内容(它不知道应该停止输入),并且 copy
不会在10个值处停止.我是否必须滚动自己的 copy_at_most
?
My problem is how to read up to 10 values. I'm trying to be robust against I/O errors here,
but it appears that copy_n
will try to read past the end of the input (it doesn't know that it should stop), and copy
won't stop at 10 values. Do I have to roll my own copy_at_most
?
查看全文