不同类型的迭代器 [英] Different types of iterators

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

问题描述

是否有其他类型的迭代器?任何显示不同类型迭代器的链接?

Are there other types of iterators? Any links that show different types of iterators?

我所知道的唯一一个是.NET的IEnumerable。

The only one I know is .NET's IEnumerable.

特别是对于C#,但所有其他人也受到欢迎。

Particularly for C#, but all others are welcomed too.

推荐答案

迭代器在不同语言中有很多不同的东西。

An iterator is a lot of different things in different languages.

不仅仅是C#Iterator的一个明显例子是C ++迭代器,它基本上是序列的标记。与C#等价物不同,它不知道序列开始或结束的位置,它只知道它当前指向哪个元素,以及如何获得下一个元素或可能是前一元素。

An obvious example of something "more than just the C# Iterator" is the C++ iterator, which is basically a marker into a sequence. Unlike the C# equivalent, it does not "know" where the sequence starts or ends, it just knows which element it currently points to, and how to get the next and perhaps previous element.

这些迭代器通常成对使用(表示序列的开始/结束),如下所示:

These iterators are typically used in pairs (denoting begin/end of a sequence), like this:

std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
v.push_back(5); // create a vector (equivalent to a C# List), and populate it with the numbers 1-5

// create two iterators, pointing to the beginning and end of this vector
std::vector<int>::iterator first = v.begin();
std::vector<int>::iterator last = v.end();

std::copy(first, last, std::ostream_iterator(std::cout)); // copy all elements found between first and last, to the standard output (in the shape of a special output iterator)

或者如果我们手动遍历它,我们会这样做:

or if we were to traverse it manually, we'd do this:

for (vector<int>::iterator cur = v.begin(); cur != v.end(); ++cur) {
  int val = *cur; // get the value pointed to by the iterator
  cout << val; //  print that value to the standard output
}

这个效果是一样的作为第一个示例中的 std :: copy 函数,但是在这里您可以看到迭代器是如何实际用于遍历序列的。有趣的是你使用了一对迭代器,所以不是有一个迭代器,而是使用HasNext()GetCurrent和MoveForward函数(或类似),你有两个迭代器,而且HasNext是粗略地说,取而代之的是平等测试。我们测试我们通过序列前进的迭代器是否等于给定的结束迭代器。如果是这样,我们知道我们已经到了序列的末尾。

The effect of this is the same as the std::copy function in the first example, but here you can see how the iterator is actually used to move through the sequence. The interesting thing is that you use a pair of iterators, so instead of having one iterator, with a "HasNext()" "GetCurrent" and "MoveForward" function (or similar), you have two iterators, and the "HasNext" is roughly speaking replaced with an equality test. We test if the iterator we're advancing through the sequence equals the given end iterator. If it does, we know we've reached the end of the sequence.

这些迭代器进一步细分为具有不同功能的不同类型。
向量的迭代器属于随机访问迭代器类别,这意味着从任何迭代器中,您可以在单个常量时间操作中到达序列中的任何其他位置。例如,在上面的例子中,我可以通过这样做从矢量的开头到结尾:

These iterators are further subdivided into different types with different capabilities. A vector's iterator belongs to the random access iterator category, which means that from any iterator, you can get to anywhere else in the sequence, in a single constant-time operation. For example, in the above example, I can get from the beginning of the vector to the end by doing this:

std::vector<int>::iterator last = v.begin() + 5; // advance 5 elements from begin

我也可以倒退

std::vector<int>::iterator first= v.end() - 5; // go 5 elements back from the end

然后有双向迭代器,它仍然允许你去序列中的前向和后向,但一次只有一个元素(所以代替+和 - 运算符,你只有++和 - )。例如,它们用于链接列表。在链表中没有办法在常量时间内跳过5个元素,因此链表实现只暴露双向迭代器,而不是随机访问。

Then there are bidirectional iterators, which still allow you to go both forward and backward in the sequence, but only one element at a time (so instead of + and - operators, you only have ++ and --). These are used for linked lists, for example. There is no way to skip, say, 5 elements in constant time in a linked list, so the linked list implementation only exposes bidirectional iterators, not random-access.

和这可以进一步缩小到前向迭代器(只有++运算符。上面的 ostream_iterator 就是一个例子。因为它包装了一个流,所以没有使用这样的迭代器向后移动的方法。

And this can be further narrowed down to a forward iterator (which only has the ++ operator. The ostream_iterator above is an example of this. Because it wraps a stream, there is no way to move backwards with such an iterator.

大多数其他语言实现的迭代器很像C#中的迭代器.C ++是我所知道的唯一一个实现更多东西的东西复杂(和强大的)

Most other languages implement iterators much like those found in C# though. C++ is the only one I know of which implements something significantly more complex (and powerful)

特别是,因为C ++迭代器与它们指向的容器分离,所以您可以轻松地表示子范围(例如,表示第一个在上面的向量中的三个元素,我可以使用迭代器对 v.begin(),v.begin()+ 3 。另一个例子是找到函数,它在迭代器范围内搜索并返回指向找到的元素的迭代器:

In particular, because C++ iterators are decoupled from the containers they point into, you can easily represent sub-ranges (for example, to denote the first three elements in the vector above, I could use the iterator pair v.begin(), v.begin() + 3. Another example is the find function, which searches in an iterator range, and returns an iterator pointing to the found element:

std::vector<int>::iterator result = std::find(v.begin(), v.end(), 3);

这个例子说要搜索第一个元素的整个向量范围,其值为3.它返回一个指向该元素的迭代器(如果没有找到结果,则为end-iterator)

This example says to search the entire vector range for the first element with the value 3. It returns an iterator pointing to that element (or the end-iterator if no result was found)

此迭代器可以与我们已经拥有的迭代器配对,例如,我们可以现在搜索搜索结果和序列结尾之间的子范围:

This iterator can be paired with the ones we already had, so for example, we can now search in the sub-range between the search result and the end of the sequence:

std::vector<int>::iterator result2 = std::find(result + 1, v.end(), 3);

因此上面将搜索值为3的 next 元素,开始在第一个搜索结果的一个过去,并以序列的结尾结束。当然,之后不会找到任何元素,因此返回 v.end()

So the above will search for the next element with value 3, starting at one past the first search result, and ending with the end of the sequence. Of course, no element will be found then, so it returns v.end().

这篇关于不同类型的迭代器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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