std :: copy如何使用流迭代器 [英] How does std::copy work with stream iterators

查看:148
本文介绍了std :: copy如何使用流迭代器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个通常的STL结构是:

A usual STL construct is:

vector<string> col;
copy(istream_iterator<string>(cin), istream_iterator<string>(),
    back_inserter(col));

其中我们使用istream_iterator从std输入(cin)复制到向量。

where we use an istream_iterator to copy from std input (cin) to a vector.

任何人都可以解释这段代码是如何工作的?

Can anyone explain how this code works?

我的问题是我不太理解这个部分:

my problem is that I don't really understand this part:

istream_iterator<string>(cin), istream_iterator<string>()


推荐答案

首先,注意在这种情况下,没有真正需要使用 std :: copy 。您可以直接从迭代器初始化向量:

First, note that in this case, there's no real need to use std::copy at all. You can just initialize the vector directly from the iterators:

vector<string> col((istream_iterator<string>(cin)),
                    istream_iterator<string>());

这可能不会让代码更容易理解。

This probably doesn't make the code a whole lot easier to understand though.

至于代码的工作原理,它可能比你想象的更加直截了当。一个istream_iterator看起来像这样模糊:

As far as how the code works, it's probably a little more straighforward than you think. An istream_iterator looks vaguely like this:

template <class T>
class istream_iterator { 
    std::istream *is;
    T data;
public:
    istream_iterator(std::istream &is) : is(&is) { ++(*this); }
    istream_iterator() : is(nullptr) {}

    T operator++() { (*is) >> data; return *this; }
    T operator++(int) { (*is) >> data; return *this; }

    T const &operator*() { return data; }   

    bool operator !=(istream_iterator &end) { return (*is).good(); }
    bool operator ==(istream_iterator &end) { return !(*is).good(); }
};

显然还有更多我跳过,但这是我们最关心的。所以,发生的是当你创建迭代器,它读(或尝试)一个项目从流到我调用 data 的变量。当取消引用迭代器时,它返回 data 。当您递增迭代器时,它会从文件读取(或尝试)下一个项目。尽管被写成像一个迭代器与另一个迭代器相比, operator == operator!=

Obviously there's more more I'm skipping over, but that's most of what we care about here. So, what happens is that when you create the iterator, it reads (or attempts to) an item from the stream into the variable I've called data. When you dereference the iterator, it returns data. When you increment the iterator, it reads (or attempts to) the next item from the file. Despite being written as if they compare one iterator to another, operator== and operator!= really just check for the end of the file1.

然后由 std :: copy 使用, (再简化)看起来像这样:

That's then used by std::copy, which (again simplified) looks vaguely like this:

template <class InIt, class OutIt>
void std::copy(InIt b, InIt e, OutIt d) { 
    while (b != e) {
        *d = *b;
        ++b;
        ++d;
    }
}



因此,这从输入迭代器读取和项目,将该项目写入输出迭代器,并重复,直到当前位置的迭代器与输入结束的迭代器相等(当到达文件末尾时会发生)。注意,与其他迭代器不同的是,允许与istream迭代器一起使用的唯一结束位置是文件的结尾。

So, this reads and item from the input iterator, writes that item to the output iterator, and repeats until the iterator for the current position compares equal to the iterator for the end of the input (which will happen when you reach the end of the file). Note that unlike other iterators, the only "end" position you're allowed to use with an istream iterator is the end of the file.


  1. 请注意,技术上,这是不符合行为。我简化了比较以保持简单。两个默认构造的迭代器应该比较相等,如果从同一个流构造两个迭代器,他们应该比较等于至少在你从流中读取任何东西之前。这实际上并没有什么实际的区别,但实际使用中唯一的比较是确定是否已到达文件末尾。

这篇关于std :: copy如何使用流迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆