简体   繁体   English

istreambuf_iterator与结构

[英]istreambuf_iterator with structs

I'm having a hard time getting istreambuf_iterator working with other types than char. 我很难让istreambuf_iterator与char以外的其他类型一起使用。 We all know you can transform a stream into a range like this: 我们都知道您可以将流转换成这样的范围:

istreambuf_iterator<char>(ifstream("some_file")) //now we have an iterator that reads chars each step.

I have a binary stream of structs, not chars. 我有结构的二进制流,而不是字符。 So I need to be able to do this: 所以我需要能够做到这一点:

istreambuf_iterator<MyStruct>(ifstream("some_file"))

I want to easily iterate over all the MyStruct's in the stream. 我想轻松地迭代流中的所有MyStruct。 This I cannot get to work since ifstream is an istream which is an basic_istream<char> which is only working with istreambuf_iterator<char> , not istreambuf_iterator<MyStruct> . 由于ifstream是一个istream,它是basic_istream<char> ,仅与istreambuf_iterator<char> ,而不与istreambuf_iterator<MyStruct>一起使用,所以我无法使用istreambuf_iterator<MyStruct>

I have already written my own iterator but I wanted to use the STD iterator because it seemed perfect. 我已经编写了自己的迭代器,但是我想使用STD迭代器,因为它看起来很完美。 Why did they make things like this? 他们为什么做这样的事情? Why can I not iterate over structs in an ifstream using istreambuf_iterator? 为什么我不能使用istreambuf_iterator遍历ifstream中的结构?

Edit: 编辑:

People have suggested using istream_iterator with a custom implementation of operator>>. 人们建议将istream_iterator与运算符>>的自定义实现一起使用。 This will work but for my specific use I need to read a specific amount of MyStruct's, not until EOF. 这将起作用,但是对于我的特定用途,我需要读取特定数量的MyStruct,直到EOF为止。 The stream contains, say 5, MyStruct's and then more data that is not supposed to be read as MyStructs. 该流包含5个MyStruct,然后包含不应视为MyStructs的更多数据。 So I need to create the "end-iterator" for the range by offsetting the "begin-iterator" like this: 因此,我需要通过抵消“ begin-iterator”来为范围创建“ end-iterator”:

auto begin = istream_iterator<MyStruct>(the_istream);
auto end = istream_iterator<MyStruct>(the_istream) + 5;

But this I don't know if it's possible. 但这我不知道是否可能。

It would be nice to have a fully standard STL iterator which has all these features and it seems they could easily have covered all these use cases if the standard iterators were somewhat more thought through. 拥有一个具有所有这些功能的完全标准的STL迭代器将是很好的,而且如果对标准迭代器进行更多的考虑,它们似乎可以轻松涵盖所有这些用例。 I guess the shortest and easiest solution is to keep using my custom iterator. 我猜最简单快捷的解决方案是继续使用我的自定义迭代器。

ifstream is a typedef for basic_ifstream<char> . ifstreambasic_ifstream<char>的typedef。 Since you are using a stream which is typed as if it contains char s, it's not surprising that you can only iterate over char s. 由于您使用的流的类型好像包含char ,因此仅对char进行迭代就不足为奇了。 You might think to try this (I did): 您可能会想尝试一下(我做到了):

istreambuf_iterator<MyStruct>(basic_ifstream<MyStruct>("some_file"))

But it won't work, because it requires a char_traits class for MyStruct , and char_traits in turn requires defining an int_type which in this case would be an integer that holds a MyStruct . 但这是行不通的,因为它需要MyStructchar_traits类,而char_traits反过来又需要定义一个int_type ,在这种情况下,该int_type将是一个保存MyStruct的整数。 Presumably MyStruct can't be stored in any integer type, so we're out of luck. 大概MyStruct不能以任何整数类型存储,因此我们很不走运。

I assume you already know you can overload operator>>(istream&, MyStruct&) .... 我假设您已经知道可以重载operator>>(istream&, MyStruct&) ....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM