简体   繁体   English

使用迭代器读取格式化的流是一件好事吗?

[英]is it a good thing to use iterators to read on a formatted stream?

I have written a class that acts like an iterator to parse CSV formatted files. 我编写了一个类似于解析CSV格式文件的迭代器的类。 I have also written other classes to read specific csv files to fill directly a MyObject structure. 我还编写了其他类来读取特定的csv文件以直接填充MyObject结构。 Thus the class can be used like that (I removed the error handling part of the code): 因此可以像这样使用类(我删除了代码的错误处理部分):

std::ifstream in(filename);
MyObjectParser parser(in);
MyObjectParser::Iterator it;
for (it = parser.begin(); it != parser.end(); it++)
{
   MyObject b = *it; 
   // do some stuff here ...
}

The program works well and I'm happy with it but I realized that the implicit meaning (only for myself?) of an iterator is that it will iterate over a collection . 该程序运行良好,我很满意它,但我意识到迭代器的隐含意义(仅对我自己?)是迭代一个集合 In this case there is no collection but a stream. 在这种情况下,没有集合而是流。

Should I prefer a form that explicitly suggest i'm using a stream by overloading >> operator and thus having something like that : 我是否应该更喜欢一个明确建议我通过重载>>运算符使用流的表单,从而得到类似的东西:

std::ifstream in(filename);
MyObjectReader reader(in);
MyObject obj;
while(reader >> obj)
{
    // do the same "some stuff" here...
}

Is it only a matter of taste? 这只是品味问题吗? I don't see clearly what are the differences (except that in the second form the object is just filled and not copied) and what are the consequences of choosing the first or the second form. 我没有清楚地看到有什么区别(除了在第二种形式中对象只是填充而不是复制)以及选择第一种或第二种形式的后果是什么。

I would be happy to get some other opinions in order to know exactly why i'm using a solution rather than another. 我很乐意得到一些其他的意见,以便确切地知道为什么我使用的是解决方案而不是另一种解决方案。

You can treat a stream as a collection if you want. 如果需要,可以将流视为集合。

I'd note, however, that by overloading operator>> , you can have both -- you can explicitly read data from the stream using operator>> directly, or you can treat the stream as a collection by using std::istream_iterator<whatever> to treat it as a collection. 但是,我注意到,通过重载operator>> ,你可以同时拥有 - 你可以直接使用operator>>从流中显式读取数据, 或者你可以使用std::istream_iterator<whatever>将流视为集合std::istream_iterator<whatever>将其视为一个集合。

That being the case, it seems to me that overloading operator>> is the obvious choice, since then you can treat things either way with essentially no extra work. 既然如此,在我看来,重载operator>>是显而易见的选择,因为那时你可以用任何方式处理事情,基本上没有额外的工作。 In addition, using std::istream_iterator<x> is a fairly recognizable idiom, since it's included in the standard library. 另外,使用std::istream_iterator<x>是一个相当容易识别的习惯用法,因为它包含在标准库中。

The concept of iteration is not dependent on that of containers. 迭代的概念不依赖于容器的概念。 Iterators iterate over a sequence of values. 迭代器迭代一系列值。 Different iterator designs define the sequence in different ways, but there is always the ideas of current value, advance and reaching the end. 不同的迭代器设计以不同的方式定义序列,但始终存在当前价值的思想,推进并达到目的。 About the only problem with input iterators is that they only terminate at the end of file; 关于输入迭代器的唯一问题是它们只在文件末尾终止; you cannot say, for example, that the next 10 lines contain doubles, and then we go on to something else. 你不能说,例如,接下来的10行包含双打,然后我们继续其他的东西。 (But of course, you can insert a filtering streambuf in the stream to detect the end.) (但是,当然,您可以在流中插入过滤streambuf来检测结束。)

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

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